aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2021-07-26 20:04:44 -0400
committerGravatar - 2021-07-26 22:56:47 -0400
commite6507540ab64e6cf252924a632a10102d501b84f (patch)
tree199f868a1809ea4e407e79f605e4073d11660fa0 /test
parentqueryparse: use demorgan's laws to simplify parsed expression (diff)
queryparse: add function to build SQL queries
SQLite allows for UNIONs and INTERSECTIONs between SELECT statements, but it does not support grouping them. By recursively applying DeMorgan's laws, the parser can reduce the parse tree to only * Literals * NOT literals (i.e. "all items not belonging to this tag") * ANDs * ORs What SQLite /can/ do is have SELECT statements come from other SELECT statements. A query like (X & Y & Z) | (A & B & C) in pseudo-SQLite becomes SELECT * FROM ( SELECT * FROM tbl WHERE name IN X INTERSECTION SELECT * FROM tbl WHERE name IN Y INTERSECTION SELECT * FROM tbl WHERE name IN Z ) UNION SELECT * FROM ( SELECT * FROM tbl WHERE name IN A INTERSECTION SELECT * FROM tbl WHERE name IN B INTERSECTION SELECT * FROM tbl WHERE name IN C ) One future optimization would be to group all literals at a certain level with each other, so that there are less direct queries to the entire tag table.
Diffstat (limited to 'test')
-rwxr-xr-xtest/t_queryparse.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/test/t_queryparse.py b/test/t_queryparse.py
index 3cf5dcb..ea15c42 100755
--- a/test/t_queryparse.py
+++ b/test/t_queryparse.py
@@ -3,11 +3,41 @@ import sys
sys.path.insert(0, "../")
import queryparse
+def prettyprint(s):
+ ind = 0
+ start = True
+ for i in s:
+ if i == "\n":
+ if not start:
+ print(" ", end="")
+ start = True
+ continue
+ if (i == " " or i == "\t") and start:
+ continue
+ start = False
+ if i == "(":
+ ind = ind + 1
+ print()
+ for j in range(0,ind):
+ print(" ", end="")
+ start = False
+ print("(", end="")
+ elif i == ")":
+ print(")")
+ ind = ind - 1
+ for j in range(0, ind):
+ print(" ", end="")
+ start = True
+ else:
+ print(i, end="")
+
def f(x):
print(x)
v,lits = queryparse.parse(x)
- print(v)
+ prettyprint(str(v))
print(lits)
+ _,exr = queryparse.build([], v)
+ prettyprint(exr)
print()
f(["x", "OR", "y"])