-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdealer.go
More file actions
217 lines (169 loc) · 4.61 KB
/
dealer.go
File metadata and controls
217 lines (169 loc) · 4.61 KB
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
package wpgx
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/jackc/pgx"
"github.com/pkg/errors"
)
// Dealer is an active subject, like an opened transaction, for query performing
//
// Deal! It executes query and loads result into a data collector. Pass nil when no result needed
//
// Load gets just one item from the database. When no collection needed
//
// Save inserts item into database. Result may need for getting new ID or properties
//
// Jail (aka Close) ends a transaction with commit or rollback respective to the flag
type Dealer interface {
Cook(text string, cols ...string) (string, error)
Deal(result Collector, query string, args ...interface{}) error
Load(item Shaper, query string, args ...interface{}) error
Save(item Shaper, key string, result Collector) error
Jail(commit bool) error
}
type tx struct {
*pgx.Tx
c *conn
}
func (t *tx) ready() error {
if t == nil || t.Tx == nil || t.c == nil {
return ErrConnClosed
}
return t.c.ready()
}
func (t *tx) Cook(text string, cols ...string) (key string, err error) {
const emsg = "preparing statement"
if err = t.ready(); err != nil {
return "", errors.Wrap(err, emsg)
}
sum := sha1.Sum([]byte(text))
key = hex.EncodeToString(sum[:])
if _, err = t.Tx.Prepare(key, text); err != nil {
return "", errors.Wrap(err, emsg)
}
t.c.Lock()
t.c.statements[key] = cols
t.c.Unlock()
if t.c.reservePath == "" {
return
}
path := filepath.Join(t.c.reservePath, key+".pgsql")
err = ioutil.WriteFile(path, []byte(text), 0755)
return key, errors.Wrap(err, emsg)
}
func (t *tx) Deal(result Collector, query string, args ...interface{}) (err error) {
if err = t.ready(); err != nil {
return errors.Wrap(err, "executing query")
}
if result == nil {
_, err = t.Exec(query, args...)
return errors.Wrap(err, "executing query")
}
var rows *pgx.Rows
if rows, err = t.Query(query, args...); err != nil {
return errors.Wrap(err, "selecting data")
}
defer rows.Close()
names := rows.FieldDescriptions()
places := make([]interface{}, len(names))
for rows.Next() {
item := result.NewItem()
if item == nil {
break
}
model := item.Extrude()
for i := range names {
places[i] = model.Translate(names[i].Name)
}
if err = rows.Scan(places...); err != nil {
return errors.Wrap(err, "scanning data row")
}
if err = item.Receive(model); err != nil {
return errors.Wrap(err, "receiving model")
}
if err = result.Collect(item); err != nil {
return errors.Wrap(err, "collecting item")
}
}
return errors.Wrap(rows.Err(), "checking result")
}
func (t *tx) Load(item Shaper, query string, args ...interface{}) (err error) {
if err = t.ready(); err != nil {
return errors.Wrap(err, "loading item")
}
var rows *pgx.Rows
if rows, err = t.Query(query, args...); err != nil {
return errors.Wrap(err, "selecting data")
}
defer rows.Close()
names := rows.FieldDescriptions()
places := make([]interface{}, len(names))
if rows.Next() {
model := item.Extrude()
for i := range names {
places[i] = model.Translate(names[i].Name)
}
if err = rows.Scan(places...); err != nil {
return errors.Wrap(err, "scanning data row")
}
if err = item.Receive(model); err != nil {
return errors.Wrap(err, "receiving model")
}
}
return errors.Wrap(rows.Err(), "checking result")
}
func (t *tx) Save(item Shaper, key string, result Collector) (err error) {
if err = t.ready(); err != nil {
return errors.Wrap(err, "saving item")
}
t.c.RLock()
cols, ok := t.c.statements[key]
t.c.RUnlock()
if !ok {
return errors.New("unknown prepared query key: " + key)
}
args := make([]interface{}, len(cols))
model := item.Extrude()
for i := range cols {
args[i] = model.Translate(cols[i])
}
defer func() {
if err == nil || t.c.reservePath == "" {
return
}
const emsg = "reserving data"
const etpl = "\n%+v\nreserve data: %+v"
dump := make(map[string]interface{})
for i := range cols {
dump[cols[i]] = args[i]
}
text, ex := json.MarshalIndent(dump, "", " ")
if ex != nil {
fmt.Printf(etpl, errors.Wrap(ex, emsg), dump)
}
sum := sha1.Sum([]byte(text))
hash := hex.EncodeToString(sum[:])
path := filepath.Join(t.c.reservePath, key+"_"+hash+".json")
if ex = ioutil.WriteFile(path, text, 0755); ex != nil {
fmt.Printf(etpl, errors.Wrap(ex, emsg), dump)
}
}()
return t.Deal(result, key, args...)
}
func (t *tx) Jail(commit bool) (err error) {
const emsg = "closing transaction"
defer func() {
t.Rollback()
t.Tx = nil
t.c = nil
t = nil
}()
if !commit {
return errors.Wrap(t.Rollback(), emsg)
}
return errors.Wrap(t.Commit(), emsg)
}