session_find.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "strings"
  10. "xorm.io/builder"
  11. "xorm.io/core"
  12. )
  13. const (
  14. tpStruct = iota
  15. tpNonStruct
  16. )
  17. // Find retrieve records from table, condiBeans's non-empty fields
  18. // are conditions. beans could be []Struct, []*Struct, map[int64]Struct
  19. // map[int64]*Struct
  20. func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
  21. if session.isAutoClose {
  22. defer session.Close()
  23. }
  24. return session.find(rowsSlicePtr, condiBean...)
  25. }
  26. // FindAndCount find the results and also return the counts
  27. func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error) {
  28. if session.isAutoClose {
  29. defer session.Close()
  30. }
  31. session.autoResetStatement = false
  32. err := session.find(rowsSlicePtr, condiBean...)
  33. if err != nil {
  34. return 0, err
  35. }
  36. sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
  37. if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Map {
  38. return 0, errors.New("needs a pointer to a slice or a map")
  39. }
  40. sliceElementType := sliceValue.Type().Elem()
  41. if sliceElementType.Kind() == reflect.Ptr {
  42. sliceElementType = sliceElementType.Elem()
  43. }
  44. session.autoResetStatement = true
  45. if session.statement.selectStr != "" {
  46. session.statement.selectStr = ""
  47. }
  48. if session.statement.OrderStr != "" {
  49. session.statement.OrderStr = ""
  50. }
  51. return session.Count(reflect.New(sliceElementType).Interface())
  52. }
  53. func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
  54. defer session.resetStatement()
  55. if session.statement.lastError != nil {
  56. return session.statement.lastError
  57. }
  58. sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
  59. if sliceValue.Kind() != reflect.Slice && sliceValue.Kind() != reflect.Map {
  60. return errors.New("needs a pointer to a slice or a map")
  61. }
  62. sliceElementType := sliceValue.Type().Elem()
  63. var tp = tpStruct
  64. if session.statement.RefTable == nil {
  65. if sliceElementType.Kind() == reflect.Ptr {
  66. if sliceElementType.Elem().Kind() == reflect.Struct {
  67. pv := reflect.New(sliceElementType.Elem())
  68. if err := session.statement.setRefValue(pv); err != nil {
  69. return err
  70. }
  71. } else {
  72. tp = tpNonStruct
  73. }
  74. } else if sliceElementType.Kind() == reflect.Struct {
  75. pv := reflect.New(sliceElementType)
  76. if err := session.statement.setRefValue(pv); err != nil {
  77. return err
  78. }
  79. } else {
  80. tp = tpNonStruct
  81. }
  82. }
  83. var table = session.statement.RefTable
  84. var addedTableName = (len(session.statement.JoinStr) > 0)
  85. var autoCond builder.Cond
  86. if tp == tpStruct {
  87. if !session.statement.noAutoCondition && len(condiBean) > 0 {
  88. var err error
  89. autoCond, err = session.statement.buildConds(table, condiBean[0], true, true, false, true, addedTableName)
  90. if err != nil {
  91. return err
  92. }
  93. } else {
  94. // !oinume! Add "<col> IS NULL" to WHERE whatever condiBean is given.
  95. // See https://github.com/go-xorm/xorm/issues/179
  96. if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled
  97. var colName = session.engine.Quote(col.Name)
  98. if addedTableName {
  99. var nm = session.statement.TableName()
  100. if len(session.statement.TableAlias) > 0 {
  101. nm = session.statement.TableAlias
  102. }
  103. colName = session.engine.Quote(nm) + "." + colName
  104. }
  105. autoCond = session.engine.CondDeleted(colName)
  106. }
  107. }
  108. }
  109. var sqlStr string
  110. var args []interface{}
  111. var err error
  112. if session.statement.RawSQL == "" {
  113. if len(session.statement.TableName()) <= 0 {
  114. return ErrTableNotFound
  115. }
  116. var columnStr = session.statement.ColumnStr
  117. if len(session.statement.selectStr) > 0 {
  118. columnStr = session.statement.selectStr
  119. } else {
  120. if session.statement.JoinStr == "" {
  121. if columnStr == "" {
  122. if session.statement.GroupByStr != "" {
  123. columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
  124. } else {
  125. columnStr = session.statement.genColumnStr()
  126. }
  127. }
  128. } else {
  129. if columnStr == "" {
  130. if session.statement.GroupByStr != "" {
  131. columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
  132. } else {
  133. columnStr = "*"
  134. }
  135. }
  136. }
  137. if columnStr == "" {
  138. columnStr = "*"
  139. }
  140. }
  141. session.statement.cond = session.statement.cond.And(autoCond)
  142. condSQL, condArgs, err := builder.ToSQL(session.statement.cond)
  143. if err != nil {
  144. return err
  145. }
  146. args = append(session.statement.joinArgs, condArgs...)
  147. sqlStr, err = session.statement.genSelectSQL(columnStr, condSQL, true, true)
  148. if err != nil {
  149. return err
  150. }
  151. // for mssql and use limit
  152. qs := strings.Count(sqlStr, "?")
  153. if len(args)*2 == qs {
  154. args = append(args, args...)
  155. }
  156. } else {
  157. sqlStr = session.statement.RawSQL
  158. args = session.statement.RawParams
  159. }
  160. if session.canCache() {
  161. if cacher := session.engine.getCacher(session.statement.TableName()); cacher != nil &&
  162. !session.statement.IsDistinct &&
  163. !session.statement.unscoped {
  164. err = session.cacheFind(sliceElementType, sqlStr, rowsSlicePtr, args...)
  165. if err != ErrCacheFailed {
  166. return err
  167. }
  168. err = nil // !nashtsai! reset err to nil for ErrCacheFailed
  169. session.engine.logger.Warn("Cache Find Failed")
  170. }
  171. }
  172. return session.noCacheFind(table, sliceValue, sqlStr, args...)
  173. }
  174. func (session *Session) noCacheFind(table *core.Table, containerValue reflect.Value, sqlStr string, args ...interface{}) error {
  175. rows, err := session.queryRows(sqlStr, args...)
  176. if err != nil {
  177. return err
  178. }
  179. defer rows.Close()
  180. fields, err := rows.Columns()
  181. if err != nil {
  182. return err
  183. }
  184. var newElemFunc func(fields []string) reflect.Value
  185. elemType := containerValue.Type().Elem()
  186. var isPointer bool
  187. if elemType.Kind() == reflect.Ptr {
  188. isPointer = true
  189. elemType = elemType.Elem()
  190. }
  191. if elemType.Kind() == reflect.Ptr {
  192. return errors.New("pointer to pointer is not supported")
  193. }
  194. newElemFunc = func(fields []string) reflect.Value {
  195. switch elemType.Kind() {
  196. case reflect.Slice:
  197. slice := reflect.MakeSlice(elemType, len(fields), len(fields))
  198. x := reflect.New(slice.Type())
  199. x.Elem().Set(slice)
  200. return x
  201. case reflect.Map:
  202. mp := reflect.MakeMap(elemType)
  203. x := reflect.New(mp.Type())
  204. x.Elem().Set(mp)
  205. return x
  206. }
  207. return reflect.New(elemType)
  208. }
  209. var containerValueSetFunc func(*reflect.Value, core.PK) error
  210. if containerValue.Kind() == reflect.Slice {
  211. containerValueSetFunc = func(newValue *reflect.Value, pk core.PK) error {
  212. if isPointer {
  213. containerValue.Set(reflect.Append(containerValue, newValue.Elem().Addr()))
  214. } else {
  215. containerValue.Set(reflect.Append(containerValue, newValue.Elem()))
  216. }
  217. return nil
  218. }
  219. } else {
  220. keyType := containerValue.Type().Key()
  221. if len(table.PrimaryKeys) == 0 {
  222. return errors.New("don't support multiple primary key's map has non-slice key type")
  223. }
  224. if len(table.PrimaryKeys) > 1 && keyType.Kind() != reflect.Slice {
  225. return errors.New("don't support multiple primary key's map has non-slice key type")
  226. }
  227. containerValueSetFunc = func(newValue *reflect.Value, pk core.PK) error {
  228. keyValue := reflect.New(keyType)
  229. err := convertPKToValue(table, keyValue.Interface(), pk)
  230. if err != nil {
  231. return err
  232. }
  233. if isPointer {
  234. containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem().Addr())
  235. } else {
  236. containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem())
  237. }
  238. return nil
  239. }
  240. }
  241. if elemType.Kind() == reflect.Struct {
  242. var newValue = newElemFunc(fields)
  243. dataStruct := rValue(newValue.Interface())
  244. tb, err := session.engine.autoMapType(dataStruct)
  245. if err != nil {
  246. return err
  247. }
  248. err = session.rows2Beans(rows, fields, tb, newElemFunc, containerValueSetFunc)
  249. rows.Close()
  250. if err != nil {
  251. return err
  252. }
  253. return session.executeProcessors()
  254. }
  255. for rows.Next() {
  256. var newValue = newElemFunc(fields)
  257. bean := newValue.Interface()
  258. switch elemType.Kind() {
  259. case reflect.Slice:
  260. err = rows.ScanSlice(bean)
  261. case reflect.Map:
  262. err = rows.ScanMap(bean)
  263. default:
  264. err = rows.Scan(bean)
  265. }
  266. if err != nil {
  267. return err
  268. }
  269. if err := containerValueSetFunc(&newValue, nil); err != nil {
  270. return err
  271. }
  272. }
  273. return nil
  274. }
  275. func convertPKToValue(table *core.Table, dst interface{}, pk core.PK) error {
  276. cols := table.PKColumns()
  277. if len(cols) == 1 {
  278. return convertAssign(dst, pk[0])
  279. }
  280. dst = pk
  281. return nil
  282. }
  283. func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
  284. if !session.canCache() ||
  285. indexNoCase(sqlStr, "having") != -1 ||
  286. indexNoCase(sqlStr, "group by") != -1 {
  287. return ErrCacheFailed
  288. }
  289. tableName := session.statement.TableName()
  290. cacher := session.engine.getCacher(tableName)
  291. if cacher == nil {
  292. return nil
  293. }
  294. for _, filter := range session.engine.dialect.Filters() {
  295. sqlStr = filter.Do(sqlStr, session.engine.dialect, session.statement.RefTable)
  296. }
  297. newsql := session.statement.convertIDSQL(sqlStr)
  298. if newsql == "" {
  299. return ErrCacheFailed
  300. }
  301. table := session.statement.RefTable
  302. ids, err := core.GetCacheSql(cacher, tableName, newsql, args)
  303. if err != nil {
  304. rows, err := session.queryRows(newsql, args...)
  305. if err != nil {
  306. return err
  307. }
  308. defer rows.Close()
  309. var i int
  310. ids = make([]core.PK, 0)
  311. for rows.Next() {
  312. i++
  313. if i > 500 {
  314. session.engine.logger.Debug("[cacheFind] ids length > 500, no cache")
  315. return ErrCacheFailed
  316. }
  317. var res = make([]string, len(table.PrimaryKeys))
  318. err = rows.ScanSlice(&res)
  319. if err != nil {
  320. return err
  321. }
  322. var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
  323. for i, col := range table.PKColumns() {
  324. pk[i], err = session.engine.idTypeAssertion(col, res[i])
  325. if err != nil {
  326. return err
  327. }
  328. }
  329. ids = append(ids, pk)
  330. }
  331. session.engine.logger.Debug("[cacheFind] cache sql:", ids, tableName, sqlStr, newsql, args)
  332. err = core.PutCacheSql(cacher, ids, tableName, newsql, args)
  333. if err != nil {
  334. return err
  335. }
  336. } else {
  337. session.engine.logger.Debug("[cacheFind] cache hit sql:", tableName, sqlStr, newsql, args)
  338. }
  339. sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
  340. ididxes := make(map[string]int)
  341. var ides []core.PK
  342. var temps = make([]interface{}, len(ids))
  343. for idx, id := range ids {
  344. sid, err := id.ToString()
  345. if err != nil {
  346. return err
  347. }
  348. bean := cacher.GetBean(tableName, sid)
  349. if bean == nil || reflect.ValueOf(bean).Elem().Type() != t {
  350. ides = append(ides, id)
  351. ididxes[sid] = idx
  352. } else {
  353. session.engine.logger.Debug("[cacheFind] cache hit bean:", tableName, id, bean)
  354. pk := session.engine.IdOf(bean)
  355. xid, err := pk.ToString()
  356. if err != nil {
  357. return err
  358. }
  359. if sid != xid {
  360. session.engine.logger.Error("[cacheFind] error cache", xid, sid, bean)
  361. return ErrCacheFailed
  362. }
  363. temps[idx] = bean
  364. }
  365. }
  366. if len(ides) > 0 {
  367. slices := reflect.New(reflect.SliceOf(t))
  368. beans := slices.Interface()
  369. if len(table.PrimaryKeys) == 1 {
  370. ff := make([]interface{}, 0, len(ides))
  371. for _, ie := range ides {
  372. ff = append(ff, ie[0])
  373. }
  374. session.In("`"+table.PrimaryKeys[0]+"`", ff...)
  375. } else {
  376. for _, ie := range ides {
  377. cond := builder.NewCond()
  378. for i, name := range table.PrimaryKeys {
  379. cond = cond.And(builder.Eq{"`" + name + "`": ie[i]})
  380. }
  381. session.Or(cond)
  382. }
  383. }
  384. err = session.NoCache().Table(tableName).find(beans)
  385. if err != nil {
  386. return err
  387. }
  388. vs := reflect.Indirect(reflect.ValueOf(beans))
  389. for i := 0; i < vs.Len(); i++ {
  390. rv := vs.Index(i)
  391. if rv.Kind() != reflect.Ptr {
  392. rv = rv.Addr()
  393. }
  394. id, err := session.engine.idOfV(rv)
  395. if err != nil {
  396. return err
  397. }
  398. sid, err := id.ToString()
  399. if err != nil {
  400. return err
  401. }
  402. bean := rv.Interface()
  403. temps[ididxes[sid]] = bean
  404. session.engine.logger.Debug("[cacheFind] cache bean:", tableName, id, bean, temps)
  405. cacher.PutBean(tableName, sid, bean)
  406. }
  407. }
  408. for j := 0; j < len(temps); j++ {
  409. bean := temps[j]
  410. if bean == nil {
  411. session.engine.logger.Warn("[cacheFind] cache no hit:", tableName, ids[j], temps)
  412. // return errors.New("cache error") // !nashtsai! no need to return error, but continue instead
  413. continue
  414. }
  415. if sliceValue.Kind() == reflect.Slice {
  416. if t.Kind() == reflect.Ptr {
  417. sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(bean)))
  418. } else {
  419. sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(bean))))
  420. }
  421. } else if sliceValue.Kind() == reflect.Map {
  422. var key = ids[j]
  423. keyType := sliceValue.Type().Key()
  424. var ikey interface{}
  425. if len(key) == 1 {
  426. ikey, err = str2PK(fmt.Sprintf("%v", key[0]), keyType)
  427. if err != nil {
  428. return err
  429. }
  430. } else {
  431. if keyType.Kind() != reflect.Slice {
  432. return errors.New("table have multiple primary keys, key is not core.PK or slice")
  433. }
  434. ikey = key
  435. }
  436. if t.Kind() == reflect.Ptr {
  437. sliceValue.SetMapIndex(reflect.ValueOf(ikey), reflect.ValueOf(bean))
  438. } else {
  439. sliceValue.SetMapIndex(reflect.ValueOf(ikey), reflect.Indirect(reflect.ValueOf(bean)))
  440. }
  441. }
  442. }
  443. return nil
  444. }