From 7b5ac89633d1288967b20d207955877218756768 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 23 Sep 2016 10:08:28 +0800 Subject: [PATCH] add *builder.Builder as pointer to .SQL --- VERSION | 2 +- engine.go | 8 +++----- session.go | 30 +++++++++--------------------- statement.go | 29 +++++++++++++++++++---------- xorm.go | 2 +- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/VERSION b/VERSION index 942e82fc..eefd2a22 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.6.0.0921 \ No newline at end of file +xorm v0.6.0.0923 \ No newline at end of file diff --git a/engine.go b/engine.go index 812bd7c6..9bbc511a 100644 --- a/engine.go +++ b/engine.go @@ -292,9 +292,7 @@ func (engine *Engine) logSQLExecutionTime(sqlStr string, args []interface{}, exe // Sql will be depracated, please use SQL instead func (engine *Engine) Sql(querystring string, args ...interface{}) *Session { - session := engine.NewSession() - session.IsAutoClose = true - return session.Sql(querystring, args...) + return engine.SQL(querystring, args...) } // SQL method let's you manualy write raw SQL and operate @@ -303,10 +301,10 @@ func (engine *Engine) Sql(querystring string, args ...interface{}) *Session { // engine.SQL("select * from user").Find(&users) // // This code will execute "select * from user" and set the records to users -func (engine *Engine) SQL(querystring string, args ...interface{}) *Session { +func (engine *Engine) SQL(query interface{}, args ...interface{}) *Session { session := engine.NewSession() session.IsAutoClose = true - return session.SQL(querystring, args...) + return session.SQL(query, args...) } // NoAutoTime Default if your struct has "created" or "updated" filed tag, the fields diff --git a/session.go b/session.go index e0391d23..b1e017db 100644 --- a/session.go +++ b/session.go @@ -114,15 +114,14 @@ func (session *Session) Prepare() *Session { } // Sql !DEPRECIATED! will be deprecated, please use SQL instead. -func (session *Session) Sql(querystring string, args ...interface{}) *Session { - session.Statement.Sql(querystring, args...) - return session +func (session *Session) Sql(query string, args ...interface{}) *Session { + return session.SQL(query, args...) } // SQL provides raw sql input parameter. When you have a complex SQL statement // and cannot use Where, Id, In and etc. Methods to describe, you can use SQL. -func (session *Session) SQL(querystring string, args ...interface{}) *Session { - session.Statement.Sql(querystring, args...) +func (session *Session) SQL(query interface{}, args ...interface{}) *Session { + session.Statement.SQL(query, args...) return session } @@ -992,7 +991,7 @@ func (session *Session) Iterate(bean interface{}, fun IterFunc) error { return err } defer rows.Close() - //b := reflect.New(iterator.beanType).Interface() + i := 0 for rows.Next() { b := reflect.New(rows.beanType).Interface() @@ -3766,27 +3765,15 @@ func (session *Session) Delete(bean interface{}) (int64, error) { if processor, ok := interface{}(bean).(BeforeDeleteProcessor); ok { processor.BeforeDelete() } + // -- - - var autoCond builder.Cond - if !session.Statement.noAutoCondition { - var err error - autoCond, err = session.Statement.buildConds(table, bean, true, true, false, true, false) - if err != nil { - return 0, err - } - } - - session.Statement.processIdParam() - - condSQL, condArgs, _ := builder.ToSQL(session.Statement.cond.And(autoCond)) + condSQL, condArgs, _ := session.Statement.genConds(bean) if len(condSQL) == 0 && session.Statement.LimitN == 0 { return 0, ErrNeedDeletedCond } - var deleteSQL, realSQL string var tableName = session.Engine.Quote(session.Statement.TableName()) - + var deleteSQL string if len(condSQL) > 0 { deleteSQL = fmt.Sprintf("DELETE FROM %v WHERE %v", tableName, condSQL) } else { @@ -3825,6 +3812,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) { } } + var realSQL string argsForCache := make([]interface{}, 0, len(condArgs)*2) if session.Statement.unscoped || table.DeletedColumn() == nil { // tag "deleted" is disabled realSQL = deleteSQL diff --git a/statement.go b/statement.go index 745024ef..1571f586 100644 --- a/statement.go +++ b/statement.go @@ -129,9 +129,21 @@ func (statement *Statement) Alias(alias string) *Statement { } // Sql add the raw sql statement -func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement { - statement.RawSQL = querystring - statement.RawParams = args +func (statement *Statement) SQL(query interface{}, args ...interface{}) *Statement { + switch query.(type) { + case (*builder.Builder): + var err error + statement.RawSQL, statement.RawParams, err = query.(*builder.Builder).ToSQL() + if err != nil { + statement.Engine.logger.Error(err) + } + case string: + statement.RawSQL = query.(string) + statement.RawParams = args + default: + statement.Engine.logger.Error("unsupported sql type") + } + return statement } @@ -1077,21 +1089,18 @@ func (statement *Statement) buildConds(table *core.Table, bean interface{}, incl } func (statement *Statement) genConds(bean interface{}) (string, []interface{}, error) { - var table = statement.RefTable - var addedTableName = (len(statement.JoinStr) > 0) - - var autoCond builder.Cond if !statement.noAutoCondition { - var err error - autoCond, err = statement.buildConds(table, bean, true, true, false, true, addedTableName) + var addedTableName = (len(statement.JoinStr) > 0) + autoCond, err := statement.buildConds(statement.RefTable, bean, true, true, false, true, addedTableName) if err != nil { return "", nil, err } + statement.cond = statement.cond.And(autoCond) } statement.processIdParam() - return builder.ToSQL(statement.cond.And(autoCond)) + return builder.ToSQL(statement.cond) } func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}) { diff --git a/xorm.go b/xorm.go index 998b23c3..07405e2d 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( const ( // Version show the xorm's version - Version string = "0.6.0.0921" + Version string = "0.6.0.0923" ) func regDrvsNDialects() bool {