Parse the usage of the SQL in operator properly (#9527)
Also adds a `treeRepr` renderer for the SQL nodes.
This commit is contained in:
parent
f9eec2aa1e
commit
ee54d6977b
2 changed files with 23 additions and 4 deletions
|
|
@ -566,7 +566,6 @@ type
|
||||||
SqlParser* = object of SqlLexer ## SQL parser object
|
SqlParser* = object of SqlLexer ## SQL parser object
|
||||||
tok: Token
|
tok: Token
|
||||||
|
|
||||||
|
|
||||||
{.deprecated: [EInvalidSql: SqlParseError, PSqlNode: SqlNode,
|
{.deprecated: [EInvalidSql: SqlParseError, PSqlNode: SqlNode,
|
||||||
TSqlNode: SqlNodeObj, TSqlParser: SqlParser, TSqlNodeKind: SqlNodeKind].}
|
TSqlNode: SqlNodeObj, TSqlParser: SqlParser, TSqlNodeKind: SqlNodeKind].}
|
||||||
|
|
||||||
|
|
@ -591,6 +590,7 @@ proc len*(n: SqlNode): int =
|
||||||
result = n.sons.len
|
result = n.sons.len
|
||||||
|
|
||||||
proc `[]`*(n: SqlNode; i: int): SqlNode = n.sons[i]
|
proc `[]`*(n: SqlNode; i: int): SqlNode = n.sons[i]
|
||||||
|
proc `[]`*(n: SqlNode; i: BackwardsIndex): SqlNode = n.sons[n.len - int(i)]
|
||||||
|
|
||||||
proc add*(father, n: SqlNode) =
|
proc add*(father, n: SqlNode) =
|
||||||
add(father.sons, n)
|
add(father.sons, n)
|
||||||
|
|
@ -674,7 +674,7 @@ proc getPrecedence(p: SqlParser): int =
|
||||||
result = 5
|
result = 5
|
||||||
elif isOpr(p, "=") or isOpr(p, "<") or isOpr(p, ">") or isOpr(p, ">=") or
|
elif isOpr(p, "=") or isOpr(p, "<") or isOpr(p, ">") or isOpr(p, ">=") or
|
||||||
isOpr(p, "<=") or isOpr(p, "<>") or isOpr(p, "!=") or isKeyw(p, "is") or
|
isOpr(p, "<=") or isOpr(p, "<>") or isOpr(p, "!=") or isKeyw(p, "is") or
|
||||||
isKeyw(p, "like"):
|
isKeyw(p, "like") or isKeyw(p, "in"):
|
||||||
result = 4
|
result = 4
|
||||||
elif isKeyw(p, "and"):
|
elif isKeyw(p, "and"):
|
||||||
result = 3
|
result = 3
|
||||||
|
|
@ -717,7 +717,10 @@ proc identOrLiteral(p: var SqlParser): SqlNode =
|
||||||
of tkParLe:
|
of tkParLe:
|
||||||
getTok(p)
|
getTok(p)
|
||||||
result = newNode(nkPrGroup)
|
result = newNode(nkPrGroup)
|
||||||
|
while true:
|
||||||
result.add(parseExpr(p))
|
result.add(parseExpr(p))
|
||||||
|
if p.tok.kind != tkComma: break
|
||||||
|
getTok(p)
|
||||||
eat(p, tkParRi)
|
eat(p, tkParRi)
|
||||||
else:
|
else:
|
||||||
if p.tok.literal == "*":
|
if p.tok.literal == "*":
|
||||||
|
|
@ -1465,6 +1468,22 @@ proc `$`*(n: SqlNode): string =
|
||||||
## an alias for `renderSQL`.
|
## an alias for `renderSQL`.
|
||||||
renderSQL(n)
|
renderSQL(n)
|
||||||
|
|
||||||
|
proc treeReprAux(s: SqlNode, level: int, result: var string) =
|
||||||
|
result.add('\n')
|
||||||
|
for i in 0 ..< level: result.add(" ")
|
||||||
|
|
||||||
|
result.add($s.kind)
|
||||||
|
if s.kind in LiteralNodes:
|
||||||
|
result.add(' ')
|
||||||
|
result.add(s.strVal)
|
||||||
|
else:
|
||||||
|
for son in s.sons:
|
||||||
|
treeReprAux(son, level + 1, result)
|
||||||
|
|
||||||
|
proc treeRepr*(s: SqlNode): string =
|
||||||
|
result = newStringOfCap(128)
|
||||||
|
treeReprAux(s, 0, result)
|
||||||
|
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
import streams
|
import streams
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -295,4 +295,4 @@ create table anon207(
|
||||||
anon209 varchar(30) not null,
|
anon209 varchar(30) not null,
|
||||||
anon204 varchar(30) default null,
|
anon204 varchar(30) default null,
|
||||||
anon70 int not null references anon40(anon41));
|
anon70 int not null references anon40(anon41));
|
||||||
|
select * from anon207 where anon41 in (1, 2, 3);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue