-
Notifications
You must be signed in to change notification settings - Fork 3
/
tables.go
296 lines (252 loc) · 6.63 KB
/
tables.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.
package dcrpg
import (
"database/sql"
"fmt"
"regexp"
"strconv"
"github.com/dcrdata/dcrdata/db/dcrpg/internal"
)
var createTableStatements = map[string]string{
"blocks": internal.CreateBlockTable,
"transactions": internal.CreateTransactionTable,
"vins": internal.CreateVinTable,
"vouts": internal.CreateVoutTable,
"block_chain": internal.CreateBlockPrevNextTable,
"addresses": internal.CreateAddressTable,
}
var createTypeStatements = map[string]string{
"vin_t": internal.CreateVinType,
"vout_t": internal.CreateVoutType,
}
func TableExists(db *sql.DB, tableName string) (bool, error) {
rows, err := db.Query(`select relname from pg_class where relname = $1`,
tableName)
if err == nil {
defer func() {
if e := rows.Close(); e != nil {
log.Errorf("Close of Query failed: %v", e)
}
}()
return rows.Next(), nil
}
return false, err
}
func DropTables(db *sql.DB) {
for tableName := range createTableStatements {
log.Infof("DROPPING the \"%s\" table.", tableName)
if err := dropTable(db, tableName); err != nil {
log.Errorf("DROP TABLE %s failed.", tableName)
}
}
_, err := db.Exec(`DROP TYPE IF EXISTS vin;`)
if err != nil {
log.Errorf("DROP TYPE vin failed.")
}
}
func dropTable(db *sql.DB, tableName string) error {
_, err := db.Exec(fmt.Sprintf(`DROP TABLE IF EXISTS %s;`, tableName))
return err
}
func CreateTypes(db *sql.DB) error {
var err error
for typeName, createCommand := range createTypeStatements {
var exists bool
exists, err = TypeExists(db, typeName)
if err != nil {
return err
}
if !exists {
log.Infof("Creating the \"%s\" type.", typeName)
_, err = db.Exec(createCommand)
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf(`COMMENT ON TYPE %s
IS 'v1';`, typeName))
if err != nil {
return err
}
} else {
log.Debugf("Type \"%s\" exist.", typeName)
}
}
return err
}
func TypeExists(db *sql.DB, tableName string) (bool, error) {
rows, err := db.Query(`select typname from pg_type where typname = $1`,
tableName)
if err == nil {
defer func() {
if e := rows.Close(); e != nil {
log.Errorf("Close of Query failed: %v", e)
}
}()
return rows.Next(), nil
}
return false, err
}
func CreateTables(db *sql.DB) error {
var err error
for tableName, createCommand := range createTableStatements {
var exists bool
exists, err = TableExists(db, tableName)
if err != nil {
return err
}
if !exists {
log.Infof("Creating the \"%s\" table.", tableName)
_, err = db.Exec(createCommand)
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf(`COMMENT ON TABLE %s
IS 'v1';`, tableName))
if err != nil {
return err
}
} else {
log.Debugf("Table \"%s\" exist.", tableName)
}
}
return err
}
// CreateTable creates one of the known tables by name
func CreateTable(db *sql.DB, tableName string) error {
var err error
createCommand, tableNameFound := createTableStatements[tableName]
if !tableNameFound {
log.Errorf("Unknown table name %v", tableName)
}
var exists bool
exists, err = TableExists(db, tableName)
if err != nil {
return err
}
if !exists {
log.Infof("Creating the \"%s\" table.", tableName)
_, err = db.Exec(createCommand)
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf(`COMMENT ON TABLE %s
IS 'v1';`, tableName))
if err != nil {
return err
}
} else {
log.Debugf("Table \"%s\" exist.", tableName)
}
return err
}
func TableVersions(db *sql.DB) map[string]int32 {
versions := map[string]int32{}
for tableName := range createTableStatements {
Result := db.QueryRow(`select obj_description($1::regclass);`, tableName)
var s string
v := int(-1)
if Result != nil {
err := Result.Scan(&s)
if err != nil {
log.Errorf("Scan of QueryRow failed: %v", err)
continue
}
re := regexp.MustCompile(`^v(\d+)$`)
subs := re.FindStringSubmatch(s)
if len(subs) > 1 {
v, err = strconv.Atoi(subs[1])
if err != nil {
fmt.Println(err)
}
}
}
versions[tableName] = int32(v)
}
return versions
}
// Vins table indexes
func IndexVinTableOnVins(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexVinTableOnVins)
return
}
func IndexVinTableOnPrevOuts(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexVinTableOnPrevOuts)
return
}
func DeindexVinTableOnVins(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexVinTableOnVins)
return
}
func DeindexVinTableOnPrevOuts(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexVinTableOnPrevOuts)
return
}
// Transactions table indexes
func IndexTransactionTableOnHashes(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexTransactionTableOnHashes)
return
}
func DeindexTransactionTableOnHashes(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexTransactionTableOnHashes)
return
}
func IndexTransactionTableOnBlockIn(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexTransactionTableOnBlockIn)
return
}
func DeindexTransactionTableOnBlockIn(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexTransactionTableOnBlockIn)
return
}
// Blocks table indexes
func IndexBlockTableOnHash(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexBlockTableOnHash)
return
}
func DeindexBlockTableOnHash(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexBlockTableOnHash)
return
}
// Vouts table indexes
func IndexVoutTableOnTxHash(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexVoutTableOnTxHash)
return
}
func IndexVoutTableOnTxHashIdx(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexVoutTableOnTxHashIdx)
return
}
func DeindexVoutTableOnTxHash(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexVoutTableOnTxHash)
return
}
func DeindexVoutTableOnTxHashIdx(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexVoutTableOnTxHashIdx)
return
}
// Addresses table indexes
func IndexAddressTableOnAddress(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexAddressTableOnAddress)
return
}
func DeindexAddressTableOnAddress(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexAddressTableOnAddress)
return
}
func IndexAddressTableOnVoutID(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexAddressTableOnVoutID)
return
}
func DeindexAddressTableOnVoutID(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexAddressTableOnVoutID)
return
}
func IndexAddressTableOnTxHash(db *sql.DB) (err error) {
_, err = db.Exec(internal.IndexAddressTableOnFundingTx)
return
}
func DeindexAddressTableOnTxHash(db *sql.DB) (err error) {
_, err = db.Exec(internal.DeindexAddressTableOnFundingTx)
return
}