-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cond.go
187 lines (155 loc) · 3.13 KB
/
cond.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
package mogi
import (
"reflect"
"strings"
// "database/sql"
"database/sql/driver"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/guregu/mogi/internal/sqlparser"
)
type cond interface {
matches(in input) bool
priority() int
fmt.Stringer
}
type condchain []cond
func (chain condchain) matches(in input) bool {
for _, c := range chain {
if !c.matches(in) {
return false
}
}
return true
}
func (chain condchain) priority() int {
p := 0
for _, c := range chain {
p += c.priority()
}
return p
}
func (chain condchain) String() string {
return "Chain..."
}
type tableCond struct {
table string
}
func (tc tableCond) matches(in input) bool {
switch x := in.statement.(type) {
case *sqlparser.Insert:
return strings.ToLower(tc.table) == strings.ToLower(string(x.Table.Name))
case *sqlparser.Update:
return strings.ToLower(tc.table) == strings.ToLower(string(x.Table.Name))
case *sqlparser.Delete:
return strings.ToLower(tc.table) == strings.ToLower(string(x.Table.Name))
}
return false
}
func (tc tableCond) priority() int {
return 1
}
func (tc tableCond) String() string {
return fmt.Sprintf("TABLE %s", tc.table)
}
type argsCond struct {
args []driver.Value
}
func (ac argsCond) matches(in input) bool {
given := unifyValues(ac.args)
return reflect.DeepEqual(given, in.args)
}
func (ac argsCond) priority() int {
return 1
}
func (ac argsCond) String() string {
return fmt.Sprintf("WITH ARGS %+v", ac.args)
}
type valueCond struct {
row int
col string
v interface{}
}
func newValueCond(row int, col string, v interface{}) valueCond {
return valueCond{
row: row,
col: col,
v: unify(v),
}
}
func (vc valueCond) matches(in input) bool {
switch in.statement.(type) {
case *sqlparser.Insert:
values := in.rows()
if vc.row > len(values)-1 {
return false
}
v, ok := values[vc.row][vc.col]
if !ok {
return false
}
return equals(v, vc.v)
case *sqlparser.Update:
values := in.values()
v, ok := values[vc.col]
if !ok {
return false
}
return equals(v, vc.v)
}
return false
}
func (vc valueCond) priority() int {
return 1
}
func (vc valueCond) String() string {
return fmt.Sprintf("VALUE %s ≈ %v (row %d)", vc.col, vc.v, vc.row)
}
type priorityCond struct {
p int
}
func (pc priorityCond) matches(in input) bool {
return true
}
func (pc priorityCond) priority() int {
return pc.p
}
func (pc priorityCond) String() string {
return "PRIORITY"
}
type notifyCond struct {
ch chan<- struct{}
}
func (nc notifyCond) matches(in input) bool {
go func() {
nc.ch <- struct{}{}
}()
return true
}
func (nc notifyCond) priority() int {
return 0
}
func (nc notifyCond) String() string {
return "NOTIFY"
}
type dumpCond struct{}
func (dc dumpCond) matches(in input) bool {
fmt.Println(in.query)
spew.Dump(in.args)
switch in.statement.(type) {
case *sqlparser.Select:
spew.Dump(in.cols(), in.where())
case *sqlparser.Insert:
spew.Dump(in.cols(), in.rows())
case *sqlparser.Update:
spew.Dump(in.cols(), in.values(), in.where())
}
spew.Dump(in.statement)
return true
}
func (dc dumpCond) priority() int {
return 0
}
func (dc dumpCond) String() string {
return "DUMP"
}