diff --git a/boil/qs/query_mods.go b/boil/qm/query_mods.go similarity index 99% rename from boil/qs/query_mods.go rename to boil/qm/query_mods.go index 204291f..f0d2e0a 100644 --- a/boil/qs/query_mods.go +++ b/boil/qm/query_mods.go @@ -1,4 +1,4 @@ -package qs +package qm import "github.com/nullbio/sqlboiler/boil" diff --git a/boil/qm/query_mods_test.go b/boil/qm/query_mods_test.go new file mode 100644 index 0000000..86f2d04 --- /dev/null +++ b/boil/qm/query_mods_test.go @@ -0,0 +1 @@ +package qm diff --git a/boil/qs/query_mods_test.go b/boil/qs/query_mods_test.go deleted file mode 100644 index d0e02ec..0000000 --- a/boil/qs/query_mods_test.go +++ /dev/null @@ -1 +0,0 @@ -package qs diff --git a/imports.go b/imports.go index e3de1a9..8e775c4 100644 --- a/imports.go +++ b/imports.go @@ -149,7 +149,7 @@ var defaultTemplateImports = imports{ }, thirdParty: importList{ `"github.com/nullbio/sqlboiler/boil"`, - `"github.com/nullbio/sqlboiler/boil/qs"`, + `"github.com/nullbio/sqlboiler/boil/qm"`, }, } @@ -158,7 +158,7 @@ var defaultSingletonTemplateImports = map[string]imports{ standard: importList{}, thirdParty: importList{ `"github.com/nullbio/sqlboiler/boil"`, - `"github.com/nullbio/sqlboiler/boil/qs"`, + `"github.com/nullbio/sqlboiler/boil/qm"`, }, }, } @@ -170,6 +170,7 @@ var defaultTestTemplateImports = imports{ }, thirdParty: importList{ `"github.com/nullbio/sqlboiler/boil"`, + `"github.com/nullbio/sqlboiler/boil/qm"`, }, } diff --git a/strmangle/strmangle.go b/strmangle/strmangle.go index fc07054..03e526d 100644 --- a/strmangle/strmangle.go +++ b/strmangle/strmangle.go @@ -275,6 +275,11 @@ func GenerateParamFlags(colCount int, startAt int) string { func WherePrimaryKey(pkeyCols []string, start int) string { var output string + // 0 is not a valid start number + if start == 0 { + start = 1 + } + cols := make([]string, len(pkeyCols)) copy(cols, pkeyCols) diff --git a/templates/all.tpl b/templates/all.tpl index 5bbbfbe..be2fc3d 100644 --- a/templates/all.tpl +++ b/templates/all.tpl @@ -6,11 +6,11 @@ type {{$varNameSingular}}Query struct { } // {{$tableNamePlural}}All retrieves all records. -func {{$tableNamePlural}}(mods ...qs.QueryMod) {{$varNameSingular}}Query { +func {{$tableNamePlural}}(mods ...qm.QueryMod) {{$varNameSingular}}Query { return {{$tableNamePlural}}X(boil.GetDB(), mods...) } -func {{$tableNamePlural}}X(exec boil.Executor, mods ...qs.QueryMod) {{$varNameSingular}}Query { - mods = append(mods, qs.Table("{{.Table.Name}}")) +func {{$tableNamePlural}}X(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query { + mods = append(mods, qm.Table("{{.Table.Name}}")) return {{$varNameSingular}}Query{NewQueryX(exec, mods...)} } diff --git a/templates/delete.tpl b/templates/delete.tpl index 930ed1a..5e75919 100644 --- a/templates/delete.tpl +++ b/templates/delete.tpl @@ -16,11 +16,11 @@ func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error { return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion") } - var mods []qs.QueryMod + var mods []qm.QueryMod mods = append(mods, - qs.Table("{{.Table.Name}}"), - qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{paramsPrimaryKey "o." .Table.PKey.Columns true}}), + qm.Table("{{.Table.Name}}"), + qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{paramsPrimaryKey "o." .Table.PKey.Columns true}}), ) query := NewQueryX(exec, mods...) @@ -61,14 +61,14 @@ func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error { return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all") } - var mods []qs.QueryMod + var mods []qm.QueryMod args := o.inPrimaryKeyArgs() in := boil.WherePrimaryKeyIn(len(o), {{commaList .Table.PKey.Columns}}) mods = append(mods, - qs.Table("{{.Table.Name}}"), - qs.Where(in, args...), + qm.Table("{{.Table.Name}}"), + qm.Where(in, args...), ) query := NewQueryX(exec, mods...) diff --git a/templates/find.tpl b/templates/find.tpl index e241acd..b17c8db 100644 --- a/templates/find.tpl +++ b/templates/find.tpl @@ -9,11 +9,11 @@ func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.C func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) { {{$varNameSingular}} := &{{$tableNameSingular}}{} - - mods := []qs.QueryMod{ - qs.Select(selectCols...), - qs.Table("{{.Table.Name}}"), - qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList "" .Table.PKey.Columns}}), + + mods := []qm.QueryMod{ + qm.Select(selectCols...), + qm.Table("{{.Table.Name}}"), + qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList "" .Table.PKey.Columns}}), } q := NewQueryX(exec, mods...) diff --git a/templates/singleton/helpers.tpl b/templates/singleton/helpers.tpl index 304b1ce..1409fbd 100644 --- a/templates/singleton/helpers.tpl +++ b/templates/singleton/helpers.tpl @@ -2,15 +2,15 @@ type M map[string]interface{} // NewQuery initializes a new Query using the passed in QueryMods -func NewQuery(mods ...qs.QueryMod) *boil.Query { +func NewQuery(mods ...qm.QueryMod) *boil.Query { return NewQueryX(boil.GetDB(), mods...) } // NewQueryX initializes a new Query using the passed in QueryMods -func NewQueryX(exec boil.Executor, mods ...qs.QueryMod) *boil.Query { +func NewQueryX(exec boil.Executor, mods ...qm.QueryMod) *boil.Query { q := &boil.Query{} boil.SetExecutor(q, exec) - qs.Apply(q, mods...) + qm.Apply(q, mods...) return q } diff --git a/templates_test/all.tpl b/templates_test/all.tpl index 65a9416..c379d71 100644 --- a/templates_test/all.tpl +++ b/templates_test/all.tpl @@ -3,6 +3,28 @@ {{- $tableNamePlural := titleCasePlural .Table.Name -}} {{- $varNamePlural := camelCasePlural .Table.Name -}} {{- $varNameSingular := camelCaseSingular .Table.Name -}} +func {{$varNameSingular}}CompareVals(o *{{$tableNameSingular}}, j *{{$tableNameSingular}}, t *testing.T) { + {{range $key, $value := .Table.Columns}} + {{if eq $value.Type "null.Time"}} + if o.{{titleCase $value.Name}}.Time.Format("02/01/2006") != j.{{titleCase $value.Name}}.Time.Format("02/01/2006") { + t.Errorf("Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Time.Format("02/01/2006"), j.{{titleCase $value.Name}}.Time.Format("02/01/2006")) + } + {{else if eq $value.Type "time.Time"}} + if o.{{titleCase $value.Name}}.Format("02/01/2006") != j.{{titleCase $value.Name}}.Format("02/01/2006") { + t.Errorf("Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Format("02/01/2006"), j.{{titleCase $value.Name}}.Format("02/01/2006")) + } + {{else if eq $value.Type "[]byte"}} + if !byteSliceEqual(o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) { + t.Errorf("Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) + } + {{else}} + if j.{{titleCase $value.Name}} != o.{{titleCase $value.Name}} { + t.Errorf("Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) + } + {{end}} + {{end}} +} + func Test{{$tableNamePlural}}(t *testing.T) { var err error diff --git a/templates_test/find.tpl b/templates_test/find.tpl index c9aa2d3..00bce88 100644 --- a/templates_test/find.tpl +++ b/templates_test/find.tpl @@ -23,26 +23,7 @@ func Test{{$tableNamePlural}}Find(t *testing.T) { // Perform all Find queries and assign result objects to slice for comparison for i := 0; i < len(j); i++ { j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}}) - - {{range $key, $value := .Table.Columns}} - {{if eq $value.Type "null.Time"}} - if o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") { - t.Errorf("%d) Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006")) - } - {{else if eq $value.Type "time.Time"}} - if o[i].{{titleCase $value.Name}}.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Format("02/01/2006") { - t.Errorf("%d) Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Format("02/01/2006")) - } - {{else if eq $value.Type "[]byte"}} - if !byteSliceEqual(o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) { - t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) - } - {{else}} - if j[i].{{titleCase $value.Name}} != o[i].{{titleCase $value.Name}} { - t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) - } - {{end}} - {{end}} + {{$varNameSingular}}CompareVals(o[i], j[i], t) } {{if hasPrimaryKey .Table.PKey}} diff --git a/templates_test/finishers.tpl b/templates_test/finishers.tpl index 889f7d4..4b68551 100644 --- a/templates_test/finishers.tpl +++ b/templates_test/finishers.tpl @@ -4,7 +4,27 @@ {{- $varNamePlural := camelCasePlural .Table.Name -}} {{- $varNameSingular := camelCaseSingular .Table.Name -}} func Test{{$tableNamePlural}}Bind(t *testing.T) { + var err error + {{$varNamePlural}}DeleteAllRows(t) + + o := {{$tableNameSingular}}{} + if err = boil.RandomizeStruct(&o); err != nil { + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) + } + + if err = o.Insert(); err != nil { + t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err) + } + + j := {{$tableNameSingular}}{} + + err = {{$tableNamePlural}}(qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{titleCaseCommaList "o." .Table.PKey.Columns}})).Bind(&j) + if err != nil { + t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err) + } + + {{$varNameSingular}}CompareVals(&o, &j, t) } func Test{{$tableNamePlural}}One(t *testing.T) { @@ -26,26 +46,7 @@ func Test{{$tableNamePlural}}One(t *testing.T) { t.Errorf("Unable to fetch One {{$tableNameSingular}} result:\n#%v\nErr: %s", j, err) } - {{range $key, $value := .Table.Columns}} - {{if eq $value.Type "null.Time"}} - if o.{{titleCase $value.Name}}.Time.Format("02/01/2006") != j.{{titleCase $value.Name}}.Time.Format("02/01/2006") { - t.Errorf("Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Time.Format("02/01/2006"), j.{{titleCase $value.Name}}.Time.Format("02/01/2006")) - } - {{else if eq $value.Type "time.Time"}} - if o.{{titleCase $value.Name}}.Format("02/01/2006") != j.{{titleCase $value.Name}}.Format("02/01/2006") { - t.Errorf("Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Format("02/01/2006"), j.{{titleCase $value.Name}}.Format("02/01/2006")) - } - {{else if eq $value.Type "[]byte"}} - if !byteSliceEqual(o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) { - t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) - - } - {{else}} - if j.{{titleCase $value.Name}} != o.{{titleCase $value.Name}} { - t.Errorf("Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) - } - {{end}} - {{end}} + {{$varNameSingular}}CompareVals(&o, j, t) } func Test{{$tableNamePlural}}All(t *testing.T) { @@ -76,25 +77,7 @@ func Test{{$tableNamePlural}}All(t *testing.T) { } for i := 0; i < len(o); i++ { - {{range $key, $value := .Table.Columns}} - {{if eq $value.Type "null.Time"}} - if o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") { - t.Errorf("%d) Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006")) - } - {{else if eq $value.Type "time.Time"}} - if o[i].{{titleCase $value.Name}}.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Format("02/01/2006") { - t.Errorf("%d) Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Format("02/01/2006")) - } - {{else if eq $value.Type "[]byte"}} - if !byteSliceEqual(o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) { - t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) - } - {{else}} - if j[i].{{titleCase $value.Name}} != o[i].{{titleCase $value.Name}} { - t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}}) - } - {{end}} - {{end}} + {{$varNameSingular}}CompareVals(o[i], j[i], t) } }