2019-05-24 13:13:13 +02:00
|
|
|
package postgres_metadata
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2019-04-03 14:18:58 +02:00
|
|
|
"fmt"
|
2019-03-04 19:35:49 +01:00
|
|
|
"github.com/serenize/snaker"
|
2019-05-27 13:11:15 +02:00
|
|
|
"strings"
|
2019-03-04 19:35:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ColumnInfo struct {
|
|
|
|
|
Name string
|
|
|
|
|
IsNullable bool
|
|
|
|
|
DataType string
|
2019-04-03 19:21:46 +02:00
|
|
|
EnumName string
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-27 13:11:15 +02:00
|
|
|
func (c ColumnInfo) SqlBuilderColumnType() string {
|
2019-04-03 11:03:07 +02:00
|
|
|
switch c.DataType {
|
|
|
|
|
case "boolean":
|
|
|
|
|
return "BoolColumn"
|
2019-05-27 13:11:15 +02:00
|
|
|
case "smallint", "integer", "bigint":
|
2019-04-03 11:03:07 +02:00
|
|
|
return "IntegerColumn"
|
2019-05-27 13:11:15 +02:00
|
|
|
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone":
|
2019-04-03 14:18:58 +02:00
|
|
|
return "TimeColumn"
|
2019-05-27 13:11:15 +02:00
|
|
|
case "USER-DEFINED", "text", "character", "character varying", "bytea", "uuid",
|
|
|
|
|
"tsvector", "bit", "bit varying", "money", "json", "jsonb", "xml", "point", "interval", "line", "ARRAY":
|
2019-04-03 11:03:07 +02:00
|
|
|
return "StringColumn"
|
2019-05-27 13:11:15 +02:00
|
|
|
case "real", "numeric", "double precision":
|
2019-04-03 11:03:07 +02:00
|
|
|
return "NumericColumn"
|
|
|
|
|
default:
|
2019-05-27 13:11:15 +02:00
|
|
|
fmt.Println("Unknown sql type: " + c.DataType + ", using string column instead for sql builder.")
|
2019-04-03 11:03:07 +02:00
|
|
|
return "StringColumn"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 19:35:49 +01:00
|
|
|
func (c ColumnInfo) GoBaseType() string {
|
2019-05-20 17:37:55 +02:00
|
|
|
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"
|
2019-05-27 13:11:15 +02:00
|
|
|
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone":
|
2019-05-20 17:37:55 +02:00
|
|
|
return "time.Time"
|
|
|
|
|
case "bytea":
|
|
|
|
|
return "[]byte"
|
2019-05-27 13:11:15 +02:00
|
|
|
case "text", "character", "character varying", "tsvector", "bit", "bit varying", "money", "json", "jsonb",
|
|
|
|
|
"xml", "point", "interval", "line", "ARRAY":
|
2019-05-20 17:37:55 +02:00
|
|
|
return "string"
|
|
|
|
|
case "real":
|
|
|
|
|
return "float32"
|
|
|
|
|
case "numeric", "double precision":
|
|
|
|
|
return "float64"
|
|
|
|
|
case "uuid":
|
|
|
|
|
return "uuid.UUID"
|
|
|
|
|
default:
|
2019-05-27 13:11:15 +02:00
|
|
|
fmt.Println("Unknown sql type: " + c.DataType + ", " + c.EnumName + ", using string instead for model type.")
|
2019-05-20 17:37:55 +02:00
|
|
|
return "string"
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 13:11:15 +02:00
|
|
|
func (c ColumnInfo) GoModelType() string {
|
2019-05-24 13:13:13 +02:00
|
|
|
typeStr := c.GoBaseType()
|
2019-05-27 13:11:15 +02:00
|
|
|
if c.IsNullable && !strings.HasPrefix(typeStr, "[]") {
|
2019-05-24 13:13:13 +02:00
|
|
|
return "*" + typeStr
|
|
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
return typeStr
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
func getColumnInfos(db *sql.DB, dbName, schemaName, tableName string) ([]ColumnInfo, error) {
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
query := `
|
2019-04-03 19:21:46 +02:00
|
|
|
SELECT column_name, is_nullable, data_type, udt_name
|
2019-03-04 19:35:49 +01:00
|
|
|
FROM information_schema.columns
|
2019-05-24 13:13:13 +02:00
|
|
|
where table_catalog = $1 and table_schema = $2 and table_name = $3
|
2019-03-04 19:35:49 +01:00
|
|
|
order by ordinal_position;`
|
|
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
rows, err := db.Query(query, dbName, schemaName, tableName)
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
ret := []ColumnInfo{}
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
columnInfo := ColumnInfo{}
|
|
|
|
|
var isNullable string
|
2019-04-03 19:21:46 +02:00
|
|
|
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType, &columnInfo.EnumName)
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|