MCPcopy Create free account
hub / github.com/araddon/qlbridge / rewriteWhere

Function rewriteWhere

rel/sql_rewrite.go:161–220  ·  view source on GitHub ↗
(stmt *SqlSelect, from *SqlSource, node expr.Node, cols Columns)

Source from the content-addressed store, hash-verified

159 }
160}
161func rewriteWhere(stmt *SqlSelect, from *SqlSource, node expr.Node, cols Columns) (expr.Node, Columns) {
162 //u.Debugf("rewrite where %s", node)
163 switch nt := node.(type) {
164 case *expr.IdentityNode:
165 if left, right, hasLeft := nt.LeftRight(); hasLeft {
166 //u.Debugf("rewriteWhere from.Name:%v l:%v r:%v", from.alias, left, right)
167 if left == from.alias {
168 in := expr.IdentityNode{Text: right}
169 cols = append(cols, NewColumn(right))
170 //u.Warnf("nice, found it! in = %v cols:%d", in, len(cols))
171 return &in, cols
172 } else {
173 //u.Warnf("what to do? source:%v %v", from.alias, nt.String())
174 }
175 } else {
176 //u.Debugf("returning original: %s", nt)
177 return node, cols
178 }
179 case *expr.NumberNode, *expr.NullNode, *expr.StringNode:
180 return nt, cols
181 case *expr.BinaryNode:
182 //u.Infof("binaryNode T:%v", nt.Operator.T.String())
183 switch nt.Operator.T {
184 case lex.TokenAnd, lex.TokenLogicAnd, lex.TokenLogicOr:
185 var n1, n2 expr.Node
186 n1, cols = rewriteWhere(stmt, from, nt.Args[0], cols)
187 n2, cols = rewriteWhere(stmt, from, nt.Args[1], cols)
188
189 if n1 != nil && n2 != nil {
190 return &expr.BinaryNode{Operator: nt.Operator, Args: []expr.Node{n1, n2}}, cols
191 } else if n1 != nil {
192 return n1, cols
193 } else if n2 != nil {
194 return n2, cols
195 } else {
196 //u.Warnf("n1=%#v n2=%#v %#v", n1, n2, nt)
197 }
198 case lex.TokenEqual, lex.TokenEqualEqual, lex.TokenGT, lex.TokenGE, lex.TokenLE, lex.TokenNE:
199 var n1, n2 expr.Node
200 n1, cols = rewriteWhere(stmt, from, nt.Args[0], cols)
201 n2, cols = rewriteWhere(stmt, from, nt.Args[1], cols)
202 //u.Debugf("n1=%#v n2=%#v %#v", n1, n2, nt)
203 if n1 != nil && n2 != nil {
204 return &expr.BinaryNode{Operator: nt.Operator, Args: []expr.Node{n1, n2}}, cols
205 // } else if n1 != nil {
206 // return n1
207 // } else if n2 != nil {
208 // return n2
209 } else {
210 //u.Warnf("n1=%#v n2=%#v %#v", n1, n2, nt)
211 }
212 default:
213 //u.Warnf("un-implemented op: %#v", nt)
214 }
215 default:
216 u.Warnf("%T node types are not suppored yet for where rewrite", node)
217 }
218 //u.Warnf("nil?? %T %s %#v", node, node, node)

Callers 1

RewriteSqlSourceFunction · 0.85

Calls 2

NewColumnFunction · 0.85
LeftRightMethod · 0.45

Tested by

no test coverage detected