session_find.go 13 KB

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