Now with the ability to do OR with LIKE

This commit is contained in:
Scott Idem
2024-06-21 14:46:19 -04:00
parent 4d3c75dbcf
commit 175c84b1a6
3 changed files with 55 additions and 0 deletions

View File

@@ -551,6 +551,7 @@ def sql_select(
fulltext_qry_dict: dict|None = None,
and_qry_dict: dict|None = None,
and_like_dict: dict|None = None,
or_like_dict: dict|None = None,
and_in_dict_li: dict|None = None,
fulltext_qry_field_li: list|None = None, # ['field_name_1', 'field_name_2']
fulltext_qry_str: str|None = None, # 'search string'
@@ -732,6 +733,14 @@ def sql_select(
# NOTE: Merge the data_qry result with the data dict
data = {**data, **data_qry}
sql_or_like = ''
if or_like_dict:
log.info('Creating partial SQL string for OR search (LIKE).')
sql_or_like, data_qry = sql_or_like_part(or_like_dict)
# NOTE: Merge the data_qry result with the data dict
data = {**data, **data_qry}
sql_and_in_dict_li = ''
if and_in_dict_li:
log.info('Creating partial SQL string for AND search (IN).')
@@ -1766,6 +1775,38 @@ def sql_and_like_part(
return sql_and_like, data
# ### BEGIN ### API DB SQL Methods ### sql_or_like_part() ###
# Updated 2024-06-21
@logger_reset
def sql_or_like_part(
or_like_dict_obj: dict, # One or more key value pairs. key = field name; value = search string
) -> bool|dict:
log.setLevel(logging.INFO)
log.debug(locals())
data = {}
sql_or_like = ''
log.debug(or_like_dict_obj)
if or_like_dict_obj and isinstance(or_like_dict_obj, dict):
log.info('Creating partial SQL string for additional OR LIKE queries.')
or_like_dict_obj_str = []
for key, value in or_like_dict_obj.items():
log.debug(f'Key = {key}; Value = {value}')
or_like_dict_obj_str.append(f'{key} LIKE :or_like_{key}')
# For now not surrounding with %... may need to be added back in later
# data[f'or_like_{key}'] = f'%{value}%'
data[f'or_like_{key}'] = f'{value}'
or_like_field_string = ' OR '.join(or_like_dict_obj_str)
sql_or_like = f'AND ({or_like_field_string})'
log.debug(sql_or_like)
log.debug(data)
return sql_or_like, data
# ### END ### API DB SQL Methods ### sql_or_like_part() ###
# ### BEGIN ### API DB SQL Methods ### sql_and_in_dict_li_part() ###
# This function takes a list of values and formats them to be used in a SQL IN statement. This may contain one or more fields to use with the IN statement.
@@ -1799,6 +1840,7 @@ def sql_and_in_dict_li_part(
return sql_and_in_dict_li, data
# ### BEGIN ### API DB SQL Methods ### sql_enable_part() ###
# Updated 2022-01-17
@logger_reset