about summary refs log tree commit diff
path: root/users/wpcarro/scratch/simple-select/main.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-03-01T22·55-0800
committerclbot <clbot@tvl.fyi>2022-03-01T22·59+0000
commit81138b25eb92440244ee4e1af5779d7e546b7d56 (patch)
tree224a64b99117d0605343bbdb20a0300b83beea0e /users/wpcarro/scratch/simple-select/main.py
parent426cc2db2c6fa5fd9cbdc3ac722df30c7fc9e684 (diff)
fix(wpcarro/simple-select): Support regexes r/3889
The python lib for sqlite doesn't support REGEXP, so we need to support it
manually.

Change-Id: I90670ff564d6fb40e8cee30c3d1509feb2c00857
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5347
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/wpcarro/scratch/simple-select/main.py')
-rw-r--r--users/wpcarro/scratch/simple-select/main.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/simple-select/main.py b/users/wpcarro/scratch/simple-select/main.py
index 003267b01906..dd7e8215c9ca 100644
--- a/users/wpcarro/scratch/simple-select/main.py
+++ b/users/wpcarro/scratch/simple-select/main.py
@@ -5,6 +5,7 @@ import sqlite3
 import string
 from scanner import Scanner
 from parser import Parser
+import re
 
 ################################################################################
 # Predicates
@@ -205,6 +206,14 @@ def compile_query(negate, query):
     return "LIKE '%{}%'".format(query_string)
 
 ################################################################################
+# Helper Functions
+################################################################################
+
+def regexp(expr, x):
+  reg = re.compile(expr)
+  return reg.search(x) is not None
+
+################################################################################
 # Main
 ################################################################################
 
@@ -212,6 +221,9 @@ def main(csv_path=None, debug=False):
   # Import CSV to SQLite
   table = "main"
   con = sqlite3.connect(":memory:")
+
+  con.create_function("REGEXP", 2, regexp)
+
   cur = con.cursor()
   with open(csv_path, "r") as f:
     r = csv.DictReader(f)