-
Notifications
You must be signed in to change notification settings - Fork 3
/
djondb_cursor.go
88 lines (80 loc) · 1.76 KB
/
djondb_cursor.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
package djondbgo
import (
"fmt"
)
const (
CS_LOADING = 1
CS_RECORDS_LOADED = 2
CS_CLOSED = 3
)
type DjondbCursor struct {
net *Network
cursorId *string
rows []map[string]interface{}
position int32
_current *map[string]interface{}
count int32
status int
}
func CreateCursor(net *Network, cursorId *string, firstPage []map[string]interface{}) *DjondbCursor {
result := &DjondbCursor{}
result.net = net
result.cursorId = cursorId
result.rows = firstPage
result.position = 0
result._current = nil
result.count = int32(len(result.rows))
if cursorId == nil {
result.status = CS_RECORDS_LOADED
} else {
result.status = CS_LOADING
}
return result
}
func (cursor *DjondbCursor) Next() (res bool, err error) {
res = false
if cursor.status == CS_CLOSED {
err = fmt.Errorf("Cursor is closed")
return
}
res = true
if cursor.count > cursor.position {
row := cursor.rows[cursor.position]
cursor._current = &row
cursor.position += 1
} else {
if cursor.status == CS_LOADING {
cmd := CreateCommand()
var page []map[string]interface{}
page, err = cmd.fetchRecords(cursor.net, *cursor.cursorId)
if len(page) == 0 {
cursor.status = CS_RECORDS_LOADED
res = false
} else {
cursor.rows = append(cursor.rows, page...)
cursor.count = int32(len(cursor.rows))
res, err = cursor.Next()
}
} else {
res = false
}
}
return
}
func (cursor *DjondbCursor) Previous() (bool, *string) {
if cursor.status == CS_CLOSED {
e := "Cursor is closed"
return false, &e
}
result := true
if cursor.count > 0 && cursor.position > 0 {
cursor.position -= 1
cursor._current = &cursor.rows[cursor.position]
} else {
result = false
}
return result, nil
}
func (cursor *DjondbCursor) Current() *map[string]interface{} {
return cursor._current
}