Add Texthelpers from foreign keys
This commit is contained in:
parent
afb006b563
commit
32b2581ead
2 changed files with 114 additions and 4 deletions
|
@ -8,6 +8,90 @@ import (
|
|||
"github.com/nullbio/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
// RelationshipToOneTexts contains text that will be used by templates.
|
||||
type RelationshipToOneTexts struct {
|
||||
LocalTable struct {
|
||||
NameGo string
|
||||
ColumnNameGo string
|
||||
}
|
||||
|
||||
ForeignTable struct {
|
||||
NameGo string
|
||||
ColumnNameGo string
|
||||
}
|
||||
|
||||
Function struct {
|
||||
Varname string
|
||||
Receiver string
|
||||
|
||||
LocalAssignment string
|
||||
ForeignAssignment string
|
||||
}
|
||||
}
|
||||
|
||||
func textsFromForeignKey(tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) RelationshipToOneTexts {
|
||||
r := RelationshipToOneTexts{}
|
||||
|
||||
r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(strings.Replace(table.Name, "_id", "", -1)))
|
||||
r.LocalTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(strings.Replace(fkey.Column, "_id", "", -1)))
|
||||
|
||||
r.ForeignTable.NameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignTable))
|
||||
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(strings.Replace(fkey.ForeignColumn, "_id", "", -1)))
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
{{- $localTable := .Table.Name | singular | titleCase -}}
|
||||
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
|
||||
{{- $foreignColumn := .ForeignColumn | remove "_id" | singular | titleCase -}}
|
||||
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
|
||||
{{- $varname := .ForeignTable | singular | camelCase -}}
|
||||
func {{$localTable}}ToOne{{$foreignTable}}_{{$localColumn}}(t *testing.T) {
|
||||
tx, err := boil.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
local := &{{$localTable}}{}
|
||||
foreign := &{{$foreignTable}}{}
|
||||
|
||||
if err := local.InsertX(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
{{if and .Nullable .ForeignColumnNullable -}}
|
||||
foreign.{{.ForeignColumn | titleCase}} = local.{{.Column | titleCase}}
|
||||
{{- else if .Nullable -}}
|
||||
foreign.{{.ForeignColumn | titleCase}} = local.{{.Column | titleCase}}
|
||||
{{- else if .ForeignColumnNullable -}}
|
||||
{{- end}}
|
||||
if err := foreign.InsertX(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
checkForeign, err := local.{{$localColumn}}()
|
||||
*/
|
||||
|
||||
// RelationshipToManyTexts contains text that will be used by templates.
|
||||
type RelationshipToManyTexts struct {
|
||||
LocalTable struct {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
|
@ -57,7 +56,6 @@ func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
|
|||
return c
|
||||
}
|
||||
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
|
||||
fmt.Println(tableName)
|
||||
return map[string]*bdb.PrimaryKey{
|
||||
"users_videos_tags": &bdb.PrimaryKey{
|
||||
Name: "user_video_id_pkey",
|
||||
|
@ -68,7 +66,36 @@ func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
|
|||
func (fakeDB) Open() error { return nil }
|
||||
func (fakeDB) Close() {}
|
||||
|
||||
func TesttextsFromRelationship(t *testing.T) {
|
||||
func TestTextsFromForeignKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables, err := bdb.Tables(fakeDB(0))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
videos := bdb.GetTable(tables, "videos")
|
||||
texts := textsFromForeignKey(tables, videos, videos.FKeys[0])
|
||||
expect := RelationshipToOneTexts{}
|
||||
|
||||
expect.LocalTable.NameGo = "Video"
|
||||
expect.LocalTable.ColumnNameGo = "User"
|
||||
|
||||
expect.ForeignTable.NameGo = "User"
|
||||
expect.ForeignTable.ColumnNameGo = "ID"
|
||||
|
||||
expect.Function.Varname = "user"
|
||||
expect.Function.Receiver = "v"
|
||||
|
||||
expect.Function.LocalAssignment = "UserID.Int32"
|
||||
expect.Function.ForeignAssignment = "ID"
|
||||
|
||||
if !reflect.DeepEqual(expect, texts) {
|
||||
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextsFromRelationship(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables, err := bdb.Tables(fakeDB(0))
|
||||
|
@ -76,7 +103,6 @@ func TesttextsFromRelationship(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
spew.Dump(tables)
|
||||
users := bdb.GetTable(tables, "users")
|
||||
texts := textsFromRelationship(tables, users, users.ToManyRelationships[0])
|
||||
expect := RelationshipToManyTexts{}
|
||||
|
|
Loading…
Add table
Reference in a new issue