-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
orm.go
130 lines (108 loc) · 3.33 KB
/
orm.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
package evm
import (
"github.com/pkg/errors"
"github.com/smartcontractkit/sqlx"
null "gopkg.in/guregu/null.v4"
"github.com/smartcontractkit/chainlink/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/core/utils"
)
type ORM interface {
CreateChain(id utils.Big, config types.ChainCfg) (types.Chain, error)
DeleteChain(id utils.Big) error
Chains(offset, limit int) ([]types.Chain, int, error)
CreateNode(data NewNode) (types.Node, error)
DeleteNode(id int64) error
Nodes(offset, limit int) ([]types.Node, int, error)
NodesForChain(chainID utils.Big, offset, limit int) ([]types.Node, int, error)
}
type orm struct {
db *sqlx.DB
}
var _ ORM = (*orm)(nil)
func NewORM(db *sqlx.DB) ORM {
return &orm{db}
}
var ErrNoRowsAffected = errors.New("no rows affected")
func (o *orm) CreateChain(id utils.Big, config types.ChainCfg) (chain types.Chain, err error) {
sql := `INSERT INTO evm_chains (id, cfg, created_at, updated_at) VALUES ($1, $2, now(), now()) RETURNING *`
err = o.db.Get(&chain, sql, id, config)
return chain, err
}
func (o *orm) DeleteChain(id utils.Big) error {
sql := `DELETE FROM evm_chains WHERE id = $1`
result, err := o.db.Exec(sql, id)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return ErrNoRowsAffected
}
return nil
}
func (o *orm) Chains(offset, limit int) (chains []types.Chain, count int, err error) {
if err = o.db.Get(&count, "SELECT COUNT(*) FROM evm_chains"); err != nil {
return
}
sql := `SELECT * FROM evm_chains ORDER BY created_at, id LIMIT $1 OFFSET $2;`
if err = o.db.Select(&chains, sql, limit, offset); err != nil {
return
}
return
}
type NewNode struct {
Name string `json:"name"`
EVMChainID utils.Big `json:"evmChainId"`
WSURL null.String `json:"wsURL" db:"ws_url"`
HTTPURL string `json:"httpURL" db:"http_url"`
SendOnly bool `json:"sendOnly"`
}
func (o *orm) CreateNode(data NewNode) (node types.Node, err error) {
sql := `INSERT INTO nodes (name, evm_chain_id, ws_url, http_url, send_only, created_at, updated_at)
VALUES (:name, :evm_chain_id, :ws_url, :http_url, :send_only, now(), now())
RETURNING *;`
stmt, err := o.db.PrepareNamed(sql)
if err != nil {
return node, err
}
err = stmt.Get(&node, data)
return node, err
}
func (o *orm) DeleteNode(id int64) error {
sql := `DELETE FROM nodes WHERE id = $1`
result, err := o.db.Exec(sql, id)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return ErrNoRowsAffected
}
return nil
}
func (o *orm) Nodes(offset, limit int) (nodes []types.Node, count int, err error) {
if err = o.db.Get(&count, "SELECT COUNT(*) FROM nodes"); err != nil {
return
}
sql := `SELECT * FROM nodes ORDER BY created_at, id LIMIT $1 OFFSET $2;`
if err = o.db.Select(&nodes, sql, limit, offset); err != nil {
return
}
return
}
func (o *orm) NodesForChain(chainID utils.Big, offset, limit int) (nodes []types.Node, count int, err error) {
if err = o.db.Get(&count, "SELECT COUNT(*) FROM nodes WHERE evm_chain_id = $1", chainID); err != nil {
return
}
sql := `SELECT * FROM nodes WHERE evm_chain_id = $1 ORDER BY created_at, id LIMIT $2 OFFSET $3;`
if err = o.db.Select(&nodes, sql, chainID, limit, offset); err != nil {
return
}
return
}