Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

106 рядки
2.4 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. "reflect"
  7. "xorm.io/xorm/internal/utils"
  8. )
  9. // IterFunc only use by Iterate
  10. type IterFunc func(idx int, bean interface{}) error
  11. // Rows return sql.Rows compatible Rows obj, as a forward Iterator object for iterating record by record, bean's non-empty fields
  12. // are conditions.
  13. func (session *Session) Rows(bean interface{}) (*Rows, error) {
  14. return newRows(session, bean)
  15. }
  16. // Iterate record by record handle records from table, condiBeans's non-empty fields
  17. // are conditions. beans could be []Struct, []*Struct, map[int64]Struct
  18. // map[int64]*Struct
  19. func (session *Session) Iterate(bean interface{}, fun IterFunc) error {
  20. if session.isAutoClose {
  21. defer session.Close()
  22. }
  23. if session.statement.LastError != nil {
  24. return session.statement.LastError
  25. }
  26. if session.statement.BufferSize > 0 {
  27. return session.bufferIterate(bean, fun)
  28. }
  29. rows, err := session.Rows(bean)
  30. if err != nil {
  31. return err
  32. }
  33. defer rows.Close()
  34. i := 0
  35. for rows.Next() {
  36. b := reflect.New(rows.beanType).Interface()
  37. err = rows.Scan(b)
  38. if err != nil {
  39. return err
  40. }
  41. err = fun(i, b)
  42. if err != nil {
  43. return err
  44. }
  45. i++
  46. }
  47. return err
  48. }
  49. // BufferSize sets the buffersize for iterate
  50. func (session *Session) BufferSize(size int) *Session {
  51. session.statement.BufferSize = size
  52. return session
  53. }
  54. func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
  55. var bufferSize = session.statement.BufferSize
  56. var pLimitN = session.statement.LimitN
  57. if pLimitN != nil && bufferSize > *pLimitN {
  58. bufferSize = *pLimitN
  59. }
  60. var start = session.statement.Start
  61. v := utils.ReflectValue(bean)
  62. sliceType := reflect.SliceOf(v.Type())
  63. var idx = 0
  64. session.autoResetStatement = false
  65. defer func() {
  66. session.autoResetStatement = true
  67. }()
  68. for bufferSize > 0 {
  69. slice := reflect.New(sliceType)
  70. if err := session.NoCache().Limit(bufferSize, start).find(slice.Interface(), bean); err != nil {
  71. return err
  72. }
  73. for i := 0; i < slice.Elem().Len(); i++ {
  74. if err := fun(idx, slice.Elem().Index(i).Addr().Interface()); err != nil {
  75. return err
  76. }
  77. idx++
  78. }
  79. if bufferSize > slice.Elem().Len() {
  80. break
  81. }
  82. start = start + slice.Elem().Len()
  83. if pLimitN != nil && start+bufferSize > *pLimitN {
  84. bufferSize = *pLimitN - start
  85. }
  86. }
  87. return nil
  88. }