74 lines
3.1 KiB
Python
74 lines
3.1 KiB
Python
"""
|
|
Modular search builder and query generators for Aether.
|
|
"""
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
def sql_limit_offset_part(limit: int, offset: int = 0) -> str|bool:
|
|
"""Creates a partial SQL string for LIMIT and OFFSET."""
|
|
if limit >= 0 and offset >= 0:
|
|
log.info(f'Creating partial SQL string for LIMIT and OFFSET. Limit: {limit}; Offset: {offset}')
|
|
return f'LIMIT {limit} OFFSET {offset}'
|
|
else:
|
|
return False
|
|
|
|
def sql_and_like_part(and_like_dict_obj: dict) -> tuple[str, dict]|bool:
|
|
"""Creates a partial SQL string for AND LIKE queries."""
|
|
data = {}
|
|
if and_like_dict_obj and isinstance(and_like_dict_obj, dict):
|
|
log.info('Creating partial SQL string for additional AND LIKE queries.')
|
|
clauses = []
|
|
for key, value in and_like_dict_obj.items():
|
|
clauses.append(f"{key} LIKE :and_like_{key}")
|
|
data[f'and_like_{key}'] = value
|
|
return f"AND ({' AND '.join(clauses)})", data
|
|
return False
|
|
|
|
def sql_or_like_part(or_like_dict_obj: dict) -> tuple[str, dict]|bool:
|
|
"""Creates a partial SQL string for OR LIKE queries."""
|
|
data = {}
|
|
if or_like_dict_obj and isinstance(or_like_dict_obj, dict):
|
|
log.info('Creating partial SQL string for additional OR LIKE queries.')
|
|
clauses = []
|
|
for key, value in or_like_dict_obj.items():
|
|
clauses.append(f"{key} LIKE :or_like_{key}")
|
|
data[f'or_like_{key}'] = value
|
|
return f"AND ({' OR '.join(clauses)})", data
|
|
return False
|
|
|
|
def sql_and_in_dict_li_part(and_in_dict_li_dict_obj: dict) -> tuple[str, dict]|bool:
|
|
"""Creates a partial SQL string for AND IN queries."""
|
|
data = {}
|
|
if and_in_dict_li_dict_obj and isinstance(and_in_dict_li_dict_obj, dict):
|
|
log.info('Creating partial SQL string for additional AND IN queries.')
|
|
clauses = []
|
|
for key, value in and_in_dict_li_dict_obj.items():
|
|
clauses.append(f"{key} IN :and_in_{key}")
|
|
data[f'and_in_{key}'] = value
|
|
return f"AND ({' AND '.join(clauses)})", data
|
|
return False
|
|
|
|
def sql_and_qry_part(and_qry_dict_obj: dict) -> tuple[str, dict]|bool:
|
|
"""Creates a partial SQL string for additional AND queries (equals)."""
|
|
data = {}
|
|
if and_qry_dict_obj and isinstance(and_qry_dict_obj, dict):
|
|
log.info('Creating partial SQL string for additional AND queries.')
|
|
clauses = []
|
|
for key, value in and_qry_dict_obj.items():
|
|
clauses.append(f"{key} = :and_{key}")
|
|
data[f'and_{key}'] = value
|
|
return f"AND ({' AND '.join(clauses)})", data
|
|
return False
|
|
|
|
def sql_fulltext_qry_part(fulltext_qry_dict: dict) -> tuple[str, dict]|bool:
|
|
"""Creates a partial SQL string for fulltext search."""
|
|
data = {}
|
|
if fulltext_qry_dict and isinstance(fulltext_qry_dict, dict):
|
|
log.info('Creating partial SQL string for fulltext search.')
|
|
clauses = []
|
|
for key, value in fulltext_qry_dict.items():
|
|
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 |