Refactor WhereMultiple into new WhereClause
This commit is contained in:
parent
db747cccfe
commit
a98e474c9c
10 changed files with 42 additions and 60 deletions
|
@ -243,25 +243,10 @@ func GenerateParamFlags(colCount int, startAt int, groupAt int) string {
|
|||
return buf.String()
|
||||
}
|
||||
|
||||
// WhereClause returns the where clause using start as the $ flag index
|
||||
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
|
||||
func WhereClause(start int, cols []string) string {
|
||||
if start == 0 {
|
||||
panic("0 is not a valid start number for whereClause")
|
||||
}
|
||||
|
||||
ret := make([]string, len(cols))
|
||||
for i, c := range cols {
|
||||
ret[i] = fmt.Sprintf(`"%s"=$%d`, c, start+i)
|
||||
}
|
||||
|
||||
return strings.Join(ret, " AND ")
|
||||
}
|
||||
|
||||
// WhereMultiple is a version of Where that binds multiple checks together
|
||||
// WhereClause is a version of Where that binds multiple checks together
|
||||
// with an or statement.
|
||||
// WhereMultiple(1, 2, "a", "b") = "(a=$1 and b=$2) or (a=$3 and b=$4)"
|
||||
func WhereMultiple(start, count int, cols []string) string {
|
||||
func WhereClause(start, count int, cols []string) string {
|
||||
if start == 0 {
|
||||
panic("0 is not a valid start number for whereMultiple")
|
||||
}
|
||||
|
|
|
@ -304,39 +304,6 @@ func TestPrefixStringSlice(t *testing.T) {
|
|||
func TestWhereClause(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
Cols []string
|
||||
Start int
|
||||
Should string
|
||||
}{
|
||||
{Cols: []string{"col1"}, Start: 2, Should: `"col1"=$2`},
|
||||
{Cols: []string{"col1", "col2"}, Start: 4, Should: `"col1"=$4 AND "col2"=$5`},
|
||||
{Cols: []string{"col1", "col2", "col3"}, Start: 4, Should: `"col1"=$4 AND "col2"=$5 AND "col3"=$6`},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
r := WhereClause(test.Start, test.Cols)
|
||||
if r != test.Should {
|
||||
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhereClausePanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
WhereClause(0, nil)
|
||||
}
|
||||
|
||||
func TestWhereMultiple(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
Cols []string
|
||||
Start int
|
||||
|
@ -349,7 +316,7 @@ func TestWhereMultiple(t *testing.T) {
|
|||
}
|
||||
|
||||
for i, test := range tests {
|
||||
r := WhereMultiple(test.Start, test.Count, test.Cols)
|
||||
r := WhereClause(test.Start, test.Count, test.Cols)
|
||||
if r != test.Should {
|
||||
t.Errorf("(%d) want: %s, got: %s", i, test.Should, r)
|
||||
}
|
||||
|
@ -365,7 +332,7 @@ func TestWhereMultiplePanic(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
WhereMultiple(0, 0, nil)
|
||||
WhereClause(0, 0, nil)
|
||||
}
|
||||
|
||||
func TestInClause(t *testing.T) {
|
||||
|
|
|
@ -1,8 +1,38 @@
|
|||
{{- define "relationship_to_one_struct_helper" -}}
|
||||
{{.Function.Name}} *{{.ForeignTable.NameGo}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $tableNameSingular := .Table.Name | singular -}}
|
||||
{{- $modelName := $tableNameSingular | titleCase -}}
|
||||
// {{$modelName}} is an object representing the database table.
|
||||
type {{$modelName}} struct {
|
||||
{{range $column := .Table.Columns -}}
|
||||
{{titleCase $column.Name}} {{$column.Type}} `boil:"{{$column.Name}}" json:"{{$column.Name}}" toml:"{{$column.Name}}" yaml:"{{$column.Name}}"`
|
||||
{{titleCase $column.Name}} {{$column.Type}} `boil:"{{$column.Name}}" json:"{{$column.Name}}{{if $column.Nullable}},omitempty{{end}}" toml:"{{$column.Name}}" yaml:"{{$column.Name}}{{if $column.Nullable}},omitempty{{end}}"`
|
||||
{{end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else}}
|
||||
//Relationships *{{$modelName}}Relationships `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
{{- $dot := . -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else}}
|
||||
// {{$modelName}}Relationships are where relationships are both cached
|
||||
// and eagerly loaded.
|
||||
type {{$modelName}}Relationships struct {
|
||||
{{range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_struct_helper" $rel}}
|
||||
{{end -}}
|
||||
{{- range .Table.ToManyRelationships -}}
|
||||
{{- if .ForeignColumnUnique -}}
|
||||
{{- template "relationship_to_one_struct_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table .) -}}
|
||||
{{- else -}}
|
||||
{{- $rel := textsFromRelationship $dot.Tables $dot.Table . -}}
|
||||
{{$rel.Function.Name}} {{$rel.ForeignTable.Slice}}
|
||||
{{end -}}{{/* if ForeignColumnUnique */}}
|
||||
{{- end -}}{{/* range tomany */}}
|
||||
}
|
||||
|
||||
{{end -}}
|
||||
|
|
|
@ -29,7 +29,7 @@ func {{$tableNameSingular}}Find(exec boil.Executor, {{$pkArgs}}, selectCols ...s
|
|||
sel = strings.Join(strmangle.IdentQuoteSlice(selectCols), ",")
|
||||
}
|
||||
sql := fmt.Sprintf(
|
||||
`select %s from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}}`, sel,
|
||||
`select %s from "{{.Table.Name}}" where {{whereClause 1 1 .Table.PKey.Columns}}`, sel,
|
||||
)
|
||||
q := boil.SQL(sql, {{$pkNames | join ", "}})
|
||||
boil.SetExecutor(q, exec)
|
||||
|
|
|
@ -49,7 +49,7 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
|
||||
lastId, err := result.lastInsertId()
|
||||
if err != nil || lastId == 0 {
|
||||
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), strmangle.WhereClause(1, wl))
|
||||
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), strmangle.WhereClause(1, 1, wl))
|
||||
rows, err := exec.Query(sel, boil.GetStructValues(o, wl...)...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)
|
||||
|
|
|
@ -30,7 +30,7 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error {
|
|||
|
||||
mods = append(mods,
|
||||
qm.From("{{.Table.Name}}"),
|
||||
qm.Where(`{{whereClause 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}),
|
||||
qm.Where(`{{whereClause 1 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}),
|
||||
)
|
||||
|
||||
query := NewQuery(exec, mods...)
|
||||
|
|
|
@ -7,7 +7,7 @@ func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error)
|
|||
var exists bool
|
||||
|
||||
row := exec.QueryRow(
|
||||
`select exists(select 1 from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}} limit 1)`,
|
||||
`select exists(select 1 from "{{.Table.Name}}" where {{whereClause 1 1 .Table.PKey.Columns}} limit 1)`,
|
||||
{{$pkNames | join ", "}},
|
||||
)
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ func Test{{$tableNamePlural}}Exists(t *testing.T) {
|
|||
t.Errorf("Expected {{$tableNameSingular}}ExistsG to return true, but got false.")
|
||||
}
|
||||
|
||||
whereClause := strmangle.WhereClause(1, {{$varNameSingular}}PrimaryKeyColumns)
|
||||
whereClause := strmangle.WhereClause(1, 1, {{$varNameSingular}}PrimaryKeyColumns)
|
||||
e, err = {{$tableNamePlural}}G(qm.Where(whereClause, boil.GetStructValues(o, {{$varNameSingular}}PrimaryKeyColumns...)...)).Exists()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to check if {{$tableNameSingular}} exists: %s", err)
|
||||
|
|
|
@ -16,7 +16,7 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) {
|
|||
|
||||
j := {{$tableNameSingular}}{}
|
||||
|
||||
err = {{$tableNamePlural}}G(qm.Where(`{{whereClause 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
|
||||
err = {{$tableNamePlural}}G(qm.Where(`{{whereClause 1 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err)
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ func Test{{$tableNamePlural}}Select(t *testing.T) {
|
|||
t.Errorf("Unable to insert item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err)
|
||||
}
|
||||
|
||||
err = {{$tableNamePlural}}G(qm.Select({{$varNameSingular}}AutoIncrementColumns...), qm.Where(`{{whereClause 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "item." | join ", "}})).Bind(x)
|
||||
err = {{$tableNamePlural}}G(qm.Select({{$varNameSingular}}AutoIncrementColumns...), qm.Where(`{{whereClause 1 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "item." | join ", "}})).Bind(x)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to select insert results with bind: %s", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue