Fix IdentQuote bug, add Reload helpers

This commit is contained in:
Patrick O'brien 2016-08-02 19:56:55 +10:00
parent 9a5742eeea
commit 775c5ba369
6 changed files with 89 additions and 4 deletions

View file

@ -73,6 +73,11 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
buf.WriteString("COUNT(")
}
if len(q.selectCols) > 0 {
cols := make([]string, len(q.selectCols))
copy(cols, q.selectCols)
for i := 0; i < len(cols); i++ {
cols[i] = strmangle.IdentQuote(cols[i])
}
buf.WriteString(strings.Join(q.selectCols, `, `))
} else {
buf.WriteByte('*')
@ -179,11 +184,10 @@ func SetExecutor(q *Query, exec Executor) {
// SetSelect on the query.
func SetSelect(q *Query, columns ...string) {
for i := 0; i < len(columns); i++ {
columns[i] = strmangle.IdentQuote(columns[i])
}
cols := make([]string, len(columns))
copy(cols, columns)
q.selectCols = append(q.selectCols, columns...)
q.selectCols = append(q.selectCols, cols...)
}
// Select returns the select columns in the query.

View file

@ -131,6 +131,7 @@ func TestTitleCase(t *testing.T) {
{"thing_guid", "ThingGUID"},
{"guid_thing", "GUIDThing"},
{"thing_guid_thing", "ThingGUIDThing"},
{"id", "ID"},
}
for i, test := range tests {

View file

@ -20,6 +20,7 @@ func {{$tableNameSingular}}FindGP({{$pkArgs}}, selectCols ...string) *{{$tableNa
}
// {{$tableNameSingular}}Find retrieves a single record by ID with an executor.
// If selectCols is empty Find will return all columns.
func {{$tableNameSingular}}Find(exec boil.Executor, {{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
{{$varNameSingular}} := &{{$tableNameSingular}}{}

36
templates/11_reload.tpl Normal file
View file

@ -0,0 +1,36 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
// ReloadGP refetches the object from the database and panics on error.
func (o *{{$tableNameSingular}}) ReloadGP() {
if err := o.ReloadG(); err != nil {
panic(boil.WrapErr(err))
}
}
// ReloadP refetches the object from the database with an executor. Panics on error.
func (o *{{$tableNameSingular}}) ReloadP(exec boil.Executor) {
if err := o.Reload(exec); err != nil {
panic(boil.WrapErr(err))
}
}
// ReloadG refetches the object from the database using the primary keys.
func (o *{{$tableNameSingular}}) ReloadG() error {
if o == nil {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for reload")
}
return o.Reload(boil.GetDB())
}
// Reload refetches the object from the database
// using the primary keys with an executor.
func (o *{{$tableNameSingular}}) Reload(exec boil.Executor) error {
ret, err := {{$tableNameSingular}}Find(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
if err != nil {
return err
}
*o = *ret
return nil
}

43
templates_test/reload.tpl Normal file
View file

@ -0,0 +1,43 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Reload(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)
}
// Create another copy of the object
o1, err := {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
if err != nil {
t.Errorf("Unable to find {{$tableNameSingular}} row.")
}
// Randomize the struct values again, except for the primary key values, so we can call update.
err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...)
if err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct members excluding primary keys: %s", err)
}
colsWithoutPrimKeys := boil.SetComplement({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns)
if err = o.UpdateG(colsWithoutPrimKeys...); err != nil {
t.Errorf("Unable to update the {{$tableNameSingular}} row: %s", err)
}
if err = o1.ReloadG(); err != nil {
t.Errorf("Unable to reload {{$tableNameSingular}} object: %s", err)
}
{{$varNameSingular}}CompareVals(&o, o1, t)
{{$varNamePlural}}DeleteAllRows(t)
}