diff --git a/boil/helpers.go b/boil/helpers.go index 5c893f5..d7f7b79 100644 --- a/boil/helpers.go +++ b/boil/helpers.go @@ -8,7 +8,7 @@ import ( "strings" "unicode" - "github.com/pobri19/sqlboiler/strmangle" + "github.com/nullbio/sqlboiler/strmangle" ) // SetComplement subtracts the elements in b from a diff --git a/boil/helpers_test.go b/boil/helpers_test.go index cad548c..4fec457 100644 --- a/boil/helpers_test.go +++ b/boil/helpers_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "gopkg.in/BlackBaronsTux/null-extended.v1" + "gopkg.in/nullbio/null.v4" ) type testObj struct { diff --git a/boil/qs/query_mods.go b/boil/qs/query_mods.go index 7d25ef9..204291f 100644 --- a/boil/qs/query_mods.go +++ b/boil/qs/query_mods.go @@ -1,6 +1,6 @@ package qs -import "github.com/pobri19/sqlboiler/boil" +import "github.com/nullbio/sqlboiler/boil" type QueryMod func(q *boil.Query) diff --git a/boil/reflect.go b/boil/reflect.go index 0c52922..d5d7c64 100644 --- a/boil/reflect.go +++ b/boil/reflect.go @@ -9,8 +9,8 @@ import ( "sort" "time" - "github.com/pobri19/sqlboiler/strmangle" - "gopkg.in/BlackBaronsTux/null-extended.v1" + "github.com/nullbio/sqlboiler/strmangle" + "gopkg.in/nullbio/null.v4" ) var ( @@ -35,6 +35,35 @@ var ( // Bind executes the query and inserts the // result into the passed in object pointer func (q *Query) Bind(obj interface{}) error { + typ := reflect.TypeOf(obj) + kind := typ.Kind() + + if kind != reflect.Ptr { + return fmt.Errorf("Bind not given a pointer to a slice or struct: %s", typ.String()) + } + + typ = typ.Elem() + kind = typ.Kind() + + if kind == reflect.Struct { + row := ExecQueryOne(q) + err := BindOne(row, q.selectCols, obj) + if err != nil { + return fmt.Errorf("Failed to execute Bind query for %s: %s", q.table, err) + } + } else if kind == reflect.Slice { + rows, err := ExecQueryAll(q) + if err != nil { + return fmt.Errorf("Failed to execute Bind query for %s: %s", q.table, err) + } + err = BindAll(rows, q.selectCols, obj) + if err != nil { + return fmt.Errorf("Failed to Bind results to object provided for %s: %s", q.table, err) + } + } else { + return fmt.Errorf("Bind given a pointer to a non-slice or non-struct: %s", typ.String()) + } + return nil } @@ -232,9 +261,11 @@ func randomizeField(field reflect.Value) error { case typeNullString: newVal = null.NewString(randStr(5+rand.Intn(25)), rand.Intn(2) == 1) case typeNullTime: - newVal = null.NewTime(time.Now().Add(time.Duration(rand.Intn((int(time.Hour * 24 * 10))))), rand.Intn(2) == 1) + randTime := rand.Int63n(int64(time.Hour) * 24 * 365 * 65) + newVal = null.NewTime(time.Unix(0, 0).Add(time.Duration(randTime)), rand.Intn(2) == 1) case typeTime: - newVal = time.Now().Add(time.Duration(rand.Intn((int(time.Hour * 24 * 10))))) + randTime := rand.Int63n(int64(time.Hour) * 24 * 365 * 65) + newVal = time.Now().Add(time.Duration(randTime)) case typeNullFloat32: newVal = null.NewFloat32(rand.Float32(), rand.Intn(2) == 1) case typeNullFloat64: diff --git a/boil/reflect_test.go b/boil/reflect_test.go index 6eb9787..d7e6fcd 100644 --- a/boil/reflect_test.go +++ b/boil/reflect_test.go @@ -4,19 +4,19 @@ import ( "testing" "time" - "gopkg.in/BlackBaronsTux/null-extended.v1" + "gopkg.in/nullbio/null.v4" ) func TestBind(t *testing.T) { - + t.Errorf("Not implemented") } func TestBindOne(t *testing.T) { - + t.Errorf("Not implemented") } func TestBindAll(t *testing.T) { - + t.Errorf("Not implemented") } func TestGetStructValues(t *testing.T) { diff --git a/cmds/config.go b/cmds/config.go index eb7abe0..d4826b3 100644 --- a/cmds/config.go +++ b/cmds/config.go @@ -6,56 +6,56 @@ import ( "text/template" "github.com/BurntSushi/toml" - "github.com/pobri19/sqlboiler/strmangle" + "github.com/nullbio/sqlboiler/strmangle" ) // sqlBoilerTypeImports imports are only included in the template output if the database // requires one of the following special types. Check TranslateColumnType to see the type assignments. var sqlBoilerTypeImports = map[string]imports{ "null.Float32": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Float64": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Int": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Int8": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Int16": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Int32": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Int64": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Uint": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Uint8": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Uint16": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Uint32": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Uint64": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.String": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Bool": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "null.Time": imports{ - thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, + thirdparty: importList{`"gopkg.in/nullbio/null.v4"`}, }, "time.Time": imports{ standard: importList{`"time"`}, @@ -70,8 +70,8 @@ var sqlBoilerImports = imports{ `"strings"`, }, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, - `"github.com/pobri19/sqlboiler/boil/qs"`, + `"github.com/nullbio/sqlboiler/boil"`, + `"github.com/nullbio/sqlboiler/boil/qs"`, }, } @@ -79,8 +79,8 @@ var sqlBoilerSinglesImports = map[string]imports{ "helpers": imports{ standard: importList{}, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, - `"github.com/pobri19/sqlboiler/boil/qs"`, + `"github.com/nullbio/sqlboiler/boil"`, + `"github.com/nullbio/sqlboiler/boil/qs"`, }, }, } @@ -91,7 +91,7 @@ var sqlBoilerTestImports = imports{ `"testing"`, }, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, + `"github.com/nullbio/sqlboiler/boil"`, }, } @@ -122,7 +122,7 @@ var sqlBoilerTestMainImports = map[string]imports{ `"math/rand"`, }, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, + `"github.com/nullbio/sqlboiler/boil"`, `"github.com/BurntSushi/toml"`, `_ "github.com/lib/pq"`, }, diff --git a/cmds/helpers.go b/cmds/helpers.go index 4bba3e1..29f42e9 100644 --- a/cmds/helpers.go +++ b/cmds/helpers.go @@ -5,7 +5,7 @@ import ( "fmt" "sort" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" ) func combineImports(a, b imports) imports { diff --git a/cmds/helpers_test.go b/cmds/helpers_test.go index ee9d7e3..7b1f1b5 100644 --- a/cmds/helpers_test.go +++ b/cmds/helpers_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" ) func TestCombineTypeImports(t *testing.T) { @@ -15,7 +15,7 @@ func TestCombineTypeImports(t *testing.T) { `"fmt"`, }, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, + `"github.com/nullbio/sqlboiler/boil"`, }, } @@ -26,8 +26,8 @@ func TestCombineTypeImports(t *testing.T) { `"time"`, }, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, - `"gopkg.in/guregu/null.v3"`, + `"github.com/nullbio/sqlboiler/boil"`, + `"gopkg.in/nullbio/null.v4"`, }, } @@ -59,8 +59,8 @@ func TestCombineTypeImports(t *testing.T) { `"time"`, }, thirdparty: importList{ - `"github.com/pobri19/sqlboiler/boil"`, - `"gopkg.in/guregu/null.v3"`, + `"github.com/nullbio/sqlboiler/boil"`, + `"gopkg.in/nullbio/null.v4"`, }, } @@ -76,11 +76,11 @@ func TestCombineImports(t *testing.T) { a := imports{ standard: importList{"fmt"}, - thirdparty: importList{"github.com/pobri19/sqlboiler", "gopkg.in/guregu/null.v3"}, + thirdparty: importList{"github.com/nullbio/sqlboiler", "gopkg.in/nullbio/null.v4"}, } b := imports{ standard: importList{"os"}, - thirdparty: importList{"github.com/pobri19/sqlboiler"}, + thirdparty: importList{"github.com/nullbio/sqlboiler"}, } c := combineImports(a, b) @@ -88,8 +88,8 @@ func TestCombineImports(t *testing.T) { if c.standard[0] != "fmt" && c.standard[1] != "os" { t.Errorf("Wanted: fmt, os got: %#v", c.standard) } - if c.thirdparty[0] != "github.com/pobri19/sqlboiler" && c.thirdparty[1] != "gopkg.in/guregu/null.v3" { - t.Errorf("Wanted: github.com/pobri19/sqlboiler, gopkg.in/guregu/null.v3 got: %#v", c.thirdparty) + if c.thirdparty[0] != "github.com/nullbio/sqlboiler" && c.thirdparty[1] != "gopkg.in/nullbio/null.v4" { + t.Errorf("Wanted: github.com/nullbio/sqlboiler, gopkg.in/nullbio/null.v4 got: %#v", c.thirdparty) } } diff --git a/cmds/sqlboiler.go b/cmds/sqlboiler.go index 3560bcd..a01f15f 100644 --- a/cmds/sqlboiler.go +++ b/cmds/sqlboiler.go @@ -9,7 +9,7 @@ import ( "strings" "text/template" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" "github.com/spf13/cobra" ) diff --git a/cmds/sqlboiler_test.go b/cmds/sqlboiler_test.go index df17394..8f42c44 100644 --- a/cmds/sqlboiler_test.go +++ b/cmds/sqlboiler_test.go @@ -12,7 +12,7 @@ import ( "strconv" "testing" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" ) var cmdData *CmdData diff --git a/cmds/templates/finishers.tpl b/cmds/templates/finishers.tpl index 19f5c96..1099a2c 100644 --- a/cmds/templates/finishers.tpl +++ b/cmds/templates/finishers.tpl @@ -23,6 +23,7 @@ func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) { if err != nil { return nil, fmt.Errorf("{{.PkgName}}: failed to execute an all query for {{.Table.Name}}: %s", err) } + defer res.Close() err = boil.BindAll(res, boil.Select(q.Query), &o) if err != nil { diff --git a/cmds/templates_test/all.tpl b/cmds/templates_test/all.tpl index e4b7c45..05be1b5 100644 --- a/cmds/templates_test/all.tpl +++ b/cmds/templates_test/all.tpl @@ -2,22 +2,24 @@ {{- $dbName := singular .Table.Name -}} {{- $tableNamePlural := titleCasePlural .Table.Name -}} {{- $varNamePlural := camelCasePlural .Table.Name -}} -// {{$tableNamePlural}}All retrieves all records. func Test{{$tableNamePlural}}All(t *testing.T) { var err error + // Start from a clean slate + {{$varNamePlural}}DeleteAllRows(t) + r := make([]{{$tableNameSingular}}, 2) // insert two random columns to test DeleteAll - for i, v := range r { - err = boil.RandomizeStruct(&v) + for i := 0; i < len(r); i++ { + err = boil.RandomizeStruct(&r[i]) if err != nil { t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err) } - err = v.Insert() + err = r[i].Insert() if err != nil { - t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", v, err) + t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", r[i], err) } } @@ -37,15 +39,15 @@ func Test{{$tableNamePlural}}All(t *testing.T) { o := make([]{{$tableNameSingular}}, 3) - for i, v := range o { - err = boil.RandomizeStruct(&v) + for i := 0; i < len(o); i++ { + err = boil.RandomizeStruct(&o[i]) if err != nil { t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err) } - err = v.Insert() + err = o[i].Insert() if err != nil { - t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", v, err) + t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err) } } diff --git a/cmds/templates_test/delete.tpl b/cmds/templates_test/delete.tpl index e69de29..eaeb7c0 100644 --- a/cmds/templates_test/delete.tpl +++ b/cmds/templates_test/delete.tpl @@ -0,0 +1,62 @@ +{{- $tableNameSingular := titleCaseSingular .Table.Name -}} +{{- $dbName := singular .Table.Name -}} +{{- $tableNamePlural := titleCasePlural .Table.Name -}} +{{- $varNamePlural := camelCasePlural .Table.Name -}} +func {{$varNamePlural}}DeleteAllRows(t *testing.T) { + // Delete all rows to give a clean slate + err := {{$tableNamePlural}}().DeleteAll() + if err != nil { + t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err) + } +} + +func Test{{$tableNamePlural}}Delete(t *testing.T) { + var err error + + // Start from a clean slate + {{$varNamePlural}}DeleteAllRows(t) + + r := make([]{{$tableNameSingular}}, 3) + + // insert random columns to test DeleteAll + for i := 0; i < len(r); i++ { + err = boil.RandomizeStruct(&r[i]) + if err != nil { + t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err) + } + + err = r[i].Insert() + if err != nil { + t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", r[i], err) + } + } + + // Test DeleteAll() + err = {{$tableNamePlural}}().DeleteAll() + if err != nil { + t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err) + } + + // Check number of rows in table to ensure DeleteAll was successful + var c int64 + c, err = {{$tableNamePlural}}().Count() + + if c != 0 { + t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c) + } + + // insert random columns to test Delete + o := make([]{{$tableNameSingular}}, 3) + for i := 0; i < len(o); i++ { + err = boil.RandomizeStruct(&o[i]) + if err != nil { + t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err) + } + + err = o[i].Insert() + if err != nil { + t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err) + } + } + +} diff --git a/cmds/types.go b/cmds/types.go index dbde66b..ec6e716 100644 --- a/cmds/types.go +++ b/cmds/types.go @@ -3,7 +3,7 @@ package cmds import ( "text/template" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" "github.com/spf13/cobra" ) diff --git a/main.go b/main.go index 229cfee..3dbcde3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( "fmt" "os" - "github.com/pobri19/sqlboiler/cmds" + "github.com/nullbio/sqlboiler/cmds" "github.com/spf13/cobra" ) @@ -29,7 +29,7 @@ func main() { Use: "sqlboiler", Short: "SQL Boiler generates boilerplate structs and statements", Long: "SQL Boiler generates boilerplate structs and statements from the template files.\n" + - `Complete documentation is available at http://github.com/pobri19/sqlboiler`, + `Complete documentation is available at http://github.com/nullbio/sqlboiler`, PreRunE: func(cmd *cobra.Command, args []string) error { return cmdData.SQLBoilerPreRun(cmd, args) }, diff --git a/strmangle/strmangle.go b/strmangle/strmangle.go index ef8906a..fc07054 100644 --- a/strmangle/strmangle.go +++ b/strmangle/strmangle.go @@ -6,7 +6,7 @@ import ( "strings" "github.com/jinzhu/inflection" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" ) var rgxAutoIncColumn = regexp.MustCompile(`^nextval\(.*\)`) diff --git a/strmangle/strmangle_test.go b/strmangle/strmangle_test.go index 1fa36d7..1338e0a 100644 --- a/strmangle/strmangle_test.go +++ b/strmangle/strmangle_test.go @@ -3,7 +3,7 @@ package strmangle import ( "testing" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" ) var testColumns = []dbdrivers.Column{ diff --git a/strmangle/testmangle.go b/strmangle/testmangle.go index af70ec7..c07073e 100644 --- a/strmangle/testmangle.go +++ b/strmangle/testmangle.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/pobri19/sqlboiler/dbdrivers" + "github.com/nullbio/sqlboiler/dbdrivers" ) func RandDBStruct(varName string, table dbdrivers.Table) string {