Start of the to_many relationships.
This commit is contained in:
parent
4d0742b6f3
commit
84a160f3a4
10 changed files with 145 additions and 0 deletions
52
dbdrivers/relationships.go
Normal file
52
dbdrivers/relationships.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package dbdrivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nullbio/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
// ToManyRelationship describes a relationship between two tables where the
|
||||
// local table has no id, and the foreign table has an id that matches a column
|
||||
// in the local table.
|
||||
type ToManyRelationship struct {
|
||||
Name string
|
||||
ForeignTable string
|
||||
ForeignColumn string
|
||||
}
|
||||
|
||||
// ToManyRelationships relationship lookups
|
||||
// Input should be the sql name of a table like: videos
|
||||
func ToManyRelationships(table string, tables []Table) []ToManyRelationship {
|
||||
var relationships []ToManyRelationship
|
||||
|
||||
for _, t := range tables {
|
||||
if t.Name == table {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, f := range t.FKeys {
|
||||
if f.ForeignTable != table {
|
||||
continue
|
||||
}
|
||||
|
||||
singularName := strmangle.Singular(table)
|
||||
standardColName := fmt.Sprintf("%s_id", singularName)
|
||||
|
||||
relationship := ToManyRelationship{
|
||||
ForeignTable: t.Name,
|
||||
ForeignColumn: f.Column,
|
||||
}
|
||||
|
||||
if standardColName == f.ForeignColumn {
|
||||
relationship.Name = strmangle.TitleCase(strmangle.Plural(name))
|
||||
} else {
|
||||
relationship.Name = strmangle.TitleCase(strmangle.Plural(name))
|
||||
}
|
||||
|
||||
relationships = append(relationships, relationship)
|
||||
}
|
||||
}
|
||||
|
||||
return relationships
|
||||
}
|
36
dbdrivers/relationships_test.go
Normal file
36
dbdrivers/relationships_test.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package dbdrivers
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestToManyRelationships(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []Table{
|
||||
Table{
|
||||
Name: "videos",
|
||||
FKeys: []ForeignKey{
|
||||
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignKey: "id"},
|
||||
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignKey: "id"},
|
||||
},
|
||||
},
|
||||
Table{
|
||||
Name: "notifications",
|
||||
FKeys: []ForeignKey{
|
||||
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignKey: "id"},
|
||||
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignKey: "id"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
relationships := ToManyRelationships("users", tables)
|
||||
r := relationships[0]
|
||||
if r.Name != "Videos" {
|
||||
t.Error("wrong name:", r.Name)
|
||||
}
|
||||
if r.ForeignTable != "videos" {
|
||||
t.Error("wrong foreign table:", r.ForeignTable)
|
||||
}
|
||||
if r.ForeignColumn != "user_id" {
|
||||
t.Error("wrong foreign column:", r.ForeignColumn)
|
||||
}
|
||||
}
|
|
@ -102,6 +102,7 @@ func (s *State) Run(includeTests bool) error {
|
|||
}
|
||||
|
||||
data := &templateData{
|
||||
Tables: s.Tables,
|
||||
Table: table,
|
||||
DriverName: s.Config.DriverName,
|
||||
PkgName: s.Config.PkgName,
|
||||
|
|
56
templates/05_relationship_to_many.tpl
Normal file
56
templates/05_relationship_to_many.tpl
Normal file
|
@ -0,0 +1,56 @@
|
|||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $pkg := .PkgName -}}
|
||||
{{- $localTable := .Table.Name -}}
|
||||
{{- $ltable := .Table.Name | singular | titleCase -}}
|
||||
{{- range $table := .Tables -}}
|
||||
{{- if eq $table.Name $localTable -}}
|
||||
{{- else -}}
|
||||
{{ range $fkey := .FKeys -}}
|
||||
{{- if eq $localTable $fkey.ForeignTable -}}
|
||||
{{- $ftable := $table.Name | plural | titleCase -}}
|
||||
{{- $recv := $localTable | substring 0 1 | toLower -}}
|
||||
{{- $fn := $ftable -}}
|
||||
{{- $col := $localTable | singular | printf "%s_id" -}}
|
||||
{{- if eq $col $fkey.Column -}}
|
||||
{{- $col := $localTable -}}
|
||||
{{- end -}}
|
||||
func ({{$recv}} *{{$ltable}}) {{$fn}}
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{/*
|
||||
// Challengee fetches the Video pointed to by the foreign key.
|
||||
func (b *Battle) Challengee(exec boil.Executor, selectCols ...string) (*Video, error) {
|
||||
video := &Video{}
|
||||
|
||||
query := fmt.Sprintf(`select %s from videos where id = $1`, strings.Join(selectCols, `,`))
|
||||
err := exec.QueryRow(query, b.ChallengeeID).Scan(boil.GetStructPointers(video, selectCols...)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`models: unable to select from videos: %v`, err)
|
||||
}
|
||||
|
||||
return video, nil
|
||||
}
|
||||
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
|
||||
{{- $foreignColumn := .Column | remove "_id" | singular | titleCase -}}
|
||||
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
|
||||
{{- $varname := .ForeignTable | singular | camelCase -}}
|
||||
{{- $receiver := $localTable | toLower | substring 0 1 -}}
|
||||
// {{$foreignColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
|
||||
func ({{$receiver}} *{{$localTable}}) {{$foreignColumn}}(exec boil.Executor, selectCols ...string) (*{{$foreignTable}}, error) {
|
||||
{{$varname}} := &{{$foreignTable}}{}
|
||||
|
||||
query := fmt.Sprintf(`select %s from {{.ForeignTable}} where id = $1`, strings.Join(selectCols, `,`))
|
||||
err := exec.QueryRow(query, {{$receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$varname}}, selectCols...)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
|
||||
}
|
||||
|
||||
return {{$varname}}, nil
|
||||
}
|
||||
*/}}
|
Loading…
Reference in a new issue