This commit is contained in:
Lunny Xiao 2016-01-02 23:55:01 +08:00
parent 8700152b6c
commit fad61020e8
3 changed files with 85 additions and 55 deletions

View File

@ -320,6 +320,12 @@ func (engine *Engine) NoAutoTime() *Session {
return session.NoAutoTime() return session.NoAutoTime()
} }
func (engine *Engine) NoAutoCondition(no ...bool) *Session {
session := engine.NewSession()
session.IsAutoClose = true
return session.NoAutoCondition(no...)
}
// Retrieve all tables, columns, indexes' informations from database. // Retrieve all tables, columns, indexes' informations from database.
func (engine *Engine) DBMetas() ([]*core.Table, error) { func (engine *Engine) DBMetas() ([]*core.Table, error) {
tables, err := engine.dialect.GetTables() tables, err := engine.dialect.GetTables()

View File

@ -258,6 +258,11 @@ func (session *Session) NoAutoTime() *Session {
return session return session
} }
func (session *Session) NoAutoCondition(no ...bool) *Session {
session.Statement.NoAutoCondition(no...)
return session
}
// Method Limit provide limit and offset query condition // Method Limit provide limit and offset query condition
func (session *Session) Limit(limit int, start ...int) *Session { func (session *Session) Limit(limit int, start ...int) *Session {
session.Statement.Limit(limit, start...) session.Statement.Limit(limit, start...)
@ -1219,7 +1224,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
} }
var addedTableName = (len(session.Statement.JoinStr) > 0) var addedTableName = (len(session.Statement.JoinStr) > 0)
if len(condiBean) > 0 { if !session.Statement.noAutoCondition && len(condiBean) > 0 {
colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true, colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true,
false, true, session.Statement.allUseBool, session.Statement.useAllCols, false, true, session.Statement.allUseBool, session.Statement.useAllCols,
session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.unscoped, session.Statement.mustColumnMap,
@ -3635,7 +3640,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
var condiColNames []string var condiColNames []string
var condiArgs []interface{} var condiArgs []interface{}
if len(condiBean) > 0 { if !session.Statement.noAutoCondition && len(condiBean) > 0 {
condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true, condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true,
false, true, session.Statement.allUseBool, session.Statement.useAllCols, false, true, session.Statement.allUseBool, session.Statement.useAllCols,
session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false) session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false)
@ -3857,11 +3862,15 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
table := session.Engine.TableInfo(bean) table := session.Engine.TableInfo(bean)
session.Statement.RefTable = table session.Statement.RefTable = table
colNames, args := buildConditions(session.Engine, table, bean, true, true, var colNames []string
false, true, session.Statement.allUseBool, session.Statement.useAllCols, var args []interface{}
session.Statement.unscoped, session.Statement.mustColumnMap,
session.Statement.TableName(), false)
if !session.Statement.noAutoCondition {
colNames, args = buildConditions(session.Engine, table, bean, true, true,
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
session.Statement.unscoped, session.Statement.mustColumnMap,
session.Statement.TableName(), false)
}
var condition = "" var condition = ""
var andStr = session.Engine.dialect.AndStr() var andStr = session.Engine.dialect.AndStr()

View File

@ -39,45 +39,46 @@ type exprParam struct {
// statement save all the sql info for executing SQL // statement save all the sql info for executing SQL
type Statement struct { type Statement struct {
RefTable *core.Table RefTable *core.Table
Engine *Engine Engine *Engine
Start int Start int
LimitN int LimitN int
WhereStr string WhereStr string
IdParam *core.PK IdParam *core.PK
Params []interface{} Params []interface{}
OrderStr string OrderStr string
JoinStr string JoinStr string
GroupByStr string GroupByStr string
HavingStr string HavingStr string
ColumnStr string ColumnStr string
selectStr string selectStr string
columnMap map[string]bool columnMap map[string]bool
useAllCols bool useAllCols bool
OmitStr string OmitStr string
ConditionStr string ConditionStr string
AltTableName string AltTableName string
RawSQL string RawSQL string
RawParams []interface{} RawParams []interface{}
UseCascade bool UseCascade bool
UseAutoJoin bool UseAutoJoin bool
StoreEngine string StoreEngine string
Charset string Charset string
BeanArgs []interface{} BeanArgs []interface{}
UseCache bool UseCache bool
UseAutoTime bool UseAutoTime bool
IsDistinct bool noAutoCondition bool
IsForUpdate bool IsDistinct bool
TableAlias string IsForUpdate bool
allUseBool bool TableAlias string
checkVersion bool allUseBool bool
unscoped bool checkVersion bool
mustColumnMap map[string]bool unscoped bool
nullableMap map[string]bool mustColumnMap map[string]bool
inColumns map[string]*inParam nullableMap map[string]bool
incrColumns map[string]incrParam inColumns map[string]*inParam
decrColumns map[string]decrParam incrColumns map[string]incrParam
exprColumns map[string]exprParam decrColumns map[string]decrParam
exprColumns map[string]exprParam
} }
// init // init
@ -103,6 +104,7 @@ func (statement *Statement) Init() {
statement.BeanArgs = make([]interface{}, 0) statement.BeanArgs = make([]interface{}, 0)
statement.UseCache = true statement.UseCache = true
statement.UseAutoTime = true statement.UseAutoTime = true
statement.noAutoCondition = false
statement.IsDistinct = false statement.IsDistinct = false
statement.IsForUpdate = false statement.IsForUpdate = false
statement.TableAlias = "" statement.TableAlias = ""
@ -119,6 +121,15 @@ func (statement *Statement) Init() {
statement.exprColumns = make(map[string]exprParam) statement.exprColumns = make(map[string]exprParam)
} }
// NoAutoCondition
func (statement *Statement) NoAutoCondition(no ...bool) *Statement {
statement.noAutoCondition = true
if len(no) > 0 {
statement.noAutoCondition = no[0]
}
return statement
}
// add the raw sql statement // add the raw sql statement
func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement { func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement {
statement.RawSQL = querystring statement.RawSQL = querystring
@ -1111,12 +1122,14 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
var addedTableName = (len(statement.JoinStr) > 0) var addedTableName = (len(statement.JoinStr) > 0)
colNames, args := buildConditions(statement.Engine, table, bean, true, true, if !statement.noAutoCondition {
false, true, statement.allUseBool, statement.useAllCols, colNames, args := buildConditions(statement.Engine, table, bean, true, true,
statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) false, true, statement.allUseBool, statement.useAllCols,
statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName)
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.selectStr) > 0 { if len(statement.selectStr) > 0 {
@ -1172,12 +1185,14 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
var addedTableName = (len(statement.JoinStr) > 0) var addedTableName = (len(statement.JoinStr) > 0)
colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, if !statement.noAutoCondition {
true, statement.allUseBool, statement.useAllCols, colNames, args := buildConditions(statement.Engine, table, bean, true, true, false,
statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) true, statement.allUseBool, statement.useAllCols,
statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName)
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 = "*"