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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
"xorm.io/builder"
|
|
|
|
"xorm.io/core"
|
2017-01-03 03:20:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tpStruct = iota
|
|
|
|
tpNonStruct
|
|
|
|
)
|
|
|
|
|
|
|
|
// Find retrieve records from table, condiBeans's non-empty fields
|
|
|
|
// are conditions. beans could be []Struct, []*Struct, map[int64]Struct
|
|
|
|
// map[int64]*Struct
|
|
|
|
func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) 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.find(rowsSlicePtr, condiBean...)
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
// FindAndCount find the results and also return the counts
|
|
|
|
func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
session.autoResetStatement = false
|
|
|
|
err := session.find(rowsSlicePtr, condiBean...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Map {
|
|
|
|
return 0, errors.New("needs a pointer to a slice or a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceElementType := sliceValue.Type().Elem()
|
|
|
|
if sliceElementType.Kind() == reflect.Ptr {
|
|
|
|
sliceElementType = sliceElementType.Elem()
|
|
|
|
}
|
|
|
|
session.autoResetStatement = true
|
|
|
|
|
|
|
|
if session.statement.selectStr != "" {
|
|
|
|
session.statement.selectStr = ""
|
|
|
|
}
|
|
|
|
if session.statement.OrderStr != "" {
|
|
|
|
session.statement.OrderStr = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.Count(reflect.New(sliceElementType).Interface())
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
|
2019-08-26 22:17:23 -04:00
|
|
|
defer session.resetStatement()
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
if session.statement.lastError != nil {
|
|
|
|
return session.statement.lastError
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Map {
|
|
|
|
return errors.New("needs a pointer to a slice or a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceElementType := sliceValue.Type().Elem()
|
|
|
|
|
|
|
|
var tp = tpStruct
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.RefTable == nil {
|
2017-01-03 03:20:28 -05:00
|
|
|
if sliceElementType.Kind() == reflect.Ptr {
|
|
|
|
if sliceElementType.Elem().Kind() == reflect.Struct {
|
|
|
|
pv := reflect.New(sliceElementType.Elem())
|
2018-07-19 22:10:17 -04:00
|
|
|
if err := session.statement.setRefValue(pv); err != nil {
|
2017-05-01 20:50:33 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
|
|
|
tp = tpNonStruct
|
|
|
|
}
|
|
|
|
} else if sliceElementType.Kind() == reflect.Struct {
|
|
|
|
pv := reflect.New(sliceElementType)
|
2018-07-19 22:10:17 -04:00
|
|
|
if err := session.statement.setRefValue(pv); err != nil {
|
2017-05-01 20:50:33 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
|
|
|
tp = tpNonStruct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
var table = session.statement.RefTable
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
var addedTableName = (len(session.statement.JoinStr) > 0)
|
2017-01-03 03:20:28 -05:00
|
|
|
var autoCond builder.Cond
|
|
|
|
if tp == tpStruct {
|
2017-08-22 07:39:52 -04:00
|
|
|
if !session.statement.noAutoCondition && len(condiBean) > 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
var err error
|
2017-08-22 07:39:52 -04:00
|
|
|
autoCond, err = session.statement.buildConds(table, condiBean[0], true, true, false, true, addedTableName)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
2017-08-22 07:39:52 -04:00
|
|
|
return err
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// !oinume! Add "<col> IS NULL" to WHERE whatever condiBean is given.
|
2019-10-17 05:26:49 -04:00
|
|
|
// See https://gitea.com/xorm/xorm/issues/179
|
2017-08-22 07:39:52 -04:00
|
|
|
if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled
|
|
|
|
var colName = session.engine.Quote(col.Name)
|
2017-01-03 03:20:28 -05:00
|
|
|
if addedTableName {
|
2017-08-22 07:39:52 -04:00
|
|
|
var nm = session.statement.TableName()
|
|
|
|
if len(session.statement.TableAlias) > 0 {
|
|
|
|
nm = session.statement.TableAlias
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
colName = session.engine.Quote(nm) + "." + colName
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
|
|
|
|
autoCond = session.engine.CondDeleted(colName)
|
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
|
|
|
|
if session.statement.RawSQL == "" {
|
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
return ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
var columnStr = session.statement.ColumnStr
|
|
|
|
if len(session.statement.selectStr) > 0 {
|
|
|
|
columnStr = session.statement.selectStr
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.JoinStr == "" {
|
2017-01-03 03:20:28 -05:00
|
|
|
if columnStr == "" {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.GroupByStr != "" {
|
2018-12-11 20:01:41 -05:00
|
|
|
columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
columnStr = session.statement.genColumnStr()
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if columnStr == "" {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.statement.GroupByStr != "" {
|
2018-12-11 20:01:41 -05:00
|
|
|
columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
|
|
|
columnStr = "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if columnStr == "" {
|
|
|
|
columnStr = "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
session.statement.cond = session.statement.cond.And(autoCond)
|
|
|
|
condSQL, condArgs, err := builder.ToSQL(session.statement.cond)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
args = append(session.statement.joinArgs, condArgs...)
|
2018-07-19 22:10:17 -04:00
|
|
|
sqlStr, err = session.statement.genSelectSQL(columnStr, condSQL, true, true)
|
2017-08-22 07:39:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
// for mssql and use limit
|
|
|
|
qs := strings.Count(sqlStr, "?")
|
|
|
|
if len(args)*2 == qs {
|
|
|
|
args = append(args, args...)
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
sqlStr = session.statement.RawSQL
|
|
|
|
args = session.statement.RawParams
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if session.canCache() {
|
2018-12-11 20:01:41 -05:00
|
|
|
if cacher := session.engine.getCacher(session.statement.TableName()); cacher != nil &&
|
2017-08-22 07:39:52 -04:00
|
|
|
!session.statement.IsDistinct &&
|
|
|
|
!session.statement.unscoped {
|
2017-01-03 03:20:28 -05:00
|
|
|
err = session.cacheFind(sliceElementType, sqlStr, rowsSlicePtr, args...)
|
|
|
|
if err != ErrCacheFailed {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = nil // !nashtsai! reset err to nil for ErrCacheFailed
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Warn("Cache Find Failed")
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 06:33:10 -05:00
|
|
|
return session.noCacheFind(table, sliceValue, sqlStr, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 06:33:10 -05:00
|
|
|
func (session *Session) noCacheFind(table *core.Table, containerValue reflect.Value, sqlStr string, args ...interface{}) 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 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
|
|
|
fields, err := rows.Columns()
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
var newElemFunc func(fields []string) reflect.Value
|
2017-02-20 06:33:10 -05:00
|
|
|
elemType := containerValue.Type().Elem()
|
2017-04-06 21:47:25 -04:00
|
|
|
var isPointer bool
|
2017-02-20 06:33:10 -05:00
|
|
|
if elemType.Kind() == reflect.Ptr {
|
2017-04-06 21:47:25 -04:00
|
|
|
isPointer = true
|
|
|
|
elemType = elemType.Elem()
|
|
|
|
}
|
|
|
|
if elemType.Kind() == reflect.Ptr {
|
|
|
|
return errors.New("pointer to pointer is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
newElemFunc = func(fields []string) reflect.Value {
|
|
|
|
switch elemType.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
slice := reflect.MakeSlice(elemType, len(fields), len(fields))
|
|
|
|
x := reflect.New(slice.Type())
|
|
|
|
x.Elem().Set(slice)
|
|
|
|
return x
|
|
|
|
case reflect.Map:
|
|
|
|
mp := reflect.MakeMap(elemType)
|
|
|
|
x := reflect.New(mp.Type())
|
|
|
|
x.Elem().Set(mp)
|
|
|
|
return x
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
return reflect.New(elemType)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 06:33:10 -05:00
|
|
|
var containerValueSetFunc func(*reflect.Value, core.PK) error
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-02-20 06:33:10 -05:00
|
|
|
if containerValue.Kind() == reflect.Slice {
|
2017-04-06 21:47:25 -04:00
|
|
|
containerValueSetFunc = func(newValue *reflect.Value, pk core.PK) error {
|
|
|
|
if isPointer {
|
|
|
|
containerValue.Set(reflect.Append(containerValue, newValue.Elem().Addr()))
|
|
|
|
} else {
|
|
|
|
containerValue.Set(reflect.Append(containerValue, newValue.Elem()))
|
2017-02-20 06:33:10 -05:00
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
return nil
|
2017-02-20 06:33:10 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keyType := containerValue.Type().Key()
|
|
|
|
if len(table.PrimaryKeys) == 0 {
|
|
|
|
return errors.New("don't support multiple primary key's map has non-slice key type")
|
|
|
|
}
|
|
|
|
if len(table.PrimaryKeys) > 1 && keyType.Kind() != reflect.Slice {
|
|
|
|
return errors.New("don't support multiple primary key's map has non-slice key type")
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
containerValueSetFunc = func(newValue *reflect.Value, pk core.PK) error {
|
|
|
|
keyValue := reflect.New(keyType)
|
|
|
|
err := convertPKToValue(table, keyValue.Interface(), pk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-20 06:33:10 -05:00
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
if isPointer {
|
|
|
|
containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem().Addr())
|
|
|
|
} else {
|
|
|
|
containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem())
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2017-04-06 21:47:25 -04:00
|
|
|
return nil
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
if elemType.Kind() == reflect.Struct {
|
|
|
|
var newValue = newElemFunc(fields)
|
|
|
|
dataStruct := rValue(newValue.Interface())
|
2017-08-22 07:39:52 -04:00
|
|
|
tb, err := session.engine.autoMapType(dataStruct)
|
2017-04-06 21:47:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-01 12:52:35 -04:00
|
|
|
err = session.rows2Beans(rows, fields, tb, newElemFunc, containerValueSetFunc)
|
|
|
|
rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return session.executeProcessors()
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
for rows.Next() {
|
2017-04-06 21:47:25 -04:00
|
|
|
var newValue = newElemFunc(fields)
|
2017-01-03 03:20:28 -05:00
|
|
|
bean := newValue.Interface()
|
|
|
|
|
2017-04-06 21:47:25 -04:00
|
|
|
switch elemType.Kind() {
|
|
|
|
case reflect.Slice:
|
2017-09-13 01:18:22 -04:00
|
|
|
err = rows.ScanSlice(bean)
|
2017-04-06 21:47:25 -04:00
|
|
|
case reflect.Map:
|
2017-09-13 01:18:22 -04:00
|
|
|
err = rows.ScanMap(bean)
|
2017-04-06 21:47:25 -04:00
|
|
|
default:
|
2017-09-13 01:18:22 -04:00
|
|
|
err = rows.Scan(bean)
|
2017-04-06 21:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-01-03 03:20:28 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-20 06:33:10 -05:00
|
|
|
if err := containerValueSetFunc(&newValue, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-20 06:33:10 -05:00
|
|
|
func convertPKToValue(table *core.Table, dst interface{}, pk core.PK) error {
|
|
|
|
cols := table.PKColumns()
|
|
|
|
if len(cols) == 1 {
|
|
|
|
return convertAssign(dst, pk[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = pk
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
|
|
|
|
if !session.canCache() ||
|
|
|
|
indexNoCase(sqlStr, "having") != -1 ||
|
|
|
|
indexNoCase(sqlStr, "group by") != -1 {
|
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
tableName := session.statement.TableName()
|
|
|
|
cacher := session.engine.getCacher(tableName)
|
|
|
|
if cacher == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
|
|
|
sqlStr = filter.Do(sqlStr, session.engine.dialect, session.statement.RefTable)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
newsql := session.statement.convertIDSQL(sqlStr)
|
2017-01-03 03:20:28 -05:00
|
|
|
if newsql == "" {
|
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
table := session.statement.RefTable
|
2017-01-03 03:20:28 -05:00
|
|
|
ids, err := core.GetCacheSql(cacher, tableName, newsql, args)
|
|
|
|
if err != nil {
|
2017-09-13 01:18:22 -04:00
|
|
|
rows, err := session.queryRows(newsql, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var i int
|
|
|
|
ids = make([]core.PK, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
i++
|
|
|
|
if i > 500 {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheFind] ids length > 500, no cache")
|
2017-01-03 03:20:28 -05:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
var res = make([]string, len(table.PrimaryKeys))
|
|
|
|
err = rows.ScanSlice(&res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
|
|
|
|
for i, col := range table.PKColumns() {
|
2017-08-22 07:39:52 -04:00
|
|
|
pk[i], err = session.engine.idTypeAssertion(col, res[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ids = append(ids, pk)
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
session.engine.logger.Debug("[cacheFind] cache sql:", ids, tableName, sqlStr, newsql, args)
|
2017-01-03 03:20:28 -05:00
|
|
|
err = core.PutCacheSql(cacher, ids, tableName, newsql, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-13 01:18:22 -04:00
|
|
|
session.engine.logger.Debug("[cacheFind] cache hit sql:", tableName, sqlStr, newsql, args)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
|
|
|
|
ididxes := make(map[string]int)
|
|
|
|
var ides []core.PK
|
|
|
|
var temps = make([]interface{}, len(ids))
|
|
|
|
|
|
|
|
for idx, id := range ids {
|
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bean := cacher.GetBean(tableName, sid)
|
2017-09-13 01:18:22 -04:00
|
|
|
if bean == nil || reflect.ValueOf(bean).Elem().Type() != t {
|
2017-01-03 03:20:28 -05:00
|
|
|
ides = append(ides, id)
|
|
|
|
ididxes[sid] = idx
|
|
|
|
} else {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheFind] cache hit bean:", tableName, id, bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
pk := session.engine.IdOf(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
xid, err := pk.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sid != xid {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Error("[cacheFind] error cache", xid, sid, bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
temps[idx] = bean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ides) > 0 {
|
|
|
|
slices := reflect.New(reflect.SliceOf(t))
|
|
|
|
beans := slices.Interface()
|
|
|
|
|
|
|
|
if len(table.PrimaryKeys) == 1 {
|
|
|
|
ff := make([]interface{}, 0, len(ides))
|
|
|
|
for _, ie := range ides {
|
|
|
|
ff = append(ff, ie[0])
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
session.In("`"+table.PrimaryKeys[0]+"`", ff...)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else {
|
|
|
|
for _, ie := range ides {
|
|
|
|
cond := builder.NewCond()
|
|
|
|
for i, name := range table.PrimaryKeys {
|
|
|
|
cond = cond.And(builder.Eq{"`" + name + "`": ie[i]})
|
|
|
|
}
|
2017-09-13 01:18:22 -04:00
|
|
|
session.Or(cond)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:18:22 -04:00
|
|
|
err = session.NoCache().Table(tableName).find(beans)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vs := reflect.Indirect(reflect.ValueOf(beans))
|
|
|
|
for i := 0; i < vs.Len(); i++ {
|
|
|
|
rv := vs.Index(i)
|
|
|
|
if rv.Kind() != reflect.Ptr {
|
|
|
|
rv = rv.Addr()
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
id, err := session.engine.idOfV(rv)
|
2017-04-06 21:47:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bean := rv.Interface()
|
|
|
|
temps[ididxes[sid]] = bean
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Debug("[cacheFind] cache bean:", tableName, id, bean, temps)
|
2017-01-03 03:20:28 -05:00
|
|
|
cacher.PutBean(tableName, sid, bean)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < len(temps); j++ {
|
|
|
|
bean := temps[j]
|
|
|
|
if bean == nil {
|
2017-08-22 07:39:52 -04:00
|
|
|
session.engine.logger.Warn("[cacheFind] cache no hit:", tableName, ids[j], temps)
|
2017-01-03 03:20:28 -05:00
|
|
|
// return errors.New("cache error") // !nashtsai! no need to return error, but continue instead
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sliceValue.Kind() == reflect.Slice {
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(bean)))
|
|
|
|
} else {
|
|
|
|
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(bean))))
|
|
|
|
}
|
|
|
|
} else if sliceValue.Kind() == reflect.Map {
|
|
|
|
var key = ids[j]
|
|
|
|
keyType := sliceValue.Type().Key()
|
|
|
|
var ikey interface{}
|
|
|
|
if len(key) == 1 {
|
|
|
|
ikey, err = str2PK(fmt.Sprintf("%v", key[0]), keyType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if keyType.Kind() != reflect.Slice {
|
|
|
|
return errors.New("table have multiple primary keys, key is not core.PK or slice")
|
|
|
|
}
|
|
|
|
ikey = key
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
sliceValue.SetMapIndex(reflect.ValueOf(ikey), reflect.ValueOf(bean))
|
|
|
|
} else {
|
|
|
|
sliceValue.SetMapIndex(reflect.ValueOf(ikey), reflect.Indirect(reflect.ValueOf(bean)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|