/
dialect.go
219 lines (203 loc) · 5.58 KB
/
dialect.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
package expr
import (
"bytes"
"io"
"strconv"
"strings"
u "github.com/araddon/gou"
"github.com/lytics/qlbridge/value"
)
type (
// DialectWriter Defines interface to allow different dialects
// to have different escape characters. IE, given an AST structure
// allow the writer to control the string output. Allows translation between
// different dialects (different escapes) as well as allows normalization
// indentation, tabs, spacing, etc.
// - postgres: literal-escape = ' identity = "
// - mysql: literal-escape = " identity = `
// - cql: literal-escape = ' identity = `
// - bigquery: literal-escape = " identity = []
DialectWriter interface {
io.Writer
Len() int
WriteLiteral(string)
WriteIdentity(string)
WriteLeftRightIdentity(string, string)
WriteIdentityQuote(string, byte)
WriteNumber(string)
WriteNull()
WriteValue(v value.Value)
String() string
}
// Default Dialect writer uses mysql escaping rules literals=" identity=`
defaultDialect struct {
bytes.Buffer
Null string
LiteralQuote byte
IdentityQuote byte
stripNamespace bool
}
// Json or String Dialect writer uses json escaping rules literals=\
jsonDialect struct {
*defaultDialect
}
// finterprinter, ie ? substitution
fingerprintDialect struct {
DialectWriter
replace string
}
// Keyword writer
keywordDialect struct {
*defaultDialect
kw map[string]struct{}
}
)
// NewDialectWriter creates a writer that is custom literal and identity
// escape characters
func NewDialectWriter(l, i byte) DialectWriter {
return &defaultDialect{LiteralQuote: l, IdentityQuote: i}
}
// NewJSONDialectWriter escape literal " with \"
func NewJSONDialectWriter() DialectWriter {
return &jsonDialect{defaultDialect: &defaultDialect{
LiteralQuote: '"',
IdentityQuote: '`',
Null: "NULL",
}}
}
// NewDefaultWriter uses mysql escaping rules literals=" identity=`
func NewDefaultWriter() DialectWriter {
return &defaultDialect{LiteralQuote: '"', IdentityQuote: '`', Null: "NULL"}
}
// NewDefaultNoNamspaceWriter uses mysql escaping rules literals=" identity=`
// Strip namespaces so that 'users.first_name` becomes `first_name` (strip users.)
func NewDefaultNoNamspaceWriter() DialectWriter {
return &defaultDialect{
LiteralQuote: '"',
IdentityQuote: '`',
Null: "NULL",
stripNamespace: true}
}
// WriteLiteral writes literal with escapes if needed
func (w *defaultDialect) WriteLiteral(l string) {
if len(l) == 1 && l == "*" {
w.WriteByte('*')
return
}
LiteralQuoteEscapeBuf(&w.Buffer, rune(w.LiteralQuote), l)
}
// WriteIdentity writes identity with escaping if needed
func (w *defaultDialect) WriteIdentity(i string) {
if len(i) == 1 && i == "*" {
w.WriteByte('*')
return
}
IdentityMaybeEscapeBuf(&w.Buffer, w.IdentityQuote, i)
}
// WriteLeftRightIdentity writes identity with escaping if needed
func (w *defaultDialect) WriteLeftRightIdentity(l, r string) {
if l == "" {
w.WriteIdentity(r)
return
}
// `user`.`email` type namespacing, may need to be escaped differently
if w.stripNamespace {
w.WriteIdentity(r)
return
}
w.WriteIdentity(l)
w.Write([]byte{'.'})
w.WriteIdentity(r)
return
}
// WriteIdentityQuote write out an identity using given quote character
func (w *defaultDialect) WriteIdentityQuote(i string, quote byte) {
if len(i) == 1 && i == "*" {
w.WriteByte('*')
return
}
LiteralQuoteEscapeBuf(&w.Buffer, rune(w.IdentityQuote), i)
}
func (w *defaultDialect) WriteNumber(n string) {
io.WriteString(w, n)
}
func (w *defaultDialect) WriteNull() {
io.WriteString(w, w.Null)
}
func (w *defaultDialect) WriteValue(v value.Value) {
switch vt := v.(type) {
case value.StringValue:
w.WriteLiteral(vt.Val())
case value.IntValue:
w.WriteNumber(vt.ToString())
case value.NumberValue:
w.WriteNumber(vt.ToString())
case value.BoolValue:
io.WriteString(w, vt.ToString())
case nil, value.NilValue:
w.WriteNull()
case value.TimeValue:
w.WriteLiteral(vt.Val().String())
case value.Slice:
// If you don't want json, then over-ride this WriteValue
by, err := vt.MarshalJSON()
if err == nil {
w.Write(by)
} else {
u.Debugf("could not convert %v", err)
w.Write([]byte("[]"))
}
case value.Map:
// If you don't want json, then over-ride this WriteValue
by, err := vt.MarshalJSON()
if err == nil {
w.Write(by)
} else {
u.Debugf("could not convert %v", err)
w.Write([]byte("null"))
}
default:
io.WriteString(w, vt.ToString())
}
}
// WriteLiteral writes literal and escapes " with \"
func (w *jsonDialect) WriteLiteral(l string) {
if len(l) == 1 && l == "*" {
w.WriteByte('*')
return
}
w.Buffer.WriteString(strconv.Quote(l))
}
func NewKeywordDialect(kw []string) DialectWriter {
m := make(map[string]struct{}, len(kw))
for _, w := range kw {
m[w] = struct{}{}
}
return &keywordDialect{
&defaultDialect{LiteralQuote: '"', IdentityQuote: '`', Null: "NULL"},
m,
}
}
func (w *keywordDialect) WriteIdentity(id string) {
_, isKeyword := w.kw[strings.ToLower(id)]
if isKeyword {
io.WriteString(w, LiteralQuoteEscape(rune(w.IdentityQuote), id))
return
}
w.defaultDialect.WriteIdentity(id)
}
func NewFingerPrinter() DialectWriter {
return &fingerprintDialect{NewDefaultWriter(), "?"}
}
func NewFingerPrintWriter(replace string, w DialectWriter) DialectWriter {
return &fingerprintDialect{w, replace}
}
func (w *fingerprintDialect) WriteLiteral(l string) {
io.WriteString(w.DialectWriter, w.replace)
}
func (w *fingerprintDialect) WriteNumber(n string) {
io.WriteString(w.DialectWriter, w.replace)
}
func (w *fingerprintDialect) WriteIdentity(id string) {
w.DialectWriter.WriteIdentity(strings.ToLower(id))
}