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,22 +7,24 @@
{{- $colName := $localTableSing | printf "%s_id" -}} {{- $colName := $localTableSing | printf "%s_id" -}}
{{- $receiver := .Table.Name | toLower | substring 0 1 -}} {{- $receiver := .Table.Name | toLower | substring 0 1 -}}
{{- range toManyRelationships .Table.Name .Tables -}} {{- range toManyRelationships .Table.Name .Tables -}}
{{- $foreignTableSing := .ForeignTable | singular}} {{- if .ToJoinTable -}}
{{- $foreignTable := $foreignTableSing | titleCase}} {{- else -}}
{{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}} {{- $foreignTableSing := .ForeignTable | singular}}
{{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}} {{- $foreignTable := $foreignTableSing | titleCase}}
{{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}} {{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}}
{{- $isNormal := eq $colName .ForeignColumn -}} {{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}}
{{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}}
{{- $isNormal := eq $colName .ForeignColumn -}}
{{- if $isNormal -}} {{- if $isNormal -}}
// {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}. // {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}.
func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}( func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}(
{{- else -}} {{- else -}}
{{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}} {{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}}
// {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column. // {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column.
func ({{$receiver}} *{{$localTable}}) {{$fnName}}( func ({{$receiver}} *{{$localTable}}) {{$fnName}}(
{{- end -}} {{- end -}}
exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) { exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
var ret {{$foreignSlice}} var ret {{$foreignSlice}}
@ -51,38 +53,6 @@ exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
return ret, nil return ret, nil
} }
{{end -}} {{end -}}{{/* if join table */}}
{{- end -}} {{- end -}}{{/* range relationships */}}
{{/* {{- end -}}{{/* outer if join table */}}
// 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
}
*/}}

View file

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

View file

@ -29,7 +29,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
return err 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 driverUsesLastInsertID .DriverName}}
if len(returnColumns) != 0 { if len(returnColumns) != 0 {
@ -40,7 +40,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
lastId, err := result.lastInsertId() lastId, err := result.lastInsertId()
if err != nil || lastId == 0 { 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...)...) rows, err := exec.Query(sel, boil.GetStructValues(o, wl...)...)
if err != nil { if err != nil {
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err) return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)

View file

@ -1,5 +1,8 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $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 // 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 // column_name's that should be updated. The primary key will be used to find
// the record to update. // 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. // 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 { func (o *{{$tableNameSingular}}) UpdateAt({{$pkArgs}}, whitelist ...string) error {
return o.UpdateAtX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, whitelist...) 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. // 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 { if err := o.doBeforeUpdateHooks(); err != nil {
return err return err
} }