forked from araddon/qlbridge
/
filter.go
229 lines (204 loc) · 4.78 KB
/
filter.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
package rel
import (
"fmt"
"hash/fnv"
"io"
"strings"
u "github.com/araddon/gou"
"github.com/fuhongbo/qlbridge/expr"
)
var (
_ = u.EMPTY
// Ensure each Filter statement implement's Filter interface
_ Filter = (*FilterStatement)(nil)
_ Filter = (*FilterSelect)(nil)
// Statements with Columns
_ ColumnsStatement = (*FilterSelect)(nil)
)
type (
// Filter interface for Filter Statements (either Filter/FilterSelect)
Filter interface {
String() string
}
// FilterSelect is a Filter but also has projected columns
FilterSelect struct {
*FilterStatement
Columns Columns
}
// FilterStatement is a statement of type = Filter
FilterStatement struct {
checkedIncludes bool
includes []string
Description string // initial pre-start comments
Raw string // full original raw statement
Filter expr.Node // FILTER <filter_expr>
Where expr.Node // WHERE <expr> [AND <expr>] syntax
OrderBy Columns // order by
From string // From is optional
Limit int // Limit
Alias string // Non-Standard sql, alias/name of sql another way of expression Prepared Statement
With u.JsonHelper // Non-Standard SQL for properties/config info, similar to Cassandra with, purse json
}
)
// NewFilterStatement Create A FilterStatement
func NewFilterStatement() *FilterStatement {
req := &FilterStatement{}
return req
}
func (m *FilterStatement) WriteDialect(w expr.DialectWriter) {
if m.Description != "" {
if !strings.Contains(m.Description, "\n") {
io.WriteString(w, "--")
io.WriteString(w, m.Description)
io.WriteString(w, "\n")
}
}
io.WriteString(w, "FILTER ")
m.Filter.WriteDialect(w)
if m.From != "" {
io.WriteString(w, " FROM ")
w.WriteIdentity(m.From)
}
if m.Limit > 0 {
io.WriteString(w, fmt.Sprintf(" LIMIT %d", m.Limit))
}
if len(m.With) > 0 {
io.WriteString(w, " WITH ")
HelperString(w, m.With)
}
if m.Alias != "" {
io.WriteString(w, " ALIAS ")
w.WriteIdentity(m.Alias)
}
}
// String representation of FilterStatement
func (m *FilterStatement) String() string {
if m == nil {
return ""
}
w := expr.NewJSONDialectWriter()
m.WriteDialect(w)
return w.String()
}
// FingerPrint consistent hashed int value of FingerPrint above
func (m *FilterStatement) FingerPrintID() int64 {
h := fnv.New64()
w := expr.NewFingerPrinter()
m.WriteDialect(w)
h.Write([]byte(w.String()))
return int64(h.Sum64())
}
// Includes Recurse this statement and find all includes
func (m *FilterStatement) Includes() []string {
if !m.checkedIncludes {
m.includes = expr.FindIncludes(m.Filter)
m.checkedIncludes = true
}
return m.includes
}
func (m *FilterStatement) Equal(s *FilterStatement) bool {
if m == nil && s == nil {
return true
}
if m == nil && s != nil {
return false
}
if m != nil && s == nil {
return false
}
if m.Description != s.Description {
return false
}
if m.From != s.From {
return false
}
if m.Limit != s.Limit {
return false
}
if m.Alias != s.Alias {
return false
}
if len(m.With) != len(s.With) {
return false
}
if len(m.With) > 0 || len(s.With) > 0 {
if !EqualWith(m.With, s.With) {
return false
}
}
if m.Filter != nil && !m.Filter.Equal(s.Filter) {
return false
}
return true
}
func NewFilterSelect() *FilterSelect {
req := &FilterSelect{}
return req
}
func (m *FilterSelect) AddColumn(colArg Column) error {
col := &colArg
col.Index = len(m.Columns)
m.Columns = append(m.Columns, col)
return nil
}
func (m *FilterSelect) WriteDialect(w expr.DialectWriter) {
io.WriteString(w, "SELECT ")
m.Columns.WriteDialect(w)
if m.From != "" {
io.WriteString(w, " FROM ")
w.WriteIdentity(m.From)
}
io.WriteString(w, " FILTER ")
m.Filter.WriteDialect(w)
if m.Limit > 0 {
io.WriteString(w, fmt.Sprintf(" LIMIT %d", m.Limit))
}
if len(m.With) > 0 {
io.WriteString(w, " WITH ")
HelperString(w, m.With)
}
if m.Alias != "" {
io.WriteString(w, " ALIAS ")
w.WriteIdentity(m.Alias)
}
}
// String representation of FilterSelect
func (m *FilterSelect) String() string {
if m == nil {
return ""
}
w := expr.NewDefaultWriter()
m.WriteDialect(w)
return w.String()
}
// FingerPrint consistent hashed int value of FingerPrint above
func (m *FilterSelect) FingerPrintID() int64 {
h := fnv.New64()
w := expr.NewFingerPrinter()
m.WriteDialect(w)
h.Write([]byte(w.String()))
return int64(h.Sum64())
}
// Equal Checks for deep equality
func (m *FilterSelect) Equal(s *FilterSelect) bool {
if m == nil && s == nil {
return true
}
if m == nil && s != nil {
return false
}
if m != nil && s == nil {
return false
}
if !m.Columns.Equal(s.Columns) {
return false
}
mfs := m.FilterStatement
if mfs != nil {
sfs := s.FilterStatement
if !mfs.Equal(sfs) {
return false
}
}
return true
}