refactor(sql): modularize status and where query builders
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
Modular search builder and query generators for Aether.
|
||||
"""
|
||||
import logging
|
||||
from sqlalchemy import text
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -71,4 +72,54 @@ def sql_fulltext_qry_part(fulltext_qry_dict: dict) -> tuple[str, dict]|bool:
|
||||
clauses.append(f"MATCH( {key} ) AGAINST( :ft_{key} IN BOOLEAN MODE )")
|
||||
data[f'ft_{key}'] = value
|
||||
return f"AND ({' OR '.join(clauses)})", data
|
||||
return False
|
||||
return False
|
||||
|
||||
def sql_enable_part(table_name: str, enabled: str) -> tuple[str, bool|None]|bool:
|
||||
"""Handles enabled/disabled status filtering with schema check."""
|
||||
from app.db_sql import db
|
||||
if not table_name: return False
|
||||
if enabled in ['enabled', 'disabled', 'all']:
|
||||
if enabled == 'all': return '', None
|
||||
try:
|
||||
db.execute(text(f"SELECT enable FROM `{table_name}` LIMIT 0"))
|
||||
except:
|
||||
log.warning(f"Table '{table_name}' missing 'enable' column. Skipping filter.")
|
||||
return '', None
|
||||
val = (enabled == 'enabled')
|
||||
return f"AND `{table_name}`.enable = {str(val).lower()}", val
|
||||
return False
|
||||
|
||||
def sql_hidden_part(table_name: str, hidden: str) -> tuple[str, bool|None]|bool:
|
||||
"""Handles hidden status filtering with schema check."""
|
||||
from app.db_sql import db
|
||||
if not table_name: return False
|
||||
if hidden in ['hidden', 'not_hidden', 'all']:
|
||||
if hidden == 'all': return '', None
|
||||
try:
|
||||
db.execute(text(f"SELECT hide FROM `{table_name}` LIMIT 0"))
|
||||
except:
|
||||
log.warning(f"Table '{table_name}' missing 'hide' column. Skipping filter.")
|
||||
return '', None
|
||||
if hidden == 'hidden':
|
||||
return f"AND `{table_name}`.hide = true", True
|
||||
return f"AND (`{table_name}`.hide = false OR `{table_name}`.hide IS NULL)", False
|
||||
return False
|
||||
|
||||
def sql_where_qry_part(qry_dict_li: list) -> tuple[str, dict]|bool:
|
||||
"""Standard v2 style WHERE clause builder."""
|
||||
data = {}
|
||||
if qry_dict_li and isinstance(qry_dict_li, list):
|
||||
log.info('Creating partial SQL string for WHERE queries.')
|
||||
clauses = []
|
||||
for qry in qry_dict_li:
|
||||
field = qry.get('field')
|
||||
op = qry.get('operator')
|
||||
val = qry.get('value')
|
||||
type_ = qry.get('type', 'AND') or 'AND'
|
||||
if op == 'MATCH':
|
||||
clauses.append(f'{type_} MATCH( {field} ) AGAINST( :{field} IN BOOLEAN MODE )')
|
||||
else:
|
||||
clauses.append(f'{type_} {field} {op} :{field}')
|
||||
data[field] = val
|
||||
return ' '.join(clauses), data
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user