rows.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2015 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. "database/sql"
  7. "fmt"
  8. "reflect"
  9. "github.com/go-xorm/core"
  10. )
  11. // Rows rows wrapper a rows to
  12. type Rows struct {
  13. NoTypeCheck bool
  14. session *Session
  15. stmt *core.Stmt
  16. rows *core.Rows
  17. fields []string
  18. beanType reflect.Type
  19. lastError error
  20. }
  21. func newRows(session *Session, bean interface{}) (*Rows, error) {
  22. rows := new(Rows)
  23. rows.session = session
  24. rows.beanType = reflect.Indirect(reflect.ValueOf(bean)).Type()
  25. defer rows.session.resetStatement()
  26. var sqlStr string
  27. var args []interface{}
  28. if err := rows.session.Statement.setRefValue(rValue(bean)); err != nil {
  29. return nil, err
  30. }
  31. if len(session.Statement.TableName()) <= 0 {
  32. return nil, ErrTableNotFound
  33. }
  34. if rows.session.Statement.RawSQL == "" {
  35. sqlStr, args = rows.session.Statement.genGetSQL(bean)
  36. } else {
  37. sqlStr = rows.session.Statement.RawSQL
  38. args = rows.session.Statement.RawParams
  39. }
  40. for _, filter := range rows.session.Engine.dialect.Filters() {
  41. sqlStr = filter.Do(sqlStr, session.Engine.dialect, rows.session.Statement.RefTable)
  42. }
  43. rows.session.saveLastSQL(sqlStr, args...)
  44. var err error
  45. if rows.session.prepareStmt {
  46. rows.stmt, err = rows.session.DB().Prepare(sqlStr)
  47. if err != nil {
  48. rows.lastError = err
  49. rows.Close()
  50. return nil, err
  51. }
  52. rows.rows, err = rows.stmt.Query(args...)
  53. if err != nil {
  54. rows.lastError = err
  55. rows.Close()
  56. return nil, err
  57. }
  58. } else {
  59. rows.rows, err = rows.session.DB().Query(sqlStr, args...)
  60. if err != nil {
  61. rows.lastError = err
  62. rows.Close()
  63. return nil, err
  64. }
  65. }
  66. rows.fields, err = rows.rows.Columns()
  67. if err != nil {
  68. rows.lastError = err
  69. rows.Close()
  70. return nil, err
  71. }
  72. return rows, nil
  73. }
  74. // Next move cursor to next record, return false if end has reached
  75. func (rows *Rows) Next() bool {
  76. if rows.lastError == nil && rows.rows != nil {
  77. hasNext := rows.rows.Next()
  78. if !hasNext {
  79. rows.lastError = sql.ErrNoRows
  80. }
  81. return hasNext
  82. }
  83. return false
  84. }
  85. // Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.
  86. func (rows *Rows) Err() error {
  87. return rows.lastError
  88. }
  89. // Scan row record to bean properties
  90. func (rows *Rows) Scan(bean interface{}) error {
  91. if rows.lastError != nil {
  92. return rows.lastError
  93. }
  94. if !rows.NoTypeCheck && reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
  95. return fmt.Errorf("scan arg is incompatible type to [%v]", rows.beanType)
  96. }
  97. dataStruct := rValue(bean)
  98. if err := rows.session.Statement.setRefValue(dataStruct); err != nil {
  99. return err
  100. }
  101. _, err := rows.session.row2Bean(rows.rows, rows.fields, len(rows.fields), bean, &dataStruct, rows.session.Statement.RefTable)
  102. return err
  103. }
  104. // Close session if session.IsAutoClose is true, and claimed any opened resources
  105. func (rows *Rows) Close() error {
  106. if rows.session.IsAutoClose {
  107. defer rows.session.Close()
  108. }
  109. if rows.lastError == nil {
  110. if rows.rows != nil {
  111. rows.lastError = rows.rows.Close()
  112. if rows.lastError != nil {
  113. defer rows.stmt.Close()
  114. return rows.lastError
  115. }
  116. }
  117. if rows.stmt != nil {
  118. rows.lastError = rows.stmt.Close()
  119. }
  120. } else {
  121. if rows.stmt != nil {
  122. defer rows.stmt.Close()
  123. }
  124. if rows.rows != nil {
  125. defer rows.rows.Close()
  126. }
  127. }
  128. return rows.lastError
  129. }