Now handles URL params with a list for IN part of SQL SELECT
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_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'
|
||||
order_by_li: dict|None = None, # {"the_field_name": "DESC"}
|
||||
@@ -713,6 +714,13 @@ def sql_select(
|
||||
# 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)
|
||||
|
||||
# NOTE: Merge the data_qry result with the data dict
|
||||
data = {**data, **data_qry}
|
||||
|
||||
# # NOTE: Version 3 of the fulltext search
|
||||
# sql_fulltext_match_against = ''
|
||||
# log.debug(fulltext_qry_dict)
|
||||
@@ -773,6 +781,7 @@ def sql_select(
|
||||
WHERE `{table_name}`.{field_name} = :{field_name}
|
||||
{sql_fulltext_match_against}
|
||||
{sql_and_qry}
|
||||
{sql_and_in_dict_li}
|
||||
{sql_enabled}
|
||||
{sql_hidden}
|
||||
{sql_order_by}
|
||||
@@ -824,10 +833,10 @@ def sql_select(
|
||||
log.warning('Nothing matched the expected combination of parameters passed to this function')
|
||||
return False # Not successful
|
||||
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(sql)
|
||||
log.debug(data)
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
result = run_sql_select(sql=sql, data=data)
|
||||
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
@@ -1676,6 +1685,38 @@ def sql_and_qry_part(
|
||||
return sql_and_qry, 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']})
|
||||
# Updated 2024-03-15
|
||||
@logger_reset
|
||||
def sql_and_in_dict_li_part(
|
||||
and_in_dict_li_dict_obj: dict
|
||||
) -> bool|dict:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
data = {}
|
||||
sql_and_in_dict_li = ''
|
||||
|
||||
log.debug(and_in_dict_li_dict_obj)
|
||||
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.')
|
||||
and_in_dict_li_dict_obj_str = []
|
||||
|
||||
for key, value in and_in_dict_li_dict_obj.items():
|
||||
log.debug(f'Key = {key}; Value = {value}')
|
||||
and_in_dict_li_dict_obj_str.append(f'{key} IN :and_in_{key}')
|
||||
# and_in_dict_li_dict_obj_str.append(f'{key} IN ( :and_in_{key} )')
|
||||
data[f'and_in_{key}'] = value
|
||||
and_in_dict_li_field_string = ' AND '.join(and_in_dict_li_dict_obj_str)
|
||||
|
||||
sql_and_in_dict_li = f'AND ({and_in_dict_li_field_string})'
|
||||
log.debug(sql_and_in_dict_li)
|
||||
log.debug(data)
|
||||
|
||||
return sql_and_in_dict_li, data
|
||||
|
||||
# ### BEGIN ### API DB SQL Methods ### sql_enable_part() ###
|
||||
# Updated 2022-01-17
|
||||
@logger_reset
|
||||
|
||||
Reference in New Issue
Block a user