Add more text helpers for ToMany.

This commit is contained in:
Aaron L 2016-08-27 12:00:42 -07:00
parent cc5ce81b8e
commit ea22d3656c
4 changed files with 14 additions and 4 deletions

View file

@ -28,11 +28,11 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
{{if .ToJoinTable -}}
queryMods = append(queryMods,
qm.InnerJoin(`"{{.JoinTable}}" as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}"`),
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }}),
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}),
)
{{else -}}
queryMods = append(queryMods,
qm.Where(`"{{id 0}}"."{{.ForeignColumn}}"=$1`, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }}),
qm.Where(`"{{id 0}}"."{{.ForeignColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}),
)
{{end}}

View file

@ -41,11 +41,11 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
}
{{if .ToJoinTable -}}
_, err = tx.Exec(`insert into {{.JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{.Column | titleCase}}, b.{{.ForeignColumn | titleCase}})
_, err = tx.Exec(`insert into "{{.JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}})
if err != nil {
t.Fatal(err)
}
_, err = tx.Exec(`insert into {{.JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{.Column | titleCase}}, c.{{.ForeignColumn | titleCase}})
_, err = tx.Exec(`insert into "{{.JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}})
if err != nil {
t.Fatal(err)
}

View file

@ -100,6 +100,7 @@ type RelationshipToManyTexts struct {
LocalTable struct {
NameGo string
NameSingular string
ColumnNameGo string
}
ForeignTable struct {
@ -107,6 +108,7 @@ type RelationshipToManyTexts struct {
NameSingular string
NamePluralGo string
NameHumanReadable string
ColumnNameGo string
Slice string
}
@ -125,10 +127,12 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
r := RelationshipToManyTexts{}
r.LocalTable.NameSingular = strmangle.Singular(table.Name)
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column)
r.ForeignTable.NameSingular = strmangle.Singular(rel.ForeignTable)
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable))
r.ForeignTable.NameGo = strmangle.TitleCase(r.ForeignTable.NameSingular)
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(rel.ForeignColumn)
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(r.ForeignTable.NameSingular))
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)

View file

@ -107,11 +107,13 @@ func TestTextsFromRelationship(t *testing.T) {
expect := RelationshipToManyTexts{}
expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "pilot"
expect.LocalTable.ColumnNameGo = "ID"
expect.ForeignTable.NameGo = "Jet"
expect.ForeignTable.NameSingular = "jet"
expect.ForeignTable.NamePluralGo = "Jets"
expect.ForeignTable.NameHumanReadable = "jets"
expect.ForeignTable.ColumnNameGo = "PilotID"
expect.ForeignTable.Slice = "JetSlice"
expect.Function.Name = "Jets"
@ -127,11 +129,13 @@ func TestTextsFromRelationship(t *testing.T) {
expect = RelationshipToManyTexts{}
expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "pilot"
expect.LocalTable.ColumnNameGo = "ID"
expect.ForeignTable.NameGo = "License"
expect.ForeignTable.NameSingular = "license"
expect.ForeignTable.NamePluralGo = "Licenses"
expect.ForeignTable.NameHumanReadable = "licenses"
expect.ForeignTable.ColumnNameGo = "PilotID"
expect.ForeignTable.Slice = "LicenseSlice"
expect.Function.Name = "Licenses"
@ -147,11 +151,13 @@ func TestTextsFromRelationship(t *testing.T) {
expect = RelationshipToManyTexts{}
expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "pilot"
expect.LocalTable.ColumnNameGo = "ID"
expect.ForeignTable.NameGo = "Language"
expect.ForeignTable.NameSingular = "language"
expect.ForeignTable.NamePluralGo = "Languages"
expect.ForeignTable.NameHumanReadable = "languages"
expect.ForeignTable.ColumnNameGo = "ID"
expect.ForeignTable.Slice = "LanguageSlice"
expect.Function.Name = "Languages"