2017-01-03 16:20:28 +08:00
|
|
|
// Copyright 2016 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 (
|
2018-01-27 23:20:59 +08:00
|
|
|
"database/sql"
|
2021-08-28 19:16:19 +08:00
|
|
|
"database/sql/driver"
|
2017-01-03 16:20:28 +08:00
|
|
|
"errors"
|
2018-12-12 09:01:41 +08:00
|
|
|
"fmt"
|
2021-08-28 19:16:19 +08:00
|
|
|
"math/big"
|
2017-01-03 16:20:28 +08:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2021-08-28 19:16:19 +08:00
|
|
|
"time"
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
"xorm.io/xorm/caches"
|
2021-08-28 19:16:19 +08:00
|
|
|
"xorm.io/xorm/convert"
|
|
|
|
"xorm.io/xorm/core"
|
2020-03-22 23:12:55 +08:00
|
|
|
"xorm.io/xorm/internal/utils"
|
|
|
|
"xorm.io/xorm/schemas"
|
2017-01-03 16:20:28 +08:00
|
|
|
)
|
|
|
|
|
2021-05-15 03:17:06 +08:00
|
|
|
var (
|
|
|
|
// ErrObjectIsNil return error of object is nil
|
|
|
|
ErrObjectIsNil = errors.New("object should not be nil")
|
|
|
|
)
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
// Get retrieve one record from database, bean's non-empty fields
|
|
|
|
// will be as conditions
|
|
|
|
func (session *Session) Get(bean interface{}) (bool, error) {
|
2017-08-22 19:39:52 +08:00
|
|
|
if session.isAutoClose {
|
2017-01-03 16:20:28 +08:00
|
|
|
defer session.Close()
|
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
return session.get(bean)
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
func isPtrOfTime(v interface{}) bool {
|
|
|
|
if _, ok := v.(*time.Time); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
el := reflect.ValueOf(v).Elem()
|
|
|
|
if el.Kind() != reflect.Struct {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.Type().ConvertibleTo(schemas.TimeType)
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) get(bean interface{}) (bool, error) {
|
2019-08-27 10:17:23 +08:00
|
|
|
defer session.resetStatement()
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if session.statement.LastError != nil {
|
|
|
|
return false, session.statement.LastError
|
2019-06-23 23:22:43 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 09:47:25 +08:00
|
|
|
beanValue := reflect.ValueOf(bean)
|
|
|
|
if beanValue.Kind() != reflect.Ptr {
|
2017-08-22 19:39:52 +08:00
|
|
|
return false, errors.New("needs a pointer to a value")
|
|
|
|
} else if beanValue.Elem().Kind() == reflect.Ptr {
|
|
|
|
return false, errors.New("a pointer to a pointer is not allowed")
|
2021-05-15 03:17:06 +08:00
|
|
|
} else if beanValue.IsNil() {
|
|
|
|
return false, ErrObjectIsNil
|
2017-04-07 09:47:25 +08:00
|
|
|
}
|
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
if beanValue.Elem().Kind() == reflect.Struct && !isPtrOfTime(bean) {
|
2020-03-22 23:12:55 +08:00
|
|
|
if err := session.statement.SetRefBean(bean); err != nil {
|
2017-05-02 08:50:33 +08:00
|
|
|
return false, err
|
|
|
|
}
|
2017-04-07 09:47:25 +08:00
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
|
|
|
var sqlStr string
|
|
|
|
var args []interface{}
|
2017-08-22 19:39:52 +08:00
|
|
|
var err error
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2017-08-22 19:39:52 +08:00
|
|
|
if session.statement.RawSQL == "" {
|
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 16:20:28 +08:00
|
|
|
return false, ErrTableNotFound
|
|
|
|
}
|
2017-08-22 19:39:52 +08:00
|
|
|
session.statement.Limit(1)
|
2020-03-22 23:12:55 +08:00
|
|
|
sqlStr, args, err = session.statement.GenGetSQL(bean)
|
2017-08-22 19:39:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
sqlStr = session.statement.GenRawSQL()
|
2017-08-22 19:39:52 +08:00
|
|
|
args = session.statement.RawParams
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
table := session.statement.RefTable
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if session.statement.ColumnMap.IsEmpty() && session.canCache() && beanValue.Elem().Kind() == reflect.Struct {
|
|
|
|
if cacher := session.engine.GetCacher(session.statement.TableName()); cacher != nil &&
|
|
|
|
!session.statement.GetUnscoped() {
|
2017-01-03 16:20:28 +08:00
|
|
|
has, err := session.cacheGet(bean, sqlStr, args...)
|
|
|
|
if err != ErrCacheFailed {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
context := session.statement.Context
|
2018-12-12 09:01:41 +08:00
|
|
|
if context != nil {
|
|
|
|
res := context.Get(fmt.Sprintf("%v-%v", sqlStr, args))
|
|
|
|
if res != nil {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("hit context cache: %s", sqlStr)
|
2019-08-27 10:17:23 +08:00
|
|
|
|
2018-12-12 09:01:41 +08:00
|
|
|
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
structValue.Set(reflect.Indirect(reflect.ValueOf(res)))
|
|
|
|
session.lastSQL = ""
|
|
|
|
session.lastSQLArgs = nil
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
has, err := session.nocacheGet(beanValue.Elem().Kind(), table, bean, sqlStr, args...)
|
|
|
|
if err != nil || !has {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if context != nil {
|
|
|
|
context.Put(fmt.Sprintf("%v-%v", sqlStr, args), bean)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
var (
|
|
|
|
valuerTypePlaceHolder driver.Valuer
|
|
|
|
valuerType = reflect.TypeOf(&valuerTypePlaceHolder).Elem()
|
|
|
|
|
|
|
|
scannerTypePlaceHolder sql.Scanner
|
|
|
|
scannerType = reflect.TypeOf(&scannerTypePlaceHolder).Elem()
|
|
|
|
|
|
|
|
conversionTypePlaceHolder convert.Conversion
|
|
|
|
conversionType = reflect.TypeOf(&conversionTypePlaceHolder).Elem()
|
|
|
|
)
|
|
|
|
|
|
|
|
func isScannableStruct(bean interface{}, typeLen int) bool {
|
|
|
|
switch bean.(type) {
|
|
|
|
case *time.Time:
|
|
|
|
return false
|
|
|
|
case sql.Scanner:
|
|
|
|
return false
|
|
|
|
case convert.Conversion:
|
|
|
|
return typeLen > 1
|
|
|
|
case *big.Float:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (session *Session) nocacheGet(beanKind reflect.Kind, table *schemas.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
|
2017-09-13 13:18:22 +08:00
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
defer rows.Close()
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
if !rows.Next() {
|
2021-08-28 19:16:19 +08:00
|
|
|
return false, rows.Err()
|
2017-08-22 19:39:52 +08:00
|
|
|
}
|
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
// WARN: Alougth rows return true, but we may also return error.
|
|
|
|
types, err := rows.ColumnTypes()
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
fields, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
switch beanKind {
|
|
|
|
case reflect.Struct:
|
|
|
|
if !isScannableStruct(bean, len(types)) {
|
|
|
|
break
|
2019-08-27 10:17:23 +08:00
|
|
|
}
|
2021-08-28 19:16:19 +08:00
|
|
|
return session.getStruct(rows, types, fields, table, bean)
|
|
|
|
case reflect.Slice:
|
|
|
|
return session.getSlice(rows, types, fields, bean)
|
|
|
|
case reflect.Map:
|
|
|
|
return session.getMap(rows, types, fields, bean)
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.getVars(rows, types, fields, bean)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) getSlice(rows *core.Rows, types []*sql.ColumnType, fields []string, bean interface{}) (bool, error) {
|
|
|
|
switch t := bean.(type) {
|
|
|
|
case *[]string:
|
|
|
|
res, err := session.engine.scanStringInterface(rows, fields, types)
|
|
|
|
if err != nil {
|
2019-08-27 10:17:23 +08:00
|
|
|
return true, err
|
|
|
|
}
|
2021-08-28 19:16:19 +08:00
|
|
|
|
|
|
|
var needAppend = len(*t) == 0 // both support slice is empty or has been initlized
|
|
|
|
for i, r := range res {
|
|
|
|
if needAppend {
|
|
|
|
*t = append(*t, r.(*sql.NullString).String)
|
|
|
|
} else {
|
|
|
|
(*t)[i] = r.(*sql.NullString).String
|
|
|
|
}
|
2019-08-27 10:17:23 +08:00
|
|
|
}
|
|
|
|
return true, nil
|
2021-08-28 19:16:19 +08:00
|
|
|
case *[]interface{}:
|
|
|
|
scanResults, err := session.engine.scanInterfaces(rows, fields, types)
|
|
|
|
if err != nil {
|
2019-08-27 10:17:23 +08:00
|
|
|
return true, err
|
|
|
|
}
|
2021-08-28 19:16:19 +08:00
|
|
|
var needAppend = len(*t) == 0
|
|
|
|
for ii := range fields {
|
|
|
|
s, err := convert.Interface2Interface(session.engine.DatabaseTZ, scanResults[ii])
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if needAppend {
|
|
|
|
*t = append(*t, s)
|
|
|
|
} else {
|
|
|
|
(*t)[ii] = s
|
|
|
|
}
|
2019-08-27 10:17:23 +08:00
|
|
|
}
|
|
|
|
return true, nil
|
2021-08-28 19:16:19 +08:00
|
|
|
default:
|
|
|
|
return true, fmt.Errorf("unspoorted slice type: %t", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) getMap(rows *core.Rows, types []*sql.ColumnType, fields []string, bean interface{}) (bool, error) {
|
|
|
|
switch t := bean.(type) {
|
|
|
|
case *map[string]string:
|
|
|
|
scanResults, err := session.engine.scanStringInterface(rows, fields, types)
|
|
|
|
if err != nil {
|
2019-08-27 10:17:23 +08:00
|
|
|
return true, err
|
|
|
|
}
|
2021-08-28 19:16:19 +08:00
|
|
|
for ii, key := range fields {
|
|
|
|
(*t)[key] = scanResults[ii].(*sql.NullString).String
|
2019-08-27 10:17:23 +08:00
|
|
|
}
|
|
|
|
return true, nil
|
2021-08-28 19:16:19 +08:00
|
|
|
case *map[string]interface{}:
|
|
|
|
scanResults, err := session.engine.scanInterfaces(rows, fields, types)
|
|
|
|
if err != nil {
|
2019-08-27 10:17:23 +08:00
|
|
|
return true, err
|
|
|
|
}
|
2021-08-28 19:16:19 +08:00
|
|
|
for ii, key := range fields {
|
|
|
|
s, err := convert.Interface2Interface(session.engine.DatabaseTZ, scanResults[ii])
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
(*t)[key] = s
|
2019-08-27 10:17:23 +08:00
|
|
|
}
|
|
|
|
return true, nil
|
2021-08-28 19:16:19 +08:00
|
|
|
default:
|
|
|
|
return true, fmt.Errorf("unspoorted map type: %t", t)
|
2018-01-27 23:20:59 +08:00
|
|
|
}
|
2021-08-28 19:16:19 +08:00
|
|
|
}
|
2018-01-27 23:20:59 +08:00
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
func (session *Session) getVars(rows *core.Rows, types []*sql.ColumnType, fields []string, beans ...interface{}) (bool, error) {
|
|
|
|
if len(beans) != len(types) {
|
|
|
|
return false, fmt.Errorf("expected columns %d, but only %d variables", len(types), len(beans))
|
|
|
|
}
|
2017-04-07 09:47:25 +08:00
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
err := session.engine.scan(rows, fields, types, beans...)
|
|
|
|
return true, err
|
|
|
|
}
|
2017-08-22 19:39:52 +08:00
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
func (session *Session) getStruct(rows *core.Rows, types []*sql.ColumnType, fields []string, table *schemas.Table, bean interface{}) (bool, error) {
|
|
|
|
scanResults, err := session.row2Slice(rows, fields, types, bean)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// close it before convert data
|
|
|
|
rows.Close()
|
2017-10-02 00:52:35 +08:00
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
dataStruct := utils.ReflectValue(bean)
|
|
|
|
_, err = session.slice2Bean(scanResults, fields, bean, &dataStruct, table)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-08-22 19:39:52 +08:00
|
|
|
|
2021-08-28 19:16:19 +08:00
|
|
|
return true, session.executeProcessors()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) {
|
|
|
|
// if has no reftable, then don't use cache currently
|
|
|
|
if !session.canCache() {
|
|
|
|
return false, ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:39:52 +08:00
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2020-03-22 23:12:55 +08:00
|
|
|
sqlStr = filter.Do(sqlStr)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
newsql := session.statement.ConvertIDSQL(sqlStr)
|
2017-01-03 16:20:28 +08:00
|
|
|
if newsql == "" {
|
|
|
|
return false, ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:39:52 +08:00
|
|
|
tableName := session.statement.TableName()
|
2020-03-22 23:12:55 +08:00
|
|
|
cacher := session.engine.cacherMgr.GetCacher(tableName)
|
2018-07-20 10:10:17 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] Get SQL: %s, %v", newsql, args)
|
2017-08-22 19:39:52 +08:00
|
|
|
table := session.statement.RefTable
|
2020-03-22 23:12:55 +08:00
|
|
|
ids, err := caches.GetCacheSql(cacher, tableName, newsql, args)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
var res = make([]string, len(table.PrimaryKeys))
|
2017-09-13 13:18:22 +08:00
|
|
|
rows, err := session.NoCache().queryRows(newsql, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
err = rows.ScanSlice(&res)
|
|
|
|
if err != nil {
|
2021-08-28 19:16:19 +08:00
|
|
|
return true, err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-28 19:16:19 +08:00
|
|
|
if rows.Err() != nil {
|
|
|
|
return false, rows.Err()
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
return false, ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
var pk schemas.PK = make([]interface{}, len(table.PrimaryKeys))
|
2017-01-03 16:20:28 +08:00
|
|
|
for i, col := range table.PKColumns() {
|
|
|
|
if col.SQLType.IsText() {
|
|
|
|
pk[i] = res[i]
|
|
|
|
} else if col.SQLType.IsNumeric() {
|
|
|
|
n, err := strconv.ParseInt(res[i], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
pk[i] = n
|
|
|
|
} else {
|
|
|
|
return false, errors.New("unsupported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
ids = []schemas.PK{pk}
|
|
|
|
session.engine.logger.Debugf("[cache] cache ids: %s, %v", newsql, ids)
|
|
|
|
err = caches.PutCacheSql(cacher, ids, tableName, newsql, args)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache hit: %s, %v", newsql, ids)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
|
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
id := ids[0]
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] get bean: %s, %v", tableName, id)
|
2017-01-03 16:20:28 +08:00
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
cacheBean := cacher.GetBean(tableName, sid)
|
|
|
|
if cacheBean == nil {
|
|
|
|
cacheBean = bean
|
2017-09-13 13:18:22 +08:00
|
|
|
has, err = session.nocacheGet(reflect.Struct, table, cacheBean, sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil || !has {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache bean: %s, %v, %v", tableName, id, cacheBean)
|
2017-01-03 16:20:28 +08:00
|
|
|
cacher.PutBean(tableName, sid, cacheBean)
|
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache hit: %s, %v, %v", tableName, id, cacheBean)
|
2017-01-03 16:20:28 +08:00
|
|
|
has = true
|
|
|
|
}
|
|
|
|
structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean)))
|
|
|
|
|
|
|
|
return has, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|