2017-01-03 03:20:28 -05: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 10:20:59 -05:00
|
|
|
"database/sql"
|
2017-01-03 03:20:28 -05:00
|
|
|
"errors"
|
2018-12-11 20:01:41 -05:00
|
|
|
"fmt"
|
2017-01-03 03:20:28 -05:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
"xorm.io/xorm/caches"
|
|
|
|
"xorm.io/xorm/internal/utils"
|
|
|
|
"xorm.io/xorm/schemas"
|
2017-01-03 03:20:28 -05: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 07:39:52 -04:00
|
|
|
if session.isAutoClose {
|
2017-01-03 03:20:28 -05:00
|
|
|
defer session.Close()
|
|
|
|
}
|
2017-09-13 01:18:22 -04:00
|
|
|
return session.get(bean)
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
func (session *Session) get(bean interface{}) (bool, error) {
|
2019-08-26 22:17:23 -04:00
|
|
|
defer session.resetStatement()
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if session.statement.LastError != nil {
|
|
|
|
return false, session.statement.LastError
|
2019-06-23 11:22:43 -04:00
|
|
|
}
|
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
beanValue := reflect.ValueOf(bean)
|
|
|
|
if beanValue.Kind() != reflect.Ptr {
|
2017-08-22 07:39:52 -04: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")
|
2017-04-06 21:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if beanValue.Elem().Kind() == reflect.Struct {
|
2020-03-22 11:12:55 -04:00
|
|
|
if err := session.statement.SetRefBean(bean); err != nil {
|
2017-05-01 20:50:33 -04:00
|
|
|
return false, err
|
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
|
|
|
var sqlStr string
|
|
|
|
var args []interface{}
|
2017-08-22 07:39:52 -04:00
|
|
|
var err error
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.RawSQL == "" {
|
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
return false, ErrTableNotFound
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
session.statement.Limit(1)
|
2020-03-22 11:12:55 -04:00
|
|
|
sqlStr, args, err = session.statement.GenGetSQL(bean)
|
2017-08-22 07:39:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
2020-03-22 11:12:55 -04:00
|
|
|
sqlStr = session.statement.GenRawSQL()
|
2017-08-22 07:39:52 -04:00
|
|
|
args = session.statement.RawParams
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
table := session.statement.RefTable
|
|
|
|
|
2020-03-22 11:12:55 -04: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 03:20:28 -05:00
|
|
|
has, err := session.cacheGet(bean, sqlStr, args...)
|
|
|
|
if err != ErrCacheFailed {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
context := session.statement.Context
|
2018-12-11 20:01:41 -05:00
|
|
|
if context != nil {
|
|
|
|
res := context.Get(fmt.Sprintf("%v-%v", sqlStr, args))
|
|
|
|
if res != nil {
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Debugf("hit context cache: %s", sqlStr)
|
2019-08-26 22:17:23 -04:00
|
|
|
|
2018-12-11 20:01:41 -05: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 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
func (session *Session) nocacheGet(beanKind reflect.Kind, table *schemas.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
|
2017-09-13 01:18:22 -04:00
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-09-13 01:18:22 -04:00
|
|
|
defer rows.Close()
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
if !rows.Next() {
|
2018-12-11 20:01:41 -05:00
|
|
|
if rows.Err() != nil {
|
|
|
|
return false, rows.Err()
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-27 10:20:59 -05:00
|
|
|
switch bean.(type) {
|
|
|
|
case sql.NullInt64, sql.NullBool, sql.NullFloat64, sql.NullString:
|
|
|
|
return true, rows.Scan(&bean)
|
|
|
|
case *sql.NullInt64, *sql.NullBool, *sql.NullFloat64, *sql.NullString:
|
|
|
|
return true, rows.Scan(bean)
|
2019-08-26 22:17:23 -04:00
|
|
|
case *string:
|
|
|
|
var res sql.NullString
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*string)) = res.String
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *int:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*int)) = int(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *int8:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*int8)) = int8(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *int16:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*int16)) = int16(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *int32:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*int32)) = int32(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *int64:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*int64)) = int64(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *uint:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*uint)) = uint(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *uint8:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*uint8)) = uint8(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *uint16:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*uint16)) = uint16(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *uint32:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*uint32)) = uint32(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *uint64:
|
|
|
|
var res sql.NullInt64
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*uint64)) = uint64(res.Int64)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
case *bool:
|
|
|
|
var res sql.NullBool
|
|
|
|
if err := rows.Scan(&res); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
if res.Valid {
|
|
|
|
*(bean.(*bool)) = res.Bool
|
|
|
|
}
|
|
|
|
return true, nil
|
2018-01-27 10:20:59 -05:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
switch beanKind {
|
|
|
|
case reflect.Struct:
|
2017-09-13 01:18:22 -04:00
|
|
|
fields, err := rows.Columns()
|
2017-08-22 07:39:52 -04:00
|
|
|
if err != nil {
|
2017-09-13 01:18:22 -04:00
|
|
|
// WARN: Alougth rows return true, but get fields failed
|
2017-08-22 07:39:52 -04:00
|
|
|
return true, err
|
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
|
2017-10-01 12:52:35 -04:00
|
|
|
scanResults, err := session.row2Slice(rows, fields, bean)
|
2017-08-22 07:39:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-09-13 01:18:22 -04:00
|
|
|
// close it before covert data
|
|
|
|
rows.Close()
|
2017-08-22 07:39:52 -04:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
dataStruct := utils.ReflectValue(bean)
|
2017-10-01 12:52:35 -04:00
|
|
|
_, err = session.slice2Bean(scanResults, fields, bean, &dataStruct, table)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, session.executeProcessors()
|
2017-08-22 07:39:52 -04:00
|
|
|
case reflect.Slice:
|
2017-09-13 01:18:22 -04:00
|
|
|
err = rows.ScanSlice(bean)
|
2017-08-22 07:39:52 -04:00
|
|
|
case reflect.Map:
|
2017-09-13 01:18:22 -04:00
|
|
|
err = rows.ScanMap(bean)
|
2019-08-26 22:17:23 -04:00
|
|
|
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
|
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
err = rows.Scan(bean)
|
2017-08-22 07:39:52 -04:00
|
|
|
default:
|
2017-09-13 01:18:22 -04:00
|
|
|
err = rows.Scan(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
|
|
|
|
return true, err
|
2017-01-03 03:20:28 -05: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 07:39:52 -04:00
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2020-03-22 11:12:55 -04:00
|
|
|
sqlStr = filter.Do(sqlStr)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
newsql := session.statement.ConvertIDSQL(sqlStr)
|
2017-01-03 03:20:28 -05:00
|
|
|
if newsql == "" {
|
|
|
|
return false, ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
tableName := session.statement.TableName()
|
2020-03-22 11:12:55 -04:00
|
|
|
cacher := session.engine.cacherMgr.GetCacher(tableName)
|
2018-07-19 22:10:17 -04:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Debugf("[cache] Get SQL: %s, %v", newsql, args)
|
2017-08-22 07:39:52 -04:00
|
|
|
table := session.statement.RefTable
|
2020-03-22 11:12:55 -04:00
|
|
|
ids, err := caches.GetCacheSql(cacher, tableName, newsql, args)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
var res = make([]string, len(table.PrimaryKeys))
|
2017-09-13 01:18:22 -04:00
|
|
|
rows, err := session.NoCache().queryRows(newsql, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
err = rows.ScanSlice(&res)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false, ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
var pk schemas.PK = make([]interface{}, len(table.PrimaryKeys))
|
2017-01-03 03:20:28 -05: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 11:12:55 -04: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 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Debugf("[cache] cache hit: %s, %v", newsql, ids)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
|
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
id := ids[0]
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Debugf("[cache] get bean: %s, %v", tableName, id)
|
2017-01-03 03:20:28 -05:00
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
cacheBean := cacher.GetBean(tableName, sid)
|
|
|
|
if cacheBean == nil {
|
|
|
|
cacheBean = bean
|
2017-09-13 01:18:22 -04:00
|
|
|
has, err = session.nocacheGet(reflect.Struct, table, cacheBean, sqlStr, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil || !has {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Debugf("[cache] cache bean: %s, %v, %v", tableName, id, cacheBean)
|
2017-01-03 03:20:28 -05:00
|
|
|
cacher.PutBean(tableName, sid, cacheBean)
|
|
|
|
} else {
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Debugf("[cache] cache hit: %s, %v, %v", tableName, id, cacheBean)
|
2017-01-03 03:20:28 -05:00
|
|
|
has = true
|
|
|
|
}
|
|
|
|
structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean)))
|
|
|
|
|
|
|
|
return has, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|