refactor create table for postgres

This commit is contained in:
Lunny Xiao 2021-08-14 00:06:57 +08:00
parent dbc2de380b
commit b4cde70a15
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 17 additions and 20 deletions

View File

@ -966,38 +966,35 @@ func (db *postgres) AutoIncrStr() string {
}
func (db *postgres) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
var sql string
sql = "CREATE TABLE IF NOT EXISTS "
if tableName == "" {
tableName = table.Name
}
quoter := db.Quoter()
sql += quoter.Quote(tableName)
sql += " ("
var b strings.Builder
b.WriteString("CREATE TABLE IF NOT EXISTS ")
quoter.QuoteTo(&b, tableName)
b.WriteString(" (")
if len(table.ColumnsSeq()) > 0 {
pkList := table.PrimaryKeys
for _, colName := range table.ColumnsSeq() {
for i, colName := range table.ColumnsSeq() {
col := table.GetColumn(colName)
s, _ := ColumnString(db, col, col.IsPrimaryKey && len(pkList) == 1)
sql += s
sql = strings.TrimSpace(sql)
sql += ", "
s, _ := ColumnString(db, col, col.IsPrimaryKey && len(table.PrimaryKeys) == 1)
b.WriteString(s)
if len(table.PrimaryKeys) > 1 {
b.WriteString("PRIMARY KEY ( ")
b.WriteString(quoter.Join(table.PrimaryKeys, ","))
b.WriteString(" )")
}
if len(pkList) > 1 {
sql += "PRIMARY KEY ( "
sql += quoter.Join(pkList, ",")
sql += " ), "
if i != len(table.ColumnsSeq())-1 {
b.WriteString(", ")
}
}
sql = sql[:len(sql)-2]
}
sql += ")"
b.WriteString(")")
return []string{sql}, true
return []string{b.String()}, false
}
func (db *postgres) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {