1)add "WhereOnly" function provides custom query condition without bean's non-empty fields for "Get" and "Count"

This commit is contained in:
“michael2008s” 2015-05-05 03:31:20 +08:00
parent 1992491553
commit 51ccecdbc4
3 changed files with 36 additions and 4 deletions

View File

@ -466,6 +466,13 @@ func (engine *Engine) Where(querystring string, args ...interface{}) *Session {
return session.Where(querystring, args...) 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) = ? // Id mehtod provoide a condition as (id) = ?
func (engine *Engine) Id(id interface{}) *Session { func (engine *Engine) Id(id interface{}) *Session {
session := engine.NewSession() session := engine.NewSession()

View File

@ -102,6 +102,12 @@ func (session *Session) Where(querystring string, args ...interface{}) *Session
return 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. // Method Where provides custom query condition.
func (session *Session) And(querystring string, args ...interface{}) *Session { func (session *Session) And(querystring string, args ...interface{}) *Session {
session.Statement.And(querystring, args...) session.Statement.And(querystring, args...)

View File

@ -73,6 +73,8 @@ type Statement struct {
incrColumns map[string]incrParam incrColumns map[string]incrParam
decrColumns map[string]decrParam decrColumns map[string]decrParam
exprColumns map[string]exprParam exprColumns map[string]exprParam
IsWhereOnly bool
} }
// init // init
@ -109,6 +111,8 @@ func (statement *Statement) Init() {
statement.incrColumns = make(map[string]incrParam) statement.incrColumns = make(map[string]incrParam)
statement.decrColumns = make(map[string]decrParam) statement.decrColumns = make(map[string]decrParam)
statement.exprColumns = make(map[string]exprParam) statement.exprColumns = make(map[string]exprParam)
statement.IsWhereOnly = false
} }
// add the raw sql statement // add the raw sql statement
@ -134,6 +138,17 @@ func (statement *Statement) Where(querystring string, args ...interface{}) *Stat
return statement 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 // 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 statement.WhereStr != "" {
@ -1142,8 +1157,10 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
false, true, statement.allUseBool, statement.useAllCols, false, true, statement.allUseBool, statement.useAllCols,
statement.unscoped, statement.mustColumnMap) statement.unscoped, statement.mustColumnMap)
if !statement.IsWhereOnly {
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
statement.BeanArgs = args statement.BeanArgs = args
}
var columnStr string = statement.ColumnStr var columnStr string = statement.ColumnStr
if len(statement.JoinStr) == 0 { if len(statement.JoinStr) == 0 {
@ -1197,8 +1214,10 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
true, statement.allUseBool, statement.useAllCols, true, statement.allUseBool, statement.useAllCols,
statement.unscoped, statement.mustColumnMap) statement.unscoped, statement.mustColumnMap)
if !statement.IsWhereOnly {
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
statement.BeanArgs = args statement.BeanArgs = args
}
// count(index fieldname) > count(0) > count(*) // count(index fieldname) > count(0) > count(*)
var id string = "*" var id string = "*"