-
Notifications
You must be signed in to change notification settings - Fork 19
/
ledgerentry_filter.go
147 lines (120 loc) · 4.55 KB
/
ledgerentry_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
137
138
139
140
141
142
143
144
145
146
147
package sqlstore
import (
"errors"
"fmt"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/protos/vega"
)
var (
ErrLedgerEntryFilterForParty = errors.New("filtering ledger entries should be limited to a single party")
ErrLedgerEntryExportForParty = errors.New("exporting ledger entries should be limited to a single party")
ErrLedgerEntryExportForAsset = errors.New("exporting ledger entries should be limited to a single asset")
)
// Return an SQL query string and corresponding bind arguments to return
// ledger entries rows resulting from different filter options.
func filterLedgerEntriesQuery(filter *entities.LedgerEntryFilter) ([3]string, []interface{}, error) {
err := handlePartiesFiltering(filter)
if err != nil {
return [3]string{}, nil, err
}
var args []interface{}
filterQueries := [3]string{}
// FromAccount filter
fromAccountDBQuery, nargs, err := accountFilterToDBQuery(filter.FromAccountFilter, &args, "account_from_")
if err != nil {
return [3]string{}, nil, fmt.Errorf("error parsing fromAccount filter values: %w", err)
}
args = *nargs
// ToAccount filter
toAccountDBQuery, nargs, err := accountFilterToDBQuery(filter.ToAccountFilter, &args, "account_to_")
if err != nil {
return [3]string{}, nil, fmt.Errorf("error parsing fromAccount filter values: %w", err)
}
args = *nargs
// TransferTypeFilters
accountTransferTypeDBQuery := transferTypeFilterToDBQuery(filter.TransferTypes, &args)
filterQueries[0] = fromAccountDBQuery
filterQueries[1] = toAccountDBQuery
filterQueries[2] = accountTransferTypeDBQuery
return filterQueries, args, nil
}
// accountFilterToDBQuery creates a DB query section string from the given account filter values.
func accountFilterToDBQuery(af entities.AccountFilter, args *[]interface{}, prefix string) (string, *[]interface{}, error) {
var (
singleAccountFilter string
err error
)
// Asset filtering
if af.AssetID.String() != "" {
singleAccountFilter = fmt.Sprintf("%sasset_id=%s", singleAccountFilter, nextBindVar(args, af.AssetID))
}
// Party filtering
if len(af.PartyIDs) == 1 {
partyIDAsBytes, err := af.PartyIDs[0].Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid party id: %w", err)
}
if singleAccountFilter != "" {
singleAccountFilter = fmt.Sprintf(`%s AND %sparty_id=%s`, singleAccountFilter, prefix, nextBindVar(args, partyIDAsBytes))
} else {
singleAccountFilter = fmt.Sprintf(`%sparty_id=%s`, prefix, nextBindVar(args, partyIDAsBytes))
}
}
// Market filtering
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)
}
}
if singleAccountFilter != "" {
singleAccountFilter = fmt.Sprintf(`%s AND %smarket_id=ANY(%s)`, singleAccountFilter, prefix, nextBindVar(args, marketIds))
} else {
singleAccountFilter = fmt.Sprintf(`%smarket_id=ANY(%s)`, prefix, nextBindVar(args, marketIds))
}
}
// Account types filtering
if len(af.AccountTypes) > 0 {
if singleAccountFilter != "" {
singleAccountFilter = fmt.Sprintf(`%s AND %saccount_type=ANY(%s)`, singleAccountFilter, prefix, nextBindVar(args, af.AccountTypes))
} else {
singleAccountFilter = fmt.Sprintf(`%saccount_type=ANY(%s)`, prefix, nextBindVar(args, af.AccountTypes))
}
}
return singleAccountFilter, args, nil
}
func transferTypeFilterToDBQuery(transferTypeFilter []entities.LedgerMovementType, args *[]interface{}) string {
transferTypeFilterString := ""
if len(transferTypeFilter) > 0 {
for i, transferType := range transferTypeFilter {
_, ok := vega.TransferType_name[int32(transferType)]
if !ok {
continue
}
transferTypeFilterString = fmt.Sprintf(`%stransfer_type=%s`, transferTypeFilterString, nextBindVar(args, transferType))
if i < len(transferTypeFilter)-1 {
transferTypeFilterString = fmt.Sprintf(`%s OR `, transferTypeFilterString)
}
}
}
return transferTypeFilterString
}
func handlePartiesFiltering(filter *entities.LedgerEntryFilter) error {
var partyIDFrom entities.PartyID
var partyIDTo entities.PartyID
if len(filter.FromAccountFilter.PartyIDs) > 1 || len(filter.ToAccountFilter.PartyIDs) > 1 {
return ErrLedgerEntryFilterForParty
}
if len(filter.FromAccountFilter.PartyIDs) > 0 {
partyIDFrom = filter.FromAccountFilter.PartyIDs[0]
}
if len(filter.ToAccountFilter.PartyIDs) > 0 {
partyIDTo = filter.ToAccountFilter.PartyIDs[0]
}
if partyIDFrom == "" && partyIDTo == "" {
return ErrLedgerEntryFilterForParty
}
return nil
}