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.

177 lines
4.5 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. "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. }