Skip to content

chore(bee api -driver=postgres): change Postgres API generate PrimaryKey and Auto tag #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions generate/g_appcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin
// retrieve columns
colDefRows, err := db.Query(
`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
information_schema.columns
WHERE
Expand Down Expand Up @@ -607,15 +607,19 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map
`SELECT
column_name,
data_type,
data_type ||
CASE
WHEN data_type = 'character' THEN '('||character_maximum_length||')'
WHEN data_type = 'numeric' THEN '(' || numeric_precision || ',' || numeric_scale ||')'
ELSE ''
WHEN data_type ~ 'character' THEN data_type || '(' || character_maximum_length || ')'
WHEN (data_type ~ 'numeric' AND numeric_scale IS NOT NULL) THEN data_type||'(' || numeric_precision || ',' || numeric_scale || ')'
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,
is_nullable,
column_default,
'' AS extra
CASE
WHEN column_default ~ 'nextval' THEN 'auto_increment'
END AS extra
FROM
information_schema.columns
WHERE
Expand Down Expand Up @@ -651,9 +655,8 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map
col.Type = "int"
if extra == "auto_increment" {
tag.Auto = true
} else {
tag.Pk = true
}
tag.Pk = true
} else {
fkCol, isFk := table.Fk[colName]
isBl := false
Expand Down Expand Up @@ -901,7 +904,7 @@ func isSQLSignedIntType(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 {
Expand All @@ -923,15 +926,19 @@ func extractColSize(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)
return strings.Trim(signRegex[2], " ")
return strings.Trim(signRegex[1], " ")
}

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)
digits, decimals = decimal[1], decimal[2]
if len(decimal) == 0 {
return
} else {
digits, decimals = decimal[1], decimal[2]
}
return
}

Expand Down