-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
252 lines (219 loc) · 5.31 KB
/
database.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
package main
import (
"fmt"
"os"
"path"
"time"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/jmoiron/sqlx"
_ "modernc.org/sqlite"
)
const defaultDatabaseFileName = "database.sqlite"
type Database struct {
db *sqlx.DB
fileName string
}
func NewDatabaseConnection(override bool, dataDir string, fileName string) (d *Database, err error) {
d = &Database{}
if fileName != "" {
d.fileName = fileName
} else {
if !override {
d.fileName = time.Now().Format(time.DateTime) + ".sqlite"
} else {
d.fileName = defaultDatabaseFileName
}
}
if dataDir != "" {
d.fileName = path.Join(dataDir, d.fileName)
}
if err = d.openDatabase(); err != nil {
return
}
err = d.PrepareTables()
return
}
func (d *Database) openDatabase() (err error) {
d.db, err = sqlx.Open("sqlite", d.fileName)
return
}
func (d *Database) PrepareTables() (err error) {
sqlFile, err := os.ReadFile("create_tables.sql")
if err != nil {
return
}
_, err = d.db.Exec(string(sqlFile))
return
}
func (d *Database) Close() error {
return d.db.Close()
}
func (d *Database) MarkAsDownloaded(address string, provider string) (err error) {
_, err = d.db.NamedExec(
"INSERT INTO download_status VALUES(:address, :provider)",
map[string]interface{}{
"address": address,
"provider": provider,
},
)
return
}
func (d *Database) Downloaded(address string) (providers []string, anyProvider bool, err error) {
stmt, err := d.db.PrepareNamed(
`SELECT provider FROM download_status WHERE address = :address`,
)
if err != nil {
return
}
err = stmt.Select(&providers, map[string]interface{}{
"address": address,
})
if err != nil {
return
}
if len(providers) > 0 {
anyProvider = true
}
return
}
type AppearanceData struct {
types.Appearance
BalanceChange bool
}
func (d *Database) SaveAppearance(provider string, appearance AppearanceData) (err error) {
var appearanceId int
err = d.db.Get(
&appearanceId,
`INSERT INTO appearances(address, block_number, transaction_index, provider) VALUES(?, ?, ?, ?) RETURNING id`,
appearance.Address.String(),
appearance.BlockNumber,
appearance.TransactionIndex,
provider,
)
if err != nil {
return err
}
_, err = d.db.NamedExec(
`INSERT INTO appearance_reasons(appearance_id, provider, reason) VALUES(:id, :provider, :reason)`,
map[string]any{
"id": appearanceId,
"provider": provider,
"reason": appearance.Reason,
},
)
if err != nil {
return err
}
if appearance.BalanceChange {
_, err = d.db.NamedExec(
`INSERT INTO appearance_balance_changes VALUES(:id, true)`,
map[string]any{
"id": appearanceId,
},
)
if err != nil {
return fmt.Errorf("inserting balance change: %w", err)
}
}
return
}
func (d *Database) SaveIncompatibleAddress(address string, appearances []types.Appearance) (err error) {
_, err = d.db.NamedExec(
`INSERT INTO incompatible_addresses VALUES(:address, :appearanceCount)`,
map[string]any{
"address": address,
"appearanceCount": len(appearances),
},
)
return
}
func (d *Database) UniqueAppearanceCount(provider string) (count int, err error) {
err = d.db.Get(
&count,
`SELECT
count(*)
FROM view_appearances_with_providers
WHERE EXISTS (SELECT 1 FROM json_each(providers) WHERE value = ? AND json_array_length(providers) = 1)`,
provider,
)
return
}
func (d *Database) AppearanceCount(provider string) (count int, err error) {
err = d.db.Get(
&count,
`SELECT
count(*)
FROM view_appearances_with_providers
WHERE EXISTS (SELECT 1 FROM json_each(providers) WHERE value = ?)`,
provider,
)
return
}
func (d *Database) AddressCountTotal() (count int, err error) {
err = d.db.Get(&count, `SELECT count(*) FROM (SELECT DISTINCT address FROM appearances)`)
return
}
func (d *Database) UniqueAddressCount(provider string) (count int, err error) {
err = d.db.Get(
&count,
`SELECT
count(*)
FROM (
SELECT DISTINCT
address
FROM
view_appearances_with_providers
WHERE EXISTS (SELECT 1 FROM json_each(providers) WHERE value = ? AND json_array_length(providers) = 1)
)`,
provider,
)
return
}
func (d *Database) AddressCount(provider string) (count int, err error) {
err = d.db.Get(
&count,
`SELECT
count(*)
FROM (
SELECT DISTINCT
address
FROM
view_appearances_with_providers
WHERE EXISTS (SELECT 1 FROM json_each(providers) WHERE value = ?)
)`,
provider,
)
return
}
type GroupedReasons struct {
Reason string
Count int
}
func (d *Database) UniqueAppearancesGroupedReasons(provider string) (groupedReasons []GroupedReasons, err error) {
err = d.db.Select(
&groupedReasons,
`WITH apps AS(
SELECT * FROM view_appearances_with_providers WHERE EXISTS (SELECT 1 FROM json_each(providers) WHERE value = ? AND json_array_length(providers) = 1)
) SELECT
reason,
count(*) as count
FROM apps
JOIN appearance_reasons r ON r.appearance_id = apps.id
GROUP BY reason ORDER BY count`,
provider,
)
return
}
func (d *Database) BalanceChangeCount(provider string) (count int, err error) {
err = d.db.Get(
&count,
`WITH apps AS(
SELECT * FROM view_appearances_with_providers WHERE EXISTS (SELECT 1 FROM json_each(providers) WHERE value = ? AND json_array_length(providers) = 1)
) SELECT
count(*) as count
FROM apps
JOIN appearance_balance_changes c ON c.appearance_id = apps.id`,
provider,
)
return
}