This commit is contained in:
Timothy Jaeryang Baek committed 2026-08-30 12:09:34 -04:00
1 parent 6fa50a6558
commit 26f37426b7
3 files changed
+30 -5

No files matched your search

+24
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import logging
import os
import re
import sys
from contextlib import asynccontextmanager, contextmanager
from datetime import datetime, timedelta, timezone
@@ -321,6 +322,29 @@ elif 'sqlite' in SQLALCHEMY_DATABASE_URL:
def _apply_sqlite_pragmas(dbapi_connection):
"""Apply all configured SQLite PRAGMAs to a raw DBAPI connection."""
# SQLite LIKE folds ASCII only; SQLAlchemy SQLite ILIKE compiles to lower(x) LIKE lower(?).
def like(pattern, value, escape=None):
if pattern is None or value is None:
return None
regex = []
escaped = False
escape = str(escape).lower() if escape is not None else None
for char in str(pattern).lower():
if escape and not escaped and char == escape:
escaped = True
continue
regex.append(
'.*' if not escaped and char == '%' else '.' if not escaped and char == '_' else re.escape(char)
)
escaped = False
if escaped:
return False
return re.fullmatch(''.join(regex), str(value).lower(), re.DOTALL) is not None
dbapi_connection.create_function('like', 2, like, deterministic=True)
dbapi_connection.create_function('like', 3, like, deterministic=True)
cursor = dbapi_connection.cursor()
if DATABASE_ENABLE_SQLITE_WAL:
cursor.execute('PRAGMA journal_mode=WAL')