-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprocesses.sql.go
More file actions
548 lines (502 loc) · 14 KB
/
Copy pathprocesses.sql.go
File metadata and controls
548 lines (502 loc) · 14 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: processes.sql
package indexerdb
import (
"context"
"database/sql"
"time"
"go.vocdoni.io/dvote/types"
)
const computeProcessVoteCount = `-- name: ComputeProcessVoteCount :execresult
UPDATE processes
SET vote_count = (SELECT COUNT(*) FROM votes WHERE process_id = id)
WHERE id = ?1
`
func (q *Queries) ComputeProcessVoteCount(ctx context.Context, id types.ProcessID) (sql.Result, error) {
return q.exec(ctx, q.computeProcessVoteCountStmt, computeProcessVoteCount, id)
}
const createProcess = `-- name: CreateProcess :execresult
INSERT INTO processes (
id, entity_id, start_date, end_date, manually_ended,
title, description,
vote_count, have_results, final_results, census_root,
max_census_size, census_uri, metadata,
census_origin, status, namespace,
envelope, mode, vote_opts,
private_keys, public_keys,
question_index, creation_time,
source_block_height, source_network_id,
chain_id,
results_votes, results_weight, results_block_height
) VALUES (
?, ?, ?, ?, ?,
?, ?,
?, ?, ?, ?,
?, ?, ?,
?, ?, ?,
?, ?, ?,
?, ?,
?, ?,
?, ?,
?,
?, '"0"', 0
)
`
type CreateProcessParams struct {
ID types.ProcessID
EntityID types.EntityID
StartDate time.Time
EndDate time.Time
ManuallyEnded bool
Title string
Description string
VoteCount int64
HaveResults bool
FinalResults bool
CensusRoot types.CensusRoot
MaxCensusSize int64
CensusUri string
Metadata string
CensusOrigin int64
Status int64
Namespace int64
Envelope []byte
Mode []byte
VoteOpts []byte
PrivateKeys string
PublicKeys string
QuestionIndex int64
CreationTime time.Time
SourceBlockHeight int64
SourceNetworkID int64
ChainID string
ResultsVotes string
}
func (q *Queries) CreateProcess(ctx context.Context, arg CreateProcessParams) (sql.Result, error) {
return q.exec(ctx, q.createProcessStmt, createProcess,
arg.ID,
arg.EntityID,
arg.StartDate,
arg.EndDate,
arg.ManuallyEnded,
arg.Title,
arg.Description,
arg.VoteCount,
arg.HaveResults,
arg.FinalResults,
arg.CensusRoot,
arg.MaxCensusSize,
arg.CensusUri,
arg.Metadata,
arg.CensusOrigin,
arg.Status,
arg.Namespace,
arg.Envelope,
arg.Mode,
arg.VoteOpts,
arg.PrivateKeys,
arg.PublicKeys,
arg.QuestionIndex,
arg.CreationTime,
arg.SourceBlockHeight,
arg.SourceNetworkID,
arg.ChainID,
arg.ResultsVotes,
)
}
const getEntityCount = `-- name: GetEntityCount :one
SELECT COUNT(DISTINCT entity_id) FROM processes
`
func (q *Queries) GetEntityCount(ctx context.Context) (int64, error) {
row := q.queryRow(ctx, q.getEntityCountStmt, getEntityCount)
var count int64
err := row.Scan(&count)
return count, err
}
const getProcess = `-- name: GetProcess :one
SELECT id, entity_id, start_date, end_date, vote_count, chain_id, have_results, final_results, results_votes, results_weight, results_block_height, census_root, max_census_size, census_uri, metadata, census_origin, status, namespace, envelope, mode, vote_opts, private_keys, public_keys, question_index, creation_time, source_block_height, source_network_id, manually_ended, title, description FROM processes
WHERE id = ?
LIMIT 1
`
func (q *Queries) GetProcess(ctx context.Context, id types.ProcessID) (Process, error) {
row := q.queryRow(ctx, q.getProcessStmt, getProcess, id)
var i Process
err := row.Scan(
&i.ID,
&i.EntityID,
&i.StartDate,
&i.EndDate,
&i.VoteCount,
&i.ChainID,
&i.HaveResults,
&i.FinalResults,
&i.ResultsVotes,
&i.ResultsWeight,
&i.ResultsBlockHeight,
&i.CensusRoot,
&i.MaxCensusSize,
&i.CensusUri,
&i.Metadata,
&i.CensusOrigin,
&i.Status,
&i.Namespace,
&i.Envelope,
&i.Mode,
&i.VoteOpts,
&i.PrivateKeys,
&i.PublicKeys,
&i.QuestionIndex,
&i.CreationTime,
&i.SourceBlockHeight,
&i.SourceNetworkID,
&i.ManuallyEnded,
&i.Title,
&i.Description,
)
return i, err
}
const getProcessCount = `-- name: GetProcessCount :one
SELECT COUNT(*) FROM processes
`
func (q *Queries) GetProcessCount(ctx context.Context) (int64, error) {
row := q.queryRow(ctx, q.getProcessCountStmt, getProcessCount)
var count int64
err := row.Scan(&count)
return count, err
}
const getProcessIDsByFinalResults = `-- name: GetProcessIDsByFinalResults :many
SELECT id FROM processes
WHERE final_results = ?
`
func (q *Queries) GetProcessIDsByFinalResults(ctx context.Context, finalResults bool) ([]types.ProcessID, error) {
rows, err := q.query(ctx, q.getProcessIDsByFinalResultsStmt, getProcessIDsByFinalResults, finalResults)
if err != nil {
return nil, err
}
defer rows.Close()
var items []types.ProcessID
for rows.Next() {
var id types.ProcessID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getProcessStatus = `-- name: GetProcessStatus :one
SELECT status FROM processes
WHERE id = ?
LIMIT 1
`
func (q *Queries) GetProcessStatus(ctx context.Context, id types.ProcessID) (int64, error) {
row := q.queryRow(ctx, q.getProcessStatusStmt, getProcessStatus, id)
var status int64
err := row.Scan(&status)
return status, err
}
const searchEntities = `-- name: SearchEntities :many
WITH results AS (
SELECT id, entity_id, start_date, end_date, vote_count, chain_id, have_results, final_results, results_votes, results_weight, results_block_height, census_root, max_census_size, census_uri, metadata, census_origin, status, namespace, envelope, mode, vote_opts, private_keys, public_keys, question_index, creation_time, source_block_height, source_network_id, manually_ended, title, description
FROM processes
WHERE (?3 = '' OR (INSTR(LOWER(HEX(entity_id)), ?3) > 0))
)
SELECT entity_id,
COUNT(id) AS process_count,
COUNT(entity_id) OVER() AS total_count
FROM results
GROUP BY entity_id
ORDER BY creation_time DESC, id ASC
LIMIT ?2
OFFSET ?1
`
type SearchEntitiesParams struct {
Offset int64
Limit int64
EntityIDSubstr interface{}
}
type SearchEntitiesRow struct {
EntityID []byte
ProcessCount int64
TotalCount int64
}
func (q *Queries) SearchEntities(ctx context.Context, arg SearchEntitiesParams) ([]SearchEntitiesRow, error) {
rows, err := q.query(ctx, q.searchEntitiesStmt, searchEntities, arg.Offset, arg.Limit, arg.EntityIDSubstr)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SearchEntitiesRow
for rows.Next() {
var i SearchEntitiesRow
if err := rows.Scan(&i.EntityID, &i.ProcessCount, &i.TotalCount); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const searchProcesses = `-- name: SearchProcesses :many
WITH results AS (
SELECT id, entity_id, start_date, end_date, vote_count, chain_id, have_results, final_results, results_votes, results_weight, results_block_height, census_root, max_census_size, census_uri, metadata, census_origin, status, namespace, envelope, mode, vote_opts, private_keys, public_keys, question_index, creation_time, source_block_height, source_network_id, manually_ended, title, description,
COUNT(*) OVER() AS total_count
FROM processes
WHERE (
LENGTH(?3) <= 40 -- if passed arg is longer, then just abort the query
AND (
?3 = ''
OR (LENGTH(?3) = 40 AND LOWER(HEX(entity_id)) = LOWER(?3))
OR (LENGTH(?3) < 40 AND INSTR(LOWER(HEX(entity_id)), LOWER(?3)) > 0)
-- TODO: consider keeping an entity_id_hex column for faster searches
)
-- TODO: replace this simple INSTR of title and description with something better
-- like https://www.sqlite.org/fts5.html
AND (?4 = '' OR INSTR(LOWER(title), LOWER(?4)) > 0)
AND (?5 = '' OR INSTR(LOWER(description), LOWER(?5)) > 0)
AND (?6 = 0 OR namespace = ?6)
AND (?7 = 0 OR status = ?7)
AND (?8 = 0 OR source_network_id = ?8)
AND LENGTH(?9) <= 64 -- if passed arg is longer, then just abort the query
AND (
?9 = ''
OR (LENGTH(?9) = 64 AND LOWER(HEX(id)) = LOWER(?9))
OR (LENGTH(?9) < 64 AND INSTR(LOWER(HEX(id)), LOWER(?9)) > 0)
-- TODO: consider keeping an id_hex column for faster searches
)
AND (
?10 = -1
OR (?10 = 1 AND have_results = TRUE)
OR (?10 = 0 AND have_results = FALSE)
)
AND (
?11 = -1
OR (?11 = 1 AND final_results = TRUE)
OR (?11 = 0 AND final_results = FALSE)
)
AND (
?12 = -1
OR (?12 = 1 AND manually_ended = TRUE)
OR (?12 = 0 AND manually_ended = FALSE)
)
AND (?13 IS NULL OR start_date >= ?13)
AND (?14 IS NULL OR start_date <= ?14)
AND (?15 IS NULL OR end_date >= ?15)
AND (?16 IS NULL OR end_date <= ?16)
)
)
SELECT id, total_count
FROM results
ORDER BY creation_time DESC, id ASC
LIMIT ?2
OFFSET ?1
`
type SearchProcessesParams struct {
Offset int64
Limit int64
EntityIDSubstr interface{}
Title interface{}
Description interface{}
Namespace interface{}
Status interface{}
SourceNetworkID interface{}
IDSubstr interface{}
HaveResults interface{}
FinalResults interface{}
ManuallyEnded interface{}
StartDateAfter interface{}
StartDateBefore interface{}
EndDateAfter interface{}
EndDateBefore interface{}
}
type SearchProcessesRow struct {
ID []byte
TotalCount int64
}
func (q *Queries) SearchProcesses(ctx context.Context, arg SearchProcessesParams) ([]SearchProcessesRow, error) {
rows, err := q.query(ctx, q.searchProcessesStmt, searchProcesses,
arg.Offset,
arg.Limit,
arg.EntityIDSubstr,
arg.Title,
arg.Description,
arg.Namespace,
arg.Status,
arg.SourceNetworkID,
arg.IDSubstr,
arg.HaveResults,
arg.FinalResults,
arg.ManuallyEnded,
arg.StartDateAfter,
arg.StartDateBefore,
arg.EndDateAfter,
arg.EndDateBefore,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SearchProcessesRow
for rows.Next() {
var i SearchProcessesRow
if err := rows.Scan(&i.ID, &i.TotalCount); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const setProcessResultsCancelled = `-- name: SetProcessResultsCancelled :execresult
UPDATE processes
SET have_results = FALSE, final_results = TRUE,
end_date = ?1,
manually_ended = ?2
WHERE id = ?3
`
type SetProcessResultsCancelledParams struct {
EndDate time.Time
ManuallyEnded bool
ID types.ProcessID
}
func (q *Queries) SetProcessResultsCancelled(ctx context.Context, arg SetProcessResultsCancelledParams) (sql.Result, error) {
return q.exec(ctx, q.setProcessResultsCancelledStmt, setProcessResultsCancelled, arg.EndDate, arg.ManuallyEnded, arg.ID)
}
const setProcessResultsReady = `-- name: SetProcessResultsReady :execresult
UPDATE processes
SET have_results = TRUE, final_results = TRUE,
results_votes = ?1,
results_weight = ?2,
results_block_height = ?3,
end_date = ?4
WHERE id = ?5
`
type SetProcessResultsReadyParams struct {
Votes string
Weight string
BlockHeight int64
EndDate time.Time
ID types.ProcessID
}
func (q *Queries) SetProcessResultsReady(ctx context.Context, arg SetProcessResultsReadyParams) (sql.Result, error) {
return q.exec(ctx, q.setProcessResultsReadyStmt, setProcessResultsReady,
arg.Votes,
arg.Weight,
arg.BlockHeight,
arg.EndDate,
arg.ID,
)
}
const updateProcessEndDate = `-- name: UpdateProcessEndDate :execresult
UPDATE processes
SET end_date = ?1,
manually_ended = ?2
WHERE id = ?3
`
type UpdateProcessEndDateParams struct {
EndDate time.Time
ManuallyEnded bool
ID types.ProcessID
}
func (q *Queries) UpdateProcessEndDate(ctx context.Context, arg UpdateProcessEndDateParams) (sql.Result, error) {
return q.exec(ctx, q.updateProcessEndDateStmt, updateProcessEndDate, arg.EndDate, arg.ManuallyEnded, arg.ID)
}
const updateProcessFromState = `-- name: UpdateProcessFromState :execresult
UPDATE processes
SET census_root = ?1,
census_uri = ?2,
private_keys = ?3,
public_keys = ?4,
metadata = ?5,
status = ?6,
max_census_size = ?7,
end_date = ?8
WHERE id = ?9
`
type UpdateProcessFromStateParams struct {
CensusRoot types.CensusRoot
CensusUri string
PrivateKeys string
PublicKeys string
Metadata string
Status int64
MaxCensusSize int64
EndDate time.Time
ID types.ProcessID
}
func (q *Queries) UpdateProcessFromState(ctx context.Context, arg UpdateProcessFromStateParams) (sql.Result, error) {
return q.exec(ctx, q.updateProcessFromStateStmt, updateProcessFromState,
arg.CensusRoot,
arg.CensusUri,
arg.PrivateKeys,
arg.PublicKeys,
arg.Metadata,
arg.Status,
arg.MaxCensusSize,
arg.EndDate,
arg.ID,
)
}
const updateProcessResultByID = `-- name: UpdateProcessResultByID :execresult
UPDATE processes
SET results_votes = ?1,
results_weight = ?2,
vote_opts = ?3,
envelope = ?4
WHERE id = ?5
`
type UpdateProcessResultByIDParams struct {
Votes string
Weight string
VoteOpts []byte
Envelope []byte
ID types.ProcessID
}
func (q *Queries) UpdateProcessResultByID(ctx context.Context, arg UpdateProcessResultByIDParams) (sql.Result, error) {
return q.exec(ctx, q.updateProcessResultByIDStmt, updateProcessResultByID,
arg.Votes,
arg.Weight,
arg.VoteOpts,
arg.Envelope,
arg.ID,
)
}
const updateProcessResults = `-- name: UpdateProcessResults :execresult
UPDATE processes
SET results_votes = ?1,
results_weight = ?2,
results_block_height = ?3
WHERE id = ?4 AND final_results = FALSE
`
type UpdateProcessResultsParams struct {
Votes string
Weight string
BlockHeight int64
ID types.ProcessID
}
func (q *Queries) UpdateProcessResults(ctx context.Context, arg UpdateProcessResultsParams) (sql.Result, error) {
return q.exec(ctx, q.updateProcessResultsStmt, updateProcessResults,
arg.Votes,
arg.Weight,
arg.BlockHeight,
arg.ID,
)
}