|
|
@@ -14,14 +14,10 @@ import (
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/go-xorm/builder"
|
|
|
"github.com/xormplus/core"
|
|
|
)
|
|
|
|
|
|
-type inParam struct {
|
|
|
- colName string
|
|
|
- args []interface{}
|
|
|
-}
|
|
|
-
|
|
|
type incrParam struct {
|
|
|
colName string
|
|
|
arg interface{}
|
|
|
@@ -43,10 +39,7 @@ type Statement struct {
|
|
|
Engine *Engine
|
|
|
Start int
|
|
|
LimitN int
|
|
|
- WhereStr string
|
|
|
IdParam *core.PK
|
|
|
- Params []interface{}
|
|
|
- inParams []interface{}
|
|
|
OrderStr string
|
|
|
JoinStr string
|
|
|
joinArgs []interface{}
|
|
|
@@ -57,7 +50,6 @@ type Statement struct {
|
|
|
columnMap map[string]bool
|
|
|
useAllCols bool
|
|
|
OmitStr string
|
|
|
- ConditionStr string
|
|
|
AltTableName string
|
|
|
tableName string
|
|
|
RawSQL string
|
|
|
@@ -66,7 +58,6 @@ type Statement struct {
|
|
|
UseAutoJoin bool
|
|
|
StoreEngine string
|
|
|
Charset string
|
|
|
- BeanArgs []interface{}
|
|
|
UseCache bool
|
|
|
UseAutoTime bool
|
|
|
noAutoCondition bool
|
|
|
@@ -78,10 +69,10 @@ type Statement struct {
|
|
|
unscoped bool
|
|
|
mustColumnMap map[string]bool
|
|
|
nullableMap map[string]bool
|
|
|
- inColumns map[string]*inParam
|
|
|
incrColumns map[string]incrParam
|
|
|
decrColumns map[string]decrParam
|
|
|
exprColumns map[string]exprParam
|
|
|
+ cond builder.Cond
|
|
|
}
|
|
|
|
|
|
// Init reset all the statment's fields
|
|
|
@@ -89,9 +80,6 @@ func (statement *Statement) Init() {
|
|
|
statement.RefTable = nil
|
|
|
statement.Start = 0
|
|
|
statement.LimitN = 0
|
|
|
- statement.WhereStr = ""
|
|
|
- statement.Params = make([]interface{}, 0)
|
|
|
- statement.inParams = make([]interface{}, 0)
|
|
|
statement.OrderStr = ""
|
|
|
statement.UseCascade = true
|
|
|
statement.JoinStr = ""
|
|
|
@@ -101,13 +89,11 @@ func (statement *Statement) Init() {
|
|
|
statement.ColumnStr = ""
|
|
|
statement.OmitStr = ""
|
|
|
statement.columnMap = make(map[string]bool)
|
|
|
- statement.ConditionStr = ""
|
|
|
statement.AltTableName = ""
|
|
|
statement.tableName = ""
|
|
|
statement.IdParam = nil
|
|
|
statement.RawSQL = ""
|
|
|
statement.RawParams = make([]interface{}, 0)
|
|
|
- statement.BeanArgs = make([]interface{}, 0)
|
|
|
statement.UseCache = true
|
|
|
statement.UseAutoTime = true
|
|
|
statement.noAutoCondition = false
|
|
|
@@ -121,10 +107,10 @@ func (statement *Statement) Init() {
|
|
|
statement.nullableMap = make(map[string]bool)
|
|
|
statement.checkVersion = true
|
|
|
statement.unscoped = false
|
|
|
- statement.inColumns = make(map[string]*inParam)
|
|
|
statement.incrColumns = make(map[string]incrParam)
|
|
|
statement.decrColumns = make(map[string]decrParam)
|
|
|
statement.exprColumns = make(map[string]exprParam)
|
|
|
+ statement.cond = builder.NewCond()
|
|
|
}
|
|
|
|
|
|
// NoAutoCondition if you do not want convert bean's field as query condition, then use this function
|
|
|
@@ -136,6 +122,12 @@ func (statement *Statement) NoAutoCondition(no ...bool) *Statement {
|
|
|
return statement
|
|
|
}
|
|
|
|
|
|
+// Alias set the table alias
|
|
|
+func (statement *Statement) Alias(alias string) *Statement {
|
|
|
+ statement.TableAlias = alias
|
|
|
+ return statement
|
|
|
+}
|
|
|
+
|
|
|
// Sql add the raw sql statement
|
|
|
func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement {
|
|
|
statement.RawSQL = querystring
|
|
|
@@ -143,52 +135,71 @@ func (statement *Statement) Sql(querystring string, args ...interface{}) *Statem
|
|
|
return statement
|
|
|
}
|
|
|
|
|
|
-// Alias set the table alias
|
|
|
-func (statement *Statement) Alias(alias string) *Statement {
|
|
|
- statement.TableAlias = alias
|
|
|
- return statement
|
|
|
+// Where add Where statment
|
|
|
+func (statement *Statement) Where(query interface{}, args ...interface{}) *Statement {
|
|
|
+ return statement.And(query, args...)
|
|
|
}
|
|
|
|
|
|
-// Where add Where statment
|
|
|
-func (statement *Statement) Where(querystring string, args ...interface{}) *Statement {
|
|
|
- // The second where will be triggered as And
|
|
|
- if len(statement.WhereStr) > 0 {
|
|
|
- return statement.And(querystring, args...)
|
|
|
+// And add Where & and statment
|
|
|
+func (statement *Statement) And(query interface{}, args ...interface{}) *Statement {
|
|
|
+ switch query.(type) {
|
|
|
+ case string:
|
|
|
+ cond := builder.Expr(query.(string), args...)
|
|
|
+ statement.cond = statement.cond.And(cond)
|
|
|
+ case builder.Cond:
|
|
|
+ cond := query.(builder.Cond)
|
|
|
+ statement.cond = statement.cond.And(cond)
|
|
|
+ for _, v := range args {
|
|
|
+ if vv, ok := v.(builder.Cond); ok {
|
|
|
+ statement.cond = statement.cond.And(vv)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ // TODO: not support condition type
|
|
|
}
|
|
|
|
|
|
- if !strings.Contains(querystring, statement.Engine.dialect.EqStr()) {
|
|
|
- querystring = strings.Replace(querystring, "=", statement.Engine.dialect.EqStr(), -1)
|
|
|
+ return statement
|
|
|
+}
|
|
|
+
|
|
|
+// Or add Where & Or statment
|
|
|
+func (statement *Statement) Or(query interface{}, args ...interface{}) *Statement {
|
|
|
+ switch query.(type) {
|
|
|
+ case string:
|
|
|
+ cond := builder.Expr(query.(string), args...)
|
|
|
+ statement.cond = statement.cond.Or(cond)
|
|
|
+ case builder.Cond:
|
|
|
+ cond := query.(builder.Cond)
|
|
|
+ statement.cond = statement.cond.Or(cond)
|
|
|
+ for _, v := range args {
|
|
|
+ if vv, ok := v.(builder.Cond); ok {
|
|
|
+ statement.cond = statement.cond.Or(vv)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ // TODO: not support condition type
|
|
|
}
|
|
|
- statement.WhereStr = querystring
|
|
|
- statement.Params = args
|
|
|
return statement
|
|
|
}
|
|
|
|
|
|
-// And add Where & and statment
|
|
|
-func (statement *Statement) And(querystring string, args ...interface{}) *Statement {
|
|
|
- if len(statement.WhereStr) > 0 {
|
|
|
- var buf bytes.Buffer
|
|
|
- fmt.Fprintf(&buf, "(%v) %s (%v)", statement.WhereStr,
|
|
|
- statement.Engine.dialect.AndStr(), querystring)
|
|
|
- statement.WhereStr = buf.String()
|
|
|
- } else {
|
|
|
- statement.WhereStr = querystring
|
|
|
+// In generate "Where column IN (?) " statment
|
|
|
+func (statement *Statement) In(column string, args ...interface{}) *Statement {
|
|
|
+ if len(args) == 0 {
|
|
|
+ return statement
|
|
|
}
|
|
|
- statement.Params = append(statement.Params, args...)
|
|
|
+
|
|
|
+ in := builder.In(column, args...)
|
|
|
+ statement.cond = statement.cond.And(in)
|
|
|
return statement
|
|
|
}
|
|
|
|
|
|
-// Or add Where & Or statment
|
|
|
-func (statement *Statement) Or(querystring string, args ...interface{}) *Statement {
|
|
|
- if len(statement.WhereStr) > 0 {
|
|
|
- var buf bytes.Buffer
|
|
|
- fmt.Fprintf(&buf, "(%v) %s (%v)", statement.WhereStr,
|
|
|
- statement.Engine.dialect.OrStr(), querystring)
|
|
|
- statement.WhereStr = buf.String()
|
|
|
- } else {
|
|
|
- statement.WhereStr = querystring
|
|
|
+// NotIn generate "Where column IN (?) " statment
|
|
|
+func (statement *Statement) NotIn(column string, args ...interface{}) *Statement {
|
|
|
+ if len(args) == 0 {
|
|
|
+ return statement
|
|
|
}
|
|
|
- statement.Params = append(statement.Params, args...)
|
|
|
+
|
|
|
+ in := builder.NotIn(column, args...)
|
|
|
+ statement.cond = statement.cond.And(in)
|
|
|
return statement
|
|
|
}
|
|
|
|
|
|
@@ -459,13 +470,11 @@ func (statement *Statement) colName(col *core.Column, tableName string) string {
|
|
|
return statement.Engine.Quote(col.Name)
|
|
|
}
|
|
|
|
|
|
-// Auto generating conditions according a struct
|
|
|
-func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|
|
+func buildConds(engine *Engine, table *core.Table, bean interface{},
|
|
|
includeVersion bool, includeUpdated bool, includeNil bool,
|
|
|
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
|
|
- mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) ([]string, []interface{}) {
|
|
|
- var colNames []string
|
|
|
- var args = make([]interface{}, 0)
|
|
|
+ mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) (builder.Cond, error) {
|
|
|
+ var conds []builder.Cond
|
|
|
for _, col := range table.Columns() {
|
|
|
if !includeVersion && col.IsVersion {
|
|
|
continue
|
|
|
@@ -502,8 +511,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|
|
}
|
|
|
|
|
|
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
|
|
- colNames = append(colNames, fmt.Sprintf("(%v IS NULL OR %v = '0001-01-01 00:00:00')",
|
|
|
- colName, colName))
|
|
|
+ conds = append(conds, builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"}))
|
|
|
}
|
|
|
|
|
|
fieldValue := *fieldValuePtr
|
|
|
@@ -524,8 +532,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|
|
if fieldType.Kind() == reflect.Ptr {
|
|
|
if fieldValue.IsNil() {
|
|
|
if includeNil {
|
|
|
- args = append(args, nil)
|
|
|
- colNames = append(colNames, fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr()))
|
|
|
+ conds = append(conds, builder.Eq{colName: nil})
|
|
|
}
|
|
|
continue
|
|
|
} else if !fieldValue.IsValid() {
|
|
|
@@ -668,17 +675,10 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|
|
val = fieldValue.Interface()
|
|
|
}
|
|
|
|
|
|
- args = append(args, val)
|
|
|
- var condi string
|
|
|
- if col.IsPrimaryKey && engine.dialect.DBType() == "ql" {
|
|
|
- condi = "id() == ?"
|
|
|
- } else {
|
|
|
- condi = fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr())
|
|
|
- }
|
|
|
- colNames = append(colNames, condi)
|
|
|
+ conds = append(conds, builder.Eq{colName: val})
|
|
|
}
|
|
|
|
|
|
- return colNames, args
|
|
|
+ return builder.And(conds...), nil
|
|
|
}
|
|
|
|
|
|
// TableName return current tableName
|
|
|
@@ -762,70 +762,6 @@ func (statement *Statement) getExpr() map[string]exprParam {
|
|
|
return statement.exprColumns
|
|
|
}
|
|
|
|
|
|
-// In generate "Where column IN (?) " statment
|
|
|
-func (statement *Statement) In(column string, args ...interface{}) *Statement {
|
|
|
- length := len(args)
|
|
|
- if length == 0 {
|
|
|
- return statement
|
|
|
- }
|
|
|
-
|
|
|
- k := strings.ToLower(column)
|
|
|
- var newargs []interface{}
|
|
|
- if length == 1 &&
|
|
|
- reflect.TypeOf(args[0]).Kind() == reflect.Slice {
|
|
|
- newargs = make([]interface{}, 0)
|
|
|
- v := reflect.ValueOf(args[0])
|
|
|
- for i := 0; i < v.Len(); i++ {
|
|
|
- newargs = append(newargs, v.Index(i).Interface())
|
|
|
- }
|
|
|
- } else {
|
|
|
- newargs = args
|
|
|
- }
|
|
|
-
|
|
|
- if _, ok := statement.inColumns[k]; ok {
|
|
|
- statement.inColumns[k].args = append(statement.inColumns[k].args, newargs...)
|
|
|
- } else {
|
|
|
- statement.inColumns[k] = &inParam{column, newargs}
|
|
|
- }
|
|
|
- return statement
|
|
|
-}
|
|
|
-
|
|
|
-func (statement *Statement) genInSql() (string, []interface{}) {
|
|
|
- if len(statement.inColumns) == 0 {
|
|
|
- return "", []interface{}{}
|
|
|
- }
|
|
|
-
|
|
|
- inStrs := make([]string, len(statement.inColumns), len(statement.inColumns))
|
|
|
- args := make([]interface{}, 0, len(statement.inColumns))
|
|
|
- var buf bytes.Buffer
|
|
|
- var i int
|
|
|
- for _, params := range statement.inColumns {
|
|
|
- buf.Reset()
|
|
|
- fmt.Fprintf(&buf, "(%v IN (%v))",
|
|
|
- statement.Engine.quoteColumn(params.colName),
|
|
|
- strings.Join(makeArray("?", len(params.args)), ","))
|
|
|
- inStrs[i] = buf.String()
|
|
|
- i++
|
|
|
- args = append(args, params.args...)
|
|
|
- }
|
|
|
-
|
|
|
- if len(statement.inColumns) == 1 {
|
|
|
- return inStrs[0], args
|
|
|
- }
|
|
|
- return fmt.Sprintf("(%v)", strings.Join(inStrs, " "+statement.Engine.dialect.AndStr()+" ")), args
|
|
|
-}
|
|
|
-
|
|
|
-func (statement *Statement) attachInSql() {
|
|
|
- inSql, inArgs := statement.genInSql()
|
|
|
- if len(inSql) > 0 {
|
|
|
- if len(statement.ConditionStr) > 0 {
|
|
|
- statement.ConditionStr += " " + statement.Engine.dialect.AndStr() + " "
|
|
|
- }
|
|
|
- statement.ConditionStr += inSql
|
|
|
- statement.inParams = inArgs
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
|
|
|
newColumns := make([]string, 0)
|
|
|
for _, col := range columns {
|
|
|
@@ -1128,19 +1064,39 @@ func (s *Statement) genDelIndexSQL() []string {
|
|
|
return sqls
|
|
|
}
|
|
|
|
|
|
-func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) {
|
|
|
- statement.setRefValue(rValue(bean))
|
|
|
+func (s *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) {
|
|
|
+ quote := s.Engine.Quote
|
|
|
+ sql := fmt.Sprintf("ALTER TABLE %v ADD %v;", quote(s.TableName()),
|
|
|
+ col.String(s.Engine.dialect))
|
|
|
+ return sql, []interface{}{}
|
|
|
+}
|
|
|
|
|
|
+func (statement *Statement) buildConds(table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) (builder.Cond, error) {
|
|
|
+ return buildConds(statement.Engine, table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols,
|
|
|
+ statement.unscoped, statement.mustColumnMap, statement.TableName(), statement.TableAlias, addedTableName)
|
|
|
+}
|
|
|
+
|
|
|
+func (statement *Statement) genConds(bean interface{}) (string, []interface{}, error) {
|
|
|
var table = statement.RefTable
|
|
|
var addedTableName = (len(statement.JoinStr) > 0)
|
|
|
|
|
|
+ var autoCond builder.Cond
|
|
|
if !statement.noAutoCondition {
|
|
|
- colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName)
|
|
|
-
|
|
|
- statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
|
|
|
- statement.BeanArgs = args
|
|
|
+ var err error
|
|
|
+ autoCond, err = statement.buildConds(table, bean, true, true, false, true, addedTableName)
|
|
|
+ if err != nil {
|
|
|
+ return "", nil, err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ statement.processIdParam()
|
|
|
+
|
|
|
+ return builder.ToSQL(statement.cond.And(autoCond))
|
|
|
+}
|
|
|
+
|
|
|
+func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}) {
|
|
|
+ statement.setRefValue(rValue(bean))
|
|
|
+
|
|
|
var columnStr = statement.ColumnStr
|
|
|
if len(statement.selectStr) > 0 {
|
|
|
columnStr = statement.selectStr
|
|
|
@@ -1165,67 +1121,33 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- statement.attachInSql() // !admpub! fix bug:Iterate func missing "... IN (...)"
|
|
|
- return statement.genSelectSQL(columnStr), append(append(append(statement.joinArgs, statement.Params...),
|
|
|
- statement.BeanArgs...), statement.inParams...)
|
|
|
-}
|
|
|
-
|
|
|
-func (s *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) {
|
|
|
- quote := s.Engine.Quote
|
|
|
- sql := fmt.Sprintf("ALTER TABLE %v ADD %v;", quote(s.TableName()),
|
|
|
- col.String(s.Engine.dialect))
|
|
|
- return sql, []interface{}{}
|
|
|
-}
|
|
|
+ condSQL, condArgs, _ := statement.genConds(bean)
|
|
|
|
|
|
-func (statement *Statement) buildConditions(table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) ([]string, []interface{}) {
|
|
|
- return buildConditions(statement.Engine, table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols,
|
|
|
- statement.unscoped, statement.mustColumnMap, statement.TableName(), statement.TableAlias, addedTableName)
|
|
|
+ return statement.genSelectSQL(columnStr, condSQL), append(statement.joinArgs, condArgs...)
|
|
|
}
|
|
|
|
|
|
-func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}) {
|
|
|
+func (statement *Statement) genCountSQL(bean interface{}) (string, []interface{}) {
|
|
|
statement.setRefValue(rValue(bean))
|
|
|
|
|
|
- var addedTableName = (len(statement.JoinStr) > 0)
|
|
|
-
|
|
|
- if !statement.noAutoCondition {
|
|
|
- colNames, args := statement.buildConditions(statement.RefTable, bean, true, true, false, true, addedTableName)
|
|
|
-
|
|
|
- statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
|
|
- statement.BeanArgs = args
|
|
|
- }
|
|
|
+ condSQL, condArgs, _ := statement.genConds(bean)
|
|
|
|
|
|
- // count(index fieldname) > count(0) > count(*)
|
|
|
- var id = "*"
|
|
|
- if statement.Engine.Dialect().DBType() == "ql" {
|
|
|
- id = ""
|
|
|
- }
|
|
|
- statement.attachInSql()
|
|
|
- return statement.genSelectSQL(fmt.Sprintf("count(%v)", id)), append(append(append(statement.joinArgs, statement.Params...),
|
|
|
- statement.BeanArgs...), statement.inParams...)
|
|
|
+ return statement.genSelectSQL("count(*)", condSQL), append(statement.joinArgs, condArgs...)
|
|
|
}
|
|
|
|
|
|
-func (statement *Statement) genSumSql(bean interface{}, columns ...string) (string, []interface{}) {
|
|
|
+func (statement *Statement) genSumSQL(bean interface{}, columns ...string) (string, []interface{}) {
|
|
|
statement.setRefValue(rValue(bean))
|
|
|
|
|
|
- var addedTableName = (len(statement.JoinStr) > 0)
|
|
|
-
|
|
|
- if !statement.noAutoCondition {
|
|
|
- colNames, args := statement.buildConditions(statement.RefTable, bean, true, true, false, true, addedTableName)
|
|
|
-
|
|
|
- statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
|
|
- statement.BeanArgs = args
|
|
|
- }
|
|
|
-
|
|
|
- statement.attachInSql()
|
|
|
var sumStrs = make([]string, 0, len(columns))
|
|
|
for _, colName := range columns {
|
|
|
sumStrs = append(sumStrs, fmt.Sprintf("COALESCE(sum(%s),0)", colName))
|
|
|
}
|
|
|
- return statement.genSelectSQL(strings.Join(sumStrs, ", ")), append(append(append(statement.joinArgs, statement.Params...),
|
|
|
- statement.BeanArgs...), statement.inParams...)
|
|
|
+
|
|
|
+ condSQL, condArgs, _ := statement.genConds(bean)
|
|
|
+
|
|
|
+ return statement.genSelectSQL(strings.Join(sumStrs, ", "), condSQL), append(statement.joinArgs, condArgs...)
|
|
|
}
|
|
|
|
|
|
-func (statement *Statement) genSelectSQL(columnStr string) (a string) {
|
|
|
+func (statement *Statement) genSelectSQL(columnStr, condSQL string) (a string) {
|
|
|
var distinct string
|
|
|
if statement.IsDistinct {
|
|
|
distinct = "DISTINCT "
|
|
|
@@ -1239,17 +1161,8 @@ func (statement *Statement) genSelectSQL(columnStr string) (a string) {
|
|
|
statement.processIdParam()
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
- if len(statement.WhereStr) > 0 {
|
|
|
- if len(statement.ConditionStr) > 0 {
|
|
|
- fmt.Fprintf(&buf, " WHERE (%v)", statement.WhereStr)
|
|
|
- } else {
|
|
|
- fmt.Fprintf(&buf, " WHERE %v", statement.WhereStr)
|
|
|
- }
|
|
|
- if statement.ConditionStr != "" {
|
|
|
- fmt.Fprintf(&buf, " %s (%v)", dialect.AndStr(), statement.ConditionStr)
|
|
|
- }
|
|
|
- } else if len(statement.ConditionStr) > 0 {
|
|
|
- fmt.Fprintf(&buf, " WHERE %v", statement.ConditionStr)
|
|
|
+ if len(condSQL) > 0 {
|
|
|
+ fmt.Fprintf(&buf, " WHERE %v", condSQL)
|
|
|
}
|
|
|
var whereStr = buf.String()
|
|
|
|
|
|
@@ -1333,27 +1246,21 @@ func (statement *Statement) genSelectSQL(columnStr string) (a string) {
|
|
|
}
|
|
|
|
|
|
func (statement *Statement) processIdParam() {
|
|
|
- if statement.IdParam != nil {
|
|
|
- if statement.Engine.dialect.DBType() != "ql" {
|
|
|
- for i, col := range statement.RefTable.PKColumns() {
|
|
|
- var colName = statement.colName(col, statement.TableName())
|
|
|
- if i < len(*(statement.IdParam)) {
|
|
|
- statement.And(fmt.Sprintf("%v %s ?", colName,
|
|
|
- statement.Engine.dialect.EqStr()), (*(statement.IdParam))[i])
|
|
|
- } else {
|
|
|
- statement.And(fmt.Sprintf("%v %s ?", colName,
|
|
|
- statement.Engine.dialect.EqStr()), "")
|
|
|
- }
|
|
|
- }
|
|
|
+ if statement.IdParam == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, col := range statement.RefTable.PKColumns() {
|
|
|
+ var colName = statement.colName(col, statement.TableName())
|
|
|
+ if i < len(*(statement.IdParam)) {
|
|
|
+ statement.cond = statement.cond.And(builder.Eq{colName: (*(statement.IdParam))[i]})
|
|
|
} else {
|
|
|
- if len(*(statement.IdParam)) <= 1 {
|
|
|
- statement.And("id() == ?", (*(statement.IdParam))[0])
|
|
|
- }
|
|
|
+ statement.cond = statement.cond.And(builder.Eq{colName: ""})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (statement *Statement) JoinColumns(cols []*core.Column, includeTableName bool) string {
|
|
|
+func (statement *Statement) joinColumns(cols []*core.Column, includeTableName bool) string {
|
|
|
var colnames = make([]string, len(cols))
|
|
|
for i, col := range cols {
|
|
|
if includeTableName {
|
|
|
@@ -1366,21 +1273,19 @@ func (statement *Statement) JoinColumns(cols []*core.Column, includeTableName bo
|
|
|
return strings.Join(colnames, ", ")
|
|
|
}
|
|
|
|
|
|
-func (statement *Statement) convertIdSql(sqlStr string) string {
|
|
|
+func (statement *Statement) convertIDSQL(sqlStr string) string {
|
|
|
if statement.RefTable != nil {
|
|
|
cols := statement.RefTable.PKColumns()
|
|
|
if len(cols) == 0 {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
- colstrs := statement.JoinColumns(cols, false)
|
|
|
+ colstrs := statement.joinColumns(cols, false)
|
|
|
sqls := splitNNoCase(sqlStr, " from ", 2)
|
|
|
if len(sqls) != 2 {
|
|
|
return ""
|
|
|
}
|
|
|
- if statement.Engine.dialect.DBType() == "ql" {
|
|
|
- return fmt.Sprintf("SELECT id() FROM %v", sqls[1])
|
|
|
- }
|
|
|
+
|
|
|
return fmt.Sprintf("SELECT %s FROM %v", colstrs, sqls[1])
|
|
|
}
|
|
|
return ""
|
|
|
@@ -1391,7 +1296,7 @@ func (statement *Statement) convertUpdateSQL(sqlStr string) (string, string) {
|
|
|
return "", ""
|
|
|
}
|
|
|
|
|
|
- colstrs := statement.JoinColumns(statement.RefTable.PKColumns(), true)
|
|
|
+ colstrs := statement.joinColumns(statement.RefTable.PKColumns(), true)
|
|
|
sqls := splitNNoCase(sqlStr, "where", 2)
|
|
|
if len(sqls) != 2 {
|
|
|
if len(sqls) == 1 {
|