2019-03-04 19:35:49 +01:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2019-04-03 14:18:58 +02:00
|
|
|
"fmt"
|
2019-03-04 19:35:49 +01:00
|
|
|
"github.com/serenize/snaker"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
TableInfo *TableInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) IsUnique() bool {
|
|
|
|
|
for _, uniqueColumn := range c.TableInfo.PrimaryKeys {
|
|
|
|
|
if uniqueColumn == c.Name {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) ToGoVarName() string {
|
2019-03-16 20:41:06 +01:00
|
|
|
return snaker.SnakeToCamel(c.Name) + "Column"
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 11:03:07 +02:00
|
|
|
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":
|
2019-04-03 14:18:58 +02:00
|
|
|
return "TimeColumn"
|
2019-04-04 13:07:21 +02:00
|
|
|
case "text", "character", "character varying", "bytea", "uuid":
|
2019-04-03 11:03:07 +02:00
|
|
|
return "StringColumn"
|
|
|
|
|
case "real":
|
|
|
|
|
return "NumericColumn"
|
|
|
|
|
case "numeric", "double precision":
|
|
|
|
|
return "NumericColumn"
|
|
|
|
|
default:
|
2019-04-04 13:07:21 +02:00
|
|
|
fmt.Println("Unknownl type: " + c.DataType + ", using string column instead.")
|
2019-04-03 11:03:07 +02:00
|
|
|
return "StringColumn"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 19:35:49 +01:00
|
|
|
func (c ColumnInfo) ToGoType() string {
|
|
|
|
|
typeStr := c.GoBaseType()
|
2019-05-20 17:37:55 +02:00
|
|
|
if c.IsNullable {
|
2019-03-04 19:35:49 +01:00
|
|
|
return "*" + typeStr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return typeStr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
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"
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) ToGoDMFieldName() string {
|
2019-05-20 17:37:55 +02:00
|
|
|
return snaker.SnakeToCamel(c.Name)
|
|
|
|
|
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) ToGoFieldName() string {
|
|
|
|
|
return snaker.SnakeToCamel(c.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fetchColumnInfos(db *sql.DB, tableInfo *TableInfo) ([]ColumnInfo, error) {
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
where table_schema = $1 and table_name = $2
|
|
|
|
|
order by ordinal_position;`
|
|
|
|
|
|
|
|
|
|
//fmt.Println(query)
|
|
|
|
|
|
|
|
|
|
rows, err := db.Query(query, tableInfo.DatabaseInfo.SchemaName, &tableInfo.Name)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
columnInfo.TableInfo = tableInfo
|
|
|
|
|
|
|
|
|
|
ret = append(ret, columnInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|