增加悲观锁选项
This commit is contained in:
parent
632a86f56a
commit
f549b458b8
15
session.go
15
session.go
|
@ -29,6 +29,7 @@ type Session struct {
|
||||||
IsCommitedOrRollbacked bool
|
IsCommitedOrRollbacked bool
|
||||||
TransType string
|
TransType string
|
||||||
IsAutoClose bool
|
IsAutoClose bool
|
||||||
|
LockRead bool //set sad lock for accepting ro read dirty data
|
||||||
|
|
||||||
// Automatically reset the statement after operations that execute a SQL
|
// Automatically reset the statement after operations that execute a SQL
|
||||||
// query such as Count(), Find(), Get(), ...
|
// query such as Count(), Find(), Get(), ...
|
||||||
|
@ -637,7 +638,7 @@ func (session *Session) canCache() bool {
|
||||||
if session.Statement.RefTable == nil ||
|
if session.Statement.RefTable == nil ||
|
||||||
session.Statement.JoinStr != "" ||
|
session.Statement.JoinStr != "" ||
|
||||||
session.Statement.RawSQL != "" ||
|
session.Statement.RawSQL != "" ||
|
||||||
session.Tx != nil ||
|
session.Tx != nil ||
|
||||||
len(session.Statement.selectStr) > 0 {
|
len(session.Statement.selectStr) > 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -744,7 +745,7 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
|
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
|
||||||
if !session.canCache() ||
|
if !session.canCache() ||
|
||||||
indexNoCase(sqlStr, "having") != -1 ||
|
indexNoCase(sqlStr, "having") != -1 ||
|
||||||
indexNoCase(sqlStr, "group by") != -1 {
|
indexNoCase(sqlStr, "group by") != -1 {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
|
@ -1001,6 +1002,10 @@ func (session *Session) Get(bean interface{}) (bool, error) {
|
||||||
|
|
||||||
if session.Statement.RawSQL == "" {
|
if session.Statement.RawSQL == "" {
|
||||||
sqlStr, args = session.Statement.genGetSql(bean)
|
sqlStr, args = session.Statement.genGetSql(bean)
|
||||||
|
//加入悲观锁 FOR oracle & pg & mysql
|
||||||
|
if session.LockRead {
|
||||||
|
sqlStr += " FOR UPDATE "
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sqlStr = session.Statement.RawSQL
|
sqlStr = session.Statement.RawSQL
|
||||||
args = session.Statement.RawParams
|
args = session.Statement.RawParams
|
||||||
|
@ -1187,7 +1192,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
var addedTableName = (len(session.Statement.JoinStr) > 0)
|
var addedTableName = (len(session.Statement.JoinStr) > 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,
|
||||||
session.Statement.TableName(), addedTableName)
|
session.Statement.TableName(), addedTableName)
|
||||||
session.Statement.ConditionStr = strings.Join(colNames, " AND ")
|
session.Statement.ConditionStr = strings.Join(colNames, " AND ")
|
||||||
session.Statement.BeanArgs = args
|
session.Statement.BeanArgs = args
|
||||||
|
@ -3414,7 +3419,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
if session.Statement.ColumnStr == "" {
|
if session.Statement.ColumnStr == "" {
|
||||||
colNames, args = buildUpdates(session.Engine, table, bean, false, false,
|
colNames, args = buildUpdates(session.Engine, table, bean, false, false,
|
||||||
false, false, session.Statement.allUseBool, session.Statement.useAllCols,
|
false, false, session.Statement.allUseBool, session.Statement.useAllCols,
|
||||||
session.Statement.mustColumnMap, session.Statement.nullableMap,
|
session.Statement.mustColumnMap, session.Statement.nullableMap,
|
||||||
session.Statement.columnMap, true)
|
session.Statement.columnMap, true)
|
||||||
} else {
|
} else {
|
||||||
colNames, args, err = genCols(table, session, bean, true, true)
|
colNames, args, err = genCols(table, session, bean, true, true)
|
||||||
|
@ -3696,7 +3701,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
colNames, args := buildConditions(session.Engine, table, bean, true, true,
|
colNames, args := buildConditions(session.Engine, table, bean, 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,
|
||||||
session.Statement.TableName(), false)
|
session.Statement.TableName(), false)
|
||||||
|
|
||||||
var condition = ""
|
var condition = ""
|
||||||
|
|
|
@ -1241,8 +1241,6 @@ func (statement *Statement) genSelectSql(columnStr string) string {
|
||||||
a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start)
|
a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//在事务中强制性加入排他锁,不开事务则不起作用 FOR oracle & pg & mysql
|
|
||||||
a += " FOR UPDATE "
|
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue