forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
97 lines (91 loc) · 2.07 KB
/
iter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package orm
import (
"gnd.la/orm/driver"
)
type Iter struct {
q *Query
limit int
driver.Iter
err error
}
// Next advances the iter to the next result,
// filling the fields in the out parameter. It
// returns true iff there was a result.
func (i *Iter) Next(out ...interface{}) bool {
if i.err != nil {
return false
}
if i.Iter == nil {
if i.q.model == nil {
i.q.model, i.q.methods, i.err = i.q.orm.models(out, i.q.q, i.q.sort, i.q.jtype)
if i.err != nil {
return false
}
} else {
i.q.methods = append(i.q.methods, i.q.model.fields.Methods)
for cur := i.q.model.join; cur != nil; cur = cur.model.join {
i.q.methods = append(i.q.methods, cur.model.fields.Methods)
}
}
i.Iter = i.q.exec(i.limit)
}
ok := i.Iter.Next(out...)
if ok {
for ii, v := range out {
if i.err = i.q.methods[ii].Load(v); i.err != nil {
break
}
}
} else {
i.Close()
}
return ok && i.err == nil
}
// Err returns the first error returned by the iterator. Once
// there's an error, Next() will return false.
func (i *Iter) Err() error {
if i.err != nil {
return i.err
}
if i.Iter != nil {
return i.Iter.Err()
}
return nil
}
// Assert panics if the iter has an error. It's intended as a shorthand
// to save a few lines of code. For iterating over
// the results, a common pattern is:
//
// for iter.Next(&obj) {
// ... do something with obj...
// }
// if err := iter.Err(); err != nil {
// panic(err)
// }
//
// With Assert() you can instead use this code and save a few keystrokes:
//
// for iter.Next(&obj) {
// ... do something with obj...
// }
// iter.Assert()
func (i *Iter) Assert() {
if err := i.Err(); err != nil {
panic(err)
}
}
// Close closes the iter. It's automatically called when the results
// are exhausted, but if you're ignoring some results you must call
// Close manually to avoid leaking resources. Close is idempotent.
func (i *Iter) Close() error {
if i.Iter != nil {
ierr := i.Iter.Err()
err := i.Iter.Close()
i.Iter = nil
if ierr != nil {
i.err = ierr
}
return err
}
return nil
}