Generator clean up.
Ensure all sql types can be processed.
This commit is contained in:
parent
b3a52ceb31
commit
64ba909381
21 changed files with 495 additions and 208 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/serenize/snaker"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ColumnInfo struct {
|
||||
|
|
@ -13,26 +14,21 @@ type ColumnInfo struct {
|
|||
EnumName string
|
||||
}
|
||||
|
||||
func (c ColumnInfo) ToSqlBuilderColumnType() string {
|
||||
func (c ColumnInfo) SqlBuilderColumnType() string {
|
||||
switch c.DataType {
|
||||
case "boolean":
|
||||
return "BoolColumn"
|
||||
case "smallint":
|
||||
case "smallint", "integer", "bigint":
|
||||
return "IntegerColumn"
|
||||
case "integer":
|
||||
return "IntegerColumn"
|
||||
case "bigint":
|
||||
return "IntegerColumn"
|
||||
case "date", "timestamp without time zone", "timestamp with time zone":
|
||||
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone":
|
||||
return "TimeColumn"
|
||||
case "text", "character", "character varying", "bytea", "uuid":
|
||||
case "USER-DEFINED", "text", "character", "character varying", "bytea", "uuid",
|
||||
"tsvector", "bit", "bit varying", "money", "json", "jsonb", "xml", "point", "interval", "line", "ARRAY":
|
||||
return "StringColumn"
|
||||
case "real":
|
||||
return "NumericColumn"
|
||||
case "numeric", "double precision":
|
||||
case "real", "numeric", "double precision":
|
||||
return "NumericColumn"
|
||||
default:
|
||||
fmt.Println("Unknownl type: " + c.DataType + ", using string column instead.")
|
||||
fmt.Println("Unknown sql type: " + c.DataType + ", using string column instead for sql builder.")
|
||||
return "StringColumn"
|
||||
}
|
||||
}
|
||||
|
|
@ -49,11 +45,12 @@ func (c ColumnInfo) GoBaseType() string {
|
|||
return "int32"
|
||||
case "bigint":
|
||||
return "int64"
|
||||
case "date", "timestamp without time zone", "timestamp with time zone":
|
||||
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone":
|
||||
return "time.Time"
|
||||
case "bytea":
|
||||
return "[]byte"
|
||||
case "text", "character", "character varying":
|
||||
case "text", "character", "character varying", "tsvector", "bit", "bit varying", "money", "json", "jsonb",
|
||||
"xml", "point", "interval", "line", "ARRAY":
|
||||
return "string"
|
||||
case "real":
|
||||
return "float32"
|
||||
|
|
@ -61,31 +58,21 @@ func (c ColumnInfo) GoBaseType() string {
|
|||
return "float64"
|
||||
case "uuid":
|
||||
return "uuid.UUID"
|
||||
case "json", "jsonb":
|
||||
return "types.JSONText"
|
||||
default:
|
||||
fmt.Println("Unknown go map type: " + c.DataType + ", " + c.EnumName + ", using string instead.")
|
||||
fmt.Println("Unknown sql type: " + c.DataType + ", " + c.EnumName + ", using string instead for model type.")
|
||||
return "string"
|
||||
}
|
||||
}
|
||||
|
||||
func (c ColumnInfo) ToGoType() string {
|
||||
func (c ColumnInfo) GoModelType() string {
|
||||
typeStr := c.GoBaseType()
|
||||
if c.IsNullable {
|
||||
if c.IsNullable && !strings.HasPrefix(typeStr, "[]") {
|
||||
return "*" + typeStr
|
||||
}
|
||||
|
||||
return typeStr
|
||||
}
|
||||
|
||||
func (c ColumnInfo) ToGoFieldName() string {
|
||||
return snaker.SnakeToCamel(c.Name)
|
||||
}
|
||||
|
||||
func (c ColumnInfo) ToGoVarName() string {
|
||||
return snaker.SnakeToCamel(c.Name) + "Column"
|
||||
}
|
||||
|
||||
func getColumnInfos(db *sql.DB, dbName, schemaName, tableName string) ([]ColumnInfo, error) {
|
||||
|
||||
query := `
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package postgres_metadata
|
|||
import (
|
||||
"database/sql"
|
||||
"github.com/serenize/snaker"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TableInfo struct {
|
||||
|
|
@ -32,8 +31,6 @@ func (t TableInfo) GetImports() []string {
|
|||
imports["time.Time"] = "time"
|
||||
case "uuid.UUID":
|
||||
imports["uuid.UUID"] = "github.com/google/uuid"
|
||||
case "types.JSONText":
|
||||
imports["types.JSONText"] = "github.com/sub0zero/go-sqlbuilder/types"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -46,26 +43,10 @@ func (t TableInfo) GetImports() []string {
|
|||
return ret
|
||||
}
|
||||
|
||||
func (t TableInfo) ToGoModelStructName() string {
|
||||
return snaker.SnakeToCamel(t.name)
|
||||
}
|
||||
|
||||
func (t TableInfo) ToGoVarName() string {
|
||||
return snaker.SnakeToCamel(t.name)
|
||||
}
|
||||
|
||||
func (t TableInfo) ToGoStructName() string {
|
||||
func (t TableInfo) GoStructName() string {
|
||||
return snaker.SnakeToCamel(t.name) + "Table"
|
||||
}
|
||||
|
||||
func (t TableInfo) ToGoColumnFieldList(sep string) string {
|
||||
columnNames := []string{}
|
||||
for _, columnInfo := range t.Columns {
|
||||
columnNames = append(columnNames, columnInfo.ToGoVarName())
|
||||
}
|
||||
return strings.Join(columnNames, sep)
|
||||
}
|
||||
|
||||
func GetTableInfo(db *sql.DB, dbName, schemaName, tableName string) (tableInfo TableInfo, err error) {
|
||||
|
||||
tableInfo.SchemaName = schemaName
|
||||
|
|
|
|||
|
|
@ -13,47 +13,57 @@ var autoGenWarningTemplate = `
|
|||
|
||||
`
|
||||
|
||||
var sqlBuilderTableTemplate = `package table
|
||||
var sqlBuilderTableTemplate = `
|
||||
{{define "column-list" -}}
|
||||
{{- range $i, $c := . }}
|
||||
{{- if gt $i 0 }}, {{end}}{{camelize $c.Name}}Column
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
|
||||
{{define "nullable" -}}
|
||||
{{- if .IsNullable}}sqlbuilder.Nullable{{else}}sqlbuilder.NotNullable{{end}}
|
||||
{{- end}}
|
||||
|
||||
package table
|
||||
|
||||
import (
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder"
|
||||
)
|
||||
|
||||
type {{.ToGoStructName}} struct {
|
||||
type {{.GoStructName}} struct {
|
||||
sqlbuilder.Table
|
||||
|
||||
//Columns
|
||||
{{- range .Columns}}
|
||||
{{.ToGoFieldName}} *sqlbuilder.{{.ToSqlBuilderColumnType}}
|
||||
{{camelize .Name}} *sqlbuilder.{{.SqlBuilderColumnType}}
|
||||
{{- end}}
|
||||
|
||||
AllColumns sqlbuilder.ColumnList
|
||||
}
|
||||
|
||||
var {{.ToGoVarName}} = new{{.ToGoStructName}}()
|
||||
var {{camelize .Name}} = new{{.GoStructName}}()
|
||||
|
||||
func new{{.ToGoStructName}}() *{{.ToGoStructName}} {
|
||||
func new{{.GoStructName}}() *{{.GoStructName}} {
|
||||
var (
|
||||
{{- range .Columns}}
|
||||
{{.ToGoVarName}} = sqlbuilder.New{{.ToSqlBuilderColumnType}}("{{.Name}}", {{if .IsNullable}}sqlbuilder.Nullable{{else}}sqlbuilder.NotNullable{{end}})
|
||||
{{camelize .Name}}Column = sqlbuilder.New{{.SqlBuilderColumnType}}("{{.Name}}", {{template "nullable" .}})
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
return &{{.ToGoStructName}}{
|
||||
Table: *sqlbuilder.NewTable("{{.SchemaName}}", "{{.Name}}", {{.ToGoColumnFieldList ", "}}),
|
||||
return &{{.GoStructName}}{
|
||||
Table: *sqlbuilder.NewTable("{{.SchemaName}}", "{{.Name}}", {{template "column-list" .Columns}}),
|
||||
|
||||
//Columns
|
||||
{{- range .Columns}}
|
||||
{{.ToGoFieldName}}: {{.ToGoVarName}},
|
||||
{{camelize .Name}}: {{camelize .Name}}Column,
|
||||
{{- end}}
|
||||
|
||||
AllColumns: sqlbuilder.ColumnList{ {{.ToGoColumnFieldList ", "}} },
|
||||
AllColumns: sqlbuilder.ColumnList{ {{template "column-list" .Columns}} },
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (a *{{.ToGoStructName}}) AS(alias string) *{{.ToGoStructName}} {
|
||||
aliasTable := new{{.ToGoStructName}}()
|
||||
func (a *{{.GoStructName}}) AS(alias string) *{{.GoStructName}} {
|
||||
aliasTable := new{{.GoStructName}}()
|
||||
|
||||
aliasTable.Table.SetAlias(alias)
|
||||
|
||||
|
|
@ -73,9 +83,9 @@ import (
|
|||
{{end}}
|
||||
|
||||
|
||||
type {{.ToGoModelStructName}} struct {
|
||||
type {{camelize .Name}} struct {
|
||||
{{- range .Columns}}
|
||||
{{.ToGoFieldName}} {{.ToGoType}} {{if $.IsUnique .Name}}` + "`sql:\"unique\"`" + ` {{end}}
|
||||
{{camelize .Name}} {{.GoModelType}} {{if $.IsUnique .Name}}` + "`sql:\"unique\"`" + ` {{end}}
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue