2016-09-09 22:31:51 +10:00
{ { - / * Begin execution of template for many - to - one or many - to - many relationship helper * / - } }
2016-06-22 22:03:05 -07:00
{ { - if . Table . IsJoinTable - } }
{ { - else - } }
2016-09-14 18:08:30 +10:00
{ { - $dot : = . - } }
{ { - $table : = . Table - } }
{ { - range . Table . ToManyRelationships - } }
{ { - $varNameSingular : = . ForeignTable | singular | camelCase - } }
{ { - if ( and . ForeignColumnUnique ( not . ToJoinTable ) ) - } }
{ { - / * Begin execution of template for many - to - one relationship . * / - } }
{ { - $txt : = textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . - } }
{ { - template "relationship_to_one_helper" ( preserveDot $dot $txt ) - } }
{ { - else - } }
{ { - / * Begin execution of template for many - to - many relationship . * / - } }
{ { - $rel : = textsFromRelationship $dot.Tables $table . - } }
{ { - $schemaForeignTable : = . ForeignTable | $dot.SchemaTable - } }
2016-08-01 15:10:10 +10:00
// { { $rel.Function.Name } } G retrieves all the { { $rel.LocalTable.NameSingular } } 's { { $rel.ForeignTable.NameHumanReadable } }
2016-07-13 20:31:44 -07:00
{ { - if not ( eq $rel.Function.Name $rel.ForeignTable.NamePluralGo ) } } via { { . ForeignColumn } } column { { - end } } .
2016-08-18 00:19:15 -07:00
func ( { { $rel.Function.Receiver } } * { { $rel.LocalTable.NameGo } } ) { { $rel.Function.Name } } G(mods ...qm.QueryMod) { { $varNameSingular } } Query {
2016-09-14 18:08:30 +10:00
return { { $rel.Function.Receiver } } . { { $rel.Function.Name } } (boil.GetDB(), mods...)
2016-07-11 15:23:47 -07:00
}
2016-08-01 15:10:10 +10:00
// { { $rel.Function.Name } } retrieves all the { { $rel.LocalTable.NameSingular } } 's { { $rel.ForeignTable.NameHumanReadable } } with an executor
2016-07-13 20:31:44 -07:00
{ { - if not ( eq $rel.Function.Name $rel.ForeignTable.NamePluralGo ) } } via { { . ForeignColumn } } column { { - end } } .
2016-08-18 00:19:15 -07:00
func ( { { $rel.Function.Receiver } } * { { $rel.LocalTable.NameGo } } ) { { $rel.Function.Name } } (exec boil.Executor, mods ...qm.QueryMod) { { $varNameSingular } } Query {
2016-09-14 18:08:30 +10:00
queryMods := []qm.QueryMod {
qm.Select(" { { id 0 | $dot.Quotes } } .*"),
}
2016-06-26 16:18:23 -07:00
2016-09-14 18:08:30 +10:00
if len(mods) != 0 {
queryMods = append(queryMods, mods...)
}
2016-08-03 22:36:08 -07:00
2016-09-14 18:08:30 +10:00
{ { if . ToJoinTable - } }
queryMods = append(queryMods,
qm.InnerJoin(" { { . JoinTable | $dot.SchemaTable } } as { { id 1 | $dot.Quotes } } on { { id 0 | $dot.Quotes } } . { { . ForeignColumn | $dot.Quotes } } = { { id 1 | $dot.Quotes } } . { { . JoinForeignColumn | $dot.Quotes } } "),
qm.Where(" { { id 1 | $dot.Quotes } } . { { . JoinLocalColumn | $dot.Quotes } } = { { if $dot.Dialect.IndexPlaceholders } } $1 { { else } } ? { { end } } ", { { $rel.Function.Receiver } } . { { $rel.LocalTable.ColumnNameGo } } ),
)
{ { else - } }
queryMods = append(queryMods,
qm.Where(" { { id 0 | $dot.Quotes } } . { { . ForeignColumn | $dot.Quotes } } = { { if $dot.Dialect.IndexPlaceholders } } $1 { { else } } ? { { end } } ", { { $rel.Function.Receiver } } . { { $rel.LocalTable.ColumnNameGo } } ),
)
{ { end } }
2016-06-26 16:18:23 -07:00
2016-09-14 18:08:30 +10:00
query := { { $rel.ForeignTable.NamePluralGo } } (exec, queryMods...)
2016-09-14 20:59:55 -07:00
queries.SetFrom(query.Query, " { { $schemaForeignTable } } as { { id 0 | $dot.Quotes } } ")
2016-09-14 18:08:30 +10:00
return query
2016-06-26 16:18:23 -07:00
}
2016-07-16 21:22:57 +10:00
2016-07-17 17:50:01 -07:00
{ { end - } } { { - / * if unique foreign key * / - } }
{ { - end - } } { { - / * range relationships * / - } }
2016-09-09 22:31:51 +10:00
{ { - end - } } { { - / * if isJoinTable * / - } }