Work on CRUD v2 and better SQL WHERE part building... I hope.

This commit is contained in:
Scott Idem
2024-08-14 14:36:07 -04:00
parent 3d48220b8f
commit 18293764fd
4 changed files with 140 additions and 9 deletions

View File

@@ -548,6 +548,7 @@ def sql_select(
field_value = None,
enabled: str|None = None, # enabled, disabled, all
hidden: str|None = None, # hidden, not_hidden, all
qry_dict_li: dict|None = None, # NEW 2024-08-14
fulltext_qry_dict: dict|None = None,
and_qry_dict: dict|None = None,
and_like_dict: dict|None = None,
@@ -703,12 +704,21 @@ def sql_select(
elif table_name and field_name and field_value and not (record_id or record_id_random or sql or data):
# Select all records from a table with a specific field and field value
# Updated 2023-11-30
# log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.info('Select all records from a table with a specific field and field value')
if not data:
data = {}
# This is the new catch all version for building the WHERE clause for the SQL SELECT statement. -2024-08-14
sql_where_qry = ''
if qry_dict_li:
log.info('Creating partial SQL string for search.')
sql_where_qry, data_qry = sql_where_qry_part(qry_dict_li)
# NOTE: Merge the data_qry result with the data dict
data = {**data, **data_qry}
sql_fulltext_match_against = ''
if fulltext_qry_dict:
log.info('Creating partial SQL string for fulltext search.')
@@ -807,6 +817,7 @@ def sql_select(
SELECT *
FROM `{table_name}`
WHERE `{table_name}`.{field_name} = :{field_name}
{sql_where_qry}
{sql_fulltext_match_against}
{sql_and_qry}
{sql_and_like}
@@ -1692,6 +1703,69 @@ def get_account_id_w_for_type_id(
# ### END ### API DB SQL Methods ### get_account_id_w_for_type_id() ###
# ### BEGIN ### API DB SQL Methods ### sql_where_qry_part() ###
# Example JSON data
# jp: {
# qry: [
# {
# type: "AND",
# field: "enable",
# operator: "=",
# value: TRUE
# },
# {
# type: "AND",
# field: "example",
# operator: ">=",
# value: 2
# },
# {
# type: "OR",
# field: "test",
# operator: "LIKE",
# value: "%xyz%"
# },
# ]
# }
# Updated 2024-08-14
def sql_where_qry_part(
qry_dict_li: list, # JSON data
) -> bool|str:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
data = {}
sql_where_qry = ''
if qry_dict_li and isinstance(qry_dict_li, list):
log.info('Creating partial SQL string for WHERE queries.')
sql_where_qry_str = []
for qry in qry_dict_li:
log.debug(qry)
if qry.get('type') == '':
sql_where_qry_str.append(f'{qry.get("field")} {qry.get("operator")} :{qry.get("field")}')
data[qry.get('field')] = qry.get('value')
elif qry.get('type') == 'AND':
sql_where_qry_str.append(f'AND {qry.get("field")} {qry.get("operator")} :{qry.get("field")}')
data[qry.get('field')] = qry.get('value')
elif qry.get('type') == 'OR':
sql_where_qry_str.append(f'OR {qry.get("field")} {qry.get("operator")} :{qry.get("field")}')
data[qry.get('field')] = qry.get('value')
else:
log.error(f'Unknown query type: {qry.get("type")}')
return False
# Should this WHERE part also be surrounded by parentheses???
# sql_where_qry = 'AND ('+' '.join(sql_where_qry_str)+')'
# sql_where_qry = sql_where_qry_str
sql_where_qry = ' '.join(sql_where_qry_str)
log.debug(sql_where_qry)
return sql_where_qry, data
# ### END ### API DB SQL Methods ### sql_where_qry_part() ###
# ### BEGIN ### API DB SQL Methods ### sql_fulltext_qry_part() ###
# Updated 2023-11-30
@logger_reset