2016-07-13 05:05:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb"
|
|
|
|
"github.com/vattle/sqlboiler/strmangle"
|
2016-07-13 05:05:33 +02:00
|
|
|
)
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
// RelationshipToOneTexts contains text that will be used by templates.
|
|
|
|
type RelationshipToOneTexts struct {
|
2016-07-17 08:56:09 +02:00
|
|
|
ForeignKey bdb.ForeignKey
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
LocalTable struct {
|
|
|
|
NameGo string
|
|
|
|
ColumnNameGo string
|
|
|
|
}
|
|
|
|
|
|
|
|
ForeignTable struct {
|
|
|
|
NameGo string
|
2016-08-02 05:37:58 +02:00
|
|
|
NamePluralGo string
|
2016-07-17 08:56:09 +02:00
|
|
|
Name string
|
2016-07-15 06:18:49 +02:00
|
|
|
ColumnNameGo string
|
2016-07-17 08:56:09 +02:00
|
|
|
ColumnName string
|
2016-07-15 06:18:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Function struct {
|
2016-07-17 08:56:09 +02:00
|
|
|
PackageName string
|
|
|
|
Name string
|
|
|
|
|
2016-07-18 02:52:55 +02:00
|
|
|
Varname string
|
|
|
|
Receiver string
|
|
|
|
ReverseInserts bool
|
2016-07-15 06:18:49 +02:00
|
|
|
|
|
|
|
LocalAssignment string
|
|
|
|
ForeignAssignment string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) RelationshipToOneTexts {
|
2016-07-15 06:18:49 +02:00
|
|
|
r := RelationshipToOneTexts{}
|
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
r.ForeignKey = fkey
|
|
|
|
|
|
|
|
r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(table.Name))
|
|
|
|
r.LocalTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.Column))
|
2016-07-15 06:18:49 +02:00
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
r.ForeignTable.Name = fkey.ForeignTable
|
2016-07-15 06:18:49 +02:00
|
|
|
r.ForeignTable.NameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignTable))
|
2016-08-03 04:05:05 +02:00
|
|
|
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(fkey.ForeignTable))
|
2016-07-17 08:56:09 +02:00
|
|
|
r.ForeignTable.ColumnName = fkey.ForeignColumn
|
|
|
|
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignColumn))
|
2016-07-15 06:18:49 +02:00
|
|
|
|
2016-07-17 08:56:09 +02:00
|
|
|
r.Function.PackageName = packageName
|
|
|
|
r.Function.Name = strmangle.TitleCase(strmangle.Singular(strings.TrimSuffix(fkey.Column, "_id")))
|
2016-07-15 06:18:49 +02:00
|
|
|
r.Function.Varname = strmangle.CamelCase(strmangle.Singular(fkey.ForeignTable))
|
|
|
|
r.Function.Receiver = strings.ToLower(table.Name[:1])
|
|
|
|
|
|
|
|
if fkey.Nullable {
|
|
|
|
col := table.GetColumn(fkey.Column)
|
|
|
|
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.Column), strings.TrimPrefix(col.Type, "null."))
|
|
|
|
} else {
|
|
|
|
r.Function.LocalAssignment = strmangle.TitleCase(fkey.Column)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fkey.ForeignColumnNullable {
|
|
|
|
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
|
|
|
|
col := foreignTable.GetColumn(fkey.ForeignColumn)
|
|
|
|
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(col.Type, "null."))
|
|
|
|
} else {
|
|
|
|
r.Function.ForeignAssignment = strmangle.TitleCase(fkey.ForeignColumn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-07-18 02:48:08 +02:00
|
|
|
func textsFromOneToOneRelationship(packageName string, tables []bdb.Table, table bdb.Table, toMany bdb.ToManyRelationship) RelationshipToOneTexts {
|
|
|
|
fkey := bdb.ForeignKey{
|
|
|
|
Name: "none",
|
|
|
|
Column: toMany.Column,
|
|
|
|
Nullable: toMany.Nullable,
|
|
|
|
Unique: toMany.Unique,
|
|
|
|
|
|
|
|
ForeignTable: toMany.ForeignTable,
|
|
|
|
ForeignColumn: toMany.ForeignColumn,
|
|
|
|
ForeignColumnNullable: toMany.ForeignColumnNullable,
|
|
|
|
ForeignColumnUnique: toMany.ForeignColumnUnique,
|
|
|
|
}
|
|
|
|
|
|
|
|
rel := textsFromForeignKey(packageName, tables, table, fkey)
|
|
|
|
rel.Function.Name = strmangle.TitleCase(strmangle.Singular(toMany.ForeignTable))
|
2016-07-18 02:52:55 +02:00
|
|
|
rel.Function.ReverseInserts = true
|
2016-07-18 02:48:08 +02:00
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
2016-07-13 05:05:33 +02:00
|
|
|
// RelationshipToManyTexts contains text that will be used by templates.
|
|
|
|
type RelationshipToManyTexts struct {
|
|
|
|
LocalTable struct {
|
|
|
|
NameGo string
|
|
|
|
NameSingular string
|
|
|
|
}
|
|
|
|
|
|
|
|
ForeignTable struct {
|
|
|
|
NameGo string
|
|
|
|
NameSingular string
|
|
|
|
NamePluralGo string
|
|
|
|
NameHumanReadable string
|
|
|
|
Slice string
|
|
|
|
}
|
|
|
|
|
|
|
|
Function struct {
|
|
|
|
Name string
|
|
|
|
Receiver string
|
|
|
|
|
|
|
|
LocalAssignment string
|
|
|
|
ForeignAssignment string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 05:29:29 +02:00
|
|
|
// textsFromRelationship creates a struct that does a lot of the text
|
2016-07-13 05:05:33 +02:00
|
|
|
// transformation in advance for a given relationship.
|
2016-07-14 05:29:29 +02:00
|
|
|
func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) RelationshipToManyTexts {
|
2016-07-13 05:05:33 +02:00
|
|
|
r := RelationshipToManyTexts{}
|
|
|
|
r.LocalTable.NameSingular = strmangle.Singular(table.Name)
|
|
|
|
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
|
|
|
|
|
|
|
|
r.ForeignTable.NameSingular = strmangle.Singular(rel.ForeignTable)
|
|
|
|
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable))
|
|
|
|
r.ForeignTable.NameGo = strmangle.TitleCase(r.ForeignTable.NameSingular)
|
2016-07-16 16:15:14 +02:00
|
|
|
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(r.ForeignTable.NameSingular))
|
2016-07-13 05:05:33 +02:00
|
|
|
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
|
|
|
|
|
|
|
|
r.Function.Receiver = strings.ToLower(table.Name[:1])
|
|
|
|
|
|
|
|
// Check to see if the foreign key name is the same as the local table name.
|
|
|
|
// Simple case: yes - we can name the function the same as the plural table name
|
|
|
|
// Not simple case: We have to name the function based off the foreign key and
|
2016-07-14 05:29:29 +02:00
|
|
|
if colName := strings.TrimSuffix(rel.ForeignColumn, "_id"); rel.ToJoinTable || r.LocalTable.NameSingular == colName {
|
2016-07-13 05:05:33 +02:00
|
|
|
r.Function.Name = r.ForeignTable.NamePluralGo
|
|
|
|
} else {
|
2016-07-13 06:34:59 +02:00
|
|
|
r.Function.Name = strmangle.TitleCase(colName) + r.ForeignTable.NamePluralGo
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if rel.Nullable {
|
|
|
|
col := table.GetColumn(rel.Column)
|
|
|
|
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null."))
|
|
|
|
} else {
|
|
|
|
r.Function.LocalAssignment = strmangle.TitleCase(rel.Column)
|
|
|
|
}
|
|
|
|
|
2016-07-14 05:29:29 +02:00
|
|
|
if rel.ForeignColumnNullable {
|
|
|
|
foreignTable := bdb.GetTable(tables, rel.ForeignTable)
|
|
|
|
col := foreignTable.GetColumn(rel.ForeignColumn)
|
|
|
|
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(col.Type, "null."))
|
|
|
|
} else {
|
|
|
|
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|