refac
This commit is contained in:
1 parent
d8133c905a
commit
49aab7451c
1 file changed
+19
-3
@@ -323,14 +323,22 @@ elif 'sqlite' in SQLALCHEMY_DATABASE_URL:
|
|||||||
def _apply_sqlite_pragmas(dbapi_connection):
|
def _apply_sqlite_pragmas(dbapi_connection):
|
||||||
"""Apply all configured SQLite PRAGMAs to a raw 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(?).
|
# SQLite LIKE folds ASCII only; SQLAlchemy SQLite ILIKE compiles to lower(x) LIKE lower(?).
|
||||||
|
compiled_patterns = {}
|
||||||
|
|
||||||
def like(pattern, value, escape=None):
|
def like(pattern, value, escape=None):
|
||||||
if pattern is None or value is None:
|
if pattern is None or value is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
pattern = str(pattern).lower()
|
||||||
|
escape = str(escape).lower() if escape is not None else None
|
||||||
|
key = (pattern, escape)
|
||||||
|
compiled = compiled_patterns.get(key)
|
||||||
|
if compiled is False:
|
||||||
|
return False
|
||||||
|
if compiled is None:
|
||||||
regex = []
|
regex = []
|
||||||
escaped = False
|
escaped = False
|
||||||
escape = str(escape).lower() if escape is not None else None
|
for char in pattern:
|
||||||
for char in str(pattern).lower():
|
|
||||||
if escape and not escaped and char == escape:
|
if escape and not escaped and char == escape:
|
||||||
escaped = True
|
escaped = True
|
||||||
continue
|
continue
|
||||||
@@ -339,9 +347,17 @@ elif 'sqlite' in SQLALCHEMY_DATABASE_URL:
|
|||||||
)
|
)
|
||||||
escaped = False
|
escaped = False
|
||||||
if escaped:
|
if escaped:
|
||||||
|
compiled = False
|
||||||
|
if len(compiled_patterns) >= 512:
|
||||||
|
compiled_patterns.clear()
|
||||||
|
compiled_patterns[key] = compiled
|
||||||
return False
|
return False
|
||||||
|
compiled = re.compile(''.join(regex), re.DOTALL)
|
||||||
|
if len(compiled_patterns) >= 512:
|
||||||
|
compiled_patterns.clear()
|
||||||
|
compiled_patterns[key] = compiled
|
||||||
|
|
||||||
return re.fullmatch(''.join(regex), str(value).lower(), re.DOTALL) is not None
|
return compiled.fullmatch(str(value).lower()) is not None
|
||||||
|
|
||||||
dbapi_connection.create_function('like', 2, like, deterministic=True)
|
dbapi_connection.create_function('like', 2, like, deterministic=True)
|
||||||
dbapi_connection.create_function('like', 3, like, deterministic=True)
|
dbapi_connection.create_function('like', 3, like, deterministic=True)
|
||||||
|
|||||||
Reference in new issue
Block a user