From fad61020e856d1dfe16f44e006d288713feb38fb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 2 Jan 2016 23:55:01 +0800 Subject: [PATCH] resolved #250, #317 --- engine.go | 6 +++ session.go | 21 +++++++--- statement.go | 113 +++++++++++++++++++++++++++++---------------------- 3 files changed, 85 insertions(+), 55 deletions(-) diff --git a/engine.go b/engine.go index 5188d27e..a035a63d 100644 --- a/engine.go +++ b/engine.go @@ -320,6 +320,12 @@ func (engine *Engine) NoAutoTime() *Session { return session.NoAutoTime() } +func (engine *Engine) NoAutoCondition(no ...bool) *Session { + session := engine.NewSession() + session.IsAutoClose = true + return session.NoAutoCondition(no...) +} + // Retrieve all tables, columns, indexes' informations from database. func (engine *Engine) DBMetas() ([]*core.Table, error) { tables, err := engine.dialect.GetTables() diff --git a/session.go b/session.go index 75ef01fe..51073fa3 100644 --- a/session.go +++ b/session.go @@ -258,6 +258,11 @@ func (session *Session) NoAutoTime() *Session { return session } +func (session *Session) NoAutoCondition(no ...bool) *Session { + session.Statement.NoAutoCondition(no...) + return session +} + // Method Limit provide limit and offset query condition func (session *Session) Limit(limit int, start ...int) *Session { session.Statement.Limit(limit, start...) @@ -1219,7 +1224,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) } var addedTableName = (len(session.Statement.JoinStr) > 0) - if len(condiBean) > 0 { + if !session.Statement.noAutoCondition && len(condiBean) > 0 { colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true, false, true, session.Statement.allUseBool, session.Statement.useAllCols, session.Statement.unscoped, session.Statement.mustColumnMap, @@ -3635,7 +3640,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 var condiColNames []string var condiArgs []interface{} - if len(condiBean) > 0 { + if !session.Statement.noAutoCondition && len(condiBean) > 0 { condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true, false, true, session.Statement.allUseBool, session.Statement.useAllCols, session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false) @@ -3857,11 +3862,15 @@ func (session *Session) Delete(bean interface{}) (int64, error) { table := session.Engine.TableInfo(bean) session.Statement.RefTable = table - colNames, args := buildConditions(session.Engine, table, bean, true, true, - false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap, - session.Statement.TableName(), false) + var colNames []string + var args []interface{} + if !session.Statement.noAutoCondition { + colNames, args = buildConditions(session.Engine, table, bean, true, true, + false, true, session.Statement.allUseBool, session.Statement.useAllCols, + session.Statement.unscoped, session.Statement.mustColumnMap, + session.Statement.TableName(), false) + } var condition = "" var andStr = session.Engine.dialect.AndStr() diff --git a/statement.go b/statement.go index 2a22a2c4..a8d82f24 100644 --- a/statement.go +++ b/statement.go @@ -39,45 +39,46 @@ type exprParam struct { // statement save all the sql info for executing SQL type Statement struct { - RefTable *core.Table - Engine *Engine - Start int - LimitN int - WhereStr string - IdParam *core.PK - Params []interface{} - OrderStr string - JoinStr string - GroupByStr string - HavingStr string - ColumnStr string - selectStr string - columnMap map[string]bool - useAllCols bool - OmitStr string - ConditionStr string - AltTableName string - RawSQL string - RawParams []interface{} - UseCascade bool - UseAutoJoin bool - StoreEngine string - Charset string - BeanArgs []interface{} - UseCache bool - UseAutoTime bool - IsDistinct bool - IsForUpdate bool - TableAlias string - allUseBool bool - checkVersion bool - 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 + RefTable *core.Table + Engine *Engine + Start int + LimitN int + WhereStr string + IdParam *core.PK + Params []interface{} + OrderStr string + JoinStr string + GroupByStr string + HavingStr string + ColumnStr string + selectStr string + columnMap map[string]bool + useAllCols bool + OmitStr string + ConditionStr string + AltTableName string + RawSQL string + RawParams []interface{} + UseCascade bool + UseAutoJoin bool + StoreEngine string + Charset string + BeanArgs []interface{} + UseCache bool + UseAutoTime bool + noAutoCondition bool + IsDistinct bool + IsForUpdate bool + TableAlias string + allUseBool bool + checkVersion bool + 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 } // init @@ -103,6 +104,7 @@ func (statement *Statement) Init() { statement.BeanArgs = make([]interface{}, 0) statement.UseCache = true statement.UseAutoTime = true + statement.noAutoCondition = false statement.IsDistinct = false statement.IsForUpdate = false statement.TableAlias = "" @@ -119,6 +121,15 @@ func (statement *Statement) Init() { statement.exprColumns = make(map[string]exprParam) } +// NoAutoCondition +func (statement *Statement) NoAutoCondition(no ...bool) *Statement { + statement.noAutoCondition = true + if len(no) > 0 { + statement.noAutoCondition = no[0] + } + return statement +} + // add the raw sql statement func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement { statement.RawSQL = querystring @@ -1111,12 +1122,14 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) var addedTableName = (len(statement.JoinStr) > 0) - colNames, args := buildConditions(statement.Engine, table, bean, true, true, - false, true, statement.allUseBool, statement.useAllCols, - statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) + if !statement.noAutoCondition { + colNames, args := buildConditions(statement.Engine, table, bean, true, true, + false, true, statement.allUseBool, statement.useAllCols, + statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) - statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") - statement.BeanArgs = args + statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") + statement.BeanArgs = args + } var columnStr string = statement.ColumnStr if len(statement.selectStr) > 0 { @@ -1172,12 +1185,14 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{} var addedTableName = (len(statement.JoinStr) > 0) - colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, - true, statement.allUseBool, statement.useAllCols, - statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) + if !statement.noAutoCondition { + colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, + true, statement.allUseBool, statement.useAllCols, + statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) - statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") - statement.BeanArgs = args + statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") + statement.BeanArgs = args + } // count(index fieldname) > count(0) > count(*) var id string = "*"