-
Notifications
You must be signed in to change notification settings - Fork 329
/
rdbms_store.go
210 lines (164 loc) · 4.5 KB
/
rdbms_store.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
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
package rdbms
import (
"context"
"database/sql"
"fmt"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/errors"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/ddl"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exec"
"github.com/doug-martin/goqu/v9/exp"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)
type (
sqlizer interface {
ToSQL() (string, []interface{}, error)
}
scanner interface {
Scan(...interface{}) error
}
txRetryOnErrHandler func(int, error) bool
Functions struct {
// returns lower case text
LOWER func(interface{}) exp.SQLFunctionExpression
// returns date part of the input (YYYY-MM-DD)
DATE func(interface{}) exp.SQLFunctionExpression
}
Store struct {
DB sqlx.ExtContext
// DAL connection
DAL dal.Connection
// Logger for connection
Logger *zap.Logger
// data definer interface use for schema information lookups and modification
DataDefiner ddl.DataDefiner
Dialect drivers.Dialect
// set to -1 to disable transactions
TxRetryLimit int
// TxRetryErrHandler should return true if transaction should be retried
//
// Because retry algorithm varies between concrete rdbms implementations
//
// Handler must return true if failed transaction should be replied
// and false if we're safe to terminate it
TxRetryErrHandler txRetryOnErrHandler
ErrorHandler store.ErrorHandler
Functions *Functions
// additional (per-resource-type) filters used when searching
// these filters can modify expression used for querying the database
Filters *extendedFilters
Ping func(ctx context.Context) error
}
)
// SetDefaults fn sets all defaults that need to be set
func (s *Store) SetDefaults() {
if s.TxRetryErrHandler == nil {
s.TxRetryErrHandler = func(i int, err error) bool { return false }
}
if s.ErrorHandler == nil {
s.ErrorHandler = func(err error) error { return err }
}
if s.Functions == nil {
s.Functions = DefaultFunctions()
}
if s.Filters == nil {
s.Filters = DefaultFilters()
}
}
// WithTx spins up new store instance with transaction
func (s *Store) withTx(tx sqlx.ExtContext) *Store {
return &Store{
DB: tx,
Logger: s.Logger,
DataDefiner: s.DataDefiner,
Dialect: s.Dialect,
TxRetryErrHandler: s.TxRetryErrHandler,
ErrorHandler: s.ErrorHandler,
Functions: s.Functions,
Filters: s.Filters,
}
}
func (s Store) Healthcheck(ctx context.Context) error {
if s.Ping == nil {
return errors.Internal("no store ping function defined")
}
return s.Ping(ctx)
}
// Exec executes the SQL and drops the result
func (s Store) Exec(ctx context.Context, q sqlizer) error {
_, err := s.exec(ctx, q)
return err
}
// ExecR executes the SQL and returns the result
func (s Store) ExecR(ctx context.Context, q sqlizer) (rs sql.Result, _ error) {
return s.exec(ctx, q)
}
func (s Store) exec(ctx context.Context, q sqlizer) (rs sql.Result, _ error) {
var (
query, args, err = q.ToSQL()
)
if err != nil {
return rs, fmt.Errorf("could not build query: %w", err)
}
rs, err = s.DB.ExecContext(ctx, query, args...)
return rs, store.HandleError(err, s.ErrorHandler)
}
func (s Store) Query(ctx context.Context, q sqlizer) (*sql.Rows, error) {
var (
rr *sql.Rows
query, args, err = q.ToSQL()
)
if err != nil {
return nil, fmt.Errorf("could not build query: %w", err)
}
rr, err = s.DB.QueryContext(ctx, query, args...)
if err = store.HandleError(err, s.ErrorHandler); err != nil {
return nil, err
}
return rr, nil
}
func (s Store) QueryOne(ctx context.Context, q sqlizer, dst interface{}) (err error) {
var (
rows *sql.Rows
)
rows, err = s.Query(ctx, q)
if err != nil {
return
}
defer func() {
closeError := rows.Close()
if err == nil {
// return error from close
err = closeError
}
}()
if err = rows.Err(); err != nil {
return
}
if !rows.Next() {
return store.ErrNotFound.Stack(1)
}
return exec.NewScanner(rows).ScanStruct(dst)
}
// ToDalConn uses store as DAL connection
func (s Store) ToDalConn() dal.Connection {
return s.DAL
}
func DefaultFunctions() *Functions {
f := &Functions{}
if f.LOWER == nil {
f.LOWER = func(value interface{}) exp.SQLFunctionExpression {
return goqu.Func("LOWER", value)
}
}
if f.DATE == nil {
f.DATE = func(value interface{}) exp.SQLFunctionExpression {
return goqu.Func("DATE", value)
}
}
return f
}