Updates related to SQL and limit and offset

This commit is contained in:
Scott Idem
2023-06-29 16:10:37 -04:00
parent ed0e20fa6a
commit 8ed55b1ed9
2 changed files with 26 additions and 6 deletions

View File

@@ -520,8 +520,8 @@ def sql_insert_or_update(
# ### BEGIN ### Core Help CRUD ### sql_select() ###
# The catch all SQL SELECT function - STI 2021-02-17
# This one does it all for SQL SELECT queries
# Updated 2021-09-07
# This one does it all for SQL SELECT queries. It now works with limit and offset! - STI 2023-06-29
# Updated 2023-06-29
@logger_reset
def sql_select(
table_name: str|None = None,
@@ -529,6 +529,8 @@ def sql_select(
record_id_random: str|None = None,
field_name: str|None = None,
field_value = None,
limit: int = 9999999,
offset: int = 0,
sql: str|None = None,
data: dict|None = None,
rm_id_random: bool = False,
@@ -539,6 +541,12 @@ def sql_select(
) -> None|bool|dict|list:
log.setLevel(log_lvl)
if limit >= 0 and offset >= 0:
log.info(f'Creating partial SQL string for LIMIT and OFFSET. Limit: {limit}; Offset: {offset}')
sql_limit_offset = f'LIMIT {limit} OFFSET {offset}'
else:
sql_limit_offset = ''
if table_name and not (record_id or record_id_random or field_name or field_value or sql or data):
# Select all records from a table
log.info('Select all records from a table')
@@ -546,6 +554,7 @@ def sql_select(
f"""
SELECT *
FROM `{table_name}`
{sql_limit_offset}
;
"""
)
@@ -561,6 +570,7 @@ def sql_select(
SELECT *
FROM `{table_name}`
WHERE `{table_name}`.id = :record_id
{sql_limit_offset}
;
"""
)
@@ -572,12 +582,14 @@ def sql_select(
SELECT *
FROM `{table_name}`
WHERE `{table_name}`.id_random = :record_id_random
{sql_limit_offset}
;
"""
)
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
log.info('Select all records from a table with a specific field and field value')
data = {}
data[field_name] = field_value
@@ -586,6 +598,7 @@ def sql_select(
SELECT *
FROM `{table_name}`
WHERE `{table_name}`.{field_name} = :{field_name}
{sql_limit_offset}
;
"""
)
@@ -611,6 +624,7 @@ def sql_select(
SELECT *
FROM `{table_name}`
WHERE {sql_where_string}
{sql_limit_offset}
;
"""
)