forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
151 lines (129 loc) · 2.83 KB
/
view.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package postgres
import (
"context"
"database/sql"
"strings"
"cosmossdk.io/schema"
"cosmossdk.io/schema/view"
)
var _ view.AppData = &indexerImpl{}
func (i *indexerImpl) AppState() view.AppState {
return i
}
func (i *indexerImpl) BlockNum() (uint64, error) {
var blockNum int64
err := i.tx.QueryRow("SELECT coalesce(max(number), 0) FROM block").Scan(&blockNum)
if err != nil {
return 0, err
}
return uint64(blockNum), nil
}
type moduleView struct {
moduleIndexer
ctx context.Context
conn dbConn
}
func (i *indexerImpl) GetModule(moduleName string) (view.ModuleState, error) {
mod, ok := i.modules[moduleName]
if !ok {
return nil, nil
}
return &moduleView{
moduleIndexer: *mod,
ctx: i.ctx,
conn: i.tx,
}, nil
}
func (i *indexerImpl) Modules(f func(modState view.ModuleState, err error) bool) {
for _, mod := range i.modules {
if !f(&moduleView{
moduleIndexer: *mod,
ctx: i.ctx,
conn: i.tx,
}, nil) {
return
}
}
}
func (i *indexerImpl) NumModules() (int, error) {
return len(i.modules), nil
}
func (m *moduleView) ModuleName() string {
return m.moduleName
}
func (m *moduleView) ModuleSchema() schema.ModuleSchema {
return m.schema
}
func (m *moduleView) GetObjectCollection(objectType string) (view.ObjectCollection, error) {
obj, ok := m.tables[objectType]
if !ok {
return nil, nil
}
return &objectView{
objectIndexer: *obj,
ctx: m.ctx,
conn: m.conn,
}, nil
}
func (m *moduleView) ObjectCollections(f func(value view.ObjectCollection, err error) bool) {
for _, obj := range m.tables {
if !f(&objectView{
objectIndexer: *obj,
ctx: m.ctx,
conn: m.conn,
}, nil) {
return
}
}
}
func (m *moduleView) NumObjectCollections() (int, error) {
return len(m.tables), nil
}
type objectView struct {
objectIndexer
ctx context.Context
conn dbConn
}
func (tm *objectView) ObjectType() schema.StateObjectType {
return tm.typ
}
func (tm *objectView) GetObject(key interface{}) (update schema.StateObjectUpdate, found bool, err error) {
return tm.get(tm.ctx, tm.conn, key)
}
func (tm *objectView) AllState(f func(schema.StateObjectUpdate, error) bool) {
buf := new(strings.Builder)
err := tm.selectAllSql(buf)
if err != nil {
panic(err)
}
sqlStr := buf.String()
if tm.options.logger != nil {
tm.options.logger.Debug("Select", "sql", sqlStr)
}
rows, err := tm.conn.QueryContext(tm.ctx, sqlStr)
if err != nil {
panic(err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
panic(err)
}
}(rows)
for rows.Next() {
update, found, err := tm.readRow(rows)
if err == nil && !found {
err = sql.ErrNoRows
}
if !f(update, err) {
return
}
}
}
func (tm *objectView) Len() (int, error) {
n, err := tm.count(tm.ctx, tm.conn)
if err != nil {
return 0, err
}
return n, nil
}