General code clean up for debugging. Also work on event badge related.
This commit is contained in:
@@ -175,9 +175,10 @@ def sql_update(
|
||||
record_id: int|None = None,
|
||||
record_id_random: str|None = None,
|
||||
rm_id_random: bool = False,
|
||||
id_random_length: None|int = None
|
||||
id_random_length: None|int = None,
|
||||
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(log_lvl)
|
||||
log.debug(locals())
|
||||
|
||||
if sql:
|
||||
@@ -458,9 +459,9 @@ 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
|
||||
) -> None|bool|dict|list:
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
log.setLevel(log_lvl)
|
||||
|
||||
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
|
||||
@@ -543,11 +544,11 @@ def sql_select(
|
||||
sql = text(sql)
|
||||
elif sql and data and not (table_name or record_id or record_id_random or field_name or field_value):
|
||||
# Select records based on the SQL statement given and with the matching data dict fields and values
|
||||
log.info('Select records based on the SQL statement given and with the matching data dict fields and values')
|
||||
|
||||
if rm_id_random:
|
||||
data = lookup_id_random_pop(obj_data=data)
|
||||
|
||||
log.info('Select records based on the SQL statement given and with the matching data dict fields and values')
|
||||
sql = text(sql)
|
||||
else:
|
||||
# Nothing matched the expected combination of parameters passed to this function
|
||||
@@ -628,36 +629,34 @@ def sql_select(
|
||||
log.debug('Successfully executed the SQL on the first try.')
|
||||
pass
|
||||
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(f'Row count: {result.rowcount}')
|
||||
#log.debug(vars(result))
|
||||
#log.debug(dir(result))
|
||||
|
||||
# NOTE: as_dict defaults to True for this function
|
||||
# NOTE: as_list defaults to False for this function
|
||||
# NOTE: After testing, this method is the fastest way to convert to a dict - STI 2021-03-09
|
||||
# NOTE: My custom sql_result_proxy_to_dict_simple(result_proxy=result.first()) is slower than using dict().
|
||||
# NOTE: list(result) was tested seems to be the slowest. Slower than my custom function.
|
||||
|
||||
if result.rowcount == 1:
|
||||
log.info(f'Found one record. as_dict={as_dict}, as_list={as_list}')
|
||||
if as_dict:
|
||||
# After testing, this method is the fastest way to convert to a dict - STI 2021-03-09
|
||||
# my custom sql_result_proxy_to_dict_simple(result_proxy=result.first()) is slower
|
||||
record = dict(result.first())
|
||||
else:
|
||||
record = result.first()
|
||||
if as_list:
|
||||
record_li = []
|
||||
record_li.append(record)
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(record_li)
|
||||
|
||||
return record_li # Successful
|
||||
else:
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(record)
|
||||
|
||||
return record # Successful
|
||||
elif result.rowcount > 1:
|
||||
log.info(f'Found {result.rowcount} records. as_dict={as_dict}, as_list={as_list}')
|
||||
#log.info('Found more than one record. Returning as a list of dicts.')
|
||||
if as_dict:
|
||||
# After testing, this method is the fastest way to convert to a list of dicts - STI 2021-03-09
|
||||
# list(result) was tested and seems to be the slowest
|
||||
# my custom sql_result_proxy_to_dict_simple(result_proxy=result.fetchall()) was tested and is only slightly faster than list(result)
|
||||
#timer_1_start = timer()
|
||||
record_li = [dict(record) for record in result.fetchall()]
|
||||
#log.debug(record_li)
|
||||
@@ -672,16 +671,14 @@ def sql_select(
|
||||
return record_li # Successful
|
||||
else:
|
||||
if as_list:
|
||||
# log.info('No records found. Returning None since the list is an empty list.')
|
||||
log.info('No records found. Returning an empty list.')
|
||||
log.debug(result)
|
||||
|
||||
return [] # Successful
|
||||
return [] # Successful even though no results
|
||||
else:
|
||||
log.info('No records found. Returning None.')
|
||||
log.debug(result)
|
||||
|
||||
return None # Successful
|
||||
return None # Successful even though no results
|
||||
# ### END ### Core Help CRUD ### sql_select() ###
|
||||
|
||||
|
||||
@@ -1264,7 +1261,7 @@ def get_account_id_w_for_type_id(
|
||||
# Updated 2022-01-17
|
||||
@logger_reset
|
||||
def sql_enable_part(table_name: str, enabled: str) -> bool|dict:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not table_name: return False
|
||||
@@ -1282,10 +1279,8 @@ def sql_enable_part(table_name: str, enabled: str) -> bool|dict:
|
||||
elif enabled == 'all':
|
||||
sql = f'AND (`{table_name}`.enable = true OR `{table_name}`.enable = false OR `{table_name}`.enable IS NULL)'
|
||||
enable = None
|
||||
|
||||
log.debug(sql)
|
||||
log.debug(enable)
|
||||
# return result
|
||||
|
||||
return sql, enable
|
||||
else:
|
||||
return False
|
||||
@@ -1296,7 +1291,7 @@ def sql_enable_part(table_name: str, enabled: str) -> bool|dict:
|
||||
# Updated 2022-01-17
|
||||
@logger_reset
|
||||
def sql_limit_offset_part(limit: int, offset: int = 0) -> bool|str:
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if limit >= 0 and offset >= 0:
|
||||
|
||||
Reference in New Issue
Block a user