sqlboiler/templates/05_relationship_to_many.tpl

47 lines
2.1 KiB
Smarty
Raw Normal View History

2016-06-23 07:03:05 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
2016-08-08 17:18:16 +02:00
{{- $dot := . -}}
{{- $table := .Table -}}
{{- range .Table.ToManyRelationships -}}
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
{{- template "relationship_to_one_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
2016-08-01 07:10:10 +02:00
// {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
2016-08-04 07:36:08 +02:00
return {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), mods...)
2016-07-12 00:23:47 +02:00
}
2016-08-01 07:10:10 +02:00
// {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query {
2016-08-04 07:36:08 +02:00
queryMods := []qm.QueryMod{
qm.Select(`"{{id 0}}".*`),
}
2016-08-04 07:36:08 +02:00
if len(mods) != 0 {
queryMods = append(queryMods, mods...)
}
2016-08-04 07:36:08 +02:00
2016-07-02 01:20:59 +02:00
{{if .ToJoinTable -}}
2016-08-04 07:36:08 +02:00
queryMods = append(queryMods,
qm.InnerJoin(`"{{.JoinTable}}" as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}"`),
2016-08-27 21:00:42 +02:00
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}),
2016-08-04 07:36:08 +02:00
)
2016-07-02 01:20:59 +02:00
{{else -}}
2016-08-04 07:36:08 +02:00
queryMods = append(queryMods,
2016-08-27 21:00:42 +02:00
qm.Where(`"{{id 0}}"."{{.ForeignColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}),
2016-08-04 07:36:08 +02:00
)
2016-07-02 01:20:59 +02:00
{{end}}
2016-08-04 07:36:08 +02:00
query := {{$rel.ForeignTable.NamePluralGo}}(exec, queryMods...)
boil.SetFrom(query.Query, `"{{.ForeignTable}}" as "{{id 0}}"`)
return query
}
{{end -}}{{- /* if unique foreign key */ -}}
{{- end -}}{{- /* range relationships */ -}}
2016-07-02 01:20:59 +02:00
{{- end -}}{{- /* outer if join table */ -}}