You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
8.4 KiB
357 lines
8.4 KiB
8 years ago
|
// 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 (
|
||
7 years ago
|
"database/sql"
|
||
8 years ago
|
"errors"
|
||
6 years ago
|
"fmt"
|
||
8 years ago
|
"reflect"
|
||
|
"strconv"
|
||
|
|
||
6 years ago
|
"xorm.io/core"
|
||
8 years ago
|
)
|
||
|
|
||
|
// Get retrieve one record from database, bean's non-empty fields
|
||
|
// will be as conditions
|
||
|
func (session *Session) Get(bean interface{}) (bool, error) {
|
||
7 years ago
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
7 years ago
|
return session.get(bean)
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
func (session *Session) get(bean interface{}) (bool, error) {
|
||
5 years ago
|
defer session.resetStatement()
|
||
|
|
||
6 years ago
|
if session.statement.lastError != nil {
|
||
|
return false, session.statement.lastError
|
||
|
}
|
||
|
|
||
8 years ago
|
beanValue := reflect.ValueOf(bean)
|
||
|
if beanValue.Kind() != reflect.Ptr {
|
||
7 years ago
|
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")
|
||
8 years ago
|
}
|
||
|
|
||
|
if beanValue.Elem().Kind() == reflect.Struct {
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
8 years ago
|
return false, err
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
var sqlStr string
|
||
|
var args []interface{}
|
||
7 years ago
|
var err error
|
||
8 years ago
|
|
||
7 years ago
|
if session.statement.RawSQL == "" {
|
||
|
if len(session.statement.TableName()) <= 0 {
|
||
8 years ago
|
return false, ErrTableNotFound
|
||
|
}
|
||
7 years ago
|
session.statement.Limit(1)
|
||
|
sqlStr, args, err = session.statement.genGetSQL(bean)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
8 years ago
|
} else {
|
||
7 years ago
|
sqlStr = session.statement.RawSQL
|
||
|
args = session.statement.RawParams
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
table := session.statement.RefTable
|
||
|
|
||
8 years ago
|
if session.canCache() && beanValue.Elem().Kind() == reflect.Struct {
|
||
6 years ago
|
if cacher := session.engine.getCacher(session.statement.TableName()); cacher != nil &&
|
||
7 years ago
|
!session.statement.unscoped {
|
||
8 years ago
|
has, err := session.cacheGet(bean, sqlStr, args...)
|
||
|
if err != ErrCacheFailed {
|
||
|
return has, err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
context := session.statement.context
|
||
|
if context != nil {
|
||
|
res := context.Get(fmt.Sprintf("%v-%v", sqlStr, args))
|
||
|
if res != nil {
|
||
5 years ago
|
session.engine.logger.Debug("hit context cache", sqlStr)
|
||
|
|
||
6 years ago
|
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
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
func (session *Session) nocacheGet(beanKind reflect.Kind, table *core.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
|
||
|
rows, err := session.queryRows(sqlStr, args...)
|
||
8 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
7 years ago
|
defer rows.Close()
|
||
8 years ago
|
|
||
7 years ago
|
if !rows.Next() {
|
||
6 years ago
|
if rows.Err() != nil {
|
||
|
return false, rows.Err()
|
||
|
}
|
||
7 years ago
|
return false, nil
|
||
|
}
|
||
|
|
||
7 years ago
|
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)
|
||
5 years ago
|
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
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
switch beanKind {
|
||
|
case reflect.Struct:
|
||
7 years ago
|
fields, err := rows.Columns()
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
// WARN: Alougth rows return true, but get fields failed
|
||
7 years ago
|
return true, err
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
scanResults, err := session.row2Slice(rows, fields, bean)
|
||
7 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
7 years ago
|
// close it before covert data
|
||
|
rows.Close()
|
||
7 years ago
|
|
||
7 years ago
|
dataStruct := rValue(bean)
|
||
7 years ago
|
_, err = session.slice2Bean(scanResults, fields, bean, &dataStruct, table)
|
||
|
if err != nil {
|
||
|
return true, err
|
||
|
}
|
||
|
|
||
|
return true, session.executeProcessors()
|
||
7 years ago
|
case reflect.Slice:
|
||
7 years ago
|
err = rows.ScanSlice(bean)
|
||
7 years ago
|
case reflect.Map:
|
||
7 years ago
|
err = rows.ScanMap(bean)
|
||
5 years ago
|
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)
|
||
7 years ago
|
default:
|
||
7 years ago
|
err = rows.Scan(bean)
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
|
return true, err
|
||
8 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
7 years ago
|
for _, filter := range session.engine.dialect.Filters() {
|
||
|
sqlStr = filter.Do(sqlStr, session.engine.dialect, session.statement.RefTable)
|
||
8 years ago
|
}
|
||
7 years ago
|
newsql := session.statement.convertIDSQL(sqlStr)
|
||
8 years ago
|
if newsql == "" {
|
||
|
return false, ErrCacheFailed
|
||
|
}
|
||
|
|
||
7 years ago
|
tableName := session.statement.TableName()
|
||
6 years ago
|
cacher := session.engine.getCacher(tableName)
|
||
|
|
||
7 years ago
|
session.engine.logger.Debug("[cacheGet] find sql:", newsql, args)
|
||
|
table := session.statement.RefTable
|
||
7 years ago
|
ids, err := core.GetCacheSql(cacher, tableName, newsql, args)
|
||
8 years ago
|
if err != nil {
|
||
|
var res = make([]string, len(table.PrimaryKeys))
|
||
7 years ago
|
rows, err := session.NoCache().queryRows(newsql, args...)
|
||
8 years ago
|
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
|
||
|
}
|
||
|
|
||
|
var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
|
||
|
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")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ids = []core.PK{pk}
|
||
7 years ago
|
session.engine.logger.Debug("[cacheGet] cache ids:", newsql, ids)
|
||
8 years ago
|
err = core.PutCacheSql(cacher, ids, tableName, newsql, args)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
} else {
|
||
7 years ago
|
session.engine.logger.Debug("[cacheGet] cache hit sql:", newsql, ids)
|
||
8 years ago
|
}
|
||
|
|
||
|
if len(ids) > 0 {
|
||
|
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
||
|
id := ids[0]
|
||
7 years ago
|
session.engine.logger.Debug("[cacheGet] get bean:", tableName, id)
|
||
8 years ago
|
sid, err := id.ToString()
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
cacheBean := cacher.GetBean(tableName, sid)
|
||
|
if cacheBean == nil {
|
||
|
cacheBean = bean
|
||
7 years ago
|
has, err = session.nocacheGet(reflect.Struct, table, cacheBean, sqlStr, args...)
|
||
8 years ago
|
if err != nil || !has {
|
||
|
return has, err
|
||
|
}
|
||
|
|
||
7 years ago
|
session.engine.logger.Debug("[cacheGet] cache bean:", tableName, id, cacheBean)
|
||
8 years ago
|
cacher.PutBean(tableName, sid, cacheBean)
|
||
|
} else {
|
||
7 years ago
|
session.engine.logger.Debug("[cacheGet] cache hit bean:", tableName, id, cacheBean)
|
||
8 years ago
|
has = true
|
||
|
}
|
||
|
structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean)))
|
||
|
|
||
|
return has, nil
|
||
|
}
|
||
|
return false, nil
|
||
|
}
|