Generate different sql builder files for MySQL and PostgreSQL.
This commit is contained in:
parent
980b9b6aac
commit
a4b4710637
4 changed files with 82 additions and 43 deletions
|
|
@ -22,6 +22,7 @@ func GenerateFiles(destDir string, schemaInfo metadata.SchemaMetaData, dialect j
|
|||
err := utils.CleanUpGeneratedFiles(destDir)
|
||||
utils.PanicOnError(err)
|
||||
|
||||
tableSQLBuilderTemplate := getTableSQLBuilderTemplate(dialect)
|
||||
generateSQLBuilderFiles(destDir, "table", tableSQLBuilderTemplate, schemaInfo.TablesMetaData, dialect)
|
||||
generateSQLBuilderFiles(destDir, "view", tableSQLBuilderTemplate, schemaInfo.ViewsMetaData, dialect)
|
||||
generateSQLBuilderFiles(destDir, "enum", enumSQLBuilderTemplate, schemaInfo.EnumsMetaData, dialect)
|
||||
|
|
@ -33,6 +34,14 @@ func GenerateFiles(destDir string, schemaInfo metadata.SchemaMetaData, dialect j
|
|||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
func getTableSQLBuilderTemplate(dialect jet.Dialect) string {
|
||||
if dialect.Name() == "PostgreSQL" {
|
||||
return tablePostgreSQLBuilderTemplate
|
||||
}
|
||||
|
||||
return tableSQLBuilderTemplate
|
||||
}
|
||||
|
||||
func generateSQLBuilderFiles(destDir, fileTypes, sqlBuilderTemplate string, metaData []metadata.MetaData, dialect jet.Dialect) {
|
||||
if len(metaData) == 0 {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -25,6 +25,63 @@ import (
|
|||
|
||||
var {{ToGoIdentifier .Name}} = new{{.GoStructName}}()
|
||||
|
||||
type {{.GoStructName}} struct {
|
||||
{{dialect.PackageName}}.Table
|
||||
|
||||
//Columns
|
||||
{{- range .Columns}}
|
||||
{{ToGoIdentifier .Name}} {{dialect.PackageName}}.Column{{.SqlBuilderColumnType}}
|
||||
{{- end}}
|
||||
|
||||
AllColumns {{dialect.PackageName}}.ColumnList
|
||||
MutableColumns {{dialect.PackageName}}.ColumnList
|
||||
}
|
||||
|
||||
// AS creates new {{.GoStructName}} with assigned alias
|
||||
func (a *{{.GoStructName}}) AS(alias string) {{.GoStructName}} {
|
||||
aliasTable := new{{.GoStructName}}()
|
||||
aliasTable.Table.AS(alias)
|
||||
return aliasTable
|
||||
}
|
||||
|
||||
func new{{.GoStructName}}() {{.GoStructName}} {
|
||||
var (
|
||||
{{- range .Columns}}
|
||||
{{ToGoIdentifier .Name}}Column = {{dialect.PackageName}}.{{.SqlBuilderColumnType}}Column("{{.Name}}")
|
||||
{{- end}}
|
||||
allColumns = {{dialect.PackageName}}.ColumnList{ {{template "column-list" .Columns}} }
|
||||
mutableColumns = {{dialect.PackageName}}.ColumnList{ {{template "column-list" .MutableColumns}} }
|
||||
)
|
||||
|
||||
return {{.GoStructName}}{
|
||||
Table: {{dialect.PackageName}}.NewTable("{{.SchemaName}}", "{{.Name}}", allColumns...),
|
||||
|
||||
//Columns
|
||||
{{- range .Columns}}
|
||||
{{ToGoIdentifier .Name}}: {{ToGoIdentifier .Name}}Column,
|
||||
{{- end}}
|
||||
|
||||
AllColumns: allColumns,
|
||||
MutableColumns: mutableColumns,
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var tablePostgreSQLBuilderTemplate = `
|
||||
{{define "column-list" -}}
|
||||
{{- range $i, $c := . }}
|
||||
{{- if gt $i 0 }}, {{end}}{{ToGoIdentifier $c.Name}}Column
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
|
||||
package {{param "package"}}
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/{{dialect.PackageName}}"
|
||||
)
|
||||
|
||||
var {{ToGoIdentifier .Name}} = new{{.GoStructName}}()
|
||||
|
||||
type {{.GoStructImplName}} struct {
|
||||
{{dialect.PackageName}}.Table
|
||||
|
||||
|
|
@ -43,7 +100,7 @@ type {{.GoStructName}} struct {
|
|||
EXCLUDED {{.GoStructImplName}}
|
||||
}
|
||||
|
||||
// creates new {{.GoStructName}} with assigned alias
|
||||
// AS creates new {{.GoStructName}} with assigned alias
|
||||
func (a *{{.GoStructName}}) AS(alias string) *{{.GoStructName}} {
|
||||
aliasTable := new{{.GoStructName}}()
|
||||
aliasTable.Table.AS(alias)
|
||||
|
|
@ -78,7 +135,6 @@ func new{{.GoStructName}}Impl(schemaName, tableName string) {{.GoStructImplName}
|
|||
MutableColumns: mutableColumns,
|
||||
}
|
||||
}
|
||||
|
||||
`
|
||||
|
||||
var tableModelTemplate = `package model
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ import (
|
|||
|
||||
var Actor = newActorTable()
|
||||
|
||||
type actorTable struct {
|
||||
type ActorTable struct {
|
||||
mysql.Table
|
||||
|
||||
//Columns
|
||||
|
|
@ -155,27 +155,14 @@ type actorTable struct {
|
|||
MutableColumns mysql.ColumnList
|
||||
}
|
||||
|
||||
type ActorTable struct {
|
||||
actorTable
|
||||
|
||||
EXCLUDED actorTable
|
||||
}
|
||||
|
||||
// creates new ActorTable with assigned alias
|
||||
func (a *ActorTable) AS(alias string) *ActorTable {
|
||||
// AS creates new ActorTable with assigned alias
|
||||
func (a *ActorTable) AS(alias string) ActorTable {
|
||||
aliasTable := newActorTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
return aliasTable
|
||||
}
|
||||
|
||||
func newActorTable() *ActorTable {
|
||||
return &ActorTable{
|
||||
actorTable: newActorTableImpl("dvds", "actor"),
|
||||
EXCLUDED: newActorTableImpl("", "excluded"),
|
||||
}
|
||||
}
|
||||
|
||||
func newActorTableImpl(schemaName, tableName string) actorTable {
|
||||
func newActorTable() ActorTable {
|
||||
var (
|
||||
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
||||
FirstNameColumn = mysql.StringColumn("first_name")
|
||||
|
|
@ -185,8 +172,8 @@ func newActorTableImpl(schemaName, tableName string) actorTable {
|
|||
mutableColumns = mysql.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn}
|
||||
)
|
||||
|
||||
return actorTable{
|
||||
Table: mysql.NewTable(schemaName, tableName, allColumns...),
|
||||
return ActorTable{
|
||||
Table: mysql.NewTable("dvds", "actor", allColumns...),
|
||||
|
||||
//Columns
|
||||
ActorID: ActorIDColumn,
|
||||
|
|
@ -238,7 +225,7 @@ import (
|
|||
|
||||
var ActorInfo = newActorInfoTable()
|
||||
|
||||
type actorInfoTable struct {
|
||||
type ActorInfoTable struct {
|
||||
mysql.Table
|
||||
|
||||
//Columns
|
||||
|
|
@ -251,27 +238,14 @@ type actorInfoTable struct {
|
|||
MutableColumns mysql.ColumnList
|
||||
}
|
||||
|
||||
type ActorInfoTable struct {
|
||||
actorInfoTable
|
||||
|
||||
EXCLUDED actorInfoTable
|
||||
}
|
||||
|
||||
// creates new ActorInfoTable with assigned alias
|
||||
func (a *ActorInfoTable) AS(alias string) *ActorInfoTable {
|
||||
// AS creates new ActorInfoTable with assigned alias
|
||||
func (a *ActorInfoTable) AS(alias string) ActorInfoTable {
|
||||
aliasTable := newActorInfoTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
return aliasTable
|
||||
}
|
||||
|
||||
func newActorInfoTable() *ActorInfoTable {
|
||||
return &ActorInfoTable{
|
||||
actorInfoTable: newActorInfoTableImpl("dvds", "actor_info"),
|
||||
EXCLUDED: newActorInfoTableImpl("", "excluded"),
|
||||
}
|
||||
}
|
||||
|
||||
func newActorInfoTableImpl(schemaName, tableName string) actorInfoTable {
|
||||
func newActorInfoTable() ActorInfoTable {
|
||||
var (
|
||||
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
||||
FirstNameColumn = mysql.StringColumn("first_name")
|
||||
|
|
@ -281,8 +255,8 @@ func newActorInfoTableImpl(schemaName, tableName string) actorInfoTable {
|
|||
mutableColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn}
|
||||
)
|
||||
|
||||
return actorInfoTable{
|
||||
Table: mysql.NewTable(schemaName, tableName, allColumns...),
|
||||
return ActorInfoTable{
|
||||
Table: mysql.NewTable("dvds", "actor_info", allColumns...),
|
||||
|
||||
//Columns
|
||||
ActorID: ActorIDColumn,
|
||||
|
|
|
|||
|
|
@ -194,7 +194,7 @@ type ActorTable struct {
|
|||
EXCLUDED actorTable
|
||||
}
|
||||
|
||||
// creates new ActorTable with assigned alias
|
||||
// AS creates new ActorTable with assigned alias
|
||||
func (a *ActorTable) AS(alias string) *ActorTable {
|
||||
aliasTable := newActorTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
|
|
@ -290,7 +290,7 @@ type ActorInfoTable struct {
|
|||
EXCLUDED actorInfoTable
|
||||
}
|
||||
|
||||
// creates new ActorInfoTable with assigned alias
|
||||
// AS creates new ActorInfoTable with assigned alias
|
||||
func (a *ActorInfoTable) AS(alias string) *ActorInfoTable {
|
||||
aliasTable := newActorInfoTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
|
|
@ -580,7 +580,7 @@ type AllTypesTable struct {
|
|||
EXCLUDED allTypesTable
|
||||
}
|
||||
|
||||
// creates new AllTypesTable with assigned alias
|
||||
// AS creates new AllTypesTable with assigned alias
|
||||
func (a *AllTypesTable) AS(alias string) *AllTypesTable {
|
||||
aliasTable := newAllTypesTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue