Working on membership, person, and user

This commit is contained in:
Scott Idem
2021-06-25 18:49:08 -04:00
parent ca43cc4dce
commit f55d9c2c62
10 changed files with 768 additions and 333 deletions

View File

@@ -192,8 +192,14 @@ def sql_update(sql:str|None=None, data:dict|None=None, table_name:str|None=None,
# ### BEGIN ### Core Help CRUD ### sql_insert_or_update() ###
# The catch all SQL INSERT or UPDATE function - STI 2021-02-17
# This one does it all for SQL INSERT and UPDATE queries
def sql_insert_or_update(sql:str|None=None, data:dict|None=None, table_name:str|None=None, rm_id_random:bool=False, id_random_length:int|None=None):
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
def sql_insert_or_update(
sql: str|None = None,
data: dict|None = None,
table_name: str|None = None,
rm_id_random: bool = False,
id_random_length: int|None = None
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
#if sql: pass
@@ -232,7 +238,7 @@ def sql_insert_or_update(sql:str|None=None, data:dict|None=None, table_name:str|
;
""")
#log.setLevel(logging.DEBUG)
log.setLevel(logging.DEBUG)
log.debug(f"""
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string})
ON DUPLICATE KEY UPDATE
@@ -242,6 +248,7 @@ def sql_insert_or_update(sql:str|None=None, data:dict|None=None, table_name:str|
trans = db.begin()
try:
log.debug(data)
result_insert_or_update = db.execute(sql_insert_or_update, data)
trans.commit()
except Exception as e:
@@ -256,15 +263,15 @@ def sql_insert_or_update(sql:str|None=None, data:dict|None=None, table_name:str|
log.debug(result_insert_or_update)
log.debug(f'rowcount = {result_insert_or_update.rowcount}; lastrowid = {result_insert_or_update.lastrowid}')
if result_insert_or_update.rowcount == 1 and result_insert_or_update.lastrowid > 0: # insert
log.info('Insert record')
record_id = result_insert_or_update.lastrowid
log.info(f'Insert record: {record_id}')
return record_id
elif result_insert_or_update.rowcount == 1 and result_insert_or_update.lastrowid == 0: # update with no change
log.info('Update record with no change')
return True
elif result_insert_or_update.rowcount == 2 and result_insert_or_update.lastrowid > 0: # update with change
log.info('Update record with changes')
record_id = result_insert_or_update.lastrowid
log.info(f'Update record with changes: {record_id}')
return record_id
else:
log.debug(result_insert_or_update)