diff --git a/templates/05_relationship_to_many.tpl b/templates/05_relationship_to_many.tpl index aa3abd4..fc4d046 100644 --- a/templates/05_relationship_to_many.tpl +++ b/templates/05_relationship_to_many.tpl @@ -7,22 +7,24 @@ {{- $colName := $localTableSing | printf "%s_id" -}} {{- $receiver := .Table.Name | toLower | substring 0 1 -}} {{- range toManyRelationships .Table.Name .Tables -}} - {{- $foreignTableSing := .ForeignTable | singular}} - {{- $foreignTable := $foreignTableSing | titleCase}} - {{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}} - {{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}} - {{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}} - {{- $isNormal := eq $colName .ForeignColumn -}} + {{- if .ToJoinTable -}} + {{- else -}} + {{- $foreignTableSing := .ForeignTable | singular}} + {{- $foreignTable := $foreignTableSing | titleCase}} + {{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}} + {{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}} + {{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}} + {{- $isNormal := eq $colName .ForeignColumn -}} - {{- if $isNormal -}} + {{- if $isNormal -}} // {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}. func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}( - {{- else -}} - {{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}} + {{- else -}} + {{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}} // {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column. func ({{$receiver}} *{{$localTable}}) {{$fnName}}( - {{- end -}} + {{- end -}} exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) { var ret {{$foreignSlice}} @@ -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 */}} diff --git a/templates/07_find.tpl b/templates/07_find.tpl index 1bae7c7..1a40f67 100644 --- a/templates/07_find.tpl +++ b/templates/07_find.tpl @@ -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...) diff --git a/templates/08_insert.tpl b/templates/08_insert.tpl index b3aa04c..a5201d7 100644 --- a/templates/08_insert.tpl +++ b/templates/08_insert.tpl @@ -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) diff --git a/templates/09_update.tpl b/templates/09_update.tpl index def2ce7..98234db 100644 --- a/templates/09_update.tpl +++ b/templates/09_update.tpl @@ -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 }