sqlboiler/text_helpers.go

206 lines
7 KiB
Go
Raw Normal View History

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
)
// TxtToOne contains text that will be used by templates for a one-to-many or
// a one-to-one relationship.
type TxtToOne struct {
ForeignKey bdb.ForeignKey
2016-07-15 06:18:49 +02:00
LocalTable struct {
Name string
2016-07-15 06:18:49 +02:00
NameGo string
ColumnNameGo string
}
ForeignTable struct {
NameGo string
NamePluralGo string
Name string
2016-07-15 06:18:49 +02:00
ColumnNameGo string
ColumnName string
2016-07-15 06:18:49 +02:00
}
Function struct {
Name string
ForeignName string
Varname string
Receiver string
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 {
r := TxtToOne{}
2016-07-15 06:18:49 +02:00
r.ForeignKey = fkey
r.LocalTable.Name = table.Name
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.Name = fkey.ForeignTable
2016-07-15 06:18:49 +02:00
r.ForeignTable.NameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignTable))
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(fkey.ForeignTable))
r.ForeignTable.ColumnName = fkey.ForeignColumn
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignColumn))
2016-07-15 06:18:49 +02:00
r.Function.Name = strmangle.TitleCase(strmangle.Singular(strings.TrimSuffix(fkey.Column, "_id")))
2016-08-28 08:01:26 +02:00
plurality := strmangle.Plural
if fkey.Unique {
plurality = strmangle.Singular
}
r.Function.ForeignName = mkFunctionName(strmangle.Singular(fkey.ForeignTable), strmangle.TitleCase(plurality(fkey.Table)), fkey.Column, false)
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)
}
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
2016-07-15 06:18:49 +02:00
if fkey.ForeignColumnNullable {
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)
}
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 {
fkey := bdb.ForeignKey{
Table: oneToOne.Table,
Name: "none",
Column: oneToOne.Column,
Nullable: oneToOne.Nullable,
Unique: oneToOne.Unique,
ForeignTable: oneToOne.ForeignTable,
ForeignColumn: oneToOne.ForeignColumn,
ForeignColumnNullable: oneToOne.ForeignColumnNullable,
ForeignColumnUnique: oneToOne.ForeignColumnUnique,
}
2016-09-18 19:31:11 +02:00
rel := txtsFromFKey(tables, table, fkey)
col := table.GetColumn(oneToOne.Column)
// 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
rel.Function.Name = strmangle.TitleCase(strmangle.Singular(oneToOne.ForeignTable))
rel.Function.ForeignName = mkFunctionName(strmangle.Singular(oneToOne.Table), strmangle.TitleCase(strmangle.Singular(oneToOne.Table)), oneToOne.ForeignColumn, false)
rel.Function.UsesBytes = col.Type == "[]byte"
return rel
}
// 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 {
Name string
2016-07-13 05:05:33 +02:00
NameGo string
NameSingular string
2016-08-27 21:00:42 +02:00
ColumnNameGo string
2016-07-13 05:05:33 +02:00
}
ForeignTable struct {
Name string
2016-07-13 05:05:33 +02:00
NameGo string
NameSingular 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 {
Name string
ForeignName string
Receiver string
2016-07-13 05:05:33 +02:00
UsesBytes bool
2016-07-13 05:05:33 +02:00
LocalAssignment string
ForeignAssignment string
}
}
// 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.
func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany {
r := TxtToMany{}
r.LocalTable.Name = table.Name
2016-07-13 05:05:33 +02:00
r.LocalTable.NameSingular = strmangle.Singular(table.Name)
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
2016-08-27 21:00:42 +02:00
r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column)
2016-07-13 05:05:33 +02:00
r.ForeignTable.Name = rel.ForeignTable
2016-07-13 05:05:33 +02:00
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-08-27 21:00:42 +02:00
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(rel.ForeignColumn)
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])
2016-08-27 21:29:49 +02:00
r.Function.Name = mkFunctionName(r.LocalTable.NameSingular, r.ForeignTable.NamePluralGo, rel.ForeignColumn, rel.ToJoinTable)
plurality := strmangle.Singular
foreignNamingColumn := rel.ForeignColumn
if rel.ToJoinTable {
plurality = strmangle.Plural
foreignNamingColumn = rel.JoinLocalColumn
}
r.Function.ForeignName = strmangle.TitleCase(plurality(strings.TrimSuffix(foreignNamingColumn, "_id")))
2016-07-13 05:05:33 +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)
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
}
r.Function.UsesBytes = col.Type == "[]byte"
2016-07-13 05:05:33 +02:00
return r
}
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
func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string, toJoinTable bool) string {
colName := strings.TrimSuffix(fkeyColumn, "_id")
if toJoinTable || fkeyTableSingular == colName {
2016-08-27 21:29:49 +02:00
return foreignTablePluralGo
}
return strmangle.TitleCase(colName) + foreignTablePluralGo
}