refac
This commit is contained in:
1 parent
d8133c905a
commit
49aab7451c
1 file changed
+28
-12
@@ -323,25 +323,41 @@ 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
|
||||||
|
|
||||||
regex = []
|
pattern = str(pattern).lower()
|
||||||
escaped = False
|
|
||||||
escape = str(escape).lower() if escape is not None else None
|
escape = str(escape).lower() if escape is not None else None
|
||||||
for char in str(pattern).lower():
|
key = (pattern, escape)
|
||||||
if escape and not escaped and char == escape:
|
compiled = compiled_patterns.get(key)
|
||||||
escaped = True
|
if compiled is False:
|
||||||
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 False
|
||||||
|
if compiled is None:
|
||||||
|
regex = []
|
||||||
|
escaped = False
|
||||||
|
for char in pattern:
|
||||||
|
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:
|
||||||
|
compiled = False
|
||||||
|
if len(compiled_patterns) >= 512:
|
||||||
|
compiled_patterns.clear()
|
||||||
|
compiled_patterns[key] = compiled
|
||||||
|
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