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/3] 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 = "*" From f2d4f12fe1675020bd2e879537797b4a4ccf72a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cmichael2008s=E2=80=9D?= Date: Mon, 27 Jul 2015 11:11:39 +0800 Subject: [PATCH 2/3] Use NoAutoCondition to replace the name WhereOnly. --- engine.go | 4 ++-- session.go | 4 ++-- statement.go | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/engine.go b/engine.go index c33e762b..b8cbc228 100644 --- a/engine.go +++ b/engine.go @@ -467,10 +467,10 @@ func (engine *Engine) Where(querystring string, args ...interface{}) *Session { } // Where method provide a condition query without bean's non-empty fields. -func (engine *Engine) WhereOnly(querystring string, args ...interface{}) *Session { +func (engine *Engine) NoAutoCondition(querystring string, args ...interface{}) *Session { session := engine.NewSession() session.IsAutoClose = true - return session.WhereOnly(querystring, args...) + return session.NoAutoCondition(querystring, args...) } // Id mehtod provoide a condition as (id) = ? diff --git a/session.go b/session.go index c0e055ee..b114f244 100644 --- a/session.go +++ b/session.go @@ -104,8 +104,8 @@ func (session *Session) Where(querystring string, args ...interface{}) *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...) +func (session *Session) NoAutoCondition(querystring string, args ...interface{}) *Session { + session.Statement.NoAutoCondition(querystring, args...) return session } diff --git a/statement.go b/statement.go index 7d5feadf..5487b19f 100644 --- a/statement.go +++ b/statement.go @@ -77,7 +77,7 @@ type Statement struct { decrColumns map[string]decrParam exprColumns map[string]exprParam - IsWhereOnly bool + IsNoAutoCondition bool } // init @@ -117,7 +117,7 @@ func (statement *Statement) Init() { statement.decrColumns = make(map[string]decrParam) statement.exprColumns = make(map[string]exprParam) - statement.IsWhereOnly = false + statement.IsNoAutoCondition = false } // add the raw sql statement @@ -144,13 +144,13 @@ func (statement *Statement) Where(querystring string, args ...interface{}) *Stat } // add where statment without bean's non-empty fields for Get Count -func (statement *Statement) WhereOnly(querystring string, args ...interface{}) *Statement { +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.IsWhereOnly = true + statement.IsNoAutoCondition = true return statement } @@ -1090,7 +1090,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) false, true, statement.allUseBool, statement.useAllCols, statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) - if !statement.IsWhereOnly { + if !statement.IsNoAutoCondition { statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") statement.BeanArgs = args } @@ -1153,7 +1153,7 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{} true, statement.allUseBool, statement.useAllCols, statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) - if !statement.IsWhereOnly { + if !statement.IsNoAutoCondition { statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") statement.BeanArgs = args } From 6b5b20a545e9c5f72601e337d8b0368606251034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cmichael2008s=E2=80=9D?= Date: Fri, 27 Nov 2015 01:21:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?1)=E5=8E=BB=E6=8E=89=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=B1=82=E7=BA=A7=E5=85=B3=E8=81=94=EF=BC=8C=E5=B0=8F=E5=BF=83?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E9=97=AE=E9=A2=98=E3=80=82=E6=9A=82=E6=97=B6?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=8D=E8=83=BD=E8=AE=BE=E7=BD=AE=E8=81=94?= =?UTF-8?q?=E7=BA=A7=E6=9F=A5=E8=AF=A2=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/session.go b/session.go index 0aeb7383..7f1d81d2 100644 --- a/session.go +++ b/session.go @@ -1867,7 +1867,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 } @@ -2582,7 +2582,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 } @@ -2930,7 +2930,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 }