new dialect interface
This commit is contained in:
parent
a4d3be797f
commit
8c79a0cc1d
|
@ -836,7 +836,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
|||
session := engine.NewSession()
|
||||
session.Statement.RefTable = table
|
||||
defer session.Close()
|
||||
isExist, err := session.isColumnExist(table.Name, col.Name)
|
||||
isExist, err := session.isColumnExist(table.Name, col)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -106,11 +106,11 @@ func (db *mssql) IndexCheckSql(tableName, idxName string) (string, []interface{}
|
|||
return sql, args
|
||||
}
|
||||
|
||||
func (db *mssql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
/*func (db *mssql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, colName}
|
||||
sql := `SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ?`
|
||||
return sql, args
|
||||
}
|
||||
}*/
|
||||
|
||||
func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
args := []interface{}{}
|
||||
|
|
|
@ -98,11 +98,11 @@ func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}
|
|||
return sql, args
|
||||
}
|
||||
|
||||
func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
/*func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{db.DbName, tableName, colName}
|
||||
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
||||
return sql, args
|
||||
}
|
||||
}*/
|
||||
|
||||
func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
args := []interface{}{db.DbName, tableName}
|
||||
|
|
|
@ -87,10 +87,26 @@ func (db *oracle) TableCheckSql(tableName string) (string, []interface{}) {
|
|||
return `SELECT table_name FROM user_tables WHERE table_name = ?`, args
|
||||
}
|
||||
|
||||
func (db *oracle) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
/*func (db *oracle) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{strings.ToUpper(tableName), strings.ToUpper(colName)}
|
||||
return "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = ?" +
|
||||
" AND column_name = ?", args
|
||||
}*/
|
||||
|
||||
func (db *oracle) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
||||
args := []interface{}{strings.ToUpper(tableName), strings.ToUpper(col.Name)}
|
||||
query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = ?" +
|
||||
" AND column_name = ?"
|
||||
rows, err := db.DB().Query(query, args...)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
return true, nil
|
||||
}
|
||||
return false, ErrNotExist
|
||||
}
|
||||
|
||||
func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||
|
|
|
@ -102,11 +102,11 @@ func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) {
|
|||
return `SELECT tablename FROM pg_tables WHERE tablename = ?`, args
|
||||
}
|
||||
|
||||
func (db *postgres) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
/*func (db *postgres) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, colName}
|
||||
return "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ?" +
|
||||
" AND column_name = ?", args
|
||||
}
|
||||
}*/
|
||||
|
||||
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||
args := []interface{}{tableName}
|
||||
|
|
11
session.go
11
session.go
|
@ -1253,7 +1253,7 @@ func (session *Session) Ping() error {
|
|||
return session.Db.Ping()
|
||||
}
|
||||
|
||||
func (session *Session) isColumnExist(tableName, colName string) (bool, error) {
|
||||
func (session *Session) isColumnExist(tableName string, col *core.Column) (bool, error) {
|
||||
err := session.newDb()
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
@ -1262,9 +1262,10 @@ func (session *Session) isColumnExist(tableName, colName string) (bool, error) {
|
|||
if session.IsAutoClose {
|
||||
defer session.Close()
|
||||
}
|
||||
sqlStr, args := session.Engine.dialect.ColumnCheckSql(tableName, colName)
|
||||
results, err := session.query(sqlStr, args...)
|
||||
return len(results) > 0, err
|
||||
return session.Engine.dialect.IsColumnExist(tableName, col)
|
||||
//sqlStr, args := session.Engine.dialect.ColumnCheckSql(tableName, colName)
|
||||
//results, err := session.query(sqlStr, args...)
|
||||
//return len(results) > 0, err
|
||||
}
|
||||
|
||||
func (session *Session) isTableExist(tableName string) (bool, error) {
|
||||
|
@ -3314,7 +3315,7 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
|
|||
}
|
||||
|
||||
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
||||
args = append(args, time.Now())
|
||||
args = append(args, session.Engine.NowTime(col.SQLType.Name))
|
||||
} else if col.IsVersion && session.Statement.checkVersion {
|
||||
args = append(args, 1)
|
||||
} else {
|
||||
|
|
|
@ -78,10 +78,25 @@ func (db *sqlite3) TableCheckSql(tableName string) (string, []interface{}) {
|
|||
return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
|
||||
}
|
||||
|
||||
func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
/*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName}
|
||||
sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
|
||||
return sql, args
|
||||
}*/
|
||||
|
||||
func (db *sqlite3) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
||||
args := []interface{}{tableName}
|
||||
query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + col.Name + "`%') or (sql like '%[" + col.Name + "]%'))"
|
||||
rows, err := db.DB().Query(query, args...)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
return true, nil
|
||||
}
|
||||
return false, ErrNotExist
|
||||
}
|
||||
|
||||
func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||
|
|
Loading…
Reference in New Issue