Add Exists feature

This commit is contained in:
Patrick O'brien 2016-08-03 20:23:43 +10:00
parent 0a2507178e
commit 964f367700
4 changed files with 131 additions and 0 deletions

View file

@ -76,3 +76,28 @@ func (q {{$varNameSingular}}Query) CountP() int64 {
return c
}
// Exists checks if the row exists in the table.
func (q {{$varNameSingular}}Query) Exists() (bool, error) {
var count int64
boil.SetCount(q.Query)
boil.SetLimit(q.Query, 1)
err := boil.ExecQueryOne(q.Query).Scan(&count)
if err != nil {
return false, fmt.Errorf("{{.PkgName}}: failed to check if {{.Table.Name}} exists: %s", err)
}
return count > 0, nil
}
// Exists checks if the row exists in the table, and panics on error.
func (q {{$varNameSingular}}Query) ExistsP() bool {
e, err := q.Exists()
if err != nil {
panic(boil.WrapErr(err))
}
return e
}

50
templates/12_exists.tpl Normal file
View file

@ -0,0 +1,50 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
// {{$tableNameSingular}}Exists checks if the {{$tableNameSingular}} row exists.
func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error) {
var count int64
mods := []qm.QueryMod{
qm.From("{{.Table.Name}}"),
qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{$pkNames | join ", "}}),
qm.Limit(1),
}
q := NewQuery(exec, mods...)
boil.SetCount(q)
err := boil.ExecQueryOne(q).Scan(&count)
if err != nil {
return false, fmt.Errorf("{{.PkgName}}: unable to check if {{.Table.Name}} exists: %v", err)
}
return count > 0, nil
}
// {{$tableNameSingular}}ExistsG checks if the {{$tableNameSingular}} row exists.
func {{$tableNameSingular}}ExistsG({{$pkArgs}}) (bool, error) {
return {{$tableNameSingular}}Exists(boil.GetDB(), {{$pkNames | join ", "}})
}
// {{$tableNameSingular}}ExistsGP checks if the {{$tableNameSingular}} row exists. Panics on error.
func {{$tableNameSingular}}ExistsGP({{$pkArgs}}) bool {
e, err := {{$tableNameSingular}}Exists(boil.GetDB(), {{$pkNames | join ", "}})
if err != nil {
panic(boil.WrapErr(err))
}
return e
}
// {{$tableNameSingular}}ExistsP checks if the {{$tableNameSingular}} row exists. Panics on error.
func {{$tableNameSingular}}ExistsP(exec boil.Executor, {{$pkArgs}}) bool {
e, err := {{$tableNameSingular}}Exists(exec, {{$pkNames | join ", "}})
if err != nil {
panic(boil.WrapErr(err))
}
return e
}

56
templates_test/exists.tpl Normal file
View file

@ -0,0 +1,56 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $pkeyArgs := .Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", " -}}
func Test{{$tableNamePlural}}Exists(t *testing.T) {
var err error
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
// Check Exists finds existing rows
e, err := {{$tableNameSingular}}ExistsG({{$pkeyArgs}})
if err != nil {
t.Errorf("Unable to check if {{$tableNameSingular}} exists: %s", err)
}
if e != true {
t.Errorf("Expected {{$tableNameSingular}}ExistsG to return true, but got false.")
}
whereClause := strmangle.WhereClause({{$varNameSingular}}PrimaryKeyColumns, 1)
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)
}
if e != true {
t.Errorf("Expected ExistsG to return true, but got false.")
}
// Check Exists does not find non-existing rows
o = {{$tableNameSingular}}{}
e, err = {{$tableNameSingular}}ExistsG({{$pkeyArgs}})
if err != nil {
t.Errorf("Unable to check if {{$tableNameSingular}} exists: %s", err)
}
if e != false {
t.Errorf("Expected {{$tableNameSingular}}ExistsG to return false, but got true.")
}
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)
}
if e != false {
t.Errorf("Expected ExistsG to return false, but got true.")
}
{{$varNamePlural}}DeleteAllRows(t)
}