From 81138b25eb92440244ee4e1af5779d7e546b7d56 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 1 Mar 2022 14:55:50 -0800 Subject: fix(wpcarro/simple-select): Support regexes 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 Autosubmit: wpcarro Tested-by: BuildkiteCI --- users/wpcarro/scratch/simple-select/main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 @@ -204,6 +205,14 @@ def compile_query(negate, query): return "NOT LIKE '%{}%'".format(query_string) 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) -- cgit 1.4.1