-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdbms_datastore.go
274 lines (246 loc) · 5.95 KB
/
rdbms_datastore.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package goquery
import (
"errors"
"fmt"
"io"
"log"
"reflect"
)
//@TODO panic on error is not complete
//implements the datastore interface
func NewRdbmsDataStore(config *RdbmsConfig) (DataStore, error) {
switch config.DbStore {
case "pgx":
db, err := NewPgxConnection(config)
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to connect to pgx datastore: %s", err))
}
return &RdbmsDataStore{&db}, nil
case "sqlx":
db, err := NewSqlxConnection(config)
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to connect to sqlx datastore: %s", err))
}
return &RdbmsDataStore{&db}, nil
default:
return nil, errors.New(fmt.Sprintf("Unsupported store type: %s", config.DbStore))
}
}
type RdbmsDataStore struct {
db RdbmsDb
}
func (sds *RdbmsDataStore) RdbmsDb() RdbmsDb {
return sds.db
}
func (sds *RdbmsDataStore) Connection() interface{} {
return sds.db.Connection()
}
func (sds *RdbmsDataStore) NewTransaction() (Tx, error) {
return sds.db.Transaction()
}
func (sds *RdbmsDataStore) Transaction(fn TransactionFunction) (err error) {
var tx Tx
tx, err = sds.NewTransaction()
if err != nil {
log.Printf("Unable to start transaction: %s\n", err)
return err
}
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}
txerr := tx.Rollback()
if txerr != nil {
log.Printf("Unable to rollback from transaction: %s", err)
}
} else {
err = tx.Commit()
if err != nil {
log.Printf("Unable to commit transaction: %s", err)
}
}
}()
fn(tx)
return err
}
func (sds *RdbmsDataStore) Fetch(tx *Tx, qi QueryInput, qo QueryOutput, dest interface{}) error {
sstmt, err := getSelectStatement(qi.DataSet, qi.StatementKey, qi.Statement, qi.Suffix, qi.StmtAppends, dest)
if err != nil {
return err
}
if qi.LogSql {
log.Println(sstmt)
}
if qo.rowFunction != nil {
rows, err := sds.FetchRows(tx, qi)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
if dest != nil {
err := rows.ScanStruct(dest)
if err != nil {
return err
}
}
err = qo.rowFunction(rows)
if err != nil {
if qi.PanicOnErr {
panic(err)
}
return err
}
}
return nil
} else {
switch qo.OutputFormat {
case JSON:
return sds.GetJSON(qo.Writer, qi, qo.Options)
case CSV:
return errors.New("CSV is not implemented.")
//return sds.GetCSV()
default:
if isSlice(dest) {
err = sds.db.Select(dest, tx, sstmt, qi.BindParams...)
} else {
err = sds.db.Get(dest, tx, sstmt, qi.BindParams...)
}
}
if err != nil && qi.PanicOnErr {
panic(err)
}
return err
}
}
func (sds *RdbmsDataStore) FetchRows(tx *Tx, qi QueryInput) (Rows, error) {
sstmt, err := getSelectStatement(qi.DataSet, qi.StatementKey, qi.Statement, qi.Suffix, qi.StmtAppends, nil)
if err != nil {
return nil, err
}
return sds.db.Query(tx, sstmt, qi.BindParams...)
}
func (sds *RdbmsDataStore) GetJSON(writer io.Writer, qi QueryInput, jo OutputOptions) error {
rows, err := sds.FetchRows(nil, qi)
if err != nil {
if qi.PanicOnErr {
panic(err)
}
return err
}
defer rows.Close()
return RowsToJSON(writer, rows, jo.ToCamelCase, jo.IsArray, jo.DateFormat, jo.OmitNull)
}
func (sds *RdbmsDataStore) GetCSV(qi QueryInput, co OutputOptions) (string, error) {
rows, err := sds.FetchRows(nil, qi)
if err != nil {
log.Println(err)
return "", err
}
defer rows.Close()
return RowsToCSV(rows, co.ToCamelCase, co.DateFormat)
}
func (sds *RdbmsDataStore) InsertRecs(tx *Tx, input InsertInput) error {
var err error
recs := input.Records
rval := reflect.ValueOf(recs)
rrecs := reflect.Indirect(rval)
if rrecs.Kind() == reflect.Slice {
if input.Batch {
err = sds.insertBatch(input.Dataset, rrecs, input.BatchSize)
} else {
if tx == nil {
err = sds.insertNewTrans(input.Dataset, rrecs)
} else {
err = sds.insert(input.Dataset, rrecs, tx)
}
}
} else {
err = sds.db.Insert(input.Dataset, recs, tx)
}
if err != nil && input.PanicOnErr {
panic(err)
}
return err
}
func (sds *RdbmsDataStore) Exec(tx *Tx, stmt string, params ...interface{}) error {
return sds.db.Exec(tx, stmt, params...)
}
func (sds *RdbmsDataStore) Execr(tx *Tx, stmt string, params ...interface{}) (ExecResult, error) {
return sds.db.Execr(tx, stmt, params...)
}
func (sds *RdbmsDataStore) MustExec(tx *Tx, stmt string, params ...interface{}) {
sds.db.MustExec(tx, stmt, params...)
}
func (sds *RdbmsDataStore) MustExecr(tx *Tx, stmt string, params ...interface{}) ExecResult {
return sds.db.MustExecr(tx, stmt, params...)
}
func (sds *RdbmsDataStore) insertNewTrans(ds DataSet, rrecs reflect.Value) error {
err := sds.Transaction(func(tx Tx) {
err := sds.insert(ds, rrecs, &tx)
if err != nil {
panic(err)
}
})
return err
}
func (sds *RdbmsDataStore) insert(ds DataSet, rrecs reflect.Value, tx *Tx) error {
for i := 0; i < rrecs.Len(); i++ {
err := sds.db.Insert(ds, rrecs.Index(i).Interface(), tx)
if err != nil {
log.Printf("Failed to insert: %s\n", err)
return err
}
}
return nil
}
func (sds *RdbmsDataStore) insertBatch(ds DataSet, rrecs reflect.Value, batchSize int) error {
batch, err := sds.db.Batch()
if err != nil {
return err
}
stmt, err := sds.db.InsertStmt(ds)
if err != nil {
return err
}
for i := 0; i < rrecs.Len(); i++ {
rec := rrecs.Index(i).Interface()
batch.Queue(stmt, StructToIArray(rec)...)
if i >= batchSize {
sds.db.SendBatch(batch)
batch, err = sds.db.Batch()
if err != nil {
return err
}
}
}
sds.db.SendBatch(batch)
return nil
}
func (sds *RdbmsDataStore) Select(stmt ...string) *FluentSelect {
stmts := ""
if len(stmt) > 0 && stmt[0] != "" {
stmts = stmt[0]
}
s := FluentSelect{
qi: QueryInput{
Statement: stmts,
},
store: sds,
}
s.CamelCase(true)
return &s
}
func (sds *RdbmsDataStore) Insert(ds DataSet) *FluentInsert {
fi := FluentInsert{
ds: ds,
store: sds,
}
return &fi
}