From c3d286a1527d3e4a33860934e5d03155cc9deb26 Mon Sep 17 00:00:00 2001 From: leo7martins Date: Fri, 27 Nov 2026 23:44:44 +0000 Subject: [PATCH] fix: register a REGEXP function when SQLite lacks one (#139) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01X3HxPN2g4W9hbavLMWDbkk --- CHANGELOG.md | 1 + litecli/sqlexecute.py | 18 ++++++++++++++++++ tests/test_sqlexecute.py | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d91317..1937d38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Bug Fixes - Expand `~` in configured log file paths before opening the log. +- Support the `REGEXP` operator when SQLite does not provide one. [(#139)](https://github.com/dbcli/litecli/issues/139) ### Internal diff --git a/litecli/sqlexecute.py b/litecli/sqlexecute.py index 5e52b73..a9b5e02 100644 --- a/litecli/sqlexecute.py +++ b/litecli/sqlexecute.py @@ -2,6 +2,7 @@ import logging import os.path +import re from contextlib import closing from typing import Any, Generator, Iterable, cast from urllib.parse import urlparse @@ -23,6 +24,22 @@ _logger = logging.getLogger(__name__) + +def _regexp(pattern: str | None, value: Any) -> bool | None: + """Implement SQLite's ``X REGEXP Y`` operator, which calls ``regexp(Y, X)``.""" + if pattern is None or value is None: + return None + return re.search(pattern, str(value)) is not None + + +def _register_regexp(conn: Any) -> None: + """Register a Python REGEXP function unless the connection already provides one (e.g. sqlean).""" + try: + conn.execute("SELECT 'a' REGEXP 'a'") + except sqlite3.OperationalError: + conn.create_function("REGEXP", 2, _regexp, deterministic=True) + + # FIELD_TYPES = decoders.copy() # FIELD_TYPES.update({ # FIELD_TYPE.NULL: type(None) @@ -90,6 +107,7 @@ def connect(self, database: str | None = None) -> None: conn = sqlite3.connect(database=db_name, isolation_level=None, uri=uri) conn.text_factory = lambda x: x.decode("utf-8", "backslashreplace") + _register_regexp(conn) if self.conn: self.conn.close() diff --git a/tests/test_sqlexecute.py b/tests/test_sqlexecute.py index 3fdaef1..7d4a30c 100644 --- a/tests/test_sqlexecute.py +++ b/tests/test_sqlexecute.py @@ -392,3 +392,17 @@ def test_time_null(executor): run(executor, """insert into time_null values(null)""") results = run(executor, """select * from time_null""") assert_result_equal(results, headers=["a"], rows=[(None,)]) + + +@dbtest +def test_regexp_operator(executor): + run(executor, "create table regexp_test(t text)") + run(executor, "insert into regexp_test (t) values ('apple'), ('banana'), ('cherry'), (null)") + results = run(executor, "select t from regexp_test where t regexp '^b|y$' order by t") + assert_result_equal(results, headers=["t"], rows=[("banana",), ("cherry",)]) + + +@dbtest +def test_regexp_null_and_non_text(executor): + results = run(executor, "select null regexp 'a', 'a' regexp null, 123 regexp '^1'") + assert results[0]["rows"] == [(None, None, 1)]