bug fixes and quote args on tests

This commit is contained in:
Lunny Xiao 2017-06-20 11:10:39 +08:00
parent a382049f26
commit ba6be0ff5b
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
12 changed files with 56 additions and 66 deletions

View File

@ -2,7 +2,6 @@ dependencies:
override: override:
# './...' is a relative pattern which means all subdirectories # './...' is a relative pattern which means all subdirectories
- go get -t -d -v ./... - go get -t -d -v ./...
- go get -t -d -v github.com/go-xorm/tests
- go get -u github.com/go-xorm/core - go get -u github.com/go-xorm/core
- go get -u github.com/go-xorm/builder - go get -u github.com/go-xorm/builder
- go build -v - go build -v
@ -21,9 +20,13 @@ database:
test: test:
override: override:
# './...' is a relative pattern which means all subdirectories # './...' is a relative pattern which means all subdirectories
- go test -v -race -db="sqlite3::mysql::postgres" -conn_str="./test.db::root:@/xorm_test::dbname=xorm_test sslmode=disable" -coverprofile=coverage.txt -covermode=atomic - go test -v -race -db="sqlite3" -conn_str="./test.db" -coverprofile=coverage1.txt -covermode=atomic
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./sqlite3.sh - go test -v -race -db="mysql" -conn_str="root:@/xorm_test" -coverprofile=coverage2.txt -covermode=atomic
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./mysql.sh - go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -coverprofile=coverage3.txt -covermode=atomic
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./postgres.sh - go test -v -race -db="sqlite3" -conn_str="./test.db" -quote=2 -coverprofile=coverage4.txt -covermode=atomic
- go test -v -race -db="mysql" -conn_str="root:@/xorm_test" -quote=2 -coverprofile=coverage5.txt -covermode=atomic
- go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -quote=2 -coverprofile=coverage6.txt -covermode=atomic
- go get github.com/wadey/gocovmerge
- gocovmerge coverage1.txt coverage2.txt coverage3.txt coverage4.txt coverage5.txt coverage6.txt > coverage.txt
post: post:
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)

View File

@ -277,7 +277,7 @@ func (db *mssql) SupportInsertMany() bool {
} }
func (db *mssql) IsReserved(name string) bool { func (db *mssql) IsReserved(name string) bool {
_, ok := mssqlReservedWords[name] _, ok := mssqlReservedWords[strings.ToUpper(name)]
return ok return ok
} }

View File

@ -249,7 +249,7 @@ func (db *mysql) SupportInsertMany() bool {
} }
func (db *mysql) IsReserved(name string) bool { func (db *mysql) IsReserved(name string) bool {
_, ok := mysqlReservedWords[name] _, ok := mysqlReservedWords[strings.ToUpper(name)]
return ok return ok
} }

View File

@ -547,7 +547,7 @@ func (db *oracle) SupportInsertMany() bool {
} }
func (db *oracle) IsReserved(name string) bool { func (db *oracle) IsReserved(name string) bool {
_, ok := oracleReservedWords[name] _, ok := oracleReservedWords[strings.ToUpper(name)]
return ok return ok
} }

View File

@ -836,7 +836,7 @@ func (db *postgres) SupportInsertMany() bool {
} }
func (db *postgres) IsReserved(name string) bool { func (db *postgres) IsReserved(name string) bool {
_, ok := postgresReservedWords[name] _, ok := postgresReservedWords[strings.ToUpper(name)]
return ok return ok
} }

View File

@ -194,7 +194,7 @@ func (db *sqlite3) SupportInsertMany() bool {
} }
func (db *sqlite3) IsReserved(name string) bool { func (db *sqlite3) IsReserved(name string) bool {
_, ok := sqlite3ReservedWords[name] _, ok := sqlite3ReservedWords[strings.ToUpper(name)]
return ok return ok
} }

View File

@ -130,15 +130,6 @@ func (engine *Engine) SupportInsertMany() bool {
return engine.dialect.SupportInsertMany() return engine.dialect.SupportInsertMany()
} }
// QuoteStr Engine's database use which character as quote.
// mysql, sqlite use ` and postgres use "
func (engine *Engine) QuoteStr() string {
if engine.QuoteMode == QuoteNoAdd {
return ""
}
return engine.dialect.QuoteStr()
}
// Quote Use QuoteStr quote the string sql // Quote Use QuoteStr quote the string sql
func (engine *Engine) Quote(value string) string { func (engine *Engine) Quote(value string) string {
var buf string var buf string
@ -147,6 +138,22 @@ func (engine *Engine) Quote(value string) string {
return b.String() return b.String()
} }
func (engine *Engine) needQuote(value string) bool {
return engine.QuoteMode == QuoteAddAlways ||
(engine.QuoteMode == QuoteAddReserved && engine.dialect.IsReserved(value))
}
func (engine *Engine) reverseQuote(value string) string {
if !engine.needQuote(value) {
return value
}
return engine.dialect.QuoteStr() + value + engine.dialect.QuoteStr()
}
func (engine *Engine) removeQuotes(value string) string {
return strings.Replace(strings.Replace(value, "`", "", -1), engine.dialect.QuoteStr(), "", -1)
}
// QuoteTo quotes string and writes into the buffer // QuoteTo quotes string and writes into the buffer
func (engine *Engine) QuoteTo(buf *bytes.Buffer, value string) { func (engine *Engine) QuoteTo(buf *bytes.Buffer, value string) {
if buf == nil { if buf == nil {
@ -159,20 +166,15 @@ func (engine *Engine) QuoteTo(buf *bytes.Buffer, value string) {
} }
v := strings.Trim(value, "`"+engine.dialect.QuoteStr()) v := strings.Trim(value, "`"+engine.dialect.QuoteStr())
if engine.QuoteMode == QuoteNoAdd || if !engine.needQuote(v) {
(engine.QuoteMode == QuoteAddReserved && !engine.dialect.IsReserved(v)) {
buf.WriteString(v) buf.WriteString(v)
return return
} }
v = strings.Replace(v, ".", engine.QuoteStr()+"."+engine.QuoteStr(), -1) v = strings.Replace(v, ".", engine.reverseQuote("."), -1)
buf.WriteString(engine.dialect.Quote(v)) buf.WriteString(engine.dialect.Quote(v))
} }
func (engine *Engine) quote(sql string) string {
return engine.QuoteStr() + sql + engine.QuoteStr()
}
// SqlType will be depracated, please use SQLType instead // SqlType will be depracated, please use SQLType instead
// //
// Deprecated: use SQLType instead // Deprecated: use SQLType instead

View File

@ -212,28 +212,16 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
} }
cleanupProcessorsClosures(&session.beforeClosures) cleanupProcessorsClosures(&session.beforeClosures)
var sql = "INSERT INTO %s (%v%v%v) VALUES (%v)" var sql = "INSERT INTO %s (%v) VALUES (%v)"
var statement string var statement string
var tbName = session.Engine.Quote(session.Statement.TableName())
var quoteColNames = session.Engine.Quote(strings.Join(colNames, session.Engine.reverseQuote(", ")))
if session.Engine.dialect.DBType() == core.ORACLE { if session.Engine.dialect.DBType() == core.ORACLE {
sql = "INSERT ALL INTO %s (%v%v%v) VALUES (%v) SELECT 1 FROM DUAL" sql = "INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL"
temp := fmt.Sprintf(") INTO %s (%v%v%v) VALUES (", temp := fmt.Sprintf(") INTO %s (%v) VALUES (", tbName, quoteColNames)
session.Engine.Quote(session.Statement.TableName()), statement = fmt.Sprintf(sql, tbName, quoteColNames, strings.Join(colMultiPlaces, temp))
session.Engine.QuoteStr(),
strings.Join(colNames, session.Engine.QuoteStr()+", "+session.Engine.QuoteStr()),
session.Engine.QuoteStr())
statement = fmt.Sprintf(sql,
session.Engine.Quote(session.Statement.TableName()),
session.Engine.QuoteStr(),
strings.Join(colNames, session.Engine.QuoteStr()+", "+session.Engine.QuoteStr()),
session.Engine.QuoteStr(),
strings.Join(colMultiPlaces, temp))
} else { } else {
statement = fmt.Sprintf(sql, statement = fmt.Sprintf(sql, tbName, quoteColNames, strings.Join(colMultiPlaces, "),("))
session.Engine.Quote(session.Statement.TableName()),
session.Engine.QuoteStr(),
strings.Join(colNames, session.Engine.QuoteStr()+", "+session.Engine.QuoteStr()),
session.Engine.QuoteStr(),
strings.Join(colMultiPlaces, "),("))
} }
res, err := session.exec(statement, args...) res, err := session.exec(statement, args...)
if err != nil { if err != nil {
@ -349,18 +337,17 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
} }
var sqlStr string var sqlStr string
var tbName = session.Engine.Quote(session.Statement.TableName())
if len(colPlaces) > 0 { if len(colPlaces) > 0 {
sqlStr = fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)", sqlStr = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)",
session.Engine.Quote(session.Statement.TableName()), tbName,
session.Engine.QuoteStr(), session.Engine.Quote(strings.Join(colNames, session.Engine.reverseQuote(", "))),
strings.Join(colNames, session.Engine.Quote(", ")),
session.Engine.QuoteStr(),
colPlaces) colPlaces)
} else { } else {
if session.Engine.dialect.DBType() == core.MYSQL { if session.Engine.dialect.DBType() == core.MYSQL {
sqlStr = fmt.Sprintf("INSERT INTO %s VALUES ()", session.Engine.Quote(session.Statement.TableName())) sqlStr = fmt.Sprintf("INSERT INTO %s VALUES ()", tbName)
} else { } else {
sqlStr = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES", session.Engine.Quote(session.Statement.TableName())) sqlStr = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES", tbName)
} }
} }

View File

@ -101,11 +101,9 @@ func (session *Session) cacheUpdate(sqlStr string, args ...interface{}) error {
sps := strings.SplitN(kv, "=", 2) sps := strings.SplitN(kv, "=", 2)
sps2 := strings.Split(sps[0], ".") sps2 := strings.Split(sps[0], ".")
colName := sps2[len(sps2)-1] colName := sps2[len(sps2)-1]
if strings.Contains(colName, "`") { colName = strings.TrimSpace(strings.Replace(colName, "`", "", -1))
colName = strings.TrimSpace(strings.Replace(colName, "`", "", -1)) colName = strings.TrimSpace(session.Engine.removeQuotes(colName))
} else if strings.Contains(colName, session.Engine.QuoteStr()) { if colName == "" {
colName = strings.TrimSpace(strings.Replace(colName, session.Engine.QuoteStr(), "", -1))
} else {
session.Engine.logger.Debug("[cacheUpdate] cannot find column", tableName, colName) session.Engine.logger.Debug("[cacheUpdate] cannot find column", tableName, colName)
return ErrCacheFailed return ErrCacheFailed
} }

View File

@ -574,16 +574,15 @@ func (statement *Statement) getExpr() map[string]exprParam {
func (statement *Statement) col2NewColsWithQuote(columns ...string) []string { func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
newColumns := make([]string, 0) newColumns := make([]string, 0)
for _, col := range columns { for _, col := range columns {
col = strings.Replace(col, "`", "", -1) col = statement.Engine.removeQuotes(col)
col = strings.Replace(col, statement.Engine.QuoteStr(), "", -1)
ccols := strings.Split(col, ",") ccols := strings.Split(col, ",")
for _, c := range ccols { for _, c := range ccols {
fields := strings.Split(strings.TrimSpace(c), ".") fields := strings.Split(strings.TrimSpace(c), ".")
if len(fields) == 1 { if len(fields) == 1 {
newColumns = append(newColumns, statement.Engine.quote(fields[0])) newColumns = append(newColumns, statement.Engine.Quote(fields[0]))
} else if len(fields) == 2 { } else if len(fields) == 2 {
newColumns = append(newColumns, statement.Engine.quote(fields[0])+"."+ newColumns = append(newColumns, statement.Engine.Quote(fields[0])+"."+
statement.Engine.quote(fields[1])) statement.Engine.Quote(fields[1]))
} else { } else {
panic(errors.New("unwanted colnames")) panic(errors.New("unwanted colnames"))
} }
@ -620,7 +619,7 @@ func (statement *Statement) Cols(columns ...string) *Statement {
newColumns := statement.col2NewColsWithQuote(columns...) newColumns := statement.col2NewColsWithQuote(columns...)
statement.ColumnStr = strings.Join(newColumns, ", ") statement.ColumnStr = strings.Join(newColumns, ", ")
statement.ColumnStr = strings.Replace(statement.ColumnStr, statement.Engine.quote("*"), "*", -1) statement.ColumnStr = strings.Replace(statement.ColumnStr, statement.Engine.Quote("*"), "*", -1)
return statement return statement
} }
@ -836,7 +835,7 @@ func (statement *Statement) genIndexSQL() []string {
for idxName, index := range statement.RefTable.Indexes { for idxName, index := range statement.RefTable.Indexes {
if index.Type == core.IndexType { if index.Type == core.IndexType {
sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)), sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)),
quote(tbName), quote(strings.Join(index.Cols, quote(",")))) quote(tbName), quote(strings.Join(index.Cols, statement.Engine.reverseQuote(","))))
sqls = append(sqls, sql) sqls = append(sqls, sql)
} }
} }

View File

@ -26,7 +26,7 @@ var colStrTests = []struct {
} }
func TestColumnsStringGeneration(t *testing.T) { func TestColumnsStringGeneration(t *testing.T) {
if dbType == "postgres" || dbType == "mssql" { if dbType == "postgres" || dbType == "mssql" || *quote != int(QuoteAddAlways) {
return return
} }

View File

@ -24,6 +24,7 @@ var (
ptrConnStr = flag.String("conn_str", "", "test database connection string") ptrConnStr = flag.String("conn_str", "", "test database connection string")
mapType = flag.String("map_type", "snake", "indicate the name mapping") mapType = flag.String("map_type", "snake", "indicate the name mapping")
cache = flag.Bool("cache", false, "if enable cache") cache = flag.Bool("cache", false, "if enable cache")
quote = flag.Int("quote", int(QuoteAddAlways), "quote mode")
) )
func createEngine(dbType, connStr string) error { func createEngine(dbType, connStr string) error {
@ -36,7 +37,7 @@ func createEngine(dbType, connStr string) error {
testEngine.ShowSQL(*showSQL) testEngine.ShowSQL(*showSQL)
testEngine.logger.SetLevel(core.LOG_DEBUG) testEngine.logger.SetLevel(core.LOG_DEBUG)
//testEngine.QuoteMode = QuoteAddReserved testEngine.QuoteMode = QuoteMode(*quote)
} }
tables, err := testEngine.DBMetas() tables, err := testEngine.DBMetas()