Finish to_many for join tables.

This commit is contained in:
Aaron L 2016-07-01 16:20:59 -07:00
parent ce30724f98
commit 0df0f964e7
2 changed files with 9 additions and 7 deletions

View file

@ -110,6 +110,7 @@ var templateFunctions = template.FuncMap{
"replace": func(rep, with, str string) string { return strings.Replace(str, rep, with, -1) }, "replace": func(rep, with, str string) string { return strings.Replace(str, rep, with, -1) },
"prefix": func(add, str string) string { return fmt.Sprintf("%s%s", add, str) }, "prefix": func(add, str string) string { return fmt.Sprintf("%s%s", add, str) },
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) }, "quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
"id": strmangle.Identifier,
// Pluralization // Pluralization
"singular": strmangle.Singular, "singular": strmangle.Singular,

View file

@ -7,16 +7,14 @@
{{- $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 -}}
{{- if .ToJoinTable -}}
{{- else -}}
{{- $foreignTableSing := .ForeignTable | singular}} {{- $foreignTableSing := .ForeignTable | singular}}
{{- $foreignTable := $foreignTableSing | titleCase}} {{- $foreignTable := $foreignTableSing | titleCase}}
{{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}} {{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}}
{{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}} {{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}}
{{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}} {{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}}
{{- $isNormal := eq $colName .ForeignColumn -}} {{- $isForeignKeySimplyTableName := or (eq $colName .ForeignColumn) .ToJoinTable -}}
{{- if $isNormal -}} {{- if $isForeignKeySimplyTableName -}}
// {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}. // {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}.
func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}( func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}(
@ -29,7 +27,11 @@ func ({{$receiver}} *{{$localTable}}) {{$fnName}}(
exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) { exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
var ret {{$foreignSlice}} var ret {{$foreignSlice}}
{{if .ToJoinTable -}}
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} "{{id 0}}" inner join {{.JoinTable}} as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}"=$1`, `"{{id 1}}".` + strings.Join(selectCols, `","{{id 0}}"."`))
{{else -}}
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where "{{.ForeignColumn}}"=$1`, strings.Join(selectCols, `","`)) query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where "{{.ForeignColumn}}"=$1`, strings.Join(selectCols, `","`))
{{end}}
rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $receiver }}) rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $receiver }})
if err != nil { if err != nil {
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err) return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
@ -53,6 +55,5 @@ exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
return ret, nil return ret, nil
} }
{{end -}}{{/* if join table */}} {{end -}}{{- /* range relationships */ -}}
{{- end -}}{{/* range relationships */}} {{- end -}}{{- /* outer if join table */ -}}
{{- end -}}{{/* outer if join table */}}