2016-06-23 07:03:05 +02:00
{ { - if . Table . IsJoinTable - } }
{ { - else - } }
2016-06-27 01:18:23 +02:00
{ { - $dot : = . } }
{ { - $table : = . Table } }
2016-07-16 22:02:09 +02:00
{ { - range . Table . ToManyRelationships - } }
2016-07-18 02:50:01 +02:00
{ { - if . ForeignColumnUnique - } }
{ { - template "relationship_to_one_helper" ( textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . ) - } }
{ { - else - } }
2016-07-14 05:31:44 +02:00
{ { - $rel : = textsFromRelationship $dot.Tables $table . - } }
// { { $rel.Function.Name } } 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 } } (selectCols ...string) ( { { $rel.ForeignTable.Slice } } , error) {
return { { $rel.Function.Receiver } } . { { $rel.Function.Name } } X(boil.GetDB(), selectCols...)
2016-07-12 00:23:47 +02:00
}
2016-07-16 13:22:57 +02:00
// { { $rel.Function.Name } } P panics on error. 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 } } P(selectCols ...string) { { $rel.ForeignTable.Slice } } {
o, err := { { $rel.Function.Receiver } } . { { $rel.Function.Name } } X(boil.GetDB(), selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
2016-07-18 02:50:01 +02:00
// { { $rel.Function.Name } } XP panics on error. 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 } } XP(exec boil.Executor, selectCols ...string) { { $rel.ForeignTable.Slice } } {
o, err := { { $rel.Function.Receiver } } . { { $rel.Function.Name } } X(exec, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
2016-07-14 05:31:44 +02:00
// { { $rel.Function.Name } } X 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 } } X(exec boil.Executor, selectCols ...string) ( { { $rel.ForeignTable.Slice } } , error) {
var ret { { $rel.ForeignTable.Slice } }
2016-06-27 01:18:23 +02:00
2016-07-15 08:28:33 +02:00
selectColumns := `" { { id 0 } } ".*`
2016-07-15 06:48:59 +02:00
if len(selectCols) != 0 {
2016-07-14 05:31:44 +02:00
selectColumns = `" { { id 0 } } ".` + strings.Join(selectCols, `"," { { id 0 } } "."`)
}
2016-07-02 01:20:59 +02:00
{ { if . ToJoinTable - } }
2016-07-14 05:31:44 +02:00
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`, selectColumns)
2016-07-02 01:20:59 +02:00
{ { else - } }
2016-07-14 05:31:44 +02:00
query := fmt.Sprintf(`select %s from { { . ForeignTable } } " { { id 0 } } " where " { { id 0 } } "." { { . ForeignColumn } } "=$1`, selectColumns)
2016-07-02 01:20:59 +02:00
{ { end } }
2016-07-14 05:31:44 +02:00
rows, err := exec.Query(query, { { . Column | titleCase | printf "%s.%s" $rel.Function.Receiver } } )
2016-06-27 01:18:23 +02:00
if err != nil {
return nil, fmt.Errorf(` { { $dot.PkgName } } : unable to select from { { . ForeignTable } } : %v`, err)
}
defer rows.Close()
for rows.Next() {
2016-07-14 05:31:44 +02:00
next := new( { { $rel.ForeignTable.NameGo } } )
2016-06-27 01:18:23 +02:00
err = rows.Scan(boil.GetStructPointers(next, selectCols...)...)
if err != nil {
2016-07-14 05:31:44 +02:00
return nil, fmt.Errorf(` { { $dot.PkgName } } : unable to scan into { { $rel.ForeignTable.NameGo } } : %v`, err)
2016-06-27 01:18:23 +02:00
}
ret = append(ret, next)
}
if err = rows.Err(); err != nil {
2016-07-14 05:31:44 +02:00
return nil, fmt.Errorf(` { { $dot.PkgName } } : unable to select from { { $rel.ForeignTable.NameGo } } : %v`, err)
2016-06-27 01:18:23 +02:00
}
return ret, nil
}
2016-07-16 13:22:57 +02:00
2016-07-18 02:50:01 +02:00
{ { end - } } { { - / * if unique foreign key * / - } }
{ { - end - } } { { - / * range relationships * / - } }
2016-07-02 01:20:59 +02:00
{ { - end - } } { { - / * outer if join table * / - } }