MySQL generator support.
This commit is contained in:
parent
3857715472
commit
043a0dc4c0
15 changed files with 659 additions and 318 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package postgresmeta
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
|
@ -12,6 +12,7 @@ type ColumnInfo struct {
|
|||
Name string
|
||||
IsNullable bool
|
||||
DataType string
|
||||
IsUnsigned bool
|
||||
EnumName string
|
||||
}
|
||||
|
||||
|
|
@ -20,11 +21,13 @@ func (c ColumnInfo) SqlBuilderColumnType() string {
|
|||
switch c.DataType {
|
||||
case "boolean":
|
||||
return "Bool"
|
||||
case "smallint", "integer", "bigint":
|
||||
case "smallint", "integer", "bigint",
|
||||
"tinyint", "mediumint", "int", "year": //MySQL
|
||||
return "Integer"
|
||||
case "date":
|
||||
return "Date"
|
||||
case "timestamp without time zone":
|
||||
case "timestamp without time zone",
|
||||
"timestamp": //MySQL:
|
||||
return "Timestamp"
|
||||
case "timestamp with time zone":
|
||||
return "Timestampz"
|
||||
|
|
@ -32,10 +35,12 @@ func (c ColumnInfo) SqlBuilderColumnType() string {
|
|||
return "Time"
|
||||
case "time with time zone":
|
||||
return "Timez"
|
||||
case "USER-DEFINED", "text", "character", "character varying", "bytea", "uuid",
|
||||
"tsvector", "bit", "bit varying", "money", "json", "jsonb", "xml", "point", "interval", "line", "ARRAY":
|
||||
case "USER-DEFINED", "enum", "text", "character", "character varying", "bytea", "uuid",
|
||||
"tsvector", "bit", "bit varying", "money", "json", "jsonb", "xml", "point", "interval", "line", "ARRAY",
|
||||
"char", "varchar", "binary", "varbinary",
|
||||
"tinyblob", "blob", "mediumblob", "longblob", "tinytext", "mediumtext", "longtext": // MySQL
|
||||
return "String"
|
||||
case "real", "numeric", "decimal", "double precision":
|
||||
case "real", "numeric", "decimal", "double precision", "float":
|
||||
return "Float"
|
||||
default:
|
||||
fmt.Println("Unsupported sql type: " + c.DataType + ", using string column instead for sql builder.")
|
||||
|
|
@ -46,22 +51,29 @@ func (c ColumnInfo) SqlBuilderColumnType() string {
|
|||
// GoBaseType returns model type for column info.
|
||||
func (c ColumnInfo) GoBaseType() string {
|
||||
switch c.DataType {
|
||||
case "USER-DEFINED":
|
||||
case "USER-DEFINED", "enum":
|
||||
return utils.ToGoIdentifier(c.EnumName)
|
||||
case "boolean":
|
||||
return "bool"
|
||||
case "smallint":
|
||||
case "tinyint":
|
||||
return "int8"
|
||||
case "smallint",
|
||||
"year":
|
||||
return "int16"
|
||||
case "integer":
|
||||
case "integer",
|
||||
"mediumint", "int": //MySQL
|
||||
return "int32"
|
||||
case "bigint":
|
||||
return "int64"
|
||||
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone":
|
||||
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone",
|
||||
"timestamp": // MySQL
|
||||
return "time.Time"
|
||||
case "bytea":
|
||||
case "bytea",
|
||||
"tinyblob", "blob", "mediumblob", "longblob": //MySQL
|
||||
return "[]byte"
|
||||
case "text", "character", "character varying", "tsvector", "bit", "bit varying", "money", "json", "jsonb",
|
||||
"xml", "point", "interval", "line", "ARRAY":
|
||||
"xml", "point", "interval", "line", "ARRAY",
|
||||
"char", "varchar", "binary", "varbinary", "tinytext", "mediumtext", "longtext": // MySQL
|
||||
return "string"
|
||||
case "real":
|
||||
return "float32"
|
||||
|
|
@ -79,6 +91,11 @@ func (c ColumnInfo) GoBaseType() string {
|
|||
// column can be NULL.
|
||||
func (c ColumnInfo) GoModelType() string {
|
||||
typeStr := c.GoBaseType()
|
||||
|
||||
if strings.Contains(typeStr, "int") && c.IsUnsigned {
|
||||
typeStr = "u" + typeStr
|
||||
}
|
||||
|
||||
if c.IsNullable {
|
||||
return "*" + typeStr
|
||||
}
|
||||
|
|
@ -101,15 +118,9 @@ func (c ColumnInfo) GoModelTag(isPrimaryKey bool) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func getColumnInfos(db *sql.DB, dbName, schemaName, tableName string) ([]ColumnInfo, error) {
|
||||
func getColumnInfos(db *sql.DB, querySet MetaDataQuerySet, 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)
|
||||
rows, err := db.Query(querySet.ListOfColumnsQuery(), schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -121,7 +132,7 @@ order by ordinal_position;`
|
|||
for rows.Next() {
|
||||
columnInfo := ColumnInfo{}
|
||||
var isNullable string
|
||||
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType, &columnInfo.EnumName)
|
||||
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType, &columnInfo.EnumName, &columnInfo.IsUnsigned)
|
||||
|
||||
columnInfo.IsNullable = isNullable == "YES"
|
||||
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
package postgresmeta
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/generator/internal/metadata"
|
||||
)
|
||||
|
||||
// EnumInfo struct
|
||||
|
|
@ -16,17 +15,9 @@ 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;`
|
||||
func getEnumInfos(db *sql.DB, querySet MetaDataQuerySet, schemaName string) ([]MetaData, error) {
|
||||
|
||||
rows, err := db.Query(query, schemaName)
|
||||
rows, err := db.Query(querySet.ListOfEnumsQuery(), schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -55,7 +46,7 @@ ORDER BY n.nspname, t.typname, e.enumsortorder;`
|
|||
return nil, err
|
||||
}
|
||||
|
||||
ret := []metadata.MetaData{}
|
||||
ret := []MetaData{}
|
||||
|
||||
for enumName, enumValues := range enumsInfosMap {
|
||||
ret = append(ret, EnumInfo{
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
package postgresmeta
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/generator/internal/metadata"
|
||||
)
|
||||
|
||||
// SchemaInfo metadata struct
|
||||
type SchemaInfo struct {
|
||||
DatabaseName string
|
||||
Name string
|
||||
TableInfos []metadata.MetaData
|
||||
EnumInfos []metadata.MetaData
|
||||
}
|
||||
|
||||
// GetSchemaInfo returns schema information from db connection.
|
||||
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)
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
143
generator/internal/metadata/query.go
Normal file
143
generator/internal/metadata/query.go
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
package metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MetaDataQuerySet interface {
|
||||
ListOfTablesQuery() string
|
||||
PrimaryKeysQuery() string
|
||||
ListOfColumnsQuery() string
|
||||
ListOfEnumsQuery() string
|
||||
|
||||
GetEnumsMetaData(db *sql.DB, schemaName string) ([]MetaData, error)
|
||||
}
|
||||
|
||||
type PostgresQuerySet struct{}
|
||||
|
||||
func (p *PostgresQuerySet) ListOfTablesQuery() string {
|
||||
return `
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
where table_schema = $1 and table_type = 'BASE TABLE';
|
||||
`
|
||||
}
|
||||
|
||||
func (p *PostgresQuerySet) PrimaryKeysQuery() string {
|
||||
return `
|
||||
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_schema = $1 AND t.table_name = $2 AND t.constraint_type = 'PRIMARY KEY';
|
||||
`
|
||||
}
|
||||
|
||||
func (p *PostgresQuerySet) ListOfColumnsQuery() string {
|
||||
return `
|
||||
SELECT column_name, is_nullable, data_type, udt_name, FALSE
|
||||
FROM information_schema.columns
|
||||
where table_schema = $1 and table_name = $2
|
||||
order by ordinal_position;`
|
||||
}
|
||||
|
||||
func (p *PostgresQuerySet) ListOfEnumsQuery() string {
|
||||
return `
|
||||
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;`
|
||||
}
|
||||
|
||||
func (p *PostgresQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) ([]MetaData, error) {
|
||||
return getEnumInfos(db, p, schemaName)
|
||||
}
|
||||
|
||||
// =======================================================================//
|
||||
|
||||
type MySqlQuerySet struct{}
|
||||
|
||||
func (m *MySqlQuerySet) ListOfTablesQuery() string {
|
||||
return `
|
||||
SELECT table_name
|
||||
FROM INFORMATION_SCHEMA.tables
|
||||
WHERE table_schema = ? and table_type = 'BASE TABLE';
|
||||
`
|
||||
}
|
||||
|
||||
func (m *MySqlQuerySet) PrimaryKeysQuery() string {
|
||||
return `
|
||||
SELECT k.column_name
|
||||
FROM information_schema.table_constraints t
|
||||
JOIN information_schema.key_column_usage k
|
||||
USING(constraint_name,table_schema,table_name)
|
||||
WHERE t.constraint_type='PRIMARY KEY'
|
||||
AND t.table_schema= ?
|
||||
AND t.table_name= ?;
|
||||
`
|
||||
}
|
||||
|
||||
func (m *MySqlQuerySet) ListOfColumnsQuery() string {
|
||||
return `
|
||||
SELECT COLUMN_NAME,
|
||||
IS_NULLABLE, IF(COLUMN_TYPE = 'tinyint(1)', 'boolean', DATA_TYPE),
|
||||
IF(DATA_TYPE = 'enum', CONCAT(TABLE_NAME, '_', COLUMN_NAME), ''),
|
||||
COLUMN_TYPE LIKE '%unsigned%'
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = ? and table_name = ?
|
||||
ORDER BY ordinal_position;
|
||||
`
|
||||
}
|
||||
|
||||
func (m *MySqlQuerySet) ListOfEnumsQuery() string {
|
||||
return `
|
||||
SELECT (CASE DATA_TYPE WHEN 'enum' then CONCAT(TABLE_NAME, '_', COLUMN_NAME) ELSE '' END ), SUBSTRING(COLUMN_TYPE,5)
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = ?
|
||||
AND DATA_TYPE = 'enum';
|
||||
`
|
||||
}
|
||||
|
||||
func (m *MySqlQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) ([]MetaData, error) {
|
||||
|
||||
rows, err := db.Query(m.ListOfEnumsQuery(), schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
ret := []MetaData{}
|
||||
|
||||
for rows.Next() {
|
||||
var enumName string
|
||||
var enumValues string
|
||||
err = rows.Scan(&enumName, &enumValues)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(enumValues)
|
||||
|
||||
enumValues = strings.Replace(enumValues[1:len(enumValues)-1], "'", "", -1)
|
||||
|
||||
ret = append(ret, EnumInfo{
|
||||
name: enumName,
|
||||
Values: strings.Split(enumValues, ","),
|
||||
})
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
||||
}
|
||||
72
generator/internal/metadata/schema_info.go
Normal file
72
generator/internal/metadata/schema_info.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// SchemaInfo metadata struct
|
||||
type SchemaInfo struct {
|
||||
TableInfos []MetaData
|
||||
EnumInfos []MetaData
|
||||
}
|
||||
|
||||
// GetSchemaInfo returns schema information from db connection.
|
||||
func GetSchemaInfo(db *sql.DB, schemaName string, querySet MetaDataQuerySet) (schemaInfo SchemaInfo, err error) {
|
||||
|
||||
schemaInfo.TableInfos, err = getTableInfos(db, querySet, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
schemaInfo.EnumInfos, err = querySet.GetEnumsMetaData(db, schemaName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(" FOUND", len(schemaInfo.TableInfos), "table(s), ", len(schemaInfo.EnumInfos), "enum(s)")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getTableInfos(db *sql.DB, querySet MetaDataQuerySet, schemaName string) ([]MetaData, error) {
|
||||
|
||||
fmt.Println(querySet.ListOfTablesQuery())
|
||||
|
||||
rows, err := db.Query(querySet.ListOfTablesQuery(), schemaName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
ret := []MetaData{}
|
||||
for rows.Next() {
|
||||
var tableName string
|
||||
|
||||
err = rows.Scan(&tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(tableName)
|
||||
|
||||
tableInfo, err := GetTableInfo(db, querySet, schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret = append(ret, tableInfo)
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package postgresmeta
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
|
@ -68,17 +68,17 @@ func (t TableInfo) GoStructName() string {
|
|||
}
|
||||
|
||||
// GetTableInfo returns table info metadata
|
||||
func GetTableInfo(db *sql.DB, dbName, schemaName, tableName string) (tableInfo TableInfo, err error) {
|
||||
func GetTableInfo(db *sql.DB, querySet MetaDataQuerySet, schemaName, tableName string) (tableInfo TableInfo, err error) {
|
||||
|
||||
tableInfo.SchemaName = schemaName
|
||||
tableInfo.name = tableName
|
||||
|
||||
tableInfo.PrimaryKeys, err = getPrimaryKeys(db, dbName, schemaName, tableName)
|
||||
tableInfo.PrimaryKeys, err = getPrimaryKeys(db, querySet, schemaName, tableName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tableInfo.Columns, err = getColumnInfos(db, dbName, schemaName, tableName)
|
||||
tableInfo.Columns, err = getColumnInfos(db, querySet, schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -87,15 +87,9 @@ func GetTableInfo(db *sql.DB, dbName, schemaName, tableName string) (tableInfo T
|
|||
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)
|
||||
func getPrimaryKeys(db *sql.DB, querySet MetaDataQuerySet, schemaName, tableName string) (map[string]bool, error) {
|
||||
|
||||
rows, err := db.Query(querySet.PrimaryKeysQuery(), schemaName, tableName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
Loading…
Add table
Add a link
Reference in a new issue