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
|
|
|
|
|
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"
|
|
|
|
|
case "text", "character", "character varying", "bytea":
|
2019-04-03 11:03:07 +02:00
|
|
|
return "StringColumn"
|
|
|
|
|
case "real":
|
|
|
|
|
return "NumericColumn"
|
|
|
|
|
case "numeric", "double precision":
|
|
|
|
|
return "NumericColumn"
|
|
|
|
|
default:
|
2019-04-03 14:18:58 +02:00
|
|
|
fmt.Println("Unknownl type: " + c.DataType + ", using string 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-03-09 14:20:44 +01:00
|
|
|
if c.IsNullable || c.TableInfo.IsForeignKey(c.Name) {
|
2019-03-04 19:35:49 +01:00
|
|
|
return "*" + typeStr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return typeStr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) GoBaseType() string {
|
|
|
|
|
if forignKeyTable, ok := c.TableInfo.ForeignTableMap[c.Name]; ok {
|
|
|
|
|
return snaker.SnakeToCamel(forignKeyTable)
|
|
|
|
|
} else {
|
|
|
|
|
switch c.DataType {
|
|
|
|
|
case "boolean":
|
|
|
|
|
return "bool"
|
|
|
|
|
case "smallint":
|
|
|
|
|
return "int16"
|
|
|
|
|
case "integer":
|
2019-03-05 18:55:47 +01:00
|
|
|
return "int32"
|
2019-03-04 19:35:49 +01:00
|
|
|
case "bigint":
|
|
|
|
|
return "int64"
|
2019-03-05 18:55:47 +01:00
|
|
|
case "date", "timestamp without time zone", "timestamp with time zone":
|
|
|
|
|
return "time.Time"
|
2019-03-04 19:35:49 +01:00
|
|
|
case "bytea":
|
|
|
|
|
return "[]byte"
|
2019-04-03 14:18:58 +02:00
|
|
|
case "text", "character", "character varying":
|
2019-03-04 19:35:49 +01:00
|
|
|
return "string"
|
2019-03-30 09:48:54 +01:00
|
|
|
case "real":
|
|
|
|
|
return "float32"
|
|
|
|
|
case "numeric", "double precision":
|
2019-03-09 14:20:44 +01:00
|
|
|
return "float64"
|
2019-03-04 19:35:49 +01:00
|
|
|
default:
|
2019-04-03 14:18:58 +02:00
|
|
|
fmt.Println("Unknown go map type: " + c.DataType + ", using string instead.")
|
2019-03-04 19:35:49 +01:00
|
|
|
return "string"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) ToGoDMFieldName() string {
|
|
|
|
|
if forignKeyTable, ok := c.TableInfo.ForeignTableMap[c.Name]; ok {
|
|
|
|
|
return snaker.SnakeToCamel(forignKeyTable)
|
|
|
|
|
} else {
|
|
|
|
|
return snaker.SnakeToCamel(c.Name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c ColumnInfo) ToGoFieldName() string {
|
|
|
|
|
return snaker.SnakeToCamel(c.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fetchColumnInfos(db *sql.DB, tableInfo *TableInfo) ([]ColumnInfo, error) {
|
|
|
|
|
|
|
|
|
|
query := `
|
|
|
|
|
SELECT column_name, is_nullable, data_type
|
|
|
|
|
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
|
|
|
|
|
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|