diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-08-22 19:39:52 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-08-22 14:39:52 +0300 |
commit | 2c6a0fdca83ab4ccc2e5628e4ca7d229c844618c (patch) | |
tree | 0389e8ad3f6b3b0a7e52d9dab7aef193753d9d21 /vendor/github.com/go-xorm/xorm/session.go | |
parent | 5c29b0a5fe1a124572ccaa55bd79fee2ce894253 (diff) | |
download | gitea-2c6a0fdca83ab4ccc2e5628e4ca7d229c844618c.tar.gz gitea-2c6a0fdca83ab4ccc2e5628e4ca7d229c844618c.zip |
update latest xorm version to vendor (#2353)
Diffstat (limited to 'vendor/github.com/go-xorm/xorm/session.go')
-rw-r--r-- | vendor/github.com/go-xorm/xorm/session.go | 163 |
1 files changed, 82 insertions, 81 deletions
diff --git a/vendor/github.com/go-xorm/xorm/session.go b/vendor/github.com/go-xorm/xorm/session.go index 475c769f79..9d04140279 100644 --- a/vendor/github.com/go-xorm/xorm/session.go +++ b/vendor/github.com/go-xorm/xorm/session.go @@ -21,16 +21,16 @@ import ( // kind of database operations. type Session struct { db *core.DB - Engine *Engine - Tx *core.Tx - Statement Statement - IsAutoCommit bool - IsCommitedOrRollbacked bool - IsAutoClose bool + engine *Engine + tx *core.Tx + statement Statement + isAutoCommit bool + isCommitedOrRollbacked bool + isAutoClose bool // Automatically reset the statement after operations that execute a SQL // query such as Count(), Find(), Get(), ... - AutoResetStatement bool + autoResetStatement bool // !nashtsai! storing these beans due to yet committed tx afterInsertBeans map[interface{}]*[]func(interface{}) @@ -48,6 +48,8 @@ type Session struct { //beforeSQLExec func(string, ...interface{}) lastSQL string lastSQLArgs []interface{} + + err error } // Clone copy all the session's content and return a new session @@ -58,12 +60,12 @@ func (session *Session) Clone() *Session { // Init reset the session as the init status. func (session *Session) Init() { - session.Statement.Init() - session.Statement.Engine = session.Engine - session.IsAutoCommit = true - session.IsCommitedOrRollbacked = false - session.IsAutoClose = false - session.AutoResetStatement = true + session.statement.Init() + session.statement.Engine = session.engine + session.isAutoCommit = true + session.isCommitedOrRollbacked = false + session.isAutoClose = false + session.autoResetStatement = true session.prepareStmt = false // !nashtsai! is lazy init better? @@ -86,19 +88,23 @@ func (session *Session) Close() { if session.db != nil { // When Close be called, if session is a transaction and do not call // Commit or Rollback, then call Rollback. - if session.Tx != nil && !session.IsCommitedOrRollbacked { + if session.tx != nil && !session.isCommitedOrRollbacked { session.Rollback() } - session.Tx = nil + session.tx = nil session.stmtCache = nil - session.Init() session.db = nil } } +// IsClosed returns if session is closed +func (session *Session) IsClosed() bool { + return session.db == nil +} + func (session *Session) resetStatement() { - if session.AutoResetStatement { - session.Statement.Init() + if session.autoResetStatement { + session.statement.Init() } } @@ -126,75 +132,75 @@ func (session *Session) After(closures func(interface{})) *Session { // Table can input a string or pointer to struct for special a table to operate. func (session *Session) Table(tableNameOrBean interface{}) *Session { - session.Statement.Table(tableNameOrBean) + session.statement.Table(tableNameOrBean) return session } // Alias set the table alias func (session *Session) Alias(alias string) *Session { - session.Statement.Alias(alias) + session.statement.Alias(alias) return session } // NoCascade indicate that no cascade load child object func (session *Session) NoCascade() *Session { - session.Statement.UseCascade = false + session.statement.UseCascade = false return session } // ForUpdate Set Read/Write locking for UPDATE func (session *Session) ForUpdate() *Session { - session.Statement.IsForUpdate = true + session.statement.IsForUpdate = true return session } // NoAutoCondition disable generate SQL condition from beans func (session *Session) NoAutoCondition(no ...bool) *Session { - session.Statement.NoAutoCondition(no...) + session.statement.NoAutoCondition(no...) return session } // Limit provide limit and offset query condition func (session *Session) Limit(limit int, start ...int) *Session { - session.Statement.Limit(limit, start...) + session.statement.Limit(limit, start...) return session } // OrderBy provide order by query condition, the input parameter is the content // after order by on a sql statement. func (session *Session) OrderBy(order string) *Session { - session.Statement.OrderBy(order) + session.statement.OrderBy(order) return session } // Desc provide desc order by query condition, the input parameters are columns. func (session *Session) Desc(colNames ...string) *Session { - session.Statement.Desc(colNames...) + session.statement.Desc(colNames...) return session } // Asc provide asc order by query condition, the input parameters are columns. func (session *Session) Asc(colNames ...string) *Session { - session.Statement.Asc(colNames...) + session.statement.Asc(colNames...) return session } // StoreEngine is only avialble mysql dialect currently func (session *Session) StoreEngine(storeEngine string) *Session { - session.Statement.StoreEngine = storeEngine + session.statement.StoreEngine = storeEngine return session } // Charset is only avialble mysql dialect currently func (session *Session) Charset(charset string) *Session { - session.Statement.Charset = charset + session.statement.Charset = charset return session } // Cascade indicates if loading sub Struct func (session *Session) Cascade(trueOrFalse ...bool) *Session { if len(trueOrFalse) >= 1 { - session.Statement.UseCascade = trueOrFalse[0] + session.statement.UseCascade = trueOrFalse[0] } return session } @@ -202,32 +208,32 @@ func (session *Session) Cascade(trueOrFalse ...bool) *Session { // NoCache ask this session do not retrieve data from cache system and // get data from database directly. func (session *Session) NoCache() *Session { - session.Statement.UseCache = false + session.statement.UseCache = false return session } // Join join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN func (session *Session) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session { - session.Statement.Join(joinOperator, tablename, condition, args...) + session.statement.Join(joinOperator, tablename, condition, args...) return session } // GroupBy Generate Group By statement func (session *Session) GroupBy(keys string) *Session { - session.Statement.GroupBy(keys) + session.statement.GroupBy(keys) return session } // Having Generate Having statement func (session *Session) Having(conditions string) *Session { - session.Statement.Having(conditions) + session.statement.Having(conditions) return session } // DB db return the wrapper of sql.DB func (session *Session) DB() *core.DB { if session.db == nil { - session.db = session.Engine.db + session.db = session.engine.db session.stmtCache = make(map[uint32]*core.Stmt, 0) } return session.db @@ -240,13 +246,13 @@ func cleanupProcessorsClosures(slices *[]func(interface{})) { } func (session *Session) canCache() bool { - if session.Statement.RefTable == nil || - session.Statement.JoinStr != "" || - session.Statement.RawSQL != "" || - !session.Statement.UseCache || - session.Statement.IsForUpdate || - session.Tx != nil || - len(session.Statement.selectStr) > 0 { + if session.statement.RefTable == nil || + session.statement.JoinStr != "" || + session.statement.RawSQL != "" || + !session.statement.UseCache || + session.statement.IsForUpdate || + session.tx != nil || + len(session.statement.selectStr) > 0 { return false } return true @@ -270,18 +276,18 @@ func (session *Session) doPrepare(sqlStr string) (stmt *core.Stmt, err error) { func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) *reflect.Value { var col *core.Column if col = table.GetColumnIdx(key, idx); col == nil { - //session.Engine.logger.Warnf("table %v has no column %v. %v", table.Name, key, table.ColumnsSeq()) + //session.engine.logger.Warnf("table %v has no column %v. %v", table.Name, key, table.ColumnsSeq()) return nil } fieldValue, err := col.ValueOfV(dataStruct) if err != nil { - session.Engine.logger.Error(err) + session.engine.logger.Error(err) return nil } if !fieldValue.IsValid() || !fieldValue.CanSet() { - session.Engine.logger.Warnf("table %v's column %v is not valid or cannot set", table.Name, key) + session.engine.logger.Warnf("table %v's column %v is not valid or cannot set", table.Name, key) return nil } return fieldValue @@ -297,7 +303,12 @@ func (session *Session) rows2Beans(rows *core.Rows, fields []string, fieldsCount var newValue = newElemFunc(fields) bean := newValue.Interface() dataStruct := rValue(bean) - pk, err := session.row2Bean(rows, fields, fieldsCount, bean, &dataStruct, table) + // handle beforeClosures + scanResults, err := session.row2Slice(rows, fields, fieldsCount, bean) + if err != nil { + return err + } + pk, err := session.slice2Bean(scanResults, fields, fieldsCount, bean, &dataStruct, table) if err != nil { return err } @@ -310,8 +321,7 @@ func (session *Session) rows2Beans(rows *core.Rows, fields []string, fieldsCount return nil } -func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount int, bean interface{}, dataStruct *reflect.Value, table *core.Table) (core.PK, error) { - // handle beforeClosures +func (session *Session) row2Slice(rows *core.Rows, fields []string, fieldsCount int, bean interface{}) ([]interface{}, error) { for _, closure := range session.beforeClosures { closure(bean) } @@ -330,7 +340,10 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i b.BeforeSet(key, Cell(scanResults[ii].(*interface{}))) } } + return scanResults, nil +} +func (session *Session) slice2Bean(scanResults []interface{}, fields []string, fieldsCount int, bean interface{}, dataStruct *reflect.Value, table *core.Table) (core.PK, error) { defer func() { if b, hasAfterSet := bean.(AfterSetProcessor); hasAfterSet { for ii, key := range fields { @@ -344,15 +357,6 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i } }() - dbTZ := session.Engine.DatabaseTZ - if dbTZ == nil { - if session.Engine.dialect.DBType() == core.SQLITE { - dbTZ = time.UTC - } else { - dbTZ = time.Local - } - } - var tempMap = make(map[string]int) var pk core.PK for ii, key := range fields { @@ -528,11 +532,9 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i } case reflect.Struct: if fieldType.ConvertibleTo(core.TimeType) { - var tz *time.Location - if col.TimeZone == nil { - tz = session.Engine.TZLocation - } else { - tz = col.TimeZone + dbTZ := session.engine.DatabaseTZ + if col.TimeZone != nil { + dbTZ = col.TimeZone } if rawValueType == core.TimeType { @@ -543,26 +545,25 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i z, _ := t.Zone() // set new location if database don't save timezone or give an incorrect timezone if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbTZ.String() { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location - session.Engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location()) + session.engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location()) t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), dbTZ) } - // !nashtsai! convert to engine location - t = t.In(tz) + t = t.In(session.engine.TZLocation) fieldValue.Set(reflect.ValueOf(t).Convert(fieldType)) } else if rawValueType == core.IntType || rawValueType == core.Int64Type || rawValueType == core.Int32Type { hasAssigned = true - t := time.Unix(vv.Int(), 0).In(tz) + t := time.Unix(vv.Int(), 0).In(session.engine.TZLocation) fieldValue.Set(reflect.ValueOf(t).Convert(fieldType)) } else { if d, ok := vv.Interface().([]uint8); ok { hasAssigned = true t, err := session.byte2Time(col, d) if err != nil { - session.Engine.logger.Error("byte2Time error:", err.Error()) + session.engine.logger.Error("byte2Time error:", err.Error()) hasAssigned = false } else { fieldValue.Set(reflect.ValueOf(t).Convert(fieldType)) @@ -571,20 +572,20 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i hasAssigned = true t, err := session.str2Time(col, d) if err != nil { - session.Engine.logger.Error("byte2Time error:", err.Error()) + session.engine.logger.Error("byte2Time error:", err.Error()) hasAssigned = false } else { fieldValue.Set(reflect.ValueOf(t).Convert(fieldType)) } } else { - panic(fmt.Sprintf("rawValueType is %v, value is %v", rawValueType, vv.Interface())) + return nil, fmt.Errorf("rawValueType is %v, value is %v", rawValueType, vv.Interface()) } } } else if nulVal, ok := fieldValue.Addr().Interface().(sql.Scanner); ok { // !<winxxp>! 增加支持sql.Scanner接口的结构,如sql.NullString hasAssigned = true if err := nulVal.Scan(vv.Interface()); err != nil { - session.Engine.logger.Error("sql.Sanner error:", err.Error()) + session.engine.logger.Error("sql.Sanner error:", err.Error()) hasAssigned = false } } else if col.SQLType.IsJson() { @@ -609,15 +610,15 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i fieldValue.Set(x.Elem()) } } - } else if session.Statement.UseCascade { - table, err := session.Engine.autoMapType(*fieldValue) + } else if session.statement.UseCascade { + table, err := session.engine.autoMapType(*fieldValue) if err != nil { return nil, err } hasAssigned = true if len(table.PrimaryKeys) != 1 { - panic("unsupported non or composited primary key cascade") + return nil, errors.New("unsupported non or composited primary key cascade") } var pk = make(core.PK, len(table.PrimaryKeys)) pk[0], err = asKind(vv, rawValueType) @@ -630,9 +631,9 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i // however, also need to consider adding a 'lazy' attribute to xorm tag which allow hasOne // property to be fetched lazily structInter := reflect.New(fieldValue.Type()) - newsession := session.Engine.NewSession() + newsession := session.engine.NewSession() defer newsession.Close() - has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface()) + has, err := newsession.ID(pk).NoCascade().Get(structInter.Interface()) if err != nil { return nil, err } @@ -777,8 +778,8 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i } func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) { - for _, filter := range session.Engine.dialect.Filters() { - *sqlStr = filter.Do(*sqlStr, session.Engine.dialect, session.Statement.RefTable) + for _, filter := range session.engine.dialect.Filters() { + *sqlStr = filter.Do(*sqlStr, session.engine.dialect, session.statement.RefTable) } session.saveLastSQL(*sqlStr, paramStr...) @@ -788,7 +789,7 @@ func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) func (session *Session) saveLastSQL(sql string, args ...interface{}) { session.lastSQL = sql session.lastSQLArgs = args - session.Engine.logSQL(sql, args...) + session.engine.logSQL(sql, args...) } // LastSQL returns last query information @@ -798,8 +799,8 @@ func (session *Session) LastSQL() (string, []interface{}) { // tbName get some table's table name func (session *Session) tbNameNoSchema(table *core.Table) string { - if len(session.Statement.AltTableName) > 0 { - return session.Statement.AltTableName + if len(session.statement.AltTableName) > 0 { + return session.statement.AltTableName } return table.Name @@ -807,6 +808,6 @@ func (session *Session) tbNameNoSchema(table *core.Table) string { // Unscoped always disable struct tag "deleted" func (session *Session) Unscoped() *Session { - session.Statement.Unscoped() + session.statement.Unscoped() return session } |