return sqls for create table (#1580)
return sqls for create table Reviewed-on: https://gitea.com/xorm/xorm/pulls/1580
This commit is contained in:
parent
ccf65397e8
commit
257653726e
|
@ -51,7 +51,7 @@ type Dialect interface {
|
|||
|
||||
GetTables(ctx context.Context) ([]*schemas.Table, error)
|
||||
IsTableExist(ctx context.Context, tableName string) (bool, error)
|
||||
CreateTableSQL(table *schemas.Table, tableName string) (string, bool)
|
||||
CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool)
|
||||
DropTableSQL(tableName string) (string, bool)
|
||||
|
||||
GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error)
|
||||
|
|
|
@ -486,7 +486,7 @@ WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =?
|
|||
return indexes, nil
|
||||
}
|
||||
|
||||
func (db *mssql) CreateTableSQL(table *schemas.Table, tableName string) (string, bool) {
|
||||
func (db *mssql) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
|
||||
var sql string
|
||||
if tableName == "" {
|
||||
tableName = table.Name
|
||||
|
@ -517,7 +517,7 @@ func (db *mssql) CreateTableSQL(table *schemas.Table, tableName string) (string,
|
|||
|
||||
sql = sql[:len(sql)-2] + ")"
|
||||
sql += ";"
|
||||
return sql, true
|
||||
return []string{sql}, true
|
||||
}
|
||||
|
||||
func (db *mssql) ForUpdateSQL(query string) string {
|
||||
|
|
|
@ -507,7 +507,7 @@ func (db *mysql) GetIndexes(ctx context.Context, tableName string) (map[string]*
|
|||
return indexes, nil
|
||||
}
|
||||
|
||||
func (db *mysql) CreateTableSQL(table *schemas.Table, tableName string) (string, bool) {
|
||||
func (db *mysql) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS "
|
||||
if tableName == "" {
|
||||
tableName = table.Name
|
||||
|
@ -560,7 +560,7 @@ func (db *mysql) CreateTableSQL(table *schemas.Table, tableName string) (string,
|
|||
if db.rowFormat != "" {
|
||||
sql += " ROW_FORMAT=" + db.rowFormat
|
||||
}
|
||||
return sql, true
|
||||
return []string{sql}, true
|
||||
}
|
||||
|
||||
func (db *mysql) Filters() []Filter {
|
||||
|
|
|
@ -556,7 +556,7 @@ func (db *oracle) DropTableSQL(tableName string) (string, bool) {
|
|||
return fmt.Sprintf("DROP TABLE `%s`", tableName), false
|
||||
}
|
||||
|
||||
func (db *oracle) CreateTableSQL(table *schemas.Table, tableName string) (string, bool) {
|
||||
func (db *oracle) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
|
||||
var sql = "CREATE TABLE "
|
||||
if tableName == "" {
|
||||
tableName = table.Name
|
||||
|
@ -585,7 +585,7 @@ func (db *oracle) CreateTableSQL(table *schemas.Table, tableName string) (string
|
|||
}
|
||||
|
||||
sql = sql[:len(sql)-2] + ")"
|
||||
return sql, false
|
||||
return []string{sql}, false
|
||||
}
|
||||
|
||||
func (db *oracle) SetQuotePolicy(quotePolicy QuotePolicy) {
|
||||
|
|
|
@ -893,7 +893,7 @@ func (db *postgres) AutoIncrStr() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
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 == "" {
|
||||
|
@ -928,7 +928,7 @@ func (db *postgres) CreateTableSQL(table *schemas.Table, tableName string) (stri
|
|||
}
|
||||
sql += ")"
|
||||
|
||||
return sql, true
|
||||
return []string{sql}, true
|
||||
}
|
||||
|
||||
func (db *postgres) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {
|
||||
|
|
|
@ -244,7 +244,7 @@ func (db *sqlite3) DropIndexSQL(tableName string, index *schemas.Index) string {
|
|||
return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName))
|
||||
}
|
||||
|
||||
func (db *sqlite3) CreateTableSQL(table *schemas.Table, tableName string) (string, bool) {
|
||||
func (db *sqlite3) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
|
||||
var sql string
|
||||
sql = "CREATE TABLE IF NOT EXISTS "
|
||||
if tableName == "" {
|
||||
|
@ -279,7 +279,7 @@ func (db *sqlite3) CreateTableSQL(table *schemas.Table, tableName string) (strin
|
|||
}
|
||||
sql += ")"
|
||||
|
||||
return sql, true
|
||||
return []string{sql}, true
|
||||
}
|
||||
|
||||
func (db *sqlite3) ForUpdateSQL(query string) string {
|
||||
|
|
10
engine.go
10
engine.go
|
@ -380,10 +380,12 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
|
|||
return err
|
||||
}
|
||||
}
|
||||
s, _ := dialect.CreateTableSQL(table, "")
|
||||
_, err = io.WriteString(w, s+";\n")
|
||||
if err != nil {
|
||||
return err
|
||||
sqls, _ := dialect.CreateTableSQL(table, "")
|
||||
for _, s := range sqls {
|
||||
_, err = io.WriteString(w, s+";\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, index := range table.Indexes {
|
||||
_, err = io.WriteString(w, dialect.CreateIndexSQL(table.Name, index)+";\n")
|
||||
|
|
|
@ -640,7 +640,7 @@ func (statement *Statement) genColumnStr() string {
|
|||
return buf.String()
|
||||
}
|
||||
|
||||
func (statement *Statement) GenCreateTableSQL() string {
|
||||
func (statement *Statement) GenCreateTableSQL() []string {
|
||||
statement.RefTable.StoreEngine = statement.StoreEngine
|
||||
statement.RefTable.Charset = statement.Charset
|
||||
s, _ := statement.dialect.CreateTableSQL(statement.RefTable, statement.TableName())
|
||||
|
|
|
@ -37,9 +37,14 @@ func (session *Session) createTable(bean interface{}) error {
|
|||
return err
|
||||
}
|
||||
|
||||
sqlStr := session.statement.GenCreateTableSQL()
|
||||
_, err := session.exec(sqlStr)
|
||||
return err
|
||||
sqlStrs := session.statement.GenCreateTableSQL()
|
||||
for _, s := range sqlStrs {
|
||||
_, err := session.exec(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateIndexes create indexes
|
||||
|
|
Loading…
Reference in New Issue