Fix MySQL compatibility

This commit is contained in:
Jinzhu 2013-11-21 14:33:06 +08:00
parent 0208ce7901
commit 66ac04ba05
3 changed files with 29 additions and 21 deletions

View File

@ -9,7 +9,7 @@ import (
type mysql struct{} type mysql struct{}
func (s *mysql) BinVar(i int) string { func (s *mysql) BinVar(i int) string {
return "?" return "$$" // ?
} }
func (s *mysql) SupportLastInsertId() bool { func (s *mysql) SupportLastInsertId() bool {

View File

@ -9,7 +9,7 @@ import (
type sqlite3 struct{} type sqlite3 struct{}
func (s *sqlite3) BinVar(i int) string { func (s *sqlite3) BinVar(i int) string {
return "?" return "$$" // ?
} }
func (s *sqlite3) SupportLastInsertId() bool { func (s *sqlite3) SupportLastInsertId() bool {

46
do.go
View File

@ -27,6 +27,10 @@ type Do struct {
startedTransaction bool startedTransaction bool
} }
func (s *Do) setSql(sql string) {
s.sql = strings.Replace(sql, "$$", "?", -1)
}
func (s *Do) table() string { func (s *Do) table() string {
if len(s.tableName) == 0 { if len(s.tableName) == 0 {
if len(s.search.tableName) == 0 { if len(s.search.tableName) == 0 {
@ -68,7 +72,7 @@ func (s *Do) trace(t time.Time) {
} }
func (s *Do) raw(query string, values ...interface{}) *Do { func (s *Do) raw(query string, values ...interface{}) *Do {
s.sql = s.buildWhereCondition(map[string]interface{}{"query": query, "args": values}) s.setSql(s.buildWhereCondition(map[string]interface{}{"query": query, "args": values}))
return s return s
} }
@ -98,13 +102,13 @@ func (s *Do) prepareCreateSql() {
sqls = append(sqls, s.addToVars(value)) sqls = append(sqls, s.addToVars(value))
} }
s.sql = fmt.Sprintf( s.setSql(fmt.Sprintf(
"INSERT INTO %v (%v) VALUES (%v) %v", "INSERT INTO %v (%v) VALUES (%v) %v",
s.table(), s.table(),
strings.Join(columns, ","), strings.Join(columns, ","),
strings.Join(sqls, ","), strings.Join(sqls, ","),
s.dialect().ReturningStr(s.model.primaryKeyDb()), s.dialect().ReturningStr(s.model.primaryKeyDb()),
) ))
return return
} }
@ -246,12 +250,12 @@ func (s *Do) prepareUpdateSql(include_self bool) {
} }
} }
s.sql = fmt.Sprintf( s.setSql(fmt.Sprintf(
"UPDATE %v SET %v %v", "UPDATE %v SET %v %v",
s.table(), s.table(),
strings.Join(sqls, ", "), strings.Join(sqls, ", "),
s.combinedSql(), s.combinedSql(),
) ))
return return
} }
@ -292,9 +296,9 @@ func (s *Do) delete() *Do {
if !s.db.hasError() { if !s.db.hasError() {
if !s.search.unscope && s.model.hasColumn("DeletedAt") { if !s.search.unscope && s.model.hasColumn("DeletedAt") {
s.sql = fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", s.table(), s.addToVars(time.Now()), s.combinedSql()) s.setSql(fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", s.table(), s.addToVars(time.Now()), s.combinedSql()))
} else { } else {
s.sql = fmt.Sprintf("DELETE FROM %v %v", s.table(), s.combinedSql()) s.setSql(fmt.Sprintf("DELETE FROM %v %v", s.table(), s.combinedSql()))
} }
s.exec() s.exec()
s.model.callMethod("AfterDelete") s.model.callMethod("AfterDelete")
@ -303,7 +307,7 @@ func (s *Do) delete() *Do {
} }
func (s *Do) prepareQuerySql() { func (s *Do) prepareQuerySql() {
s.sql = fmt.Sprintf("SELECT %v FROM %v %v", s.selectSql(), s.table(), s.combinedSql()) s.setSql(fmt.Sprintf("SELECT %v FROM %v %v", s.selectSql(), s.table(), s.combinedSql()))
return return
} }
@ -506,6 +510,7 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) {
if valuer, ok := interface{}(arg).(driver.Valuer); ok { if valuer, ok := interface{}(arg).(driver.Valuer); ok {
arg, _ = valuer.Value() arg, _ = valuer.Value()
} }
str = strings.Replace(str, "?", s.addToVars(arg), 1) str = strings.Replace(str, "?", s.addToVars(arg), 1)
} }
} }
@ -686,24 +691,24 @@ func (s *Do) createTable() *Do {
} }
} }
s.sql = fmt.Sprintf("CREATE TABLE %v (%v)", s.table(), strings.Join(sqls, ",")) s.setSql(fmt.Sprintf("CREATE TABLE %v (%v)", s.table(), strings.Join(sqls, ",")))
s.exec() s.exec()
return s return s
} }
func (s *Do) dropTable() *Do { func (s *Do) dropTable() *Do {
s.sql = fmt.Sprintf("DROP TABLE %v", s.table()) s.setSql(fmt.Sprintf("DROP TABLE %v", s.table()))
s.exec() s.exec()
return s return s
} }
func (s *Do) modifyColumn(column string, typ string) { func (s *Do) modifyColumn(column string, typ string) {
s.sql = fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", s.table(), column, typ) s.setSql(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", s.table(), column, typ))
s.exec() s.exec()
} }
func (s *Do) dropColumn(column string) { func (s *Do) dropColumn(column string) {
s.sql = fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", s.table(), column) s.setSql(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", s.table(), column))
s.exec() s.exec()
} }
@ -715,19 +720,19 @@ func (s *Do) addIndex(column string, names ...string) {
index_name = fmt.Sprintf("index_%v_on_%v", s.table(), column) index_name = fmt.Sprintf("index_%v_on_%v", s.table(), column)
} }
s.sql = fmt.Sprintf("CREATE INDEX %v ON %v(%v);", index_name, s.table(), column) s.setSql(fmt.Sprintf("CREATE INDEX %v ON %v(%v);", index_name, s.table(), column))
s.exec() s.exec()
} }
func (s *Do) removeIndex(index_name string) { func (s *Do) removeIndex(index_name string) {
s.sql = fmt.Sprintf("DROP INDEX %v ON %v", index_name, s.table()) s.setSql(fmt.Sprintf("DROP INDEX %v ON %v", index_name, s.table()))
s.exec() s.exec()
} }
func (s *Do) autoMigrate() *Do { func (s *Do) autoMigrate() *Do {
var table_name string var table_name string
sql := fmt.Sprintf("SELECT table_name FROM INFORMATION_SCHEMA.tables where table_name = %v", s.addToVars(s.table())) s.setSql(fmt.Sprintf("SELECT table_name FROM INFORMATION_SCHEMA.tables where table_name = %v", s.addToVars(s.table())))
s.db.db.QueryRow(sql, s.sqlVars...).Scan(&table_name) s.db.db.QueryRow(s.sql, s.sqlVars...).Scan(&table_name)
s.sqlVars = []interface{}{} s.sqlVars = []interface{}{}
// If table doesn't exist // If table doesn't exist
@ -736,13 +741,16 @@ func (s *Do) autoMigrate() *Do {
} else { } else {
for _, field := range s.model.fields("migration") { for _, field := range s.model.fields("migration") {
var column_name, data_type string var column_name, data_type string
sql := fmt.Sprintf("SELECT column_name, data_type FROM information_schema.columns WHERE table_name = %v", s.addToVars(s.table())) s.setSql(fmt.Sprintf("SELECT column_name, data_type FROM information_schema.columns WHERE table_name = %v and column_name = %v",
s.db.db.QueryRow(fmt.Sprintf(sql+" and column_name = %v", s.addToVars(field.dbName)), s.sqlVars...).Scan(&column_name, &data_type) s.addToVars(s.table()),
s.addToVars(field.dbName),
))
s.db.db.QueryRow(s.sql, s.sqlVars...).Scan(&column_name, &data_type)
s.sqlVars = []interface{}{} s.sqlVars = []interface{}{}
// If column doesn't exist // If column doesn't exist
if len(column_name) == 0 && len(field.sqlTag()) > 0 { if len(column_name) == 0 && len(field.sqlTag()) > 0 {
s.sql = fmt.Sprintf("ALTER TABLE %v ADD %v %v;", s.table(), field.dbName, field.sqlTag()) s.setSql(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", s.table(), field.dbName, field.sqlTag()))
s.exec() s.exec()
} }
} }