2016-06-23 07:03:05 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
2016-06-27 01:18:23 +02:00
|
|
|
{{- $dot := . }}
|
|
|
|
{{- $table := .Table }}
|
|
|
|
{{- $localTableSing := .Table.Name | singular -}}
|
|
|
|
{{- $localTable := $localTableSing | titleCase -}}
|
|
|
|
{{- $colName := $localTableSing | printf "%s_id" -}}
|
|
|
|
{{- $receiver := .Table.Name | toLower | substring 0 1 -}}
|
|
|
|
{{- range toManyRelationships .Table.Name .Tables -}}
|
2016-06-27 08:56:05 +02:00
|
|
|
{{- $foreignTableSing := .ForeignTable | singular}}
|
|
|
|
{{- $foreignTable := $foreignTableSing | titleCase}}
|
|
|
|
{{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}}
|
|
|
|
{{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}}
|
|
|
|
{{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}}
|
2016-07-02 01:20:59 +02:00
|
|
|
{{- $isForeignKeySimplyTableName := or (eq $colName .ForeignColumn) .ToJoinTable -}}
|
2016-06-27 08:56:05 +02:00
|
|
|
|
2016-07-02 01:20:59 +02:00
|
|
|
{{- if $isForeignKeySimplyTableName -}}
|
2016-06-27 01:18:23 +02:00
|
|
|
// {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}.
|
|
|
|
func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}(
|
|
|
|
|
2016-06-27 08:56:05 +02:00
|
|
|
{{- else -}}
|
|
|
|
{{- $fnName := .ForeignColumn | remove "_id" | titleCase | printf "%[2]s%[1]s" $foreignPluralNoun -}}
|
2016-06-27 01:18:23 +02:00
|
|
|
// {{$fnName}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}} via {{.ForeignColumn}} column.
|
|
|
|
func ({{$receiver}} *{{$localTable}}) {{$fnName}}(
|
|
|
|
|
2016-07-09 19:13:35 +02:00
|
|
|
{{- end -}}
|
2016-06-27 01:18:23 +02:00
|
|
|
exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) {
|
|
|
|
var ret {{$foreignSlice}}
|
|
|
|
|
2016-07-02 01:20:59 +02:00
|
|
|
{{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 -}}
|
2016-06-27 01:18:23 +02:00
|
|
|
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where "{{.ForeignColumn}}"=$1`, strings.Join(selectCols, `","`))
|
2016-07-02 01:20:59 +02:00
|
|
|
{{end}}
|
2016-06-27 01:18:23 +02:00
|
|
|
rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $receiver }})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
next := new({{$foreignTable}})
|
|
|
|
|
|
|
|
err = rows.Scan(boil.GetStructPointers(next, selectCols...)...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to scan into {{$foreignTable}}: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, next)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-07-02 01:20:59 +02:00
|
|
|
{{end -}}{{- /* range relationships */ -}}
|
|
|
|
{{- end -}}{{- /* outer if join table */ -}}
|