Finished insert and update, added UpdateAt

This commit is contained in:
Patrick O'brien 2016-05-06 19:31:51 +10:00
parent 92e2608a1b
commit 8f3ff66339
6 changed files with 76 additions and 32 deletions

View file

@ -90,6 +90,7 @@ var sqlBoilerTemplateFuncs = template.FuncMap{
"titleCase": strmangle.TitleCase,
"titleCaseSingular": strmangle.TitleCaseSingular,
"titleCasePlural": strmangle.TitleCasePlural,
"titleCaseCommaList": strmangle.TitleCaseCommaList,
"camelCase": strmangle.CamelCase,
"camelCaseSingular": strmangle.CamelCaseSingular,
"camelCasePlural": strmangle.CamelCasePlural,

View file

@ -4,7 +4,7 @@
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
// {{$tableNameSingular}}Find retrieves a single record by ID.
func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
return {{$tableNameSingular}}FindX(boil.GetDB(), {{camelCaseCommaList .Table.PKey.Columns}}, selectCols...)
return {{$tableNameSingular}}FindX(boil.GetDB(), {{camelCaseCommaList "" .Table.PKey.Columns}}, selectCols...)
}
func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
@ -12,7 +12,7 @@ func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.
mods := []qs.QueryMod{
qs.Select(selectCols...),
qs.Table("{{.Table.Name}}"),
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList .Table.PKey.Columns}}),
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList "" .Table.PKey.Columns}}),
}
q := NewQueryX(exec, mods...)

View file

@ -11,36 +11,36 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
}
var wl []string
wl = append(wl, whitelist...)
if len(whitelist) == 0 {
whitelist = {{$varNameSingular}}ColumnsWithoutDefault
wl = append(wl, {{$varNameSingular}}ColumnsWithoutDefault...)
}
nzDefaultSet := boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o)
if len(nzDefaultSet) != 0 {
whitelist = append(nzDefaultSet, whitelist...)
}
wl = append(boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o), wl...)
// Only return the columns with default values that are not in the insert whitelist
returnColumns := boil.SetComplement({{$varNameSingular}}ColumnsWithDefault, whitelist)
returnColumns := boil.SetComplement({{$varNameSingular}}ColumnsWithDefault, wl)
var err error
if err := o.doBeforeCreateHooks(); err != nil {
return err
}
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} (%s) VALUES (%s)`, strings.Join(whitelist, ","), boil.GenerateParamFlags(len(whitelist), 1))
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} (%s) VALUES (%s)`, strings.Join(wl, ","), boil.GenerateParamFlags(len(wl), 1))
{{if supportsResultObject .DriverName}}
if len(returnColumns) != 0 {
result, err := exec.Exec(ins, boil.GetStructValues(o, whitelist...))
result, err := exec.Exec(ins, boil.GetStructValues(o, wl...))
if err != nil {
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)
}
lastId, err := result.lastInsertId()
if err != nil || lastId == 0 {
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, ","), boil.WhereClause(whitelist))
rows, err := exec.Query(sel, boil.GetStructValues(o, whitelist...))
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, ","), boil.WhereClause(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)
}
@ -58,12 +58,12 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s=$1`, strings.Join(returnColumns, ","), {{$varNameSingular}}AutoIncPrimaryKey, lastId)
}
} else {
_, err = exec.Exec(ins, boil.GetStructValues(o, whitelist...))
_, err = exec.Exec(ins, boil.GetStructValues(o, wl...))
}
{{else}}
if len(returnColumns) != 0 {
ins = ins + fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ","))
err = exec.QueryRow(ins, boil.GetStructValues(o, whitelist...)).Scan(boil.GetStructPointers(o, returnColumns...))
err = exec.QueryRow(ins, boil.GetStructValues(o, wl...)).Scan(boil.GetStructPointers(o, returnColumns...))
} else {
_, err = exec.Exec(ins, {{insertParamVariables "o." .Table.Columns}})
}

View file

@ -11,15 +11,27 @@ func (o *{{$tableNameSingular}}) Update(whitelist ... string) error {
}
func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... string) error {
return o.UpdateAtX(exec, {{titleCaseCommaList "o." .Table.PKey.Columns}}, whitelist...)
}
func (o *{{$tableNameSingular}}) UpdateAt({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, whitelist ...string) error {
return o.UpdateAtX(boil.GetDB(), {{camelCaseCommaList "" .Table.PKey.Columns}}, whitelist...)
}
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, whitelist ...string) error {
if err := o.doBeforeUpdateHooks(); err != nil {
return err
}
if len(whitelist) == 0 {
whitelist = {{$varNameSingular}}ColumnsWithoutDefault
whitelist = append(boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o), whitelist...)
whitelist = boil.SetComplement(whitelist, {{$varNameSingular}}PrimaryKeyColumns)
whitelist = boil.SetComplement(whitelist, {{$varNameSingular}}AutoIncrementColumns)
cols := {{$varNameSingular}}ColumnsWithoutDefault
cols = append(boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o), cols...)
// Subtract primary keys and autoincrement columns
cols = boil.SetComplement(cols, {{$varNameSingular}}PrimaryKeyColumns)
cols = boil.SetComplement(cols, {{$varNameSingular}}AutoIncrementColumns)
whitelist = make([]string, len(cols))
copy(whitelist, cols)
}
var err error
@ -27,7 +39,7 @@ func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... strin
query := fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE %s`, boil.SetParamNames(whitelist), boil.WherePrimaryKey(len(whitelist)+1, {{commaList .Table.PKey.Columns}}))
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...), {{paramsPrimaryKey "o." .Table.PKey.Columns true}})
} else {
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}}, could not build a whitelist for row: %s")
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}}, could not build a whitelist for row: %s", err)
}
if err != nil {
@ -41,14 +53,6 @@ func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... strin
return nil
}
func (o *{{$tableNameSingular}}) UpdateAt({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, whitelist ...string) error {
return o.UpdateAtX(boil.GetDB(), {{camelCaseCommaList .Table.PKey.Columns}}, whitelist...)
}
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, whitelist ...string) error {
return nil
}
func (v {{$varNameSingular}}Query) UpdateAll(cols M) error {
return nil
}

View file

@ -100,12 +100,24 @@ func CamelCasePlural(name string) string {
}
// CamelCaseCommaList generates a list of comma seperated camel cased column names
// example: thingName, stuffName, etc
func CamelCaseCommaList(pkeyColumns []string) string {
// example: thingName, o.stuffName, etc
func CamelCaseCommaList(prefix string, cols []string) string {
var output []string
for _, c := range pkeyColumns {
output = append(output, CamelCase(c))
for _, c := range cols {
output = append(output, prefix+CamelCase(c))
}
return strings.Join(output, ", ")
}
// TitleCaseCommaList generates a list of comma seperated title cased column names
// example: o.ThingName, o.Stuff, ThingStuff, etc
func TitleCaseCommaList(prefix string, cols []string) string {
var output []string
for _, c := range cols {
output = append(output, prefix+TitleCase(c))
}
return strings.Join(output, ", ")

View file

@ -41,6 +41,27 @@ func TestCommaList(t *testing.T) {
}
}
func TestTitleCaseCommaList(t *testing.T) {
cols := []string{
"test_id",
"test_thing",
"test_stuff_thing",
"test",
}
x := TitleCaseCommaList("", cols)
expected := `TestID, TestThing, TestStuffThing, Test`
if x != expected {
t.Errorf("Expected %s, got %s", expected, x)
}
x = TitleCaseCommaList("o.", cols)
expected = `o.TestID, o.TestThing, o.TestStuffThing, o.Test`
if x != expected {
t.Errorf("Expected %s, got %s", expected, x)
}
}
func TestCamelCaseCommaList(t *testing.T) {
cols := []string{
"test_id",
@ -49,11 +70,17 @@ func TestCamelCaseCommaList(t *testing.T) {
"test",
}
x := CamelCaseCommaList(cols)
x := CamelCaseCommaList("", cols)
expected := `testID, testThing, testStuffThing, test`
if x != expected {
t.Errorf("Expected %s, got %s", expected, x)
}
x = CamelCaseCommaList("o.", cols)
expected = `o.testID, o.testThing, o.testStuffThing, o.test`
if x != expected {
t.Errorf("Expected %s, got %s", expected, x)
}
}
func TestAutoIncPrimaryKey(t *testing.T) {