refactor create table for postgres
This commit is contained in:
parent
dbc2de380b
commit
b4cde70a15
|
@ -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
|
col := table.GetColumn(colName)
|
||||||
|
s, _ := ColumnString(db, col, col.IsPrimaryKey && len(table.PrimaryKeys) == 1)
|
||||||
|
b.WriteString(s)
|
||||||
|
|
||||||
for _, colName := range table.ColumnsSeq() {
|
if len(table.PrimaryKeys) > 1 {
|
||||||
col := table.GetColumn(colName)
|
b.WriteString("PRIMARY KEY ( ")
|
||||||
s, _ := ColumnString(db, col, col.IsPrimaryKey && len(pkList) == 1)
|
b.WriteString(quoter.Join(table.PrimaryKeys, ","))
|
||||||
sql += s
|
b.WriteString(" )")
|
||||||
sql = strings.TrimSpace(sql)
|
|
||||||
sql += ", "
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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]
|
|
||||||
}
|
}
|
||||||
sql += ")"
|
|
||||||
|
|
||||||
return []string{sql}, true
|
b.WriteString(")")
|
||||||
|
|
||||||
|
return []string{b.String()}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {
|
func (db *postgres) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {
|
||||||
|
|
Loading…
Reference in New Issue