session_get.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "reflect"
  8. "strconv"
  9. "github.com/go-xorm/core"
  10. )
  11. // Get retrieve one record from database, bean's non-empty fields
  12. // will be as conditions
  13. func (session *Session) Get(bean interface{}) (bool, error) {
  14. defer session.resetStatement()
  15. if session.IsAutoClose {
  16. defer session.Close()
  17. }
  18. session.Statement.setRefValue(rValue(bean))
  19. var sqlStr string
  20. var args []interface{}
  21. if session.Statement.RawSQL == "" {
  22. if len(session.Statement.TableName()) <= 0 {
  23. return false, ErrTableNotFound
  24. }
  25. session.Statement.Limit(1)
  26. sqlStr, args = session.Statement.genGetSQL(bean)
  27. } else {
  28. sqlStr = session.Statement.RawSQL
  29. args = session.Statement.RawParams
  30. }
  31. if session.canCache() {
  32. if cacher := session.Engine.getCacher2(session.Statement.RefTable); cacher != nil &&
  33. !session.Statement.unscoped {
  34. has, err := session.cacheGet(bean, sqlStr, args...)
  35. if err != ErrCacheFailed {
  36. return has, err
  37. }
  38. }
  39. }
  40. return session.nocacheGet(bean, sqlStr, args...)
  41. }
  42. func (session *Session) nocacheGet(bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
  43. var rawRows *core.Rows
  44. var err error
  45. session.queryPreprocess(&sqlStr, args...)
  46. if session.IsAutoCommit {
  47. _, rawRows, err = session.innerQuery(sqlStr, args...)
  48. } else {
  49. rawRows, err = session.Tx.Query(sqlStr, args...)
  50. }
  51. if err != nil {
  52. return false, err
  53. }
  54. defer rawRows.Close()
  55. if rawRows.Next() {
  56. fields, err := rawRows.Columns()
  57. if err == nil {
  58. err = session.row2Bean(rawRows, fields, len(fields), bean)
  59. }
  60. return true, err
  61. }
  62. return false, nil
  63. }
  64. func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) {
  65. // if has no reftable, then don't use cache currently
  66. if !session.canCache() {
  67. return false, ErrCacheFailed
  68. }
  69. for _, filter := range session.Engine.dialect.Filters() {
  70. sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
  71. }
  72. newsql := session.Statement.convertIDSQL(sqlStr)
  73. if newsql == "" {
  74. return false, ErrCacheFailed
  75. }
  76. cacher := session.Engine.getCacher2(session.Statement.RefTable)
  77. tableName := session.Statement.TableName()
  78. session.Engine.logger.Debug("[cacheGet] find sql:", newsql, args)
  79. ids, err := core.GetCacheSql(cacher, tableName, newsql, args)
  80. table := session.Statement.RefTable
  81. if err != nil {
  82. var res = make([]string, len(table.PrimaryKeys))
  83. rows, err := session.DB().Query(newsql, args...)
  84. if err != nil {
  85. return false, err
  86. }
  87. defer rows.Close()
  88. if rows.Next() {
  89. err = rows.ScanSlice(&res)
  90. if err != nil {
  91. return false, err
  92. }
  93. } else {
  94. return false, ErrCacheFailed
  95. }
  96. var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
  97. for i, col := range table.PKColumns() {
  98. if col.SQLType.IsText() {
  99. pk[i] = res[i]
  100. } else if col.SQLType.IsNumeric() {
  101. n, err := strconv.ParseInt(res[i], 10, 64)
  102. if err != nil {
  103. return false, err
  104. }
  105. pk[i] = n
  106. } else {
  107. return false, errors.New("unsupported")
  108. }
  109. }
  110. ids = []core.PK{pk}
  111. session.Engine.logger.Debug("[cacheGet] cache ids:", newsql, ids)
  112. err = core.PutCacheSql(cacher, ids, tableName, newsql, args)
  113. if err != nil {
  114. return false, err
  115. }
  116. } else {
  117. session.Engine.logger.Debug("[cacheGet] cache hit sql:", newsql)
  118. }
  119. if len(ids) > 0 {
  120. structValue := reflect.Indirect(reflect.ValueOf(bean))
  121. id := ids[0]
  122. session.Engine.logger.Debug("[cacheGet] get bean:", tableName, id)
  123. sid, err := id.ToString()
  124. if err != nil {
  125. return false, err
  126. }
  127. cacheBean := cacher.GetBean(tableName, sid)
  128. if cacheBean == nil {
  129. /*newSession := session.Engine.NewSession()
  130. defer newSession.Close()
  131. cacheBean = reflect.New(structValue.Type()).Interface()
  132. newSession.Id(id).NoCache()
  133. if session.Statement.AltTableName != "" {
  134. newSession.Table(session.Statement.AltTableName)
  135. }
  136. if !session.Statement.UseCascade {
  137. newSession.NoCascade()
  138. }
  139. has, err = newSession.Get(cacheBean)
  140. */
  141. cacheBean = bean
  142. has, err = session.nocacheGet(cacheBean, sqlStr, args...)
  143. if err != nil || !has {
  144. return has, err
  145. }
  146. session.Engine.logger.Debug("[cacheGet] cache bean:", tableName, id, cacheBean)
  147. cacher.PutBean(tableName, sid, cacheBean)
  148. } else {
  149. session.Engine.logger.Debug("[cacheGet] cache hit bean:", tableName, id, cacheBean)
  150. has = true
  151. }
  152. structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean)))
  153. return has, nil
  154. }
  155. return false, nil
  156. }