Generator clean up refactoring.
This commit is contained in:
parent
7de8c1c45e
commit
b3a52ceb31
16 changed files with 372 additions and 476 deletions
127
generator/postgres-metadata/column_info.go
Normal file
127
generator/postgres-metadata/column_info.go
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
package postgres_metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/serenize/snaker"
|
||||
)
|
||||
|
||||
type ColumnInfo struct {
|
||||
Name string
|
||||
IsNullable bool
|
||||
DataType string
|
||||
EnumName string
|
||||
}
|
||||
|
||||
func (c ColumnInfo) ToSqlBuilderColumnType() string {
|
||||
switch c.DataType {
|
||||
case "boolean":
|
||||
return "BoolColumn"
|
||||
case "smallint":
|
||||
return "IntegerColumn"
|
||||
case "integer":
|
||||
return "IntegerColumn"
|
||||
case "bigint":
|
||||
return "IntegerColumn"
|
||||
case "date", "timestamp without time zone", "timestamp with time zone":
|
||||
return "TimeColumn"
|
||||
case "text", "character", "character varying", "bytea", "uuid":
|
||||
return "StringColumn"
|
||||
case "real":
|
||||
return "NumericColumn"
|
||||
case "numeric", "double precision":
|
||||
return "NumericColumn"
|
||||
default:
|
||||
fmt.Println("Unknownl type: " + c.DataType + ", using string column instead.")
|
||||
return "StringColumn"
|
||||
}
|
||||
}
|
||||
|
||||
func (c ColumnInfo) GoBaseType() string {
|
||||
switch c.DataType {
|
||||
case "USER-DEFINED":
|
||||
return snaker.SnakeToCamel(c.EnumName)
|
||||
case "boolean":
|
||||
return "bool"
|
||||
case "smallint":
|
||||
return "int16"
|
||||
case "integer":
|
||||
return "int32"
|
||||
case "bigint":
|
||||
return "int64"
|
||||
case "date", "timestamp without time zone", "timestamp with time zone":
|
||||
return "time.Time"
|
||||
case "bytea":
|
||||
return "[]byte"
|
||||
case "text", "character", "character varying":
|
||||
return "string"
|
||||
case "real":
|
||||
return "float32"
|
||||
case "numeric", "double precision":
|
||||
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.")
|
||||
return "string"
|
||||
}
|
||||
}
|
||||
|
||||
func (c ColumnInfo) ToGoType() string {
|
||||
typeStr := c.GoBaseType()
|
||||
if c.IsNullable {
|
||||
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 := `
|
||||
SELECT column_name, is_nullable, data_type, udt_name
|
||||
FROM information_schema.columns
|
||||
where table_catalog = $1 and table_schema = $2 and table_name = $3
|
||||
order by ordinal_position;`
|
||||
|
||||
rows, err := db.Query(query, dbName, schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
ret := []ColumnInfo{}
|
||||
|
||||
for rows.Next() {
|
||||
columnInfo := ColumnInfo{}
|
||||
var isNullable string
|
||||
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType, &columnInfo.EnumName)
|
||||
|
||||
columnInfo.IsNullable = isNullable == "YES"
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret = append(ret, columnInfo)
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
69
generator/postgres-metadata/enum_info.go
Normal file
69
generator/postgres-metadata/enum_info.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package postgres_metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/sub0zero/go-sqlbuilder/generator/metadata"
|
||||
)
|
||||
|
||||
type EnumInfo struct {
|
||||
name string
|
||||
Values []string
|
||||
}
|
||||
|
||||
func (e EnumInfo) Name() string {
|
||||
return e.name
|
||||
}
|
||||
|
||||
func getEnumInfos(db *sql.DB, schemaName string) ([]metadata.MetaData, error) {
|
||||
query := `
|
||||
SELECT t.typname,
|
||||
e.enumlabel
|
||||
FROM pg_catalog.pg_type t
|
||||
JOIN pg_catalog.pg_enum e on t.oid = e.enumtypid
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
||||
WHERE n.nspname = $1
|
||||
ORDER BY n.nspname, t.typname, e.enumsortorder;`
|
||||
|
||||
rows, err := db.Query(query, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
enumsInfosMap := map[string][]string{}
|
||||
for rows.Next() {
|
||||
var enumName string
|
||||
var enumValue string
|
||||
err = rows.Scan(&enumName, &enumValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
enumValues := enumsInfosMap[enumName]
|
||||
|
||||
enumValues = append(enumValues, enumValue)
|
||||
|
||||
enumsInfosMap[enumName] = enumValues
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := []metadata.MetaData{}
|
||||
|
||||
for enumName, enumValues := range enumsInfosMap {
|
||||
ret = append(ret, EnumInfo{
|
||||
enumName,
|
||||
enumValues,
|
||||
})
|
||||
}
|
||||
|
||||
fmt.Println("FOUND", len(ret), " enums")
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
76
generator/postgres-metadata/schema_info.go
Normal file
76
generator/postgres-metadata/schema_info.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package postgres_metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/sub0zero/go-sqlbuilder/generator/metadata"
|
||||
)
|
||||
|
||||
type SchemaInfo struct {
|
||||
DatabaseName string
|
||||
Name string
|
||||
TableInfos []metadata.MetaData
|
||||
EnumInfos []metadata.MetaData
|
||||
}
|
||||
|
||||
func GetSchemaInfo(db *sql.DB, databaseName, schemaName string) (schemaInfo SchemaInfo, err error) {
|
||||
|
||||
schemaInfo.DatabaseName = databaseName
|
||||
schemaInfo.Name = schemaName
|
||||
|
||||
schemaInfo.TableInfos, err = getTableInfos(db, databaseName, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
schemaInfo.EnumInfos, err = getEnumInfos(db, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getTableInfos(db *sql.DB, dbName, schemaName string) ([]metadata.MetaData, error) {
|
||||
|
||||
query := `
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
where table_catalog = $1 and table_schema = $2 and table_type = 'BASE TABLE';`
|
||||
|
||||
rows, err := db.Query(query, dbName, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
ret := []metadata.MetaData{}
|
||||
for rows.Next() {
|
||||
var tableName string
|
||||
err = rows.Scan(&tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tableInfo, err := GetTableInfo(db, dbName, schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret = append(ret, tableInfo)
|
||||
}
|
||||
|
||||
fmt.Println("FOUND", len(ret), "tables")
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
116
generator/postgres-metadata/table_info.go
Normal file
116
generator/postgres-metadata/table_info.go
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package postgres_metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/serenize/snaker"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TableInfo struct {
|
||||
SchemaName string
|
||||
name string
|
||||
PrimaryKeys map[string]bool
|
||||
Columns []ColumnInfo
|
||||
}
|
||||
|
||||
func (t TableInfo) Name() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (t TableInfo) IsUnique(columnName string) bool {
|
||||
return t.PrimaryKeys[columnName]
|
||||
}
|
||||
|
||||
func (t TableInfo) GetImports() []string {
|
||||
imports := map[string]string{}
|
||||
|
||||
for _, column := range t.Columns {
|
||||
columnType := column.GoBaseType()
|
||||
|
||||
switch columnType {
|
||||
case "time.Time":
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
||||
ret := []string{}
|
||||
|
||||
for _, packageImport := range imports {
|
||||
ret = append(ret, packageImport)
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
tableInfo.name = tableName
|
||||
|
||||
tableInfo.PrimaryKeys, err = getPrimaryKeys(db, dbName, schemaName, tableName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tableInfo.Columns, err = getColumnInfos(db, dbName, schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getPrimaryKeys(db *sql.DB, dbName, schemaName, tableName string) (map[string]bool, error) {
|
||||
query := `
|
||||
SELECT c.column_name
|
||||
FROM information_schema.key_column_usage AS c
|
||||
LEFT JOIN information_schema.table_constraints AS t
|
||||
ON t.constraint_name = c.constraint_name
|
||||
WHERE t.table_catalog = $1 AND t.table_schema = $2 AND t.table_name = $3 AND t.constraint_type = 'PRIMARY KEY';
|
||||
`
|
||||
rows, err := db.Query(query, dbName, schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
primaryKeyMap := map[string]bool{}
|
||||
|
||||
for rows.Next() {
|
||||
primaryKey := ""
|
||||
err := rows.Scan(&primaryKey)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
primaryKeyMap[primaryKey] = true
|
||||
}
|
||||
|
||||
return primaryKeyMap, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue