2016-11-03 22:16:01 +00:00
|
|
|
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-23 15:22:43 +00:00
|
|
|
|
"context"
|
2020-06-15 20:46:01 +00:00
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/sha256"
|
2016-11-03 22:16:01 +00:00
|
|
|
|
"database/sql"
|
2020-06-15 20:46:01 +00:00
|
|
|
|
"encoding/hex"
|
2016-11-03 22:16:01 +00:00
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"hash/crc32"
|
2020-06-15 20:46:01 +00:00
|
|
|
|
"io"
|
2016-11-03 22:16:01 +00:00
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
"xorm.io/xorm/contexts"
|
|
|
|
|
"xorm.io/xorm/convert"
|
|
|
|
|
"xorm.io/xorm/core"
|
|
|
|
|
"xorm.io/xorm/internal/json"
|
|
|
|
|
"xorm.io/xorm/internal/statements"
|
2020-06-15 20:46:01 +00:00
|
|
|
|
"xorm.io/xorm/log"
|
2020-03-22 15:12:55 +00:00
|
|
|
|
"xorm.io/xorm/schemas"
|
2019-06-23 15:22:43 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
// ErrFieldIsNotExist columns does not exist
|
|
|
|
|
type ErrFieldIsNotExist struct {
|
|
|
|
|
FieldName string
|
|
|
|
|
TableName string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e ErrFieldIsNotExist) Error() string {
|
|
|
|
|
return fmt.Sprintf("field %s is not valid on table %s", e.FieldName, e.TableName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ErrFieldIsNotValid is not valid
|
|
|
|
|
type ErrFieldIsNotValid struct {
|
|
|
|
|
FieldName string
|
|
|
|
|
TableName string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e ErrFieldIsNotValid) Error() string {
|
|
|
|
|
return fmt.Sprintf("field %s is not valid on table %s", e.FieldName, e.TableName)
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
type sessionType bool
|
2019-06-23 15:22:43 +00:00
|
|
|
|
|
|
|
|
|
const (
|
2020-06-15 20:46:01 +00:00
|
|
|
|
engineSession sessionType = false
|
|
|
|
|
groupSession sessionType = true
|
2016-11-03 22:16:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Session keep a pointer to sql.DB and provides all execution of all
|
|
|
|
|
// kind of database operations.
|
|
|
|
|
type Session struct {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
engine *Engine
|
|
|
|
|
tx *core.Tx
|
2020-03-22 15:12:55 +00:00
|
|
|
|
statement *statements.Statement
|
2017-08-22 11:39:52 +00:00
|
|
|
|
isAutoCommit bool
|
|
|
|
|
isCommitedOrRollbacked bool
|
|
|
|
|
isAutoClose bool
|
2020-06-15 20:46:01 +00:00
|
|
|
|
isClosed bool
|
|
|
|
|
prepareStmt bool
|
2016-11-03 22:16:01 +00:00
|
|
|
|
// Automatically reset the statement after operations that execute a SQL
|
|
|
|
|
// query such as Count(), Find(), Get(), ...
|
2017-08-22 11:39:52 +00:00
|
|
|
|
autoResetStatement bool
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
|
|
|
|
// !nashtsai! storing these beans due to yet committed tx
|
|
|
|
|
afterInsertBeans map[interface{}]*[]func(interface{})
|
|
|
|
|
afterUpdateBeans map[interface{}]*[]func(interface{})
|
|
|
|
|
afterDeleteBeans map[interface{}]*[]func(interface{})
|
|
|
|
|
// --
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
beforeClosures []func(interface{})
|
|
|
|
|
afterClosures []func(interface{})
|
2017-10-01 16:52:35 +00:00
|
|
|
|
afterProcessors []executedProcessor
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr))
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
|
|
|
|
lastSQL string
|
|
|
|
|
lastSQLArgs []interface{}
|
2017-08-22 11:39:52 +00:00
|
|
|
|
|
2019-06-23 15:22:43 +00:00
|
|
|
|
ctx context.Context
|
|
|
|
|
sessionType sessionType
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
func newSessionID() string {
|
|
|
|
|
hash := sha256.New()
|
|
|
|
|
_, err := io.CopyN(hash, rand.Reader, 50)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "????????????????????"
|
|
|
|
|
}
|
|
|
|
|
md := hash.Sum(nil)
|
|
|
|
|
mdStr := hex.EncodeToString(md)
|
|
|
|
|
return mdStr[0:20]
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
func newSession(engine *Engine) *Session {
|
|
|
|
|
var ctx context.Context
|
|
|
|
|
if engine.logSessionID {
|
2020-09-03 01:58:04 +00:00
|
|
|
|
ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID())
|
2020-06-15 20:46:01 +00:00
|
|
|
|
} else {
|
|
|
|
|
ctx = engine.defaultContext
|
|
|
|
|
}
|
2019-06-23 15:22:43 +00:00
|
|
|
|
|
2020-07-21 12:28:27 +00:00
|
|
|
|
session := &Session{
|
2020-06-15 20:46:01 +00:00
|
|
|
|
ctx: ctx,
|
|
|
|
|
engine: engine,
|
|
|
|
|
tx: nil,
|
|
|
|
|
statement: statements.NewStatement(
|
|
|
|
|
engine.dialect,
|
|
|
|
|
engine.tagParser,
|
|
|
|
|
engine.DatabaseTZ,
|
|
|
|
|
),
|
|
|
|
|
isClosed: false,
|
|
|
|
|
isAutoCommit: true,
|
|
|
|
|
isCommitedOrRollbacked: false,
|
|
|
|
|
isAutoClose: false,
|
|
|
|
|
autoResetStatement: true,
|
|
|
|
|
prepareStmt: false,
|
|
|
|
|
|
|
|
|
|
afterInsertBeans: make(map[interface{}]*[]func(interface{}), 0),
|
|
|
|
|
afterUpdateBeans: make(map[interface{}]*[]func(interface{}), 0),
|
|
|
|
|
afterDeleteBeans: make(map[interface{}]*[]func(interface{}), 0),
|
|
|
|
|
beforeClosures: make([]func(interface{}), 0),
|
|
|
|
|
afterClosures: make([]func(interface{}), 0),
|
|
|
|
|
afterProcessors: make([]executedProcessor, 0),
|
|
|
|
|
stmtCache: make(map[uint32]*core.Stmt),
|
|
|
|
|
|
|
|
|
|
lastSQL: "",
|
|
|
|
|
lastSQLArgs: make([]interface{}, 0),
|
|
|
|
|
|
|
|
|
|
sessionType: engineSession,
|
|
|
|
|
}
|
2020-07-21 12:28:27 +00:00
|
|
|
|
if engine.logSessionID {
|
|
|
|
|
session.ctx = context.WithValue(session.ctx, log.SessionKey, session)
|
|
|
|
|
}
|
|
|
|
|
return session
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close release the connection from pool
|
2020-06-15 20:46:01 +00:00
|
|
|
|
func (session *Session) Close() error {
|
2016-11-03 22:16:01 +00:00
|
|
|
|
for _, v := range session.stmtCache {
|
2020-06-15 20:46:01 +00:00
|
|
|
|
if err := v.Close(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
if !session.isClosed {
|
2016-11-03 22:16:01 +00:00
|
|
|
|
// When Close be called, if session is a transaction and do not call
|
|
|
|
|
// Commit or Rollback, then call Rollback.
|
2017-08-22 11:39:52 +00:00
|
|
|
|
if session.tx != nil && !session.isCommitedOrRollbacked {
|
2020-06-15 20:46:01 +00:00
|
|
|
|
if err := session.Rollback(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.tx = nil
|
2016-11-03 22:16:01 +00:00
|
|
|
|
session.stmtCache = nil
|
2020-06-15 20:46:01 +00:00
|
|
|
|
session.isClosed = true
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2020-06-15 20:46:01 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (session *Session) db() *core.DB {
|
|
|
|
|
return session.engine.db
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 19:17:06 +00:00
|
|
|
|
// Engine returns session Engine
|
2020-07-21 12:28:27 +00:00
|
|
|
|
func (session *Session) Engine() *Engine {
|
|
|
|
|
return session.engine
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 14:32:23 +00:00
|
|
|
|
func (session *Session) getQueryer() core.Queryer {
|
|
|
|
|
if session.tx != nil {
|
|
|
|
|
return session.tx
|
|
|
|
|
}
|
2020-06-15 20:46:01 +00:00
|
|
|
|
return session.db()
|
2020-03-25 14:32:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-12 01:01:41 +00:00
|
|
|
|
// ContextCache enable context cache or not
|
2020-03-22 15:12:55 +00:00
|
|
|
|
func (session *Session) ContextCache(context contexts.ContextCache) *Session {
|
|
|
|
|
session.statement.SetContextCache(context)
|
2018-12-12 01:01:41 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
|
// IsClosed returns if session is closed
|
|
|
|
|
func (session *Session) IsClosed() bool {
|
2020-06-15 20:46:01 +00:00
|
|
|
|
return session.isClosed
|
2017-08-22 11:39:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 22:16:01 +00:00
|
|
|
|
func (session *Session) resetStatement() {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
if session.autoResetStatement {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.statement.Reset()
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 09:11:18 +00:00
|
|
|
|
// Prepare set a flag to session that should be prepare statement before execute query
|
2016-11-03 22:16:01 +00:00
|
|
|
|
func (session *Session) Prepare() *Session {
|
|
|
|
|
session.prepareStmt = true
|
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Before Apply before Processor, affected bean is passed to closure arg
|
|
|
|
|
func (session *Session) Before(closures func(interface{})) *Session {
|
|
|
|
|
if closures != nil {
|
|
|
|
|
session.beforeClosures = append(session.beforeClosures, closures)
|
|
|
|
|
}
|
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// After Apply after Processor, affected bean is passed to closure arg
|
|
|
|
|
func (session *Session) After(closures func(interface{})) *Session {
|
|
|
|
|
if closures != nil {
|
|
|
|
|
session.afterClosures = append(session.afterClosures, closures)
|
|
|
|
|
}
|
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Table can input a string or pointer to struct for special a table to operate.
|
|
|
|
|
func (session *Session) Table(tableNameOrBean interface{}) *Session {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
if err := session.statement.SetTable(tableNameOrBean); err != nil {
|
|
|
|
|
session.statement.LastError = err
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Alias set the table alias
|
|
|
|
|
func (session *Session) Alias(alias string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Alias(alias)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoCascade indicate that no cascade load child object
|
|
|
|
|
func (session *Session) NoCascade() *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.UseCascade = false
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ForUpdate Set Read/Write locking for UPDATE
|
|
|
|
|
func (session *Session) ForUpdate() *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.IsForUpdate = true
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoAutoCondition disable generate SQL condition from beans
|
|
|
|
|
func (session *Session) NoAutoCondition(no ...bool) *Session {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.statement.SetNoAutoCondition(no...)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Limit provide limit and offset query condition
|
|
|
|
|
func (session *Session) Limit(limit int, start ...int) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Limit(limit, start...)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
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 {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.OrderBy(order)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Desc provide desc order by query condition, the input parameters are columns.
|
|
|
|
|
func (session *Session) Desc(colNames ...string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Desc(colNames...)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Asc provide asc order by query condition, the input parameters are columns.
|
|
|
|
|
func (session *Session) Asc(colNames ...string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Asc(colNames...)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StoreEngine is only avialble mysql dialect currently
|
|
|
|
|
func (session *Session) StoreEngine(storeEngine string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.StoreEngine = storeEngine
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Charset is only avialble mysql dialect currently
|
|
|
|
|
func (session *Session) Charset(charset string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Charset = charset
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cascade indicates if loading sub Struct
|
|
|
|
|
func (session *Session) Cascade(trueOrFalse ...bool) *Session {
|
|
|
|
|
if len(trueOrFalse) >= 1 {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.UseCascade = trueOrFalse[0]
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 13:07:36 +00:00
|
|
|
|
// MustLogSQL means record SQL or not and don't follow engine's setting
|
2020-06-15 20:46:01 +00:00
|
|
|
|
func (session *Session) MustLogSQL(logs ...bool) *Session {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
var showSQL = true
|
2020-06-15 20:46:01 +00:00
|
|
|
|
if len(logs) > 0 {
|
|
|
|
|
showSQL = logs[0]
|
2019-12-18 13:07:36 +00:00
|
|
|
|
}
|
2020-06-15 20:46:01 +00:00
|
|
|
|
session.ctx = context.WithValue(session.ctx, log.SessionShowSQLKey, showSQL)
|
2019-12-18 13:07:36 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 22:16:01 +00:00
|
|
|
|
// NoCache ask this session do not retrieve data from cache system and
|
|
|
|
|
// get data from database directly.
|
|
|
|
|
func (session *Session) NoCache() *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.UseCache = false
|
2016-11-03 22:16:01 +00:00
|
|
|
|
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 {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Join(joinOperator, tablename, condition, args...)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GroupBy Generate Group By statement
|
|
|
|
|
func (session *Session) GroupBy(keys string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.GroupBy(keys)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Having Generate Having statement
|
|
|
|
|
func (session *Session) Having(conditions string) *Session {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
session.statement.Having(conditions)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DB db return the wrapper of sql.DB
|
|
|
|
|
func (session *Session) DB() *core.DB {
|
2020-06-15 20:46:01 +00:00
|
|
|
|
return session.db()
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
|
func (session *Session) canCache() bool {
|
2017-08-22 11:39:52 +00:00
|
|
|
|
if session.statement.RefTable == nil ||
|
|
|
|
|
session.statement.JoinStr != "" ||
|
|
|
|
|
session.statement.RawSQL != "" ||
|
|
|
|
|
!session.statement.UseCache ||
|
|
|
|
|
session.statement.IsForUpdate ||
|
|
|
|
|
session.tx != nil ||
|
2020-03-22 15:12:55 +00:00
|
|
|
|
len(session.statement.SelectStr) > 0 {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-01-27 15:20:59 +00:00
|
|
|
|
func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt, err error) {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
crc := crc32.ChecksumIEEE([]byte(sqlStr))
|
|
|
|
|
// TODO try hash(sqlStr+len(sqlStr))
|
|
|
|
|
var has bool
|
|
|
|
|
stmt, has = session.stmtCache[crc]
|
|
|
|
|
if !has {
|
2019-06-23 15:22:43 +00:00
|
|
|
|
stmt, err = db.PrepareContext(session.ctx, sqlStr)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
session.stmtCache[crc] = stmt
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
return
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
func (session *Session) getField(dataStruct *reflect.Value, key string, table *schemas.Table, idx int) (*reflect.Value, error) {
|
|
|
|
|
var col *schemas.Column
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if col = table.GetColumnIdx(key, idx); col == nil {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
return nil, ErrFieldIsNotExist{key, table.Name}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
|
fieldValue, err := col.ValueOfV(dataStruct)
|
|
|
|
|
if err != nil {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
return nil, err
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2021-07-04 13:10:46 +00:00
|
|
|
|
if fieldValue == nil {
|
|
|
|
|
return nil, ErrFieldIsNotValid{key, table.Name}
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if !fieldValue.IsValid() || !fieldValue.CanSet() {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
return nil, ErrFieldIsNotValid{key, table.Name}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
|
|
|
|
|
return fieldValue, nil
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
|
// Cell cell is a result of one column field
|
|
|
|
|
type Cell *interface{}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2017-10-01 16:52:35 +00:00
|
|
|
|
func (session *Session) rows2Beans(rows *core.Rows, fields []string,
|
2020-03-22 15:12:55 +00:00
|
|
|
|
table *schemas.Table, newElemFunc func([]string) reflect.Value,
|
|
|
|
|
sliceValueSetFunc func(*reflect.Value, schemas.PK) error) error {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
for rows.Next() {
|
2017-04-07 01:47:25 +00:00
|
|
|
|
var newValue = newElemFunc(fields)
|
2017-01-03 08:20:28 +00:00
|
|
|
|
bean := newValue.Interface()
|
2017-10-01 16:52:35 +00:00
|
|
|
|
dataStruct := newValue.Elem()
|
2017-09-13 05:18:22 +00:00
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
|
// handle beforeClosures
|
2017-10-01 16:52:35 +00:00
|
|
|
|
scanResults, err := session.row2Slice(rows, fields, bean)
|
2017-08-22 11:39:52 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-10-01 16:52:35 +00:00
|
|
|
|
pk, err := session.slice2Bean(scanResults, fields, bean, &dataStruct, table)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-10-01 16:52:35 +00:00
|
|
|
|
session.afterProcessors = append(session.afterProcessors, executedProcessor{
|
|
|
|
|
fun: func(*Session, interface{}) error {
|
|
|
|
|
return sliceValueSetFunc(&newValue, pk)
|
|
|
|
|
},
|
|
|
|
|
session: session,
|
|
|
|
|
bean: bean,
|
|
|
|
|
})
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 16:52:35 +00:00
|
|
|
|
func (session *Session) row2Slice(rows *core.Rows, fields []string, bean interface{}) ([]interface{}, error) {
|
2017-05-02 00:50:33 +00:00
|
|
|
|
for _, closure := range session.beforeClosures {
|
|
|
|
|
closure(bean)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 16:52:35 +00:00
|
|
|
|
scanResults := make([]interface{}, len(fields))
|
2017-01-03 08:20:28 +00:00
|
|
|
|
for i := 0; i < len(fields); i++ {
|
|
|
|
|
var cell interface{}
|
|
|
|
|
scanResults[i] = &cell
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if err := rows.Scan(scanResults...); err != nil {
|
2017-02-20 11:33:10 +00:00
|
|
|
|
return nil, err
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
executeBeforeSet(bean, fields, scanResults)
|
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
|
return scanResults, nil
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
func (session *Session) slice2Bean(scanResults []interface{}, fields []string, bean interface{}, dataStruct *reflect.Value, table *schemas.Table) (schemas.PK, error) {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
defer func() {
|
2020-06-15 20:46:01 +00:00
|
|
|
|
executeAfterSet(bean, fields, scanResults)
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}()
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
|
buildAfterProcessors(session, bean)
|
2017-10-01 16:52:35 +00:00
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
|
var tempMap = make(map[string]int)
|
2020-03-22 15:12:55 +00:00
|
|
|
|
var pk schemas.PK
|
2017-01-03 08:20:28 +00:00
|
|
|
|
for ii, key := range fields {
|
|
|
|
|
var idx int
|
|
|
|
|
var ok bool
|
|
|
|
|
var lKey = strings.ToLower(key)
|
|
|
|
|
if idx, ok = tempMap[lKey]; !ok {
|
|
|
|
|
idx = 0
|
2016-11-03 22:16:01 +00:00
|
|
|
|
} else {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
idx = idx + 1
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
tempMap[lKey] = idx
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
fieldValue, err := session.getField(dataStruct, key, table, idx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !strings.Contains(err.Error(), "is not valid") {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.engine.logger.Warnf("%v", err)
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if fieldValue == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResults[ii]))
|
2017-01-03 08:20:28 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
// if row is null then ignore
|
|
|
|
|
if rawValue.Interface() == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if fieldValue.CanAddr() {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
if structConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if data, err := value2Bytes(&rawValue); err == nil {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err := structConvert.FromDB(data); err != nil {
|
|
|
|
|
return nil, err
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-05-02 00:50:33 +00:00
|
|
|
|
return nil, err
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
continue
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
if _, ok := fieldValue.Interface().(convert.Conversion); ok {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if data, err := value2Bytes(&rawValue); err == nil {
|
|
|
|
|
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
|
|
|
|
|
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
fieldValue.Interface().(convert.Conversion).FromDB(data)
|
2018-07-20 02:10:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
2017-02-20 11:33:10 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
rawValueType := reflect.TypeOf(rawValue.Interface())
|
|
|
|
|
vv := reflect.ValueOf(rawValue.Interface())
|
|
|
|
|
col := table.GetColumnIdx(key, idx)
|
|
|
|
|
if col.IsPrimaryKey {
|
|
|
|
|
pk = append(pk, rawValue.Interface())
|
|
|
|
|
}
|
|
|
|
|
fieldType := fieldValue.Type()
|
|
|
|
|
hasAssigned := false
|
|
|
|
|
|
2021-01-05 06:28:51 +00:00
|
|
|
|
if col.IsJSON {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
var bs []byte
|
|
|
|
|
if rawValueType.Kind() == reflect.String {
|
|
|
|
|
bs = []byte(vv.String())
|
2020-03-22 15:12:55 +00:00
|
|
|
|
} else if rawValueType.ConvertibleTo(schemas.BytesType) {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
bs = vv.Bytes()
|
|
|
|
|
} else {
|
|
|
|
|
return nil, fmt.Errorf("unsupported database data type: %s %v", key, rawValueType.Kind())
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if len(bs) > 0 {
|
|
|
|
|
if fieldType.Kind() == reflect.String {
|
|
|
|
|
fieldValue.SetString(string(bs))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if fieldValue.CanAddr() {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2018-01-27 15:20:59 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
x := reflect.New(fieldType)
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
fieldValue.Set(x.Elem())
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
switch fieldType.Kind() {
|
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
|
// TODO: reimplement this
|
|
|
|
|
var bs []byte
|
|
|
|
|
if rawValueType.Kind() == reflect.String {
|
|
|
|
|
bs = []byte(vv.String())
|
2020-03-22 15:12:55 +00:00
|
|
|
|
} else if rawValueType.ConvertibleTo(schemas.BytesType) {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
bs = vv.Bytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
if len(bs) > 0 {
|
|
|
|
|
if fieldValue.CanAddr() {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
x := reflect.New(fieldType)
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
fieldValue.Set(x.Elem())
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
}
|
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
|
switch rawValueType.Kind() {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
case reflect.Slice, reflect.Array:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
switch rawValueType.Elem().Kind() {
|
|
|
|
|
case reflect.Uint8:
|
|
|
|
|
if fieldType.Elem().Kind() == reflect.Uint8 {
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
if col.SQLType.IsText() {
|
|
|
|
|
x := reflect.New(fieldType)
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal(vv.Bytes(), x.Interface())
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
fieldValue.Set(x.Elem())
|
|
|
|
|
} else {
|
|
|
|
|
if fieldValue.Len() > 0 {
|
|
|
|
|
for i := 0; i < fieldValue.Len(); i++ {
|
|
|
|
|
if i < vv.Len() {
|
|
|
|
|
fieldValue.Index(i).Set(vv.Index(i))
|
|
|
|
|
}
|
2017-04-07 01:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
for i := 0; i < vv.Len(); i++ {
|
|
|
|
|
fieldValue.Set(reflect.Append(*fieldValue, vv.Index(i)))
|
2017-04-07 01:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
}
|
|
|
|
|
case reflect.String:
|
|
|
|
|
if rawValueType.Kind() == reflect.String {
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.SetString(vv.String())
|
|
|
|
|
}
|
|
|
|
|
case reflect.Bool:
|
|
|
|
|
if rawValueType.Kind() == reflect.Bool {
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.SetBool(vv.Bool())
|
|
|
|
|
}
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
|
switch rawValueType.Kind() {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.SetInt(vv.Int())
|
|
|
|
|
}
|
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
|
switch rawValueType.Kind() {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
case reflect.Float32, reflect.Float64:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.SetFloat(vv.Float())
|
|
|
|
|
}
|
|
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
|
|
|
|
switch rawValueType.Kind() {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.SetUint(vv.Uint())
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.SetUint(uint64(vv.Int()))
|
|
|
|
|
}
|
|
|
|
|
case reflect.Struct:
|
2020-03-22 15:12:55 +00:00
|
|
|
|
if fieldType.ConvertibleTo(schemas.TimeType) {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
dbTZ := session.engine.DatabaseTZ
|
|
|
|
|
if col.TimeZone != nil {
|
|
|
|
|
dbTZ = col.TimeZone
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
2017-04-07 01:47:25 +00:00
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
if rawValueType == schemas.TimeType {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
t := vv.Convert(schemas.TimeType).Interface().(time.Time)
|
2017-04-07 01:47:25 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
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())
|
|
|
|
|
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
|
|
|
|
|
t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
|
2017-04-07 01:47:25 +00:00
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
t = t.In(session.engine.TZLocation)
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
|
2020-03-22 15:12:55 +00:00
|
|
|
|
} else if rawValueType == schemas.IntType || rawValueType == schemas.Int64Type ||
|
|
|
|
|
rawValueType == schemas.Int32Type {
|
2017-04-07 01:47:25 +00:00
|
|
|
|
hasAssigned = true
|
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
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)
|
2017-04-07 01:47:25 +00:00
|
|
|
|
if err != nil {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.engine.logger.Errorf("byte2Time error: %v", err)
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = false
|
|
|
|
|
} else {
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
|
2017-04-07 01:47:25 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
} else if d, ok := vv.Interface().(string); ok {
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
t, err := session.str2Time(col, d)
|
|
|
|
|
if err != nil {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.engine.logger.Errorf("byte2Time error: %v", err)
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = false
|
2017-04-07 01:47:25 +00:00
|
|
|
|
} else {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
return nil, fmt.Errorf("rawValueType is %v, value is %v", rawValueType, vv.Interface())
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
} 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 {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.engine.logger.Errorf("sql.Sanner error: %v", err)
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = false
|
|
|
|
|
}
|
2021-01-05 06:28:51 +00:00
|
|
|
|
} else if col.IsJSON {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.String {
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
x := reflect.New(fieldType)
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if len([]byte(vv.String())) > 0 {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal([]byte(vv.String()), x.Interface())
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if err != nil {
|
2017-05-02 00:50:33 +00:00
|
|
|
|
return nil, err
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
fieldValue.Set(x.Elem())
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
} else if rawValueType.Kind() == reflect.Slice {
|
2017-01-03 08:20:28 +00:00
|
|
|
|
hasAssigned = true
|
2018-07-20 02:10:17 +00:00
|
|
|
|
x := reflect.New(fieldType)
|
|
|
|
|
if len(vv.Bytes()) > 0 {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal(vv.Bytes(), x.Interface())
|
2017-01-03 08:20:28 +00:00
|
|
|
|
if err != nil {
|
2017-05-02 00:50:33 +00:00
|
|
|
|
return nil, err
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
fieldValue.Set(x.Elem())
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
}
|
|
|
|
|
} else if session.statement.UseCascade {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
table, err := session.engine.tagParser.ParseWithCache(*fieldValue)
|
2017-05-02 00:50:33 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
|
|
|
|
if len(table.PrimaryKeys) != 1 {
|
|
|
|
|
return nil, errors.New("unsupported non or composited primary key cascade")
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
var pk = make(schemas.PK, len(table.PrimaryKeys))
|
2018-07-20 02:10:17 +00:00
|
|
|
|
pk[0], err = asKind(vv, rawValueType)
|
|
|
|
|
if err != nil {
|
2017-05-02 00:50:33 +00:00
|
|
|
|
return nil, err
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
2018-07-20 02:10:17 +00:00
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
|
if !pk.IsZero() {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
// !nashtsai! TODO for hasOne relationship, it's preferred to use join query for eager fetch
|
|
|
|
|
// 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())
|
|
|
|
|
has, err := session.ID(pk).NoCascade().get(structInter.Interface())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if has {
|
|
|
|
|
fieldValue.Set(structInter.Elem())
|
|
|
|
|
} else {
|
|
|
|
|
return nil, errors.New("cascade obj is not exist")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
|
// !nashtsai! TODO merge duplicated codes above
|
|
|
|
|
switch fieldType {
|
|
|
|
|
// following types case matching ptr's native type, therefore assign ptr directly
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrStringType:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.String {
|
|
|
|
|
x := vv.String()
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrBoolType:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Bool {
|
|
|
|
|
x := vv.Bool()
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrTimeType:
|
|
|
|
|
if rawValueType == schemas.PtrTimeType {
|
2018-07-20 02:10:17 +00:00
|
|
|
|
hasAssigned = true
|
|
|
|
|
var x = rawValue.Interface().(time.Time)
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrFloat64Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Float64 {
|
|
|
|
|
x := vv.Float()
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrUint64Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = uint64(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrInt64Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
x := vv.Int()
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrFloat32Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Float64 {
|
|
|
|
|
var x = float32(vv.Float())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrIntType:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = int(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrInt32Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = int32(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrInt8Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = int8(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrInt16Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = int16(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrUintType:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = uint(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.PtrUint32Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = uint32(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.Uint8Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = uint8(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.Uint16Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if rawValueType.Kind() == reflect.Int64 {
|
|
|
|
|
var x = uint16(vv.Int())
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.Complex64Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
var x complex64
|
|
|
|
|
if len([]byte(vv.String())) > 0 {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal([]byte(vv.String()), &x)
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
|
|
|
|
hasAssigned = true
|
2020-03-22 15:12:55 +00:00
|
|
|
|
case schemas.Complex128Type:
|
2018-07-20 02:10:17 +00:00
|
|
|
|
var x complex128
|
|
|
|
|
if len([]byte(vv.String())) > 0 {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
err := json.DefaultJSONHandler.Unmarshal([]byte(vv.String()), &x)
|
2018-07-20 02:10:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(&x))
|
|
|
|
|
}
|
|
|
|
|
hasAssigned = true
|
|
|
|
|
} // switch fieldType
|
|
|
|
|
} // switch fieldType.Kind()
|
|
|
|
|
|
|
|
|
|
// !nashtsai! for value can't be assigned directly fallback to convert to []byte then back to value
|
|
|
|
|
if !hasAssigned {
|
|
|
|
|
data, err := value2Bytes(&rawValue)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = session.bytes2Value(col, fieldValue, data); err != nil {
|
|
|
|
|
return nil, err
|
2016-11-03 22:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-20 11:33:10 +00:00
|
|
|
|
return pk, nil
|
2017-01-03 08:20:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 22:16:01 +00:00
|
|
|
|
// saveLastSQL stores executed query information
|
|
|
|
|
func (session *Session) saveLastSQL(sql string, args ...interface{}) {
|
|
|
|
|
session.lastSQL = sql
|
|
|
|
|
session.lastSQLArgs = args
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LastSQL returns last query information
|
|
|
|
|
func (session *Session) LastSQL() (string, []interface{}) {
|
|
|
|
|
return session.lastSQL, session.lastSQLArgs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unscoped always disable struct tag "deleted"
|
|
|
|
|
func (session *Session) Unscoped() *Session {
|
2020-03-22 15:12:55 +00:00
|
|
|
|
session.statement.SetUnscoped()
|
2016-11-03 22:16:01 +00:00
|
|
|
|
return session
|
|
|
|
|
}
|
2018-12-12 01:01:41 +00:00
|
|
|
|
|
|
|
|
|
func (session *Session) incrVersionFieldValue(fieldValue *reflect.Value) {
|
|
|
|
|
switch fieldValue.Kind() {
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
|
fieldValue.SetInt(fieldValue.Int() + 1)
|
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
|
fieldValue.SetUint(fieldValue.Uint() + 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-22 15:12:55 +00:00
|
|
|
|
|
2021-05-14 19:17:06 +00:00
|
|
|
|
// Context sets the context on this session
|
2020-03-22 15:12:55 +00:00
|
|
|
|
func (session *Session) Context(ctx context.Context) *Session {
|
|
|
|
|
session.ctx = ctx
|
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PingContext test if database is ok
|
|
|
|
|
func (session *Session) PingContext(ctx context.Context) error {
|
|
|
|
|
if session.isAutoClose {
|
|
|
|
|
defer session.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName())
|
|
|
|
|
return session.DB().PingContext(ctx)
|
|
|
|
|
}
|