add *builder.Builder as pointer to .SQL
This commit is contained in:
parent
bcc1d3eb5a
commit
7b5ac89633
|
@ -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
|
||||
|
|
30
session.go
30
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
|
||||
|
|
27
statement.go
27
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
|
||||
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{}) {
|
||||
|
|
Loading…
Reference in New Issue