rows.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. rows *core.Rows
  16. fields []string
  17. beanType reflect.Type
  18. lastError error
  19. }
  20. func newRows(session *Session, bean interface{}) (*Rows, error) {
  21. rows := new(Rows)
  22. rows.session = session
  23. rows.beanType = reflect.Indirect(reflect.ValueOf(bean)).Type()
  24. var sqlStr string
  25. var args []interface{}
  26. var err error
  27. if err = rows.session.statement.setRefBean(bean); err != nil {
  28. return nil, err
  29. }
  30. if len(session.statement.TableName()) <= 0 {
  31. return nil, ErrTableNotFound
  32. }
  33. if rows.session.statement.RawSQL == "" {
  34. sqlStr, args, err = rows.session.statement.genGetSQL(bean)
  35. if err != nil {
  36. return nil, err
  37. }
  38. } else {
  39. sqlStr = rows.session.statement.RawSQL
  40. args = rows.session.statement.RawParams
  41. }
  42. rows.rows, err = rows.session.queryRows(sqlStr, args...)
  43. if err != nil {
  44. rows.lastError = err
  45. rows.Close()
  46. return nil, err
  47. }
  48. rows.fields, err = rows.rows.Columns()
  49. if err != nil {
  50. rows.lastError = err
  51. rows.Close()
  52. return nil, err
  53. }
  54. return rows, nil
  55. }
  56. // Next move cursor to next record, return false if end has reached
  57. func (rows *Rows) Next() bool {
  58. if rows.lastError == nil && rows.rows != nil {
  59. hasNext := rows.rows.Next()
  60. if !hasNext {
  61. rows.lastError = sql.ErrNoRows
  62. }
  63. return hasNext
  64. }
  65. return false
  66. }
  67. // Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.
  68. func (rows *Rows) Err() error {
  69. return rows.lastError
  70. }
  71. // Scan row record to bean properties
  72. func (rows *Rows) Scan(bean interface{}) error {
  73. if rows.lastError != nil {
  74. return rows.lastError
  75. }
  76. if !rows.NoTypeCheck && reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
  77. return fmt.Errorf("scan arg is incompatible type to [%v]", rows.beanType)
  78. }
  79. if err := rows.session.statement.setRefBean(bean); err != nil {
  80. return err
  81. }
  82. scanResults, err := rows.session.row2Slice(rows.rows, rows.fields, bean)
  83. if err != nil {
  84. return err
  85. }
  86. dataStruct := rValue(bean)
  87. _, err = rows.session.slice2Bean(scanResults, rows.fields, bean, &dataStruct, rows.session.statement.RefTable)
  88. if err != nil {
  89. return err
  90. }
  91. return rows.session.executeProcessors()
  92. }
  93. // Close session if session.IsAutoClose is true, and claimed any opened resources
  94. func (rows *Rows) Close() error {
  95. if rows.session.isAutoClose {
  96. defer rows.session.Close()
  97. }
  98. if rows.lastError == nil {
  99. if rows.rows != nil {
  100. rows.lastError = rows.rows.Close()
  101. if rows.lastError != nil {
  102. return rows.lastError
  103. }
  104. }
  105. } else {
  106. if rows.rows != nil {
  107. defer rows.rows.Close()
  108. }
  109. }
  110. return rows.lastError
  111. }