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 (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
|
2019-06-23 23:22:43 +08:00
|
|
|
"xorm.io/builder"
|
2020-03-22 23:12:55 +08:00
|
|
|
"xorm.io/xorm/caches"
|
2021-08-13 07:11:42 +08:00
|
|
|
"xorm.io/xorm/convert"
|
2020-03-25 22:32:23 +08:00
|
|
|
"xorm.io/xorm/internal/statements"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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 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.find(rowsSlicePtr, condiBean...)
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2018-07-20 10:10:17 +08: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
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if session.statement.SelectStr != "" {
|
|
|
|
session.statement.SelectStr = ""
|
2018-07-20 10:10:17 +08:00
|
|
|
}
|
2021-01-05 14:28:51 +08:00
|
|
|
if len(session.statement.ColumnMap) > 0 {
|
|
|
|
session.statement.ColumnMap = []string{}
|
|
|
|
}
|
2018-07-20 10:10:17 +08:00
|
|
|
if session.statement.OrderStr != "" {
|
|
|
|
session.statement.OrderStr = ""
|
|
|
|
}
|
2020-06-16 04:46:01 +08:00
|
|
|
if session.statement.LimitN != nil {
|
|
|
|
session.statement.LimitN = nil
|
|
|
|
}
|
|
|
|
if session.statement.Start > 0 {
|
|
|
|
session.statement.Start = 0
|
|
|
|
}
|
2018-07-20 10:10:17 +08:00
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
// session has stored the conditions so we use `unscoped` to avoid duplicated condition.
|
|
|
|
return session.Unscoped().Count(reflect.New(sliceElementType).Interface())
|
2018-07-20 10:10:17 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) 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 session.statement.LastError
|
2019-06-23 23:22:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
2020-03-22 23:12:55 +08:00
|
|
|
var isSlice = sliceValue.Kind() == reflect.Slice
|
|
|
|
var isMap = sliceValue.Kind() == reflect.Map
|
|
|
|
if !isSlice && !isMap {
|
2017-01-03 16:20:28 +08:00
|
|
|
return errors.New("needs a pointer to a slice or a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceElementType := sliceValue.Type().Elem()
|
|
|
|
|
|
|
|
var tp = tpStruct
|
2017-08-22 19:39:52 +08:00
|
|
|
if session.statement.RefTable == nil {
|
2017-01-03 16:20:28 +08:00
|
|
|
if sliceElementType.Kind() == reflect.Ptr {
|
|
|
|
if sliceElementType.Elem().Kind() == reflect.Struct {
|
|
|
|
pv := reflect.New(sliceElementType.Elem())
|
2020-03-22 23:12:55 +08:00
|
|
|
if err := session.statement.SetRefValue(pv); err != nil {
|
2017-05-02 08:50:33 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
} else {
|
|
|
|
tp = tpNonStruct
|
|
|
|
}
|
|
|
|
} else if sliceElementType.Kind() == reflect.Struct {
|
|
|
|
pv := reflect.New(sliceElementType)
|
2020-03-22 23:12:55 +08:00
|
|
|
if err := session.statement.SetRefValue(pv); err != nil {
|
2017-05-02 08:50:33 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
} else {
|
|
|
|
tp = tpNonStruct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
var (
|
|
|
|
table = session.statement.RefTable
|
|
|
|
addedTableName = (len(session.statement.JoinStr) > 0)
|
|
|
|
autoCond builder.Cond
|
|
|
|
)
|
2017-01-03 16:20:28 +08:00
|
|
|
if tp == tpStruct {
|
2020-03-22 23:12:55 +08:00
|
|
|
if !session.statement.NoAutoCondition && len(condiBean) > 0 {
|
2020-06-16 04:46:01 +08:00
|
|
|
condTable, err := session.engine.tagParser.Parse(reflect.ValueOf(condiBean[0]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
autoCond, err = session.statement.BuildConds(condTable, condiBean[0], true, true, false, true, addedTableName)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
2017-08-22 19:39:52 +08:00
|
|
|
return err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
if col := table.DeletedColumn(); col != nil && !session.statement.GetUnscoped() { // tag "deleted" is enabled
|
|
|
|
autoCond = session.statement.CondDeleted(col)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
// if it's a map with Cols but primary key not in column list, we still need the primary key
|
|
|
|
if isMap && !session.statement.ColumnMap.IsEmpty() {
|
|
|
|
for _, k := range session.statement.RefTable.PrimaryKeys {
|
|
|
|
session.statement.ColumnMap.Add(k)
|
2017-08-22 19:39:52 +08:00
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
sqlStr, args, err := session.statement.GenFindSQL(autoCond)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if session.statement.ColumnMap.IsEmpty() && session.canCache() {
|
|
|
|
if cacher := session.engine.GetCacher(session.statement.TableName()); cacher != nil &&
|
2017-08-22 19:39:52 +08:00
|
|
|
!session.statement.IsDistinct &&
|
2020-03-22 23:12:55 +08:00
|
|
|
!session.statement.GetUnscoped() {
|
2017-01-03 16:20:28 +08:00
|
|
|
err = session.cacheFind(sliceElementType, sqlStr, rowsSlicePtr, args...)
|
|
|
|
if err != ErrCacheFailed {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = nil // !nashtsai! reset err to nil for ErrCacheFailed
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Warnf("Cache Find Failed")
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:33:10 +08:00
|
|
|
return session.noCacheFind(table, sliceValue, sqlStr, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect.Value, sqlStr string, args ...interface{}) error {
|
2021-09-17 01:53:28 +08:00
|
|
|
elemType := containerValue.Type().Elem()
|
|
|
|
var isPointer bool
|
|
|
|
if elemType.Kind() == reflect.Ptr {
|
|
|
|
isPointer = true
|
|
|
|
elemType = elemType.Elem()
|
|
|
|
}
|
|
|
|
if elemType.Kind() == reflect.Ptr {
|
|
|
|
return errors.New("pointer to pointer is not supported")
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
fields, err := rows.Columns()
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-13 07:11:42 +08:00
|
|
|
types, err := rows.ColumnTypes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-17 01:53:28 +08:00
|
|
|
var newElemFunc = func(fields []string) reflect.Value {
|
|
|
|
return utils.New(elemType, len(fields), len(fields))
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
var containerValueSetFunc func(*reflect.Value, schemas.PK) error
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2017-02-20 19:33:10 +08:00
|
|
|
if containerValue.Kind() == reflect.Slice {
|
2020-03-22 23:12:55 +08:00
|
|
|
containerValueSetFunc = func(newValue *reflect.Value, pk schemas.PK) error {
|
2017-04-07 09:47:25 +08:00
|
|
|
if isPointer {
|
|
|
|
containerValue.Set(reflect.Append(containerValue, newValue.Elem().Addr()))
|
|
|
|
} else {
|
|
|
|
containerValue.Set(reflect.Append(containerValue, newValue.Elem()))
|
2017-02-20 19:33:10 +08:00
|
|
|
}
|
2017-04-07 09:47:25 +08:00
|
|
|
return nil
|
2017-02-20 19:33:10 +08: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")
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
containerValueSetFunc = func(newValue *reflect.Value, pk schemas.PK) error {
|
2017-04-07 09:47:25 +08:00
|
|
|
keyValue := reflect.New(keyType)
|
2021-09-17 01:53:28 +08:00
|
|
|
cols := table.PKColumns()
|
|
|
|
if len(cols) == 1 {
|
|
|
|
if err := convert.AssignValue(keyValue, pk[0]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keyValue.Set(reflect.ValueOf(&pk))
|
2017-02-20 19:33:10 +08:00
|
|
|
}
|
2021-09-17 01:53:28 +08:00
|
|
|
|
2017-04-07 09:47:25 +08:00
|
|
|
if isPointer {
|
|
|
|
containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem().Addr())
|
|
|
|
} else {
|
|
|
|
containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem())
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2017-04-07 09:47:25 +08:00
|
|
|
return nil
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 09:47:25 +08:00
|
|
|
if elemType.Kind() == reflect.Struct {
|
|
|
|
var newValue = newElemFunc(fields)
|
2021-09-17 01:53:28 +08:00
|
|
|
tb, err := session.engine.tagParser.ParseWithCache(newValue)
|
2017-04-07 09:47:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-13 07:11:42 +08:00
|
|
|
err = session.rows2Beans(rows, fields, types, tb, newElemFunc, containerValueSetFunc)
|
2017-10-02 00:52:35 +08:00
|
|
|
rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return session.executeProcessors()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
for rows.Next() {
|
2017-04-07 09:47:25 +08:00
|
|
|
var newValue = newElemFunc(fields)
|
2017-01-03 16:20:28 +08:00
|
|
|
bean := newValue.Interface()
|
|
|
|
|
2017-04-07 09:47:25 +08:00
|
|
|
switch elemType.Kind() {
|
|
|
|
case reflect.Slice:
|
2017-09-13 13:18:22 +08:00
|
|
|
err = rows.ScanSlice(bean)
|
2017-04-07 09:47:25 +08:00
|
|
|
case reflect.Map:
|
2017-09-13 13:18:22 +08:00
|
|
|
err = rows.ScanMap(bean)
|
2017-04-07 09:47:25 +08:00
|
|
|
default:
|
2017-09-13 13:18:22 +08:00
|
|
|
err = rows.Scan(bean)
|
2017-04-07 09:47:25 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-01-03 16:20:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:33:10 +08:00
|
|
|
if err := containerValueSetFunc(&newValue, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2021-08-13 07:11:42 +08:00
|
|
|
return rows.Err()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
|
|
|
|
if !session.canCache() ||
|
2020-03-22 23:12:55 +08:00
|
|
|
utils.IndexNoCase(sqlStr, "having") != -1 ||
|
|
|
|
utils.IndexNoCase(sqlStr, "group by") != -1 {
|
2017-01-03 16:20:28 +08:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:10:17 +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
|
|
|
if cacher == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-09-13 13:18:22 +08:00
|
|
|
rows, err := session.queryRows(newsql, args...)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var i int
|
2020-03-22 23:12:55 +08:00
|
|
|
ids = make([]schemas.PK, 0)
|
2017-01-03 16:20:28 +08:00
|
|
|
for rows.Next() {
|
|
|
|
i++
|
|
|
|
if i > 500 {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cacheFind] ids length > 500, no cache")
|
2017-01-03 16:20:28 +08:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
var res = make([]string, len(table.PrimaryKeys))
|
|
|
|
err = rows.ScanSlice(&res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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() {
|
2020-06-16 04:46:01 +08:00
|
|
|
pk[i], err = col.ConvertID(res[i])
|
2017-08-22 19:39:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ids = append(ids, pk)
|
|
|
|
}
|
2021-08-13 07:11:42 +08:00
|
|
|
if rows.Err() != nil {
|
|
|
|
return rows.Err()
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache sql: %v, %v, %v, %v, %v", ids, tableName, sqlStr, newsql, args)
|
|
|
|
err = caches.PutCacheSql(cacher, ids, tableName, newsql, args)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache hit sql: %v, %v, %v, %v", tableName, sqlStr, newsql, args)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
|
|
|
|
ididxes := make(map[string]int)
|
2020-03-22 23:12:55 +08:00
|
|
|
var ides []schemas.PK
|
2017-01-03 16:20:28 +08:00
|
|
|
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)
|
2020-03-22 23:12:55 +08:00
|
|
|
|
|
|
|
// fix issue #894
|
|
|
|
isHit := func() (ht bool) {
|
|
|
|
if bean == nil {
|
|
|
|
ht = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ckb := reflect.ValueOf(bean).Elem().Type()
|
|
|
|
ht = ckb == t
|
|
|
|
if !ht && t.Kind() == reflect.Ptr {
|
|
|
|
ht = t.Elem() == ckb
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !isHit() {
|
2017-01-03 16:20:28 +08:00
|
|
|
ides = append(ides, id)
|
|
|
|
ididxes[sid] = idx
|
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache hit bean: %v, %v, %v", tableName, id, bean)
|
|
|
|
|
2020-06-16 04:46:01 +08:00
|
|
|
pk, err := table.IDOfV(reflect.ValueOf(bean))
|
2020-03-22 23:12:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
|
|
|
xid, err := pk.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sid != xid {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Errorf("[cache] error cache: %v, %v, %v", xid, sid, bean)
|
2017-01-03 16:20:28 +08:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
temps[idx] = bean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ides) > 0 {
|
|
|
|
slices := reflect.New(reflect.SliceOf(t))
|
|
|
|
beans := slices.Interface()
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
statement := session.statement
|
|
|
|
session.statement = statements.NewStatement(
|
|
|
|
session.engine.dialect,
|
|
|
|
session.engine.tagParser,
|
|
|
|
session.engine.DatabaseTZ,
|
|
|
|
)
|
2017-01-03 16:20:28 +08:00
|
|
|
if len(table.PrimaryKeys) == 1 {
|
|
|
|
ff := make([]interface{}, 0, len(ides))
|
|
|
|
for _, ie := range ides {
|
|
|
|
ff = append(ff, ie[0])
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
session.In("`"+table.PrimaryKeys[0]+"`", ff...)
|
2017-01-03 16:20:28 +08: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 13:18:22 +08:00
|
|
|
session.Or(cond)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:18:22 +08:00
|
|
|
err = session.NoCache().Table(tableName).find(beans)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-25 22:32:23 +08:00
|
|
|
session.statement = statement
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
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()
|
|
|
|
}
|
2020-06-16 04:46:01 +08:00
|
|
|
id, err := table.IDOfV(rv)
|
2017-04-07 09:47:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bean := rv.Interface()
|
|
|
|
temps[ididxes[sid]] = bean
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Debugf("[cache] cache bean: %v, %v, %v, %v", tableName, id, bean, temps)
|
2017-01-03 16:20:28 +08:00
|
|
|
cacher.PutBean(tableName, sid, bean)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < len(temps); j++ {
|
|
|
|
bean := temps[j]
|
|
|
|
if bean == nil {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.engine.logger.Warnf("[cache] cache no hit: %v, %v, %v", tableName, ids[j], temps)
|
2017-01-03 16:20:28 +08: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()
|
2021-08-13 07:11:42 +08:00
|
|
|
keyValue := reflect.New(keyType)
|
2017-01-03 16:20:28 +08:00
|
|
|
var ikey interface{}
|
|
|
|
if len(key) == 1 {
|
2021-08-13 07:11:42 +08:00
|
|
|
if err := convert.AssignValue(keyValue, key[0]); err != nil {
|
2017-01-03 16:20:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-08-13 07:11:42 +08:00
|
|
|
ikey = keyValue.Elem().Interface()
|
2017-01-03 16:20:28 +08:00
|
|
|
} else {
|
|
|
|
if keyType.Kind() != reflect.Slice {
|
2020-03-22 23:12:55 +08:00
|
|
|
return errors.New("table have multiple primary keys, key is not schemas.PK or slice")
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|