jet/generator/internal/metadata/column_info.go

154 lines
3.7 KiB
Go
Raw Normal View History

2019-07-27 10:40:30 +02:00
package metadata
import (
"database/sql"
"fmt"
2019-07-04 17:54:15 +02:00
"github.com/go-jet/jet/internal/utils"
"strings"
)
// ColumnInfo metadata struct
type ColumnInfo struct {
Name string
IsNullable bool
DataType string
2019-07-27 10:40:30 +02:00
IsUnsigned bool
2019-04-03 19:21:46 +02:00
EnumName string
}
// SqlBuilderColumnType returns type of jet sql builder column
func (c ColumnInfo) SqlBuilderColumnType() string {
switch c.DataType {
case "boolean":
2019-06-08 16:34:15 +02:00
return "Bool"
2019-07-27 10:40:30 +02:00
case "smallint", "integer", "bigint",
"tinyint", "mediumint", "int", "year": //MySQL
2019-06-08 16:34:15 +02:00
return "Integer"
case "date":
2019-06-08 16:34:15 +02:00
return "Date"
2019-07-27 10:40:30 +02:00
case "timestamp without time zone",
"timestamp": //MySQL:
2019-06-08 16:34:15 +02:00
return "Timestamp"
case "timestamp with time zone":
2019-06-08 16:34:15 +02:00
return "Timestampz"
case "time without time zone":
2019-06-08 16:34:15 +02:00
return "Time"
case "time with time zone":
2019-06-08 16:34:15 +02:00
return "Timez"
2019-07-27 10:40:30 +02:00
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
2019-06-08 16:34:15 +02:00
return "String"
2019-07-27 10:40:30 +02:00
case "real", "numeric", "decimal", "double precision", "float":
2019-06-08 16:34:15 +02:00
return "Float"
default:
2019-07-17 13:22:14 +02:00
fmt.Println("Unsupported sql type: " + c.DataType + ", using string column instead for sql builder.")
2019-06-08 16:34:15 +02:00
return "String"
}
}
// GoBaseType returns model type for column info.
func (c ColumnInfo) GoBaseType() string {
2019-05-20 17:37:55 +02:00
switch c.DataType {
2019-07-27 10:40:30 +02:00
case "USER-DEFINED", "enum":
2019-07-04 17:54:15 +02:00
return utils.ToGoIdentifier(c.EnumName)
2019-05-20 17:37:55 +02:00
case "boolean":
return "bool"
2019-07-27 10:40:30 +02:00
case "tinyint":
return "int8"
case "smallint",
"year":
2019-05-20 17:37:55 +02:00
return "int16"
2019-07-27 10:40:30 +02:00
case "integer",
"mediumint", "int": //MySQL
2019-05-20 17:37:55 +02:00
return "int32"
case "bigint":
return "int64"
2019-07-27 10:40:30 +02:00
case "date", "timestamp without time zone", "timestamp with time zone", "time with time zone", "time without time zone",
"timestamp": // MySQL
2019-05-20 17:37:55 +02:00
return "time.Time"
2019-07-27 10:40:30 +02:00
case "bytea",
"tinyblob", "blob", "mediumblob", "longblob": //MySQL
2019-05-20 17:37:55 +02:00
return "[]byte"
case "text", "character", "character varying", "tsvector", "bit", "bit varying", "money", "json", "jsonb",
2019-07-27 10:40:30 +02:00
"xml", "point", "interval", "line", "ARRAY",
"char", "varchar", "binary", "varbinary", "tinytext", "mediumtext", "longtext": // MySQL
2019-05-20 17:37:55 +02:00
return "string"
case "real":
return "float32"
case "numeric", "decimal", "double precision":
2019-05-20 17:37:55 +02:00
return "float64"
case "uuid":
return "uuid.UUID"
default:
2019-07-17 13:22:14 +02:00
fmt.Println("Unsupported sql type: " + c.DataType + ", " + c.EnumName + ", using string instead for model type.")
2019-05-20 17:37:55 +02:00
return "string"
}
}
// GoModelType returns model type for column info with optional pointer if
// column can be NULL.
func (c ColumnInfo) GoModelType() string {
2019-05-24 13:13:13 +02:00
typeStr := c.GoBaseType()
2019-07-27 10:40:30 +02:00
if strings.Contains(typeStr, "int") && c.IsUnsigned {
typeStr = "u" + typeStr
}
if c.IsNullable {
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
}
// GoModelTag returns model field tag for column
2019-06-12 12:47:30 +02:00
func (c ColumnInfo) GoModelTag(isPrimaryKey bool) string {
tags := []string{}
if isPrimaryKey {
tags = append(tags, "primary_key")
}
if len(tags) > 0 {
return "`sql:\"" + strings.Join(tags, ",") + "\"`"
}
return ""
}
2019-07-27 10:40:30 +02:00
func getColumnInfos(db *sql.DB, querySet MetaDataQuerySet, schemaName, tableName string) ([]ColumnInfo, error) {
2019-07-27 10:40:30 +02:00
rows, err := db.Query(querySet.ListOfColumnsQuery(), schemaName, tableName)
if err != nil {
return nil, err
}
defer rows.Close()
ret := []ColumnInfo{}
for rows.Next() {
columnInfo := ColumnInfo{}
var isNullable string
2019-07-27 10:40:30 +02:00
err := rows.Scan(&columnInfo.Name, &isNullable, &columnInfo.DataType, &columnInfo.EnumName, &columnInfo.IsUnsigned)
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
}