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-09-18 08:36:10 +02:00
|
|
|
// TxtToOne contains text that will be used by templates for a one-to-many or
|
|
|
|
// a one-to-one relationship.
|
|
|
|
type TxtToOne 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-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
|
|
|
Name string
|
2016-08-28 02:20:26 +02:00
|
|
|
ForeignName string
|
2016-07-17 08:56:09 +02:00
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
UsesBytes bool
|
2016-07-15 06:18:49 +02:00
|
|
|
|
|
|
|
LocalAssignment string
|
|
|
|
ForeignAssignment string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:31:11 +02:00
|
|
|
func txtsFromFKey(tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) TxtToOne {
|
2016-09-18 08:36:10 +02:00
|
|
|
r := TxtToOne{}
|
2016-07-15 06:18:49 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
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-09-22 08:36:29 +02:00
|
|
|
r.Function.Name, r.Function.ForeignName = txtNameToOne(fkey)
|
2016-07-15 06:18:49 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
|
|
|
|
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
if fkey.ForeignColumnNullable {
|
2016-09-17 09:02:03 +02:00
|
|
|
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
|
2016-07-15 06:18:49 +02:00
|
|
|
} else {
|
|
|
|
r.Function.ForeignAssignment = strmangle.TitleCase(fkey.ForeignColumn)
|
|
|
|
}
|
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
r.Function.UsesBytes = foreignColumn.Type == "[]byte"
|
|
|
|
|
2016-07-15 06:18:49 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:31:11 +02:00
|
|
|
func txtsFromOneToOne(tables []bdb.Table, table bdb.Table, oneToOne bdb.ToOneRelationship) TxtToOne {
|
2016-07-18 02:48:08 +02:00
|
|
|
fkey := bdb.ForeignKey{
|
2016-09-18 08:36:10 +02:00
|
|
|
Table: oneToOne.Table,
|
2016-07-18 02:48:08 +02:00
|
|
|
Name: "none",
|
2016-09-18 08:36:10 +02:00
|
|
|
Column: oneToOne.Column,
|
|
|
|
Nullable: oneToOne.Nullable,
|
|
|
|
Unique: oneToOne.Unique,
|
|
|
|
|
|
|
|
ForeignTable: oneToOne.ForeignTable,
|
|
|
|
ForeignColumn: oneToOne.ForeignColumn,
|
|
|
|
ForeignColumnNullable: oneToOne.ForeignColumnNullable,
|
|
|
|
ForeignColumnUnique: oneToOne.ForeignColumnUnique,
|
2016-07-18 02:48:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 19:31:11 +02:00
|
|
|
rel := txtsFromFKey(tables, table, fkey)
|
2016-09-18 08:36:10 +02:00
|
|
|
col := table.GetColumn(oneToOne.Column)
|
2016-09-17 09:02:03 +02:00
|
|
|
|
2016-09-18 08:36:10 +02:00
|
|
|
// Reverse foreign key
|
|
|
|
rel.ForeignKey.Table, rel.ForeignKey.ForeignTable = rel.ForeignKey.ForeignTable, rel.ForeignKey.Table
|
|
|
|
rel.ForeignKey.Column, rel.ForeignKey.ForeignColumn = rel.ForeignKey.ForeignColumn, rel.ForeignKey.Column
|
|
|
|
rel.ForeignKey.Nullable, rel.ForeignKey.ForeignColumnNullable = rel.ForeignKey.ForeignColumnNullable, rel.ForeignKey.Nullable
|
|
|
|
rel.ForeignKey.Unique, rel.ForeignKey.ForeignColumnUnique = rel.ForeignKey.ForeignColumnUnique, rel.ForeignKey.Unique
|
2016-09-17 09:02:03 +02:00
|
|
|
rel.Function.UsesBytes = col.Type == "[]byte"
|
2016-09-22 08:46:47 +02:00
|
|
|
rel.Function.ForeignName, rel.Function.Name = txtNameToOne(bdb.ForeignKey{
|
|
|
|
Table: oneToOne.ForeignTable,
|
|
|
|
Column: oneToOne.ForeignColumn,
|
|
|
|
Unique: true,
|
|
|
|
ForeignTable: oneToOne.Table,
|
|
|
|
ForeignColumn: oneToOne.Column,
|
|
|
|
})
|
2016-07-18 02:48:08 +02:00
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:36:10 +02:00
|
|
|
// TxtToMany contains text that will be used by many-to-one relationships.
|
|
|
|
type TxtToMany struct {
|
2016-07-13 05:05:33 +02:00
|
|
|
LocalTable struct {
|
|
|
|
NameGo string
|
2016-08-27 21:00:42 +02:00
|
|
|
ColumnNameGo string
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ForeignTable struct {
|
|
|
|
NameGo string
|
|
|
|
NamePluralGo string
|
|
|
|
NameHumanReadable string
|
2016-08-27 21:00:42 +02:00
|
|
|
ColumnNameGo string
|
2016-07-13 05:05:33 +02:00
|
|
|
Slice string
|
|
|
|
}
|
|
|
|
|
|
|
|
Function struct {
|
2016-08-28 02:20:26 +02:00
|
|
|
Name string
|
|
|
|
ForeignName string
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
UsesBytes bool
|
|
|
|
|
2016-07-13 05:05:33 +02:00
|
|
|
LocalAssignment string
|
|
|
|
ForeignAssignment string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:18:43 +02:00
|
|
|
// txtsFromToMany 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-09-18 08:36:10 +02:00
|
|
|
func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany {
|
|
|
|
r := TxtToMany{}
|
2016-09-22 09:03:57 +02:00
|
|
|
r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(table.Name))
|
2016-08-27 21:00:42 +02:00
|
|
|
r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column)
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-09-22 09:03:57 +02:00
|
|
|
foreignNameSingular := strmangle.Singular(rel.ForeignTable)
|
2016-07-13 05:05:33 +02:00
|
|
|
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable))
|
2016-09-22 09:03:57 +02:00
|
|
|
r.ForeignTable.NameGo = strmangle.TitleCase(foreignNameSingular)
|
2016-08-27 21:00:42 +02:00
|
|
|
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(rel.ForeignColumn)
|
2016-09-22 09:03:57 +02:00
|
|
|
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(foreignNameSingular))
|
2016-07-13 05:05:33 +02:00
|
|
|
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
|
|
|
|
|
2016-09-22 08:36:29 +02:00
|
|
|
r.Function.Name, r.Function.ForeignName = txtNameToMany(rel)
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
col := table.GetColumn(rel.Column)
|
2016-07-13 05:05:33 +02:00
|
|
|
if rel.Nullable {
|
|
|
|
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)
|
2016-09-17 09:02:03 +02:00
|
|
|
foreignColumn := foreignTable.GetColumn(rel.ForeignColumn)
|
|
|
|
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
|
2016-07-14 05:29:29 +02:00
|
|
|
} else {
|
|
|
|
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
r.Function.UsesBytes = col.Type == "[]byte"
|
|
|
|
|
2016-07-13 05:05:33 +02:00
|
|
|
return r
|
|
|
|
}
|
2016-08-27 21:29:49 +02:00
|
|
|
|
2016-09-22 08:36:29 +02:00
|
|
|
// txtNameToOne creates the local and foreign function names for
|
|
|
|
// one-to-many and one-to-one relationships, where local == lhs (one).
|
|
|
|
//
|
|
|
|
// = many-to-one
|
|
|
|
// users - videos : user_id
|
|
|
|
// users - videos : producer_id
|
|
|
|
//
|
|
|
|
// fk == table = user.Videos | video.User
|
|
|
|
// fk != table = user.ProducerVideos | video.Producer
|
|
|
|
//
|
|
|
|
// = many-to-one
|
|
|
|
// industries - industries : parent_id
|
|
|
|
//
|
|
|
|
// fk == table = industry.Industries | industry.Industry
|
|
|
|
// fk != table = industry.ParentIndustries | industry.Parent
|
|
|
|
//
|
|
|
|
// = one-to-one
|
|
|
|
// users - videos : user_id
|
|
|
|
// users - videos : producer_id
|
|
|
|
//
|
|
|
|
// fk == table = user.Video | video.User
|
|
|
|
// fk != table = user.ProducerVideo | video.Producer
|
|
|
|
//
|
|
|
|
// = one-to-one
|
|
|
|
// industries - industries : parent_id
|
|
|
|
//
|
|
|
|
// fk == table = industry.Industry | industry.Industry
|
|
|
|
// fk != table = industry.ParentIndustry | industry.Industry
|
|
|
|
func txtNameToOne(fk bdb.ForeignKey) (localFn, foreignFn string) {
|
2016-09-23 07:11:30 +02:00
|
|
|
localFn = strmangle.Singular(trimSuffixes(fk.Column))
|
2016-09-22 08:36:29 +02:00
|
|
|
fkeyIsTableName := localFn != strmangle.Singular(fk.ForeignTable)
|
|
|
|
localFn = strmangle.TitleCase(localFn)
|
|
|
|
|
|
|
|
if fkeyIsTableName {
|
|
|
|
foreignFn = localFn
|
|
|
|
}
|
|
|
|
|
|
|
|
plurality := strmangle.Plural
|
|
|
|
if fk.Unique {
|
|
|
|
plurality = strmangle.Singular
|
|
|
|
}
|
|
|
|
foreignFn += strmangle.TitleCase(plurality(fk.Table))
|
|
|
|
|
|
|
|
return localFn, foreignFn
|
|
|
|
}
|
|
|
|
|
|
|
|
// txtNameToMany creates the local and foreign function names for
|
|
|
|
// many-to-one and many-to-many relationship, where local == lhs (many)
|
|
|
|
//
|
|
|
|
// cases:
|
|
|
|
// = many-to-many
|
|
|
|
// sponsors - constests
|
|
|
|
// sponsor_id contest_id
|
|
|
|
// fk == table = sponsor.Contests | contest.Sponsors
|
|
|
|
//
|
|
|
|
// = many-to-many
|
|
|
|
// sponsors - constests
|
|
|
|
// wiggle_id jiggle_id
|
|
|
|
// fk != table = sponsor.JiggleSponsors | contest.WiggleContests
|
|
|
|
//
|
|
|
|
// = many-to-many
|
|
|
|
// industries - industries
|
|
|
|
// industry_id mapped_industry_id
|
|
|
|
//
|
|
|
|
// fk == table = industry.Industries
|
|
|
|
// fk != table = industry.MappedIndustryIndustry
|
|
|
|
func txtNameToMany(toMany bdb.ToManyRelationship) (localFn, foreignFn string) {
|
|
|
|
if toMany.ToJoinTable {
|
2016-09-23 07:11:30 +02:00
|
|
|
localFkey := strmangle.Singular(trimSuffixes(toMany.JoinLocalColumn))
|
|
|
|
foreignFkey := strmangle.Singular(trimSuffixes(toMany.JoinForeignColumn))
|
2016-09-22 08:36:29 +02:00
|
|
|
|
|
|
|
if localFkey != strmangle.Singular(toMany.Table) {
|
|
|
|
foreignFn = strmangle.TitleCase(localFkey)
|
|
|
|
}
|
|
|
|
foreignFn += strmangle.TitleCase(strmangle.Plural(toMany.Table))
|
|
|
|
|
|
|
|
if foreignFkey != strmangle.Singular(toMany.ForeignTable) {
|
|
|
|
localFn = strmangle.TitleCase(foreignFkey)
|
|
|
|
}
|
|
|
|
localFn += strmangle.TitleCase(strmangle.Plural(toMany.ForeignTable))
|
|
|
|
|
|
|
|
return localFn, foreignFn
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:11:30 +02:00
|
|
|
fkeyName := strmangle.Singular(trimSuffixes(toMany.ForeignColumn))
|
2016-09-22 08:36:29 +02:00
|
|
|
if fkeyName != strmangle.Singular(toMany.Table) {
|
|
|
|
localFn = strmangle.TitleCase(fkeyName)
|
|
|
|
}
|
|
|
|
localFn += strmangle.TitleCase(strmangle.Plural(toMany.ForeignTable))
|
|
|
|
foreignFn = strmangle.TitleCase(strmangle.Singular(fkeyName))
|
|
|
|
return localFn, foreignFn
|
|
|
|
}
|
|
|
|
|
2016-08-27 21:29:49 +02:00
|
|
|
// mkFunctionName checks to see if the foreign key name is the same as the local table name (minus _id suffix)
|
|
|
|
// 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 the foreign table name
|
2016-08-28 02:20:26 +02:00
|
|
|
func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string, toJoinTable bool) string {
|
2016-09-20 07:00:39 +02:00
|
|
|
colName := trimSuffixes(fkeyColumn)
|
2016-08-28 02:20:26 +02:00
|
|
|
if toJoinTable || fkeyTableSingular == colName {
|
2016-08-27 21:29:49 +02:00
|
|
|
return foreignTablePluralGo
|
|
|
|
}
|
|
|
|
|
|
|
|
return strmangle.TitleCase(colName) + foreignTablePluralGo
|
|
|
|
}
|
2016-09-20 07:00:39 +02:00
|
|
|
|
|
|
|
var identifierSuffixes = []string{"_id", "_uuid", "_guid", "_oid"}
|
|
|
|
|
|
|
|
// trimSuffixes from the identifier
|
|
|
|
func trimSuffixes(str string) string {
|
|
|
|
ln := len(str)
|
|
|
|
for _, s := range identifierSuffixes {
|
|
|
|
str = strings.TrimSuffix(str, s)
|
|
|
|
if len(str) != ln {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|