-
Notifications
You must be signed in to change notification settings - Fork 19
/
accounts_filter.go
136 lines (113 loc) · 4.08 KB
/
accounts_filter.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sqlstore
import (
"fmt"
"code.vegaprotocol.io/vega/datanode/entities"
)
// Return an SQL query string and corresponding bind arguments to return rows
// from the account table filtered according to this AccountFilter.
func filterAccountsQuery(af entities.AccountFilter, includeVegaTime bool) (string, []interface{}, error) {
var args []interface{}
var err error
query := `SELECT id, party_id, asset_id, market_id, type, tx_hash FROM ACCOUNTS `
if includeVegaTime {
query = `SELECT id, party_id, asset_id, market_id, type, tx_hash, vega_time FROM ACCOUNTS `
}
if af.AssetID.String() != "" {
query = fmt.Sprintf("%s WHERE asset_id=%s", query, nextBindVar(&args, af.AssetID))
} else {
query = fmt.Sprintf("%s WHERE true", query)
}
if len(af.PartyIDs) > 0 {
partyIDs := make([][]byte, len(af.PartyIDs))
for i, party := range af.PartyIDs {
partyIDs[i], err = party.Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid party id: %w", err)
}
}
query += " AND party_id=ANY(" + nextBindVar(&args, partyIDs) + ")"
}
if len(af.AccountTypes) > 0 {
query += " AND type=ANY(" + nextBindVar(&args, af.AccountTypes) + ")"
}
if len(af.MarketIDs) > 0 {
marketIds := make([][]byte, len(af.MarketIDs))
for i, market := range af.MarketIDs {
marketIds[i], err = market.Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid market id: %w", err)
}
}
query += " AND market_id=ANY(" + nextBindVar(&args, marketIds) + ")"
}
return query, args, nil
}
func currentAccountBalancesQuery() string {
return `SELECT ACCOUNTS.id, ACCOUNTS.party_id, ACCOUNTS.asset_id, ACCOUNTS.market_id, ACCOUNTS.type,
current_balances.balance, current_balances.tx_hash, current_balances.vega_time
FROM ACCOUNTS JOIN current_balances ON ACCOUNTS.id = current_balances.account_id `
}
func accountBalancesQuery() string {
return `SELECT ACCOUNTS.id, ACCOUNTS.party_id, ACCOUNTS.asset_id, ACCOUNTS.market_id, ACCOUNTS.type,
balances.balance, balances.tx_hash, balances.vega_time
FROM ACCOUNTS JOIN balances ON ACCOUNTS.id = balances.account_id `
}
func filterAccountBalancesQuery(af entities.AccountFilter) (string, []interface{}, error) {
var args []interface{}
where := ""
and := ""
if len(af.AssetID.String()) != 0 {
where = fmt.Sprintf("ACCOUNTS.asset_id=%s", nextBindVar(&args, af.AssetID))
and = " AND "
}
if len(af.PartyIDs) > 0 {
partyIDs := make([][]byte, len(af.PartyIDs))
for i, party := range af.PartyIDs {
bytes, err := party.Bytes()
if err != nil {
return "", nil, fmt.Errorf("could not decode party ID: %w", err)
}
partyIDs[i] = bytes
}
where = fmt.Sprintf(`%s%sACCOUNTS.party_id=ANY(%s)`, where, and, nextBindVar(&args, partyIDs))
if and == "" {
and = " AND "
}
}
if len(af.AccountTypes) > 0 {
where = fmt.Sprintf(`%s%stype=ANY(%s)`, where, and, nextBindVar(&args, af.AccountTypes))
if and == "" {
and = " AND "
}
}
if len(af.MarketIDs) > 0 {
marketIDs := make([][]byte, len(af.MarketIDs))
for i, market := range af.MarketIDs {
bytes, err := market.Bytes()
if err != nil {
return "", nil, fmt.Errorf("could not decode market ID: %w", err)
}
marketIDs[i] = bytes
}
where = fmt.Sprintf(`%s%sACCOUNTS.market_id=ANY(%s)`, where, and, nextBindVar(&args, marketIDs))
}
query := currentAccountBalancesQuery()
if where != "" {
query = fmt.Sprintf("%s WHERE %s", query, where)
}
return query, args, nil
}