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)
|
err := utils.CleanUpGeneratedFiles(destDir)
|
||||||
utils.PanicOnError(err)
|
utils.PanicOnError(err)
|
||||||
|
|
||||||
|
tableSQLBuilderTemplate := getTableSQLBuilderTemplate(dialect)
|
||||||
generateSQLBuilderFiles(destDir, "table", tableSQLBuilderTemplate, schemaInfo.TablesMetaData, dialect)
|
generateSQLBuilderFiles(destDir, "table", tableSQLBuilderTemplate, schemaInfo.TablesMetaData, dialect)
|
||||||
generateSQLBuilderFiles(destDir, "view", tableSQLBuilderTemplate, schemaInfo.ViewsMetaData, dialect)
|
generateSQLBuilderFiles(destDir, "view", tableSQLBuilderTemplate, schemaInfo.ViewsMetaData, dialect)
|
||||||
generateSQLBuilderFiles(destDir, "enum", enumSQLBuilderTemplate, schemaInfo.EnumsMetaData, dialect)
|
generateSQLBuilderFiles(destDir, "enum", enumSQLBuilderTemplate, schemaInfo.EnumsMetaData, dialect)
|
||||||
|
|
@ -33,6 +34,14 @@ func GenerateFiles(destDir string, schemaInfo metadata.SchemaMetaData, dialect j
|
||||||
fmt.Println("Done")
|
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) {
|
func generateSQLBuilderFiles(destDir, fileTypes, sqlBuilderTemplate string, metaData []metadata.MetaData, dialect jet.Dialect) {
|
||||||
if len(metaData) == 0 {
|
if len(metaData) == 0 {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,63 @@ import (
|
||||||
|
|
||||||
var {{ToGoIdentifier .Name}} = new{{.GoStructName}}()
|
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 {
|
type {{.GoStructImplName}} struct {
|
||||||
{{dialect.PackageName}}.Table
|
{{dialect.PackageName}}.Table
|
||||||
|
|
||||||
|
|
@ -43,7 +100,7 @@ type {{.GoStructName}} struct {
|
||||||
EXCLUDED {{.GoStructImplName}}
|
EXCLUDED {{.GoStructImplName}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates new {{.GoStructName}} with assigned alias
|
// AS creates new {{.GoStructName}} with assigned alias
|
||||||
func (a *{{.GoStructName}}) AS(alias string) *{{.GoStructName}} {
|
func (a *{{.GoStructName}}) AS(alias string) *{{.GoStructName}} {
|
||||||
aliasTable := new{{.GoStructName}}()
|
aliasTable := new{{.GoStructName}}()
|
||||||
aliasTable.Table.AS(alias)
|
aliasTable.Table.AS(alias)
|
||||||
|
|
@ -78,7 +135,6 @@ func new{{.GoStructName}}Impl(schemaName, tableName string) {{.GoStructImplName}
|
||||||
MutableColumns: mutableColumns,
|
MutableColumns: mutableColumns,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
var tableModelTemplate = `package model
|
var tableModelTemplate = `package model
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ import (
|
||||||
|
|
||||||
var Actor = newActorTable()
|
var Actor = newActorTable()
|
||||||
|
|
||||||
type actorTable struct {
|
type ActorTable struct {
|
||||||
mysql.Table
|
mysql.Table
|
||||||
|
|
||||||
//Columns
|
//Columns
|
||||||
|
|
@ -155,27 +155,14 @@ type actorTable struct {
|
||||||
MutableColumns mysql.ColumnList
|
MutableColumns mysql.ColumnList
|
||||||
}
|
}
|
||||||
|
|
||||||
type ActorTable struct {
|
// AS creates new ActorTable with assigned alias
|
||||||
actorTable
|
func (a *ActorTable) AS(alias string) ActorTable {
|
||||||
|
|
||||||
EXCLUDED actorTable
|
|
||||||
}
|
|
||||||
|
|
||||||
// creates new ActorTable with assigned alias
|
|
||||||
func (a *ActorTable) AS(alias string) *ActorTable {
|
|
||||||
aliasTable := newActorTable()
|
aliasTable := newActorTable()
|
||||||
aliasTable.Table.AS(alias)
|
aliasTable.Table.AS(alias)
|
||||||
return aliasTable
|
return aliasTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func newActorTable() *ActorTable {
|
func newActorTable() ActorTable {
|
||||||
return &ActorTable{
|
|
||||||
actorTable: newActorTableImpl("dvds", "actor"),
|
|
||||||
EXCLUDED: newActorTableImpl("", "excluded"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newActorTableImpl(schemaName, tableName string) actorTable {
|
|
||||||
var (
|
var (
|
||||||
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
||||||
FirstNameColumn = mysql.StringColumn("first_name")
|
FirstNameColumn = mysql.StringColumn("first_name")
|
||||||
|
|
@ -185,8 +172,8 @@ func newActorTableImpl(schemaName, tableName string) actorTable {
|
||||||
mutableColumns = mysql.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn}
|
mutableColumns = mysql.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn}
|
||||||
)
|
)
|
||||||
|
|
||||||
return actorTable{
|
return ActorTable{
|
||||||
Table: mysql.NewTable(schemaName, tableName, allColumns...),
|
Table: mysql.NewTable("dvds", "actor", allColumns...),
|
||||||
|
|
||||||
//Columns
|
//Columns
|
||||||
ActorID: ActorIDColumn,
|
ActorID: ActorIDColumn,
|
||||||
|
|
@ -238,7 +225,7 @@ import (
|
||||||
|
|
||||||
var ActorInfo = newActorInfoTable()
|
var ActorInfo = newActorInfoTable()
|
||||||
|
|
||||||
type actorInfoTable struct {
|
type ActorInfoTable struct {
|
||||||
mysql.Table
|
mysql.Table
|
||||||
|
|
||||||
//Columns
|
//Columns
|
||||||
|
|
@ -251,27 +238,14 @@ type actorInfoTable struct {
|
||||||
MutableColumns mysql.ColumnList
|
MutableColumns mysql.ColumnList
|
||||||
}
|
}
|
||||||
|
|
||||||
type ActorInfoTable struct {
|
// AS creates new ActorInfoTable with assigned alias
|
||||||
actorInfoTable
|
func (a *ActorInfoTable) AS(alias string) ActorInfoTable {
|
||||||
|
|
||||||
EXCLUDED actorInfoTable
|
|
||||||
}
|
|
||||||
|
|
||||||
// creates new ActorInfoTable with assigned alias
|
|
||||||
func (a *ActorInfoTable) AS(alias string) *ActorInfoTable {
|
|
||||||
aliasTable := newActorInfoTable()
|
aliasTable := newActorInfoTable()
|
||||||
aliasTable.Table.AS(alias)
|
aliasTable.Table.AS(alias)
|
||||||
return aliasTable
|
return aliasTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func newActorInfoTable() *ActorInfoTable {
|
func newActorInfoTable() ActorInfoTable {
|
||||||
return &ActorInfoTable{
|
|
||||||
actorInfoTable: newActorInfoTableImpl("dvds", "actor_info"),
|
|
||||||
EXCLUDED: newActorInfoTableImpl("", "excluded"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newActorInfoTableImpl(schemaName, tableName string) actorInfoTable {
|
|
||||||
var (
|
var (
|
||||||
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
||||||
FirstNameColumn = mysql.StringColumn("first_name")
|
FirstNameColumn = mysql.StringColumn("first_name")
|
||||||
|
|
@ -281,8 +255,8 @@ func newActorInfoTableImpl(schemaName, tableName string) actorInfoTable {
|
||||||
mutableColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn}
|
mutableColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn}
|
||||||
)
|
)
|
||||||
|
|
||||||
return actorInfoTable{
|
return ActorInfoTable{
|
||||||
Table: mysql.NewTable(schemaName, tableName, allColumns...),
|
Table: mysql.NewTable("dvds", "actor_info", allColumns...),
|
||||||
|
|
||||||
//Columns
|
//Columns
|
||||||
ActorID: ActorIDColumn,
|
ActorID: ActorIDColumn,
|
||||||
|
|
|
||||||
|
|
@ -194,7 +194,7 @@ type ActorTable struct {
|
||||||
EXCLUDED actorTable
|
EXCLUDED actorTable
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates new ActorTable with assigned alias
|
// AS creates new ActorTable with assigned alias
|
||||||
func (a *ActorTable) AS(alias string) *ActorTable {
|
func (a *ActorTable) AS(alias string) *ActorTable {
|
||||||
aliasTable := newActorTable()
|
aliasTable := newActorTable()
|
||||||
aliasTable.Table.AS(alias)
|
aliasTable.Table.AS(alias)
|
||||||
|
|
@ -290,7 +290,7 @@ type ActorInfoTable struct {
|
||||||
EXCLUDED actorInfoTable
|
EXCLUDED actorInfoTable
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates new ActorInfoTable with assigned alias
|
// AS creates new ActorInfoTable with assigned alias
|
||||||
func (a *ActorInfoTable) AS(alias string) *ActorInfoTable {
|
func (a *ActorInfoTable) AS(alias string) *ActorInfoTable {
|
||||||
aliasTable := newActorInfoTable()
|
aliasTable := newActorInfoTable()
|
||||||
aliasTable.Table.AS(alias)
|
aliasTable.Table.AS(alias)
|
||||||
|
|
@ -580,7 +580,7 @@ type AllTypesTable struct {
|
||||||
EXCLUDED allTypesTable
|
EXCLUDED allTypesTable
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates new AllTypesTable with assigned alias
|
// AS creates new AllTypesTable with assigned alias
|
||||||
func (a *AllTypesTable) AS(alias string) *AllTypesTable {
|
func (a *AllTypesTable) AS(alias string) *AllTypesTable {
|
||||||
aliasTable := newAllTypesTable()
|
aliasTable := newAllTypesTable()
|
||||||
aliasTable.Table.AS(alias)
|
aliasTable.Table.AS(alias)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue