This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
log_filter_repository.go
92 lines (78 loc) · 2.57 KB
/
log_filter_repository.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
// VulcanizeDB
// Copyright © 2019 Vulcanize
// 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 repositories
import (
"database/sql"
"encoding/json"
"errors"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/filters"
)
type FilterRepository struct {
*postgres.DB
}
func (filterRepository FilterRepository) CreateFilter(query filters.LogFilter) error {
_, err := filterRepository.DB.Exec(
`INSERT INTO log_filters
(name, from_block, to_block, address, topic0, topic1, topic2, topic3)
VALUES ($1, NULLIF($2, -1), NULLIF($3, -1), $4, NULLIF($5, ''), NULLIF($6, ''), NULLIF($7, ''), NULLIF($8, ''))`,
query.Name, query.FromBlock, query.ToBlock, query.Address, query.Topics[0], query.Topics[1], query.Topics[2], query.Topics[3])
if err != nil {
return err
}
return nil
}
func (filterRepository FilterRepository) GetFilter(name string) (filters.LogFilter, error) {
lf := DBLogFilter{}
err := filterRepository.DB.Get(&lf,
`SELECT
id,
name,
from_block,
to_block,
address,
json_build_array(topic0, topic1, topic2, topic3) AS topics
FROM log_filters
WHERE name = $1`, name)
if err != nil {
switch err {
case sql.ErrNoRows:
return filters.LogFilter{}, datastore.ErrFilterDoesNotExist(name)
default:
return filters.LogFilter{}, err
}
}
dbLogFilterToCoreLogFilter(lf)
return *lf.LogFilter, nil
}
type DBTopics []*string
func (t *DBTopics) Scan(src interface{}) error {
asBytes, ok := src.([]byte)
if !ok {
return error(errors.New("scan source was not []byte"))
}
return json.Unmarshal(asBytes, &t)
}
type DBLogFilter struct {
ID int
*filters.LogFilter
Topics DBTopics
}
func dbLogFilterToCoreLogFilter(lf DBLogFilter) {
for i, v := range lf.Topics {
if v != nil {
lf.LogFilter.Topics[i] = *v
}
}
}