small optimizations for statement string generation
This commit is contained in:
parent
d776f5da3e
commit
81d7f834b1
128
statement.go
128
statement.go
|
@ -5,6 +5,7 @@
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -143,9 +144,11 @@ func (statement *Statement) Where(querystring string, args ...interface{}) *Stat
|
||||||
|
|
||||||
// add Where & and statment
|
// add Where & and statment
|
||||||
func (statement *Statement) And(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) And(querystring string, args ...interface{}) *Statement {
|
||||||
if statement.WhereStr != "" {
|
if len(statement.WhereStr) > 0 {
|
||||||
statement.WhereStr = fmt.Sprintf("(%v) %s (%v)", statement.WhereStr,
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprintf(&buf, "(%v) %s (%v)", statement.WhereStr,
|
||||||
statement.Engine.dialect.AndStr(), querystring)
|
statement.Engine.dialect.AndStr(), querystring)
|
||||||
|
statement.WhereStr = buf.String()
|
||||||
} else {
|
} else {
|
||||||
statement.WhereStr = querystring
|
statement.WhereStr = querystring
|
||||||
}
|
}
|
||||||
|
@ -155,9 +158,11 @@ func (statement *Statement) And(querystring string, args ...interface{}) *Statem
|
||||||
|
|
||||||
// add Where & Or statment
|
// add Where & Or statment
|
||||||
func (statement *Statement) Or(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) Or(querystring string, args ...interface{}) *Statement {
|
||||||
if statement.WhereStr != "" {
|
if len(statement.WhereStr) > 0 {
|
||||||
statement.WhereStr = fmt.Sprintf("(%v) %s (%v)", statement.WhereStr,
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprintf(&buf, "(%v) %s (%v)", statement.WhereStr,
|
||||||
statement.Engine.dialect.OrStr(), querystring)
|
statement.Engine.dialect.OrStr(), querystring)
|
||||||
|
statement.WhereStr = buf.String()
|
||||||
} else {
|
} else {
|
||||||
statement.WhereStr = querystring
|
statement.WhereStr = querystring
|
||||||
}
|
}
|
||||||
|
@ -727,12 +732,17 @@ func (statement *Statement) genInSql() (string, []interface{}) {
|
||||||
return "", []interface{}{}
|
return "", []interface{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
inStrs := make([]string, 0, len(statement.inColumns))
|
inStrs := make([]string, len(statement.inColumns), len(statement.inColumns))
|
||||||
args := make([]interface{}, 0)
|
args := make([]interface{}, 0)
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var i int
|
||||||
for _, params := range statement.inColumns {
|
for _, params := range statement.inColumns {
|
||||||
inStrs = append(inStrs, fmt.Sprintf("(%v IN (%v))",
|
buf.Reset()
|
||||||
|
fmt.Fprintf(&buf, "(%v IN (%v))",
|
||||||
statement.Engine.autoQuote(params.colName),
|
statement.Engine.autoQuote(params.colName),
|
||||||
strings.Join(makeArray("?", len(params.args)), ",")))
|
strings.Join(makeArray("?", len(params.args)), ","))
|
||||||
|
inStrs[i] = buf.String()
|
||||||
|
i++
|
||||||
args = append(args, params.args...)
|
args = append(args, params.args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -745,7 +755,7 @@ func (statement *Statement) genInSql() (string, []interface{}) {
|
||||||
func (statement *Statement) attachInSql() {
|
func (statement *Statement) attachInSql() {
|
||||||
inSql, inArgs := statement.genInSql()
|
inSql, inArgs := statement.genInSql()
|
||||||
if len(inSql) > 0 {
|
if len(inSql) > 0 {
|
||||||
if statement.ConditionStr != "" {
|
if len(statement.ConditionStr) > 0 {
|
||||||
statement.ConditionStr += " " + statement.Engine.dialect.AndStr() + " "
|
statement.ConditionStr += " " + statement.Engine.dialect.AndStr() + " "
|
||||||
}
|
}
|
||||||
statement.ConditionStr += inSql
|
statement.ConditionStr += inSql
|
||||||
|
@ -845,15 +855,6 @@ func (statement *Statement) MustCols(columns ...string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update use only: not update columns
|
|
||||||
/*func (statement *Statement) NotCols(columns ...string) *Statement {
|
|
||||||
newColumns := col2NewCols(columns...)
|
|
||||||
for _, nc := range newColumns {
|
|
||||||
statement.mustColumnMap[strings.ToLower(nc)] = false
|
|
||||||
}
|
|
||||||
return statement
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// indicates that use bool fields as update contents and query contiditions
|
// indicates that use bool fields as update contents and query contiditions
|
||||||
func (statement *Statement) UseBool(columns ...string) *Statement {
|
func (statement *Statement) UseBool(columns ...string) *Statement {
|
||||||
if len(columns) > 0 {
|
if len(columns) > 0 {
|
||||||
|
@ -898,7 +899,7 @@ func (statement *Statement) Limit(limit int, start ...int) *Statement {
|
||||||
|
|
||||||
// Generate "Order By order" statement
|
// Generate "Order By order" statement
|
||||||
func (statement *Statement) OrderBy(order string) *Statement {
|
func (statement *Statement) OrderBy(order string) *Statement {
|
||||||
if statement.OrderStr != "" {
|
if len(statement.OrderStr) > 0 {
|
||||||
statement.OrderStr += ", "
|
statement.OrderStr += ", "
|
||||||
}
|
}
|
||||||
statement.OrderStr += order
|
statement.OrderStr += order
|
||||||
|
@ -906,44 +907,51 @@ func (statement *Statement) OrderBy(order string) *Statement {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) Desc(colNames ...string) *Statement {
|
func (statement *Statement) Desc(colNames ...string) *Statement {
|
||||||
if statement.OrderStr != "" {
|
var buf bytes.Buffer
|
||||||
statement.OrderStr += ", "
|
fmt.Fprintf(&buf, statement.OrderStr)
|
||||||
|
if len(statement.OrderStr) > 0 {
|
||||||
|
fmt.Fprint(&buf, ", ")
|
||||||
}
|
}
|
||||||
newColNames := statement.col2NewColsWithQuote(colNames...)
|
newColNames := statement.col2NewColsWithQuote(colNames...)
|
||||||
sqlStr := strings.Join(newColNames, " DESC, ")
|
fmt.Fprintf(&buf, "%q DESC", strings.Join(newColNames, " DESC, "))
|
||||||
statement.OrderStr += sqlStr + " DESC"
|
statement.OrderStr = buf.String()
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Asc provide asc order by query condition, the input parameters are columns.
|
// Method Asc provide asc order by query condition, the input parameters are columns.
|
||||||
func (statement *Statement) Asc(colNames ...string) *Statement {
|
func (statement *Statement) Asc(colNames ...string) *Statement {
|
||||||
if statement.OrderStr != "" {
|
var buf bytes.Buffer
|
||||||
statement.OrderStr += ", "
|
fmt.Fprintf(&buf, statement.OrderStr)
|
||||||
|
if len(statement.OrderStr) > 0 {
|
||||||
|
fmt.Fprint(&buf, ", ")
|
||||||
}
|
}
|
||||||
newColNames := statement.col2NewColsWithQuote(colNames...)
|
newColNames := statement.col2NewColsWithQuote(colNames...)
|
||||||
sqlStr := strings.Join(newColNames, " ASC, ")
|
fmt.Fprintf(&buf, "%q ASC", strings.Join(newColNames, " ASC, "))
|
||||||
statement.OrderStr += sqlStr + " ASC"
|
statement.OrderStr = buf.String()
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(join_operator string, tablename interface{}, condition string) *Statement {
|
func (statement *Statement) Join(join_operator string, tablename interface{}, condition string) *Statement {
|
||||||
var joinTable string
|
var buf bytes.Buffer
|
||||||
|
if len(statement.JoinStr) > 0 {
|
||||||
|
fmt.Fprintf(&buf, "%q %v JOIN ", statement.JoinStr, join_operator)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(&buf, "%v JOIN ", join_operator)
|
||||||
|
}
|
||||||
|
|
||||||
switch tablename.(type) {
|
switch tablename.(type) {
|
||||||
case []string:
|
case []string:
|
||||||
t := tablename.([]string)
|
t := tablename.([]string)
|
||||||
l := len(t)
|
if len(t) > 1 {
|
||||||
if l > 1 {
|
fmt.Fprintf(&buf, "%q AS %q", statement.Engine.Quote(t[0]), statement.Engine.Quote(t[1]))
|
||||||
table := t[0]
|
} else if len(t) == 1 {
|
||||||
joinTable = statement.Engine.Quote(table) + " AS " + statement.Engine.Quote(t[1])
|
fmt.Fprintf(&buf, statement.Engine.Quote(t[0]))
|
||||||
} else if l == 1 {
|
|
||||||
table := t[0]
|
|
||||||
joinTable = statement.Engine.Quote(table)
|
|
||||||
}
|
}
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
t := tablename.([]interface{})
|
t := tablename.([]interface{})
|
||||||
l := len(t)
|
l := len(t)
|
||||||
table := ""
|
var table string
|
||||||
if l > 0 {
|
if l > 0 {
|
||||||
f := t[0]
|
f := t[0]
|
||||||
v := rValue(f)
|
v := rValue(f)
|
||||||
|
@ -956,21 +964,17 @@ func (statement *Statement) Join(join_operator string, tablename interface{}, co
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if l > 1 {
|
if l > 1 {
|
||||||
joinTable = statement.Engine.Quote(table) + " AS " + statement.Engine.Quote(fmt.Sprintf("%v", t[1]))
|
fmt.Fprintf(&buf, "%q AS %q", statement.Engine.Quote(table),
|
||||||
|
statement.Engine.Quote(fmt.Sprintf("%v", t[1])))
|
||||||
} else if l == 1 {
|
} else if l == 1 {
|
||||||
joinTable = statement.Engine.Quote(table)
|
fmt.Fprintf(&buf, statement.Engine.Quote(table))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
t := fmt.Sprintf("%v", tablename)
|
fmt.Fprintf(&buf, statement.Engine.Quote(fmt.Sprintf("%v", tablename)))
|
||||||
joinTable = statement.Engine.Quote(t)
|
|
||||||
}
|
|
||||||
if statement.JoinStr != "" {
|
|
||||||
statement.JoinStr = statement.JoinStr + fmt.Sprintf(" %v JOIN %v ON %v", join_operator,
|
|
||||||
joinTable, condition)
|
|
||||||
} else {
|
|
||||||
statement.JoinStr = fmt.Sprintf("%v JOIN %v ON %v", join_operator,
|
|
||||||
joinTable, condition)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(&buf, " ON %v", condition)
|
||||||
|
statement.JoinStr = buf.String()
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1087,11 +1091,6 @@ func (s *Statement) genDelIndexSQL() []string {
|
||||||
return sqls
|
return sqls
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func (s *Statement) genDropSQL() string {
|
|
||||||
return s.Engine.dialect.MustDropTa(s.TableName()) + ";"
|
|
||||||
}*/
|
|
||||||
|
|
||||||
func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) {
|
func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) {
|
||||||
var table *core.Table
|
var table *core.Table
|
||||||
if statement.RefTable == nil {
|
if statement.RefTable == nil {
|
||||||
|
@ -1191,20 +1190,22 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
var mssqlCondi string
|
var mssqlCondi string
|
||||||
|
|
||||||
statement.processIdParam()
|
statement.processIdParam()
|
||||||
var whereStr string
|
|
||||||
if statement.WhereStr != "" {
|
var buf bytes.Buffer
|
||||||
if statement.ConditionStr != "" {
|
if len(statement.WhereStr) > 0 {
|
||||||
whereStr = fmt.Sprintf(" WHERE (%v)", statement.WhereStr)
|
if len(statement.ConditionStr) > 0 {
|
||||||
|
fmt.Fprintf(&buf, " WHERE (%v)", statement.WhereStr)
|
||||||
} else {
|
} else {
|
||||||
whereStr = fmt.Sprintf(" WHERE %v", statement.WhereStr)
|
fmt.Fprintf(&buf, " WHERE %v", statement.WhereStr)
|
||||||
}
|
}
|
||||||
if statement.ConditionStr != "" {
|
if statement.ConditionStr != "" {
|
||||||
whereStr = fmt.Sprintf("%v %s (%v)", whereStr, dialect.AndStr(),
|
fmt.Fprintf(&buf, " %s (%v)", dialect.AndStr(), statement.ConditionStr)
|
||||||
statement.ConditionStr)
|
|
||||||
}
|
}
|
||||||
} else if statement.ConditionStr != "" {
|
} else if len(statement.ConditionStr) > 0 {
|
||||||
whereStr = fmt.Sprintf(" WHERE %v", statement.ConditionStr)
|
fmt.Fprintf(&buf, " WHERE %v", statement.ConditionStr)
|
||||||
}
|
}
|
||||||
|
var whereStr = buf.String()
|
||||||
|
|
||||||
var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName())
|
var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName())
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
if dialect.DBType() == core.ORACLE {
|
if dialect.DBType() == core.ORACLE {
|
||||||
|
@ -1248,10 +1249,9 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
||||||
a = fmt.Sprintf("SELECT %v%v%v%v%v", top, distinct, columnStr,
|
a = fmt.Sprintf("SELECT %v%v%v%v%v", top, distinct, columnStr, fromStr, whereStr)
|
||||||
fromStr, whereStr)
|
if len(mssqlCondi) > 0 {
|
||||||
if mssqlCondi != "" {
|
if len(whereStr) > 0 {
|
||||||
if whereStr != "" {
|
|
||||||
a += " AND " + mssqlCondi
|
a += " AND " + mssqlCondi
|
||||||
} else {
|
} else {
|
||||||
a += " WHERE " + mssqlCondi
|
a += " WHERE " + mssqlCondi
|
||||||
|
|
Loading…
Reference in New Issue