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"
|
2017-01-03 16:20:28 +08:00
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-xorm/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) get(bean interface{}) (bool, error) {
|
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")
|
2017-04-07 09:47:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if beanValue.Elem().Kind() == reflect.Struct {
|
2017-08-22 19:39:52 +08:00
|
|
|
if err := session.statement.setRefValue(beanValue.Elem()); 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)
|
|
|
|
sqlStr, args, err = session.statement.genGetSQL(bean)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
} else {
|
2017-08-22 19:39:52 +08:00
|
|
|
sqlStr = session.statement.RawSQL
|
|
|
|
args = session.statement.RawParams
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
table := session.statement.RefTable
|
|
|
|
|
2017-04-07 09:47:25 +08:00
|
|
|
if session.canCache() && beanValue.Elem().Kind() == reflect.Struct {
|
2017-09-13 13:18:22 +08:00
|
|
|
if cacher := session.engine.getCacher2(table); cacher != nil &&
|
2017-08-22 19:39:52 +08:00
|
|
|
!session.statement.unscoped {
|
2017-01-03 16:20:28 +08:00
|
|
|
has, err := session.cacheGet(bean, sqlStr, args...)
|
|
|
|
if err != ErrCacheFailed {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
return session.nocacheGet(beanValue.Elem().Kind(), table, bean, sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) nocacheGet(beanKind reflect.Kind, table *core.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
|
|
|
|
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() {
|
2017-08-22 19:39:52 +08:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-01-27 23:20:59 +08: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)
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:39:52 +08:00
|
|
|
switch beanKind {
|
|
|
|
case reflect.Struct:
|
2017-09-13 13:18:22 +08:00
|
|
|
fields, err := rows.Columns()
|
2017-08-22 19:39:52 +08:00
|
|
|
if err != nil {
|
2017-09-13 13:18:22 +08:00
|
|
|
// WARN: Alougth rows return true, but get fields failed
|
2017-08-22 19:39:52 +08:00
|
|
|
return true, err
|
|
|
|
}
|
2017-04-07 09:47:25 +08:00
|
|
|
|
2017-10-02 00:52:35 +08:00
|
|
|
scanResults, err := session.row2Slice(rows, fields, bean)
|
2017-08-22 19:39:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-09-13 13:18:22 +08:00
|
|
|
// close it before covert data
|
|
|
|
rows.Close()
|
2017-08-22 19:39:52 +08:00
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
dataStruct := rValue(bean)
|
2017-10-02 00:52:35 +08:00
|
|
|
_, err = session.slice2Bean(scanResults, fields, bean, &dataStruct, table)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, session.executeProcessors()
|
2017-08-22 19:39:52 +08:00
|
|
|
case reflect.Slice:
|
2017-09-13 13:18:22 +08:00
|
|
|
err = rows.ScanSlice(bean)
|
2017-08-22 19:39:52 +08:00
|
|
|
case reflect.Map:
|
2017-09-13 13:18:22 +08:00
|
|
|
err = rows.ScanMap(bean)
|
2017-08-22 19:39:52 +08:00
|
|
|
default:
|
2017-09-13 13:18:22 +08:00
|
|
|
err = rows.Scan(bean)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-08-22 19:39:52 +08:00
|
|
|
|
|
|
|
return true, err
|
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() {
|
|
|
|
sqlStr = filter.Do(sqlStr, session.engine.dialect, session.statement.RefTable)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-08-22 19:39:52 +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
|
|
|
cacher := session.engine.getCacher2(session.statement.RefTable)
|
|
|
|
tableName := session.statement.TableName()
|
|
|
|
session.engine.logger.Debug("[cacheGet] find sql:", newsql, args)
|
|
|
|
table := session.statement.RefTable
|
2017-09-13 13:18:22 +08:00
|
|
|
ids, err := core.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 {
|
|
|
|
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}
|
2017-08-22 19:39:52 +08:00
|
|
|
session.engine.logger.Debug("[cacheGet] cache ids:", newsql, ids)
|
2017-01-03 16:20:28 +08:00
|
|
|
err = core.PutCacheSql(cacher, ids, tableName, newsql, args)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-13 13:18:22 +08:00
|
|
|
session.engine.logger.Debug("[cacheGet] cache hit sql:", newsql, ids)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
|
structValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
id := ids[0]
|
2017-08-22 19:39:52 +08:00
|
|
|
session.engine.logger.Debug("[cacheGet] get bean:", 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
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:39:52 +08:00
|
|
|
session.engine.logger.Debug("[cacheGet] cache bean:", tableName, id, cacheBean)
|
2017-01-03 16:20:28 +08:00
|
|
|
cacher.PutBean(tableName, sid, cacheBean)
|
|
|
|
} else {
|
2017-08-22 19:39:52 +08:00
|
|
|
session.engine.logger.Debug("[cacheGet] cache hit bean:", 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
|
|
|
|
}
|