You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
12 KiB

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