You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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