Add self join support.

This commit is contained in:
sub0Zero 2019-03-16 20:41:06 +01:00 committed by zer0sub
parent 20c6f39665
commit 1cb997fc54
5 changed files with 89 additions and 15 deletions

View file

@ -2,7 +2,9 @@ package generator
var SqlBuilderTableTemplate = `package table
import "github.com/sub0Zero/go-sqlbuilder/sqlbuilder"
import (
"github.com/sub0Zero/go-sqlbuilder/sqlbuilder"
)
type {{.ToGoStructName}} struct {
sqlbuilder.Table
@ -15,22 +17,36 @@ type {{.ToGoStructName}} struct {
AllColumns sqlbuilder.ColumnList
}
var {{.ToGoVarName}} = &{{.ToGoStructName}}{
Table: *sqlbuilder.NewTable("{{.DatabaseInfo.SchemaName}}", "{{.Name}}", {{.ToGoColumnFieldList ", "}}),
//Columns
var {{.ToGoVarName}} = new{{.ToGoStructName}}()
func new{{.ToGoStructName}}() *{{.ToGoStructName}} {
var (
{{- range .Columns}}
{{.ToGoVarName}} = sqlbuilder.IntColumn("{{.Name}}", {{if .IsNullable}}sqlbuilder.Nullable{{else}}sqlbuilder.NotNullable{{end}})
{{- end}}
)
return &{{.ToGoStructName}}{
Table: *sqlbuilder.NewTable("{{.DatabaseInfo.SchemaName}}", "{{.Name}}", {{.ToGoColumnFieldList ", "}}),
//Columns
{{- range .Columns}}
{{.ToGoFieldName}}: {{.ToGoVarName}},
{{.ToGoFieldName}}: {{.ToGoVarName}},
{{- end}}
AllColumns: sqlbuilder.ColumnList{ {{.ToGoColumnFieldList ", "}} },
AllColumns: sqlbuilder.ColumnList{ {{.ToGoColumnFieldList ", "}} },
}
}
func (a *{{.ToGoStructName}}) As(alias string) *{{.ToGoStructName}} {
aliasTable := new{{.ToGoStructName}}()
aliasTable.Table.SetAlias(alias)
return aliasTable
}
var (
{{- range .Columns}}
{{.ToGoVarName}} = sqlbuilder.IntColumn("{{.Name}}", {{if .IsNullable}}sqlbuilder.Nullable{{else}}sqlbuilder.NotNullable{{end}})
{{- end}}
)
`
var DataModelTemplate = `package model