diff --git a/app/db_sql.py b/app/db_sql.py index fbae860..fe62db2 100644 --- a/app/db_sql.py +++ b/app/db_sql.py @@ -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 diff --git a/app/routers/api_crud.py b/app/routers/api_crud.py index d5f144f..1bef5a1 100644 --- a/app/routers/api_crud.py +++ b/app/routers/api_crud.py @@ -383,6 +383,9 @@ def handle_get_obj_li( # 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 + # This should be a dict list of fields with a list of values to search for using OR LIKE. + or_like_dict_obj = None + jp_obj = None if jp: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL @@ -404,6 +407,9 @@ def handle_get_obj_li( 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('or_like'): # NOTE: This is for the additional OR LIKE clauses in the WHERE statement + or_like_dict_obj = jp_obj['or_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'] @@ -496,6 +502,7 @@ def handle_get_obj_li( fulltext_qry_dict = fulltext_qry_dict_obj, and_qry_dict = and_qry_dict_obj, and_like_dict = and_like_dict_obj, + or_like_dict = or_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, @@ -515,6 +522,7 @@ def handle_get_obj_li( fulltext_qry_dict = fulltext_qry_dict_obj, and_qry_dict = and_qry_dict_obj, and_like_dict = and_like_dict_obj, + or_like_dict = or_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, diff --git a/app/routers/api_crud_v2.py b/app/routers/api_crud_v2.py index be2abf6..64536d1 100644 --- a/app/routers/api_crud_v2.py +++ b/app/routers/api_crud_v2.py @@ -331,6 +331,9 @@ def handle_get_obj_li( 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('or_like'): # NOTE: This is for the additional OR LIKE clauses in the WHERE statement + or_like_dict_obj = jp_obj['or_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'] @@ -442,6 +445,7 @@ def handle_get_obj_li( fulltext_qry_dict = fulltext_qry_dict_obj, and_qry_dict = and_qry_dict_obj, and_like_dict = and_like_dict_obj, + or_like_dict = or_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, @@ -461,6 +465,7 @@ def handle_get_obj_li( fulltext_qry_dict = fulltext_qry_dict_obj, and_qry_dict = and_qry_dict_obj, and_like_dict = and_like_dict_obj, + or_like_dict = or_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,