本站源代码
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.

101 lines
2.3KB

  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.lastError != nil {
  21. return session.statement.lastError
  22. }
  23. if session.statement.bufferSize > 0 {
  24. return session.bufferIterate(bean, fun)
  25. }
  26. rows, err := session.Rows(bean)
  27. if err != nil {
  28. return err
  29. }
  30. defer rows.Close()
  31. i := 0
  32. for rows.Next() {
  33. b := reflect.New(rows.beanType).Interface()
  34. err = rows.Scan(b)
  35. if err != nil {
  36. return err
  37. }
  38. err = fun(i, b)
  39. if err != nil {
  40. return err
  41. }
  42. i++
  43. }
  44. return err
  45. }
  46. // BufferSize sets the buffersize for iterate
  47. func (session *Session) BufferSize(size int) *Session {
  48. session.statement.bufferSize = size
  49. return session
  50. }
  51. func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
  52. if session.isAutoClose {
  53. defer session.Close()
  54. }
  55. var bufferSize = session.statement.bufferSize
  56. var limit = session.statement.LimitN
  57. if limit > 0 && bufferSize > limit {
  58. bufferSize = limit
  59. }
  60. var start = session.statement.Start
  61. v := rValue(bean)
  62. sliceType := reflect.SliceOf(v.Type())
  63. var idx = 0
  64. for {
  65. slice := reflect.New(sliceType)
  66. if err := session.Limit(bufferSize, start).find(slice.Interface(), bean); err != nil {
  67. return err
  68. }
  69. for i := 0; i < slice.Elem().Len(); i++ {
  70. if err := fun(idx, slice.Elem().Index(i).Addr().Interface()); err != nil {
  71. return err
  72. }
  73. idx++
  74. }
  75. start = start + slice.Elem().Len()
  76. if limit > 0 && idx+bufferSize > limit {
  77. bufferSize = limit - idx
  78. }
  79. if bufferSize <= 0 || slice.Elem().Len() < bufferSize || idx == limit {
  80. break
  81. }
  82. }
  83. return nil
  84. }
上海开阖软件有限公司 沪ICP备12045867号-1