resolved gogits/gogs#2743
This commit is contained in:
parent
d92dc9d373
commit
5df1bc92df
40
engine.go
40
engine.go
|
@ -123,16 +123,42 @@ func (engine *Engine) QuoteStr() string {
|
||||||
|
|
||||||
// Use QuoteStr quote the string sql
|
// Use QuoteStr quote the string sql
|
||||||
func (engine *Engine) Quote(sql string) string {
|
func (engine *Engine) Quote(sql string) string {
|
||||||
if len(sql) == 0 {
|
return engine.quoteTable(sql)
|
||||||
return sql
|
}
|
||||||
}
|
|
||||||
if string(sql[0]) == engine.dialect.QuoteStr() || sql[0] == '`' {
|
func (engine *Engine) quote(sql string) string {
|
||||||
return sql
|
|
||||||
}
|
|
||||||
sql = strings.Replace(sql, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1)
|
|
||||||
return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr()
|
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
|
// A simple wrapper to dialect's core.SqlType method
|
||||||
func (engine *Engine) SqlType(c *core.Column) string {
|
func (engine *Engine) SqlType(c *core.Column) string {
|
||||||
return engine.dialect.SqlType(c)
|
return engine.dialect.SqlType(c)
|
||||||
|
|
33
statement.go
33
statement.go
|
@ -772,7 +772,7 @@ func (statement *Statement) genInSql() (string, []interface{}) {
|
||||||
for _, params := range statement.inColumns {
|
for _, params := range statement.inColumns {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
fmt.Fprintf(&buf, "(%v IN (%v))",
|
fmt.Fprintf(&buf, "(%v IN (%v))",
|
||||||
statement.Engine.autoQuote(params.colName),
|
statement.Engine.quoteColumn(params.colName),
|
||||||
strings.Join(makeArray("?", len(params.args)), ","))
|
strings.Join(makeArray("?", len(params.args)), ","))
|
||||||
inStrs[i] = buf.String()
|
inStrs[i] = buf.String()
|
||||||
i++
|
i++
|
||||||
|
@ -809,16 +809,6 @@ func col2NewCols(columns ...string) []string {
|
||||||
return newColumns
|
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 {
|
func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
|
||||||
newColumns := make([]string, 0)
|
newColumns := make([]string, 0)
|
||||||
for _, col := range columns {
|
for _, col := range columns {
|
||||||
|
@ -828,10 +818,10 @@ func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
|
||||||
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"))
|
||||||
}
|
}
|
||||||
|
@ -861,16 +851,15 @@ func (s *Statement) Select(str string) *Statement {
|
||||||
|
|
||||||
// Generate "col1, col2" statement
|
// Generate "col1, col2" statement
|
||||||
func (statement *Statement) Cols(columns ...string) *Statement {
|
func (statement *Statement) Cols(columns ...string) *Statement {
|
||||||
newColumns := col2NewCols(columns...)
|
cols := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
for _, nc := range cols {
|
||||||
statement.columnMap[strings.ToLower(nc)] = true
|
statement.columnMap[strings.ToLower(nc)] = true
|
||||||
}
|
}
|
||||||
statement.ColumnStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
|
|
||||||
if strings.Contains(statement.ColumnStr, ".") {
|
newColumns := statement.col2NewColsWithQuote(columns...)
|
||||||
statement.ColumnStr = strings.Replace(statement.ColumnStr, ".",
|
fmt.Println("=====", columns, newColumns, cols)
|
||||||
statement.Engine.dialect.QuoteStr()+"."+statement.Engine.dialect.QuoteStr(), -1)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue