rows.go 2.6 KB

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