Use new templating features in all templates

- Add another check for join tables in the to_many relationships stuff.
- Fix a number of quoting bugs in the templates.
This commit is contained in:
Aaron L 2016-06-26 23:56:05 -07:00
parent 6dac631a93
commit c731661261
4 changed files with 30 additions and 54 deletions

View file

@ -7,6 +7,8 @@
{{- $colName := $localTableSing | printf "%s_id" -}}
{{- $receiver := .Table.Name | toLower | substring 0 1 -}}
{{- range toManyRelationships .Table.Name .Tables -}}
{{- if .ToJoinTable -}}
{{- else -}}
{{- $foreignTableSing := .ForeignTable | singular}}
{{- $foreignTable := $foreignTableSing | titleCase}}
{{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}}
@ -51,38 +53,6 @@ exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
return ret, nil
}
{{end -}}
{{- end -}}
{{/*
// Challengee fetches the Video pointed to by the foreign key.
func (b *Battle) Challengee(exec boil.Executor, selectCols ...string) (*Video, error) {
video := &Video{}
query := fmt.Sprintf(`select %s from videos where id = $1`, strings.Join(selectCols, `,`))
err := exec.QueryRow(query, b.ChallengeeID).Scan(boil.GetStructPointers(video, selectCols...)...)
if err != nil {
return nil, fmt.Errorf(`models: unable to select from videos: %v`, err)
}
return video, nil
}
{{- range .Table.FKeys -}}
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
{{- $foreignColumn := .Column | remove "_id" | singular | titleCase -}}
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
{{- $varname := .ForeignTable | singular | camelCase -}}
{{- $receiver := $localTable | toLower | substring 0 1 -}}
// {{$foreignColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
func ({{$receiver}} *{{$localTable}}) {{$foreignColumn}}(exec boil.Executor, selectCols ...string) (*{{$foreignTable}}, error) {
{{$varname}} := &{{$foreignTable}}{}
query := fmt.Sprintf(`select %s from {{.ForeignTable}} where id = $1`, strings.Join(selectCols, `,`))
err := exec.QueryRow(query, {{$receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$varname}}, selectCols...)...)
if err != nil {
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
}
return {{$varname}}, nil
}
*/}}
{{end -}}{{/* if join table */}}
{{- end -}}{{/* range relationships */}}
{{- end -}}{{/* outer if join table */}}

View file

@ -1,18 +1,21 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $dbName := singular .Table.Name -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
// {{$tableNameSingular}}Find retrieves a single record by ID.
func {{$tableNameSingular}}Find({{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " " }}, selectCols ...string) (*{{$tableNameSingular}}, error) {
return {{$tableNameSingular}}FindX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, selectCols...)
func {{$tableNameSingular}}Find({{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
return {{$tableNameSingular}}FindX(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
}
func {{$tableNameSingular}}FindX(exec boil.Executor, {{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
func {{$tableNameSingular}}FindX(exec boil.Executor, {{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
mods := []qm.QueryMod{
qm.Select(selectCols...),
qm.Table("{{.Table.Name}}"),
qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}),
qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{$pkNames | join ", "}}),
}
q := NewQueryX(exec, mods...)

View file

@ -29,7 +29,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
return err
}
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} (%s) VALUES (%s)`, strings.Join(wl, ","), boil.GenerateParamFlags(len(wl), 1))
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), boil.GenerateParamFlags(len(wl), 1))
{{if driverUsesLastInsertID .DriverName}}
if len(returnColumns) != 0 {
@ -40,7 +40,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
lastId, err := result.lastInsertId()
if err != nil || lastId == 0 {
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, ","), boil.WhereClause(wl))
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)

View file

@ -1,5 +1,8 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
// Update a single {{$tableNameSingular}} record. It takes a whitelist of
// column_name's that should be updated. The primary key will be used to find
// the record to update.
@ -15,12 +18,12 @@ func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... strin
}
// UpdateAt updates the {{$tableNameSingular}} using the primary key to find the row to update.
func (o *{{$tableNameSingular}}) UpdateAt({{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, whitelist ...string) error {
return o.UpdateAtX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, whitelist...)
func (o *{{$tableNameSingular}}) UpdateAt({{$pkArgs}}, whitelist ...string) error {
return o.UpdateAtX(boil.GetDB(), {{$pkNames | join ", "}}, whitelist...)
}
// UpdateAtX uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{sqlColDefinitions .Table.Columns .Table.PKey.Columns | sqlColDefStrings | join " "}}, whitelist ...string) error {
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{$pkArgs}}, whitelist ...string) error {
if err := o.doBeforeUpdateHooks(); err != nil {
return err
}