API CRUD functions can now handle sorting. And headers are now handled a little better.
This commit is contained in:
@@ -535,6 +535,7 @@ def sql_select(
|
||||
field_value = None,
|
||||
enabled: str|None = None, # enabled, disabled, all
|
||||
hidden: str|None = None, # hidden, not_hidden, all
|
||||
order_by_li: dict|None = None, # {"the_field_name": "DESC"}
|
||||
limit: int = 9999999,
|
||||
offset: int = 0,
|
||||
sql: str|None = None,
|
||||
@@ -543,7 +544,7 @@ def sql_select(
|
||||
as_dict: bool|None = True,
|
||||
as_list: bool|None = False,
|
||||
max_count: int = 100000,
|
||||
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log_lvl: int = logging.DEBUG, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
) -> None|bool|dict|list:
|
||||
log.setLevel(log_lvl)
|
||||
|
||||
@@ -553,6 +554,30 @@ def sql_select(
|
||||
else:
|
||||
sql_limit_offset = ''
|
||||
|
||||
sql_order_by = ''
|
||||
# order_by_li = {'created_on': 'DESC', 'updated_on': 'DESC'}
|
||||
log.debug(order_by_li)
|
||||
if order_by_li and isinstance(order_by_li, dict): # This should be a list
|
||||
order_by_str_li = []
|
||||
for key, value in order_by_li.items():
|
||||
order_by_str_li.append(f'`{table_name}`.`{key}` {value}')
|
||||
|
||||
# log.debug(order_by_str_li)
|
||||
|
||||
# if isinstance(value, dict) or isinstance(value, list):
|
||||
# data[key] = json.dumps(value)
|
||||
|
||||
log.debug(order_by_str_li)
|
||||
|
||||
order_by_string = ', '.join(order_by_str_li)
|
||||
sql_order_by = f'ORDER BY {order_by_string}'
|
||||
else:
|
||||
sql_order_by = ''
|
||||
log.debug(sql_order_by)
|
||||
|
||||
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
|
||||
# if enabled:
|
||||
# sql_enabled = sql_enable_part(table_name=table_name, enabled=enabled) # Reasonably safe return str
|
||||
# else:
|
||||
@@ -594,6 +619,7 @@ def sql_select(
|
||||
f"""
|
||||
SELECT *
|
||||
FROM `{table_name}`
|
||||
{sql_order_by}
|
||||
{sql_limit_offset}
|
||||
;
|
||||
"""
|
||||
@@ -610,6 +636,7 @@ def sql_select(
|
||||
SELECT *
|
||||
FROM `{table_name}`
|
||||
WHERE `{table_name}`.id = :record_id
|
||||
{sql_order_by}
|
||||
{sql_limit_offset}
|
||||
;
|
||||
"""
|
||||
@@ -622,6 +649,7 @@ def sql_select(
|
||||
SELECT *
|
||||
FROM `{table_name}`
|
||||
WHERE `{table_name}`.id_random = :record_id_random
|
||||
{sql_order_by}
|
||||
{sql_limit_offset}
|
||||
;
|
||||
"""
|
||||
@@ -665,6 +693,7 @@ def sql_select(
|
||||
WHERE `{table_name}`.{field_name} = :{field_name}
|
||||
{sql_enabled}
|
||||
{sql_hidden}
|
||||
{sql_order_by}
|
||||
{sql_limit_offset}
|
||||
;
|
||||
"""
|
||||
@@ -691,6 +720,7 @@ def sql_select(
|
||||
SELECT *
|
||||
FROM `{table_name}`
|
||||
WHERE {sql_where_string}
|
||||
{sql_order_by}
|
||||
{sql_limit_offset}
|
||||
;
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user