This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
/
stmt.go
262 lines (200 loc) · 5.67 KB
/
stmt.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
package infos
import (
"fmt"
"strings"
"unicode"
"github.com/beevik/etree"
"github.com/huangjunwen/sqlw/datasrc"
)
// StmtInfo contains information of a statement.
type StmtInfo struct {
stmtType string // 'SELECT'/'UPDATE'/'INSERT'/'DELETE'
stmtName string
text string
resultCols []*datasrc.Column // for SELECT stmt only
locals map[interface{}]interface{} // directive locals
}
// NewStmtInfo creates a new StmtInfo from an xml element, example statement xml element:
//
// <stmt name="BlogByUser">
// <arg name="userId" type="int" />
// SELECT <wc table="blog" /> FROM blog WHERE user_id=<repl with=":userId">1</repl>
// </stmt>
//
// A statement xml element contains SQL statement fragments and special directives.
func NewStmtInfo(loader *datasrc.Loader, db *DBInfo, stmtElem *etree.Element) (*StmtInfo, error) {
info := &StmtInfo{
locals: map[interface{}]interface{}{},
}
if stmtElem.Tag != "stmt" {
return nil, fmt.Errorf("Expect <stmt> but got <%s>", stmtElem.Tag)
}
// Name attribute
info.stmtName = stmtElem.SelectAttrValue("name", "")
if info.stmtName == "" {
return nil, fmt.Errorf("Missing 'name' attribute of <%s>", info.stmtType)
}
// Process it.
if err := info.process(loader, db, stmtElem); err != nil {
return nil, err
}
return info, nil
}
func (info *StmtInfo) token2TerminalDirectives(loader *datasrc.Loader, db *DBInfo, token etree.Token) ([]TerminalDirective, error) {
directive := Directive(nil)
// Token -> Directive.
switch tok := token.(type) {
case *etree.CharData:
directive = &textDirective{}
case *etree.Element:
factory := directiveFactories[tok.Tag]
if factory == nil {
return nil, fmt.Errorf("Unknown directive <%s>", tok.Tag)
}
directive = factory()
default:
return nil, nil
}
// Initialize
if err := directive.Initialize(loader, db, info, token); err != nil {
return nil, err
}
// Expand directive recursively if it is NonterminalDirective.
switch d := directive.(type) {
case TerminalDirective:
return []TerminalDirective{d}, nil
case NonterminalDirective:
ts, err := d.Expand()
if err != nil {
return nil, err
}
ret := []TerminalDirective{}
for _, t := range ts {
terminalDirectives, err := info.token2TerminalDirectives(loader, db, t)
if err != nil {
return nil, err
}
ret = append(ret, terminalDirectives...)
}
return ret, nil
default:
panic(fmt.Errorf("Directive must be either TerminalDirective or NonterminalDirective"))
}
}
func (info *StmtInfo) process(loader *datasrc.Loader, db *DBInfo, stmtElem *etree.Element) error {
// Convert stmtElem to a list of TerminalDirective
directives := []TerminalDirective{}
for _, token := range stmtElem.Child {
ds, err := info.token2TerminalDirectives(loader, db, token)
if err != nil {
return err
}
directives = append(directives, ds...)
}
// Construct query.
query := ""
{
fragments := []string{}
for _, directive := range directives {
fragment, err := directive.QueryFragment()
if err != nil {
return err
}
fragments = append(fragments, fragment)
}
query = strings.TrimSpace(strings.Join(fragments, ""))
}
// Determine statement type.
{
sp := strings.IndexFunc(query, unicode.IsSpace)
if sp < 0 {
return fmt.Errorf("Can't determine statement type for %+q", query)
}
verb := strings.ToUpper(query[:sp])
switch verb {
case "SELECT", "INSERT", "UPDATE", "DELETE", "REPLACE":
default:
return fmt.Errorf("Not supported statement type %+q", verb)
}
info.stmtType = verb
}
// If it's a SELECT statement, load query result columns.
if info.StmtType() == "SELECT" {
cols, err := loader.LoadQueryResultColumns(query)
if err != nil {
return err
}
// Process query result
for _, directive := range directives {
if err := directive.ProcessQueryResultColumns(&cols); err != nil {
return err
}
}
info.resultCols = cols
}
// Final text
{
fragments := []string{}
for _, directive := range directives {
fragment, err := directive.Fragment()
if err != nil {
return err
}
fragments = append(fragments, fragment)
}
info.text = strings.TrimSpace(strings.Join(fragments, ""))
}
return nil
}
// Valid returns true if info != nil.
func (info *StmtInfo) Valid() bool {
return info != nil
}
// String is same as StmtName.
func (info *StmtInfo) String() string {
return info.StmtName()
}
// StmtName returns the name of the StmtInfo. It returns "" if info is nil.
func (info *StmtInfo) StmtName() string {
if info == nil {
return ""
}
return info.stmtName
}
// StmtType returns the statement type, one of "SELECT"/"UPDATE"/"INSERT"/"UPDATE". It returns "" if info is nil.
func (info *StmtInfo) StmtType() string {
if info == nil {
return ""
}
return info.stmtType
}
// Text returns the statment text. It returns "" if info is nil.
func (info *StmtInfo) Text() string {
if info == nil {
return ""
}
return info.text
}
// NumResultCol returns the number of result columns. It returns 0 if info is nil or it is not "SELECT" statement.
func (info *StmtInfo) NumResultCol() int {
if info == nil {
return 0
}
return len(info.resultCols)
}
// ResultCols returns result columns. It returns nil if info is nil.
func (info *StmtInfo) ResultCols() []*datasrc.Column {
if info == nil {
return nil
}
return info.resultCols
}
// Locals returns the associated value for the given key in StmtInfo's locals map.
// This map is used by directives to store directive specific variables.
func (info *StmtInfo) Locals(key interface{}) interface{} {
return info.locals[key]
}
// SetLocals set key/value into StmtInfo's locals map. See document in Locals.
func (info *StmtInfo) SetLocals(key, val interface{}) {
info.locals[key] = val
}