-
Notifications
You must be signed in to change notification settings - Fork 9
/
result.go
329 lines (287 loc) · 7.07 KB
/
result.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright 2018. bigpigeon. All rights reserved.
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*/
package toyorm
import (
"bytes"
"database/sql"
"errors"
"fmt"
)
type Pair struct {
Main int
Sub int
}
type Result struct {
Records ModelRecords
ActionFlow []SqlAction
RecordsActions map[int][]int
Preload map[string]*Result
// container is simple object
SimpleRelation map[string]map[int]int
// container is slice object
MultipleRelation map[string]map[int]Pair
// in many-to-many model, have a middle model query need to record
MiddleModelPreload map[string]*Result
}
func (r *Result) Err() error {
var errStr string
for _, action := range r.ActionFlow {
if err := action.Err(); err != nil {
errStr += fmt.Sprintf("%s errors(\n%s\n)\n", action.String(), err)
}
}
for name, preload := range r.Preload {
err := preload.Err()
if err != nil {
errStr += fmt.Sprintf("Preload %s errors:\n%s", name, err)
}
}
for name, preload := range r.MiddleModelPreload {
err := preload.Err()
if err != nil {
errStr += fmt.Sprintf("Middle Preload %s errors:\n%s", name, err)
}
}
if errStr != "" {
return errors.New(errStr)
}
return nil
}
func (r *Result) AddRecord(q SqlAction) {
last := len(r.ActionFlow)
r.ActionFlow = append(r.ActionFlow, q)
for _, i := range q.AffectData() {
r.RecordsActions[i] = append(r.RecordsActions[i], last)
}
}
type AffectNode struct {
Val int
Ignore bool
Next *AffectNode
}
func NewAffectNode(val int, next *AffectNode) *AffectNode {
return &AffectNode{val, false, next}
}
func IgnoreAffectNode(next *AffectNode) *AffectNode {
return &AffectNode{0, true, next}
}
type ReportData struct {
Depth int
AffectData []*AffectNode
Str string
}
func (r *Result) report() (reportData []ReportData) {
for _, action := range r.ActionFlow {
data := ReportData{0, nil, action.String()}
for _, d := range action.AffectData() {
data.AffectData = append(data.AffectData, NewAffectNode(d, nil))
}
reportData = append(reportData, data)
}
if len(r.Preload) != 0 {
for name, preloadResults := range r.Preload {
reportData = append(reportData, ReportData{1, nil, "preload " + name})
for _, oldData := range preloadResults.report() {
var data ReportData
if oldData.AffectData == nil {
data = ReportData{oldData.Depth + 1, nil, oldData.Str}
} else {
data = ReportData{oldData.Depth + 1, make([]*AffectNode, len(oldData.AffectData)), oldData.Str}
if relation := r.SimpleRelation[name]; relation != nil {
for i, node := range oldData.AffectData {
a := NewAffectNode(relation[node.Val], IgnoreAffectNode(node.Next))
data.AffectData[i] = a
}
} else if relation := r.MultipleRelation[name]; relation != nil {
for i, node := range oldData.AffectData {
pair := relation[node.Val]
data.AffectData[i] = NewAffectNode(pair.Main, NewAffectNode(pair.Sub, node.Next))
}
} else {
panic("relation map not found")
}
}
reportData = append(reportData, data)
}
}
}
return reportData
}
// Rename to Log
func (r *Result) Report() string {
reportData := r.report()
var buf bytes.Buffer
for _, report := range reportData {
for i := 0; i < report.Depth; i++ {
buf.WriteByte('\t')
}
if report.AffectData == nil {
buf.WriteString(report.Str + "\n")
} else {
buf.WriteByte('[')
for _, affect := range report.AffectData {
if affect != nil {
if affect.Ignore {
buf.WriteString("-")
} else {
buf.WriteString(fmt.Sprintf("%d", affect.Val))
}
affect = affect.Next
}
for affect != nil {
if affect.Ignore {
buf.WriteString("-")
} else {
buf.WriteString(fmt.Sprintf("-%d", affect.Val))
}
affect = affect.Next
}
buf.WriteString(", ")
}
buf.WriteByte(']')
buf.WriteString(" " + report.Str)
buf.WriteByte('\n')
}
}
return buf.String()
}
type SqlActionType int
const (
ResultActionExec = SqlActionType(iota)
ResultActionQuery
)
type SqlAction interface {
String() string
Type() SqlActionType
AffectData() []int
SetAffectData([]int)
Err() error
}
type ExecAction struct {
//Type ResultType
Exec ExecValue
Result sql.Result
affectData []int
Error error
}
func (r ExecAction) String() string {
return fmt.Sprintf("%s args:%s", r.Exec.Query(), r.Exec.JsonArgs())
}
func (r ExecAction) Type() SqlActionType {
return ResultActionExec
}
func (r ExecAction) AffectData() []int {
return r.affectData
}
func (r ExecAction) SetAffectData(d []int) {
r.affectData = d
}
func (r ExecAction) Err() error {
return r.Error
}
type QueryAction struct {
Exec ExecValue
affectData []int
Error []error
}
func (r QueryAction) String() string {
var errors []error
for _, err := range r.Error {
if err != nil {
errors = append(errors, err)
}
}
if len(errors) != 0 {
return fmt.Sprintf("%s args:%s error(%v)", r.Exec.Query(), r.Exec.JsonArgs(), errors)
}
return fmt.Sprintf("%s args:%s", r.Exec.Query(), r.Exec.JsonArgs())
}
func (r QueryAction) Type() SqlActionType {
return ResultActionQuery
}
func (r QueryAction) AffectData() []int {
return r.affectData
}
func (r QueryAction) SetAffectData(d []int) {
r.affectData = d
}
func (r QueryAction) Err() error {
errs := ""
for i, err := range r.Error {
if err != nil {
errs += fmt.Sprintf("\t[%d]%s\n", i, err)
}
}
if errs != "" {
return errors.New(errs)
}
return nil
}
type CollectionExecAction struct {
//Type ResultType
Exec ExecValue
Result sql.Result
dbIndex int
affectData []int
Error error
}
func (r CollectionExecAction) String() string {
if r.Error != nil {
return fmt.Sprintf("db[%d] %s args:%s error(%v)", r.dbIndex, r.Exec.Query(), r.Exec.JsonArgs(), r.Error)
}
return fmt.Sprintf("db[%d] %s args:%s", r.dbIndex, r.Exec.Query(), r.Exec.JsonArgs())
}
func (r CollectionExecAction) Type() SqlActionType {
return ResultActionExec
}
func (r CollectionExecAction) AffectData() []int {
return r.affectData
}
func (r CollectionExecAction) SetAffectData(d []int) {
r.affectData = d
}
func (r CollectionExecAction) Err() error {
return r.Error
}
type CollectionQueryAction struct {
Exec ExecValue
affectData []int
Error []error
dbIndex int
}
func (r CollectionQueryAction) String() string {
var errors []error
for _, err := range r.Error {
if err != nil {
errors = append(errors, err)
}
}
if len(errors) != 0 {
return fmt.Sprintf("db[%d] %s args:%s error(%v)", r.dbIndex, r.Exec.Query(), r.Exec.JsonArgs(), errors)
}
return fmt.Sprintf("db[%d] %s args:%s", r.dbIndex, r.Exec.Query(), r.Exec.JsonArgs())
}
func (r CollectionQueryAction) Type() SqlActionType {
return ResultActionQuery
}
func (r CollectionQueryAction) AffectData() []int {
return r.affectData
}
func (r CollectionQueryAction) SetAffectData(d []int) {
r.affectData = d
}
func (r CollectionQueryAction) Err() error {
errs := ""
for i, err := range r.Error {
if err != nil {
errs += fmt.Sprintf("\t[%d]%s\n", i, err)
}
}
if errs != "" {
return errors.New(errs)
}
return nil
}