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