From 51ccecdbc4b80dc262550c93162ca4539e6f572a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cmichael2008s=E2=80=9D?= Date: Tue, 5 May 2015 03:31:20 +0800 Subject: [PATCH] 1)add "WhereOnly" function provides custom query condition without bean's non-empty fields for "Get" and "Count" --- engine.go | 7 +++++++ session.go | 6 ++++++ statement.go | 27 +++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/engine.go b/engine.go index 5f652101..915912de 100644 --- a/engine.go +++ b/engine.go @@ -466,6 +466,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) WhereOnly(querystring string, args ...interface{}) *Session { + session := engine.NewSession() + session.IsAutoClose = true + return session.WhereOnly(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 34738b89..a701c2bc 100644 --- a/session.go +++ b/session.go @@ -102,6 +102,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) WhereOnly(querystring string, args ...interface{}) *Session { + session.Statement.WhereOnly(querystring, args...) + return session +} + // Method Where provides custom query condition. func (session *Session) And(querystring string, args ...interface{}) *Session { session.Statement.And(querystring, args...) diff --git a/statement.go b/statement.go index c7ee11cf..6e1e87c2 100644 --- a/statement.go +++ b/statement.go @@ -73,6 +73,8 @@ type Statement struct { incrColumns map[string]incrParam decrColumns map[string]decrParam exprColumns map[string]exprParam + + IsWhereOnly bool } // init @@ -109,6 +111,8 @@ func (statement *Statement) Init() { statement.incrColumns = make(map[string]incrParam) statement.decrColumns = make(map[string]decrParam) statement.exprColumns = make(map[string]exprParam) + + statement.IsWhereOnly = false } // add the raw sql statement @@ -134,6 +138,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) WhereOnly(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.IsWhereOnly = true + return statement +} + // add Where & and statment func (statement *Statement) And(querystring string, args ...interface{}) *Statement { if statement.WhereStr != "" { @@ -1142,8 +1157,10 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) false, true, statement.allUseBool, statement.useAllCols, statement.unscoped, statement.mustColumnMap) - statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") - statement.BeanArgs = args + if !statement.IsWhereOnly { + statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") + statement.BeanArgs = args + } var columnStr string = statement.ColumnStr if len(statement.JoinStr) == 0 { @@ -1197,8 +1214,10 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{} true, statement.allUseBool, statement.useAllCols, statement.unscoped, statement.mustColumnMap) - statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") - statement.BeanArgs = args + if !statement.IsWhereOnly { + statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") + statement.BeanArgs = args + } // count(index fieldname) > count(0) > count(*) var id string = "*"