Work on LIKE part of query
This commit is contained in:
@@ -543,6 +543,7 @@ def sql_select(
|
||||
hidden: str|None = None, # hidden, not_hidden, all
|
||||
fulltext_qry_dict: dict|None = None,
|
||||
and_qry_dict: dict|None = None,
|
||||
and_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'
|
||||
@@ -714,6 +715,13 @@ def sql_select(
|
||||
# NOTE: Merge the data_qry result with the data dict
|
||||
data = {**data, **data_qry}
|
||||
|
||||
sql_and_like = ''
|
||||
if and_like_dict:
|
||||
sql_and_like, data_qry = sql_and_like_part(and_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:
|
||||
sql_and_in_dict_li, data_qry = sql_and_in_dict_li_part(and_in_dict_li)
|
||||
@@ -781,6 +789,7 @@ def sql_select(
|
||||
WHERE `{table_name}`.{field_name} = :{field_name}
|
||||
{sql_fulltext_match_against}
|
||||
{sql_and_qry}
|
||||
{sql_and_like}
|
||||
{sql_and_in_dict_li}
|
||||
{sql_enabled}
|
||||
{sql_hidden}
|
||||
@@ -1710,6 +1719,39 @@ def sql_and_qry_part(
|
||||
return sql_and_qry, data
|
||||
|
||||
|
||||
# ### BEGIN ### API DB SQL Methods ### sql_and_like_part() ###
|
||||
# Updated 2024-04-07
|
||||
@logger_reset
|
||||
def sql_and_like_part(
|
||||
and_like_dict_obj: dict, # One or more key value pairs. key = field name; value = search string
|
||||
) -> bool|dict:
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
data = {}
|
||||
sql_and_like = ''
|
||||
|
||||
log.debug(and_like_dict_obj)
|
||||
if and_like_dict_obj and isinstance(and_like_dict_obj, dict):
|
||||
log.info('Creating partial SQL string for additional AND LIKE queries.')
|
||||
and_like_dict_obj_str = []
|
||||
|
||||
for key, value in and_like_dict_obj.items():
|
||||
log.debug(f'Key = {key}; Value = {value}')
|
||||
and_like_dict_obj_str.append(f'{key} LIKE :and_like_{key}')
|
||||
# For now not surrounding with %... may need to be added back in later
|
||||
# data[f'and_like_{key}'] = f'%{value}%'
|
||||
data[f'and_like_{key}'] = f'{value}'
|
||||
and_like_field_string = ' AND '.join(and_like_dict_obj_str)
|
||||
|
||||
sql_and_like = f'AND ({and_like_field_string})'
|
||||
log.debug(sql_and_like)
|
||||
log.debug(data)
|
||||
|
||||
return sql_and_like, data
|
||||
|
||||
|
||||
|
||||
# ### 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.
|
||||
# Example: sql_and_in_dict_li_part({'field1': [1, 2, 3], 'field2': ['hello', 'world', 'day']})
|
||||
|
||||
@@ -243,6 +243,9 @@ async def get_obj_li(
|
||||
# This should be a dict list of fields with a list of values to search for using AND.
|
||||
and_qry_dict_obj = None
|
||||
|
||||
# This should be a dict list of fields with a list of values to search for using AND LIKE.
|
||||
and_like_dict_obj = None
|
||||
|
||||
# This should be a dict list of fields with a list of values to search for using AND IN.
|
||||
and_in_dict_li_obj = None
|
||||
|
||||
@@ -264,6 +267,9 @@ async def get_obj_li(
|
||||
if jp_obj.get('and_qry'): # NOTE: This is for the additional AND clauses in the WHERE statement
|
||||
and_qry_dict_obj = jp_obj['and_qry']
|
||||
|
||||
if jp_obj.get('and_like'): # NOTE: This is for the additional AND LIKE clauses in the WHERE statement
|
||||
and_like_dict_obj = jp_obj['and_like']
|
||||
|
||||
if jp_obj.get('and_in_li'): # NOTE: This is for the additional AND IN clauses in the WHERE statement
|
||||
and_in_dict_li_obj = jp_obj['and_in_li']
|
||||
|
||||
@@ -355,6 +361,7 @@ async def get_obj_li(
|
||||
hidden = hidden,
|
||||
fulltext_qry_dict = fulltext_qry_dict_obj,
|
||||
and_qry_dict = and_qry_dict_obj,
|
||||
and_like_dict = and_like_dict_obj,
|
||||
and_in_dict_li = and_in_dict_li_obj,
|
||||
# fulltext_qry_field_li = fulltext_qry_field_li,
|
||||
# fulltext_qry_str = fulltext_qry_str,
|
||||
@@ -373,6 +380,7 @@ async def get_obj_li(
|
||||
hidden = hidden,
|
||||
fulltext_qry_dict = fulltext_qry_dict_obj,
|
||||
and_qry_dict = and_qry_dict_obj,
|
||||
and_like_dict = and_like_dict_obj,
|
||||
and_in_dict_li = and_in_dict_li_obj,
|
||||
# fulltext_qry_field_li = fulltext_qry_field_li,
|
||||
# fulltext_qry_str = fulltext_qry_str,
|
||||
|
||||
Reference in New Issue
Block a user