From 5df1bc92dff719bc558db503ee3e3e370bf8a431 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 6 Mar 2016 12:05:20 +0800 Subject: [PATCH] resolved gogits/gogs#2743 --- VERSION | 2 +- engine.go | 40 +++++++++++++++++++++++++++++++++------- statement.go | 33 +++++++++++---------------------- xorm.go | 2 +- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/VERSION b/VERSION index 2107ad3c..63cbd99a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.5.2.0304 +xorm v0.5.2.0306 diff --git a/engine.go b/engine.go index 8cb13ec5..ba248cf0 100644 --- a/engine.go +++ b/engine.go @@ -123,16 +123,42 @@ func (engine *Engine) QuoteStr() string { // Use QuoteStr quote the string sql func (engine *Engine) Quote(sql string) string { - if len(sql) == 0 { - return sql - } - if string(sql[0]) == engine.dialect.QuoteStr() || sql[0] == '`' { - return sql - } - sql = strings.Replace(sql, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1) + return engine.quoteTable(sql) +} + +func (engine *Engine) quote(sql string) string { return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr() } +func (engine *Engine) quoteColumn(keyName string) string { + if len(keyName) == 0 { + return keyName + } + + keyName = strings.TrimSpace(keyName) + keyName = strings.Replace(keyName, "`", "", -1) + keyName = strings.Replace(keyName, engine.QuoteStr(), "", -1) + + keyName = strings.Replace(keyName, ",", engine.dialect.QuoteStr()+","+engine.dialect.QuoteStr(), -1) + + return engine.dialect.QuoteStr() + keyName + engine.dialect.QuoteStr() +} + +func (engine *Engine) quoteTable(keyName string) string { + keyName = strings.TrimSpace(keyName) + if len(keyName) == 0 { + return keyName + } + + if string(keyName[0]) == engine.dialect.QuoteStr() || keyName[0] == '`' { + return keyName + } + + keyName = strings.Replace(keyName, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1) + + return engine.dialect.QuoteStr() + keyName + engine.dialect.QuoteStr() +} + // A simple wrapper to dialect's core.SqlType method func (engine *Engine) SqlType(c *core.Column) string { return engine.dialect.SqlType(c) diff --git a/statement.go b/statement.go index 06b72739..a1d3dd43 100644 --- a/statement.go +++ b/statement.go @@ -772,7 +772,7 @@ func (statement *Statement) genInSql() (string, []interface{}) { for _, params := range statement.inColumns { buf.Reset() fmt.Fprintf(&buf, "(%v IN (%v))", - statement.Engine.autoQuote(params.colName), + statement.Engine.quoteColumn(params.colName), strings.Join(makeArray("?", len(params.args)), ",")) inStrs[i] = buf.String() i++ @@ -809,16 +809,6 @@ func col2NewCols(columns ...string) []string { return newColumns } -func (engine *Engine) autoQuote(col string) string { - col = strings.Replace(col, "`", "", -1) - col = strings.Replace(col, engine.QuoteStr(), "", -1) - fields := strings.Split(strings.TrimSpace(col), ".") - for i, field := range fields { - fields[i] = engine.Quote(field) - } - return strings.Join(fields, ".") -} - func (statement *Statement) col2NewColsWithQuote(columns ...string) []string { newColumns := make([]string, 0) for _, col := range columns { @@ -828,10 +818,10 @@ func (statement *Statement) col2NewColsWithQuote(columns ...string) []string { for _, c := range ccols { fields := strings.Split(strings.TrimSpace(c), ".") 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 { - newColumns = append(newColumns, statement.Engine.Quote(fields[0])+"."+ - statement.Engine.Quote(fields[1])) + newColumns = append(newColumns, statement.Engine.quote(fields[0])+"."+ + statement.Engine.quote(fields[1])) } else { panic(errors.New("unwanted colnames")) } @@ -861,16 +851,15 @@ func (s *Statement) Select(str string) *Statement { // Generate "col1, col2" statement func (statement *Statement) Cols(columns ...string) *Statement { - newColumns := col2NewCols(columns...) - for _, nc := range newColumns { + cols := col2NewCols(columns...) + for _, nc := range cols { statement.columnMap[strings.ToLower(nc)] = true } - statement.ColumnStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", "))) - if strings.Contains(statement.ColumnStr, ".") { - statement.ColumnStr = strings.Replace(statement.ColumnStr, ".", - statement.Engine.dialect.QuoteStr()+"."+statement.Engine.dialect.QuoteStr(), -1) - } - statement.ColumnStr = strings.Replace(statement.ColumnStr, statement.Engine.Quote("*"), "*", -1) + + newColumns := statement.col2NewColsWithQuote(columns...) + fmt.Println("=====", columns, newColumns, cols) + statement.ColumnStr = strings.Join(newColumns, ", ") + statement.ColumnStr = strings.Replace(statement.ColumnStr, statement.Engine.quote("*"), "*", -1) return statement } diff --git a/xorm.go b/xorm.go index d0a88ab2..04a4fb53 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.5.2.0304" + Version string = "0.5.2.0306" ) func regDrvsNDialects() bool {