diff --git a/engine.go b/engine.go index 676101a3..7d3eeced 100644 --- a/engine.go +++ b/engine.go @@ -478,6 +478,13 @@ func (engine *Engine) Where(querystring string, args ...interface{}) *Session { return session.Where(querystring, args...) } +// Where method provide a condition query without bean's non-empty fields. +func (engine *Engine) NoAutoCondition(querystring string, args ...interface{}) *Session { + session := engine.NewSession() + session.IsAutoClose = true + return session.NoAutoCondition(querystring, args...) +} + // Id mehtod provoide a condition as (id) = ? func (engine *Engine) Id(id interface{}) *Session { session := engine.NewSession() diff --git a/session.go b/session.go index 75ef01fe..80a6f4d1 100644 --- a/session.go +++ b/session.go @@ -118,6 +118,12 @@ func (session *Session) Where(querystring string, args ...interface{}) *Session return session } +// Method Where provides custom query condition without bean's non-empty fields. +func (session *Session) NoAutoCondition(querystring string, args ...interface{}) *Session { + session.Statement.NoAutoCondition(querystring, args...) + return session +} + // Method Where provides custom query condition. func (session *Session) And(querystring string, args ...interface{}) *Session { session.Statement.And(querystring, args...) @@ -1884,7 +1890,7 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount structInter := reflect.New(fieldValue.Type()) newsession := session.Engine.NewSession() defer newsession.Close() - has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface()) + has, err := newsession.Id(pk).Cascade().Get(structInter.Interface()) // FIXME NoCascade if err != nil { return err } @@ -2623,7 +2629,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, structInter := reflect.New(fieldValue.Type()) newsession := session.Engine.NewSession() defer newsession.Close() - has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface()) + has, err := newsession.Id(pk).Cascade().Get(structInter.Interface()) // FIXME NoCascade if err != nil { return err } @@ -2971,7 +2977,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, // property to be fetched lazily newsession := session.Engine.NewSession() defer newsession.Close() - has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface()) + has, err := newsession.Id(pk).Cascade().Get(structInter.Interface()) // FIXME NoCascade if err != nil { return err } diff --git a/statement.go b/statement.go index 2a22a2c4..d29faabc 100644 --- a/statement.go +++ b/statement.go @@ -78,6 +78,8 @@ type Statement struct { incrColumns map[string]incrParam decrColumns map[string]decrParam exprColumns map[string]exprParam + + IsNoAutoCondition bool } // init @@ -117,6 +119,8 @@ func (statement *Statement) Init() { statement.incrColumns = make(map[string]incrParam) statement.decrColumns = make(map[string]decrParam) statement.exprColumns = make(map[string]exprParam) + + statement.IsNoAutoCondition = false } // add the raw sql statement @@ -142,6 +146,17 @@ func (statement *Statement) Where(querystring string, args ...interface{}) *Stat return statement } +// add where statment without bean's non-empty fields for Get Count +func (statement *Statement) NoAutoCondition(querystring string, args ...interface{}) *Statement { + if !strings.Contains(querystring, statement.Engine.dialect.EqStr()) { + querystring = strings.Replace(querystring, "=", statement.Engine.dialect.EqStr(), -1) + } + statement.WhereStr = querystring + statement.Params = args + statement.IsNoAutoCondition = true + return statement +} + // add Where & and statment func (statement *Statement) And(querystring string, args ...interface{}) *Statement { if len(statement.WhereStr) > 0 { @@ -1115,8 +1130,10 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) 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 + if !statement.IsNoAutoCondition { + statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") + statement.BeanArgs = args + } var columnStr string = statement.ColumnStr if len(statement.selectStr) > 0 { @@ -1176,8 +1193,10 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{} true, statement.allUseBool, statement.useAllCols, statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) - statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") - statement.BeanArgs = args + if !statement.IsNoAutoCondition { + statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") + statement.BeanArgs = args + } // count(index fieldname) > count(0) > count(*) var id string = "*"