chore(bee api -driver=postgres): change Postgres API generate Primary Key with Serial (Auto increme$

Change in
Methods:
1. func (postgresDB) GetColumns()
2. func isSQLDecimal()
3. func extractIntSignness()
4. func extractDecimal()

Description
1. Update query, extra will return "auto_increment" instead of blank "", Primary Key can work toget$
2. Increase type of decimal
3. Fix return INT length
4. Fix return DECIMAL types precision
This commit is contained in:
maplerichie 2018-10-05 04:56:43 +08:00
parent 5ed819a025
commit 197d8e97f1
1 changed files with 20 additions and 13 deletions

View File

@ -415,7 +415,7 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin
// retrieve columns // retrieve columns
colDefRows, err := db.Query( colDefRows, err := db.Query(
`SELECT `SELECT
column_name, data_type, column_type, is_nullable, column_default, extra, column_comment column_name, data_type, column_type, is_nullable, column_default, extra, column_comment
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
@ -607,15 +607,19 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map
`SELECT `SELECT
column_name, column_name,
data_type, data_type,
data_type ||
CASE CASE
WHEN data_type = 'character' THEN '('||character_maximum_length||')' WHEN data_type ~ 'character' THEN data_type || '(' || character_maximum_length || ')'
WHEN data_type = 'numeric' THEN '(' || numeric_precision || ',' || numeric_scale ||')' WHEN (data_type ~ 'numeric' AND numeric_scale IS NOT NULL) THEN data_type||'(' || numeric_precision || ',' || numeric_scale || ')'
ELSE '' WHEN (data_type ~ 'numeric' AND numeric_scale IS NULL) THEN data_type
WHEN data_type ~ 'double' THEN data_type || '(' || numeric_precision || ')'
WHEN data_type ~ 'real' THEN data_type || '(' || numeric_precision || ')'
ELSE NULL
END AS column_type, END AS column_type,
is_nullable, is_nullable,
column_default, column_default,
'' AS extra CASE
WHEN column_default ~ 'nextval' THEN 'auto_increment'
END AS extra
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
@ -651,9 +655,8 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map
col.Type = "int" col.Type = "int"
if extra == "auto_increment" { if extra == "auto_increment" {
tag.Auto = true tag.Auto = true
} else {
tag.Pk = true
} }
tag.Pk = true
} else { } else {
fkCol, isFk := table.Fk[colName] fkCol, isFk := table.Fk[colName]
isBl := false isBl := false
@ -901,7 +904,7 @@ func isSQLSignedIntType(t string) bool {
} }
func isSQLDecimal(t string) bool { func isSQLDecimal(t string) bool {
return t == "decimal" return t == "decimal" || t == "double" || t == "numeric" || t == "real" || t == "double precision"
} }
func isSQLBinaryType(t string) bool { func isSQLBinaryType(t string) bool {
@ -923,15 +926,19 @@ func extractColSize(colType string) string {
} }
func extractIntSignness(colType string) string { func extractIntSignness(colType string) string {
regex := regexp.MustCompile(`(int|smallint|mediumint|bigint)\([0-9]+\)(.*)`) regex := regexp.MustCompile(`[int|smallint|mediumint|bigint]\(([0-9]+)\)(.*)`)
signRegex := regex.FindStringSubmatch(colType) signRegex := regex.FindStringSubmatch(colType)
return strings.Trim(signRegex[2], " ") return strings.Trim(signRegex[1], " ")
} }
func extractDecimal(colType string) (digits string, decimals string) { func extractDecimal(colType string) (digits string, decimals string) {
decimalRegex := regexp.MustCompile(`decimal\(([0-9]+),([0-9]+)\)`) decimalRegex := regexp.MustCompile(`[decimal|double|numeric|real|double precision]\(([0-9]+),?([0-9]+)?\)`)
decimal := decimalRegex.FindStringSubmatch(colType) decimal := decimalRegex.FindStringSubmatch(colType)
digits, decimals = decimal[1], decimal[2] if len(decimal) == 0 {
return
} else {
digits, decimals = decimal[1], decimal[2]
}
return return
} }