rows.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. fieldsCount int
  19. beanType reflect.Type
  20. lastError error
  21. }
  22. func newRows(session *Session, bean interface{}) (*Rows, error) {
  23. rows := new(Rows)
  24. rows.session = session
  25. rows.beanType = reflect.Indirect(reflect.ValueOf(bean)).Type()
  26. defer rows.session.resetStatement()
  27. var sqlStr string
  28. var args []interface{}
  29. rows.session.Statement.setRefValue(rValue(bean))
  30. if len(session.Statement.TableName()) <= 0 {
  31. return nil, ErrTableNotFound
  32. }
  33. if rows.session.Statement.RawSQL == "" {
  34. sqlStr, args = rows.session.Statement.genGetSQL(bean)
  35. } else {
  36. sqlStr = rows.session.Statement.RawSQL
  37. args = rows.session.Statement.RawParams
  38. }
  39. for _, filter := range rows.session.Engine.dialect.Filters() {
  40. sqlStr = filter.Do(sqlStr, session.Engine.dialect, rows.session.Statement.RefTable)
  41. }
  42. rows.session.saveLastSQL(sqlStr, args...)
  43. var err error
  44. if rows.session.prepareStmt {
  45. rows.stmt, err = rows.session.DB().Prepare(sqlStr)
  46. if err != nil {
  47. rows.lastError = err
  48. rows.Close()
  49. return nil, err
  50. }
  51. rows.rows, err = rows.stmt.Query(args...)
  52. if err != nil {
  53. rows.lastError = err
  54. rows.Close()
  55. return nil, err
  56. }
  57. } else {
  58. rows.rows, err = rows.session.DB().Query(sqlStr, args...)
  59. if err != nil {
  60. rows.lastError = err
  61. rows.Close()
  62. return nil, err
  63. }
  64. }
  65. rows.fields, err = rows.rows.Columns()
  66. if err != nil {
  67. rows.lastError = err
  68. rows.Close()
  69. return nil, err
  70. }
  71. rows.fieldsCount = len(rows.fields)
  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. return rows.session.row2Bean(rows.rows, rows.fields, rows.fieldsCount, bean)
  98. }
  99. // Close session if session.IsAutoClose is true, and claimed any opened resources
  100. func (rows *Rows) Close() error {
  101. if rows.session.IsAutoClose {
  102. defer rows.session.Close()
  103. }
  104. if rows.lastError == nil {
  105. if rows.rows != nil {
  106. rows.lastError = rows.rows.Close()
  107. if rows.lastError != nil {
  108. defer rows.stmt.Close()
  109. return rows.lastError
  110. }
  111. }
  112. if rows.stmt != nil {
  113. rows.lastError = rows.stmt.Close()
  114. }
  115. } else {
  116. if rows.stmt != nil {
  117. defer rows.stmt.Close()
  118. }
  119. if rows.rows != nil {
  120. defer rows.rows.Close()
  121. }
  122. }
  123. return rows.lastError
  124. }