Working on general PATCH template function.
This commit is contained in:
103
app/db_sql.py
103
app/db_sql.py
@@ -25,7 +25,7 @@ db = engine.connect()
|
||||
|
||||
# ### BEGIN ### Core Help CRUD ### sql_insert() ###
|
||||
def sql_insert(sql:str=None, data:dict=None, table_name:str=None, id_random_length:int=8):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if sql:
|
||||
@@ -81,7 +81,7 @@ def sql_insert(sql:str=None, data:dict=None, table_name:str=None, id_random_leng
|
||||
return False
|
||||
else:
|
||||
log.debug(result_insert)
|
||||
log.debug(result_insert.rowcount)
|
||||
log.debug(f'rowcount = {result_insert.rowcount}; lastrowid = {result_insert.lastrowid}')
|
||||
if result_insert.rowcount == 1 and result_insert.lastrowid > 0: # insert
|
||||
log.info('Insert record')
|
||||
log.debug(result_insert.lastrowid)
|
||||
@@ -106,10 +106,77 @@ def sql_insert(sql:str=None, data:dict=None, table_name:str=None, id_random_leng
|
||||
|
||||
|
||||
# ### BEGIN ### Core Help CRUD ### sql_update() ###
|
||||
def sql_update(sql=None, data=None, table_name=None, rm_id_random=None, id_random_length=None):
|
||||
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
def sql_update(sql:str=None, data:dict=None, table_name:str=None, record_id:int=None, record_id_random:str=None, rm_id_random=None, id_random_length:int=8):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if sql:
|
||||
sql_update = text(sql)
|
||||
elif table_name and data:
|
||||
#if rm_id_random:
|
||||
#data = lookup_id_random_pop(obj_data=data)
|
||||
if not data.get('id_random', None) and id_random_length:
|
||||
data['id_random'] = secrets.token_urlsafe(id_random_length)
|
||||
|
||||
log.debug(data)
|
||||
|
||||
fields_string = []
|
||||
for key, value in data.items():
|
||||
if key != 'id': # Creating a special exception for the id field.
|
||||
fields_string.append('`'+str(key) + '` = :' + str(key))
|
||||
|
||||
sql_set = ', '.join(fields_string)
|
||||
|
||||
if record_id:
|
||||
log.info('Update record with ID')
|
||||
data['id'] = record_id
|
||||
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id = :id'
|
||||
elif record_id_random:
|
||||
log.info('Update record with ID random')
|
||||
data['id_random'] = record_id_random
|
||||
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id_random = :id_random'
|
||||
elif 'id' in data:
|
||||
log.info('Update record with ID')
|
||||
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id = :id'
|
||||
elif 'id_random' in data:
|
||||
# NOTE: For now it is not possible to update the id_random when supplying the id_random as the primary key for a record.
|
||||
# NOTE: In the future I can use record_id_random=True as a special case SQL UPDATE.
|
||||
log.info('Update record with ID random')
|
||||
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id_random = :id_random'
|
||||
else:
|
||||
return False
|
||||
sql_update = text(sql)
|
||||
|
||||
trans = db.begin()
|
||||
try:
|
||||
result_update = db.execute(sql_update, data)
|
||||
trans.commit()
|
||||
except Exception as e:
|
||||
trans.rollback()
|
||||
log.exception('*** An exception happened. ***')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
return False
|
||||
else:
|
||||
log.debug(result_update)
|
||||
log.debug(f'rowcount = {result_update.rowcount}; lastrowid = {result_update.lastrowid}')
|
||||
if result_update.rowcount == 1 and result_update.lastrowid == 0: # update with no change
|
||||
log.info('Update record (with no change???)') # With SQL UPDATE this record may have actually changed
|
||||
return True
|
||||
elif result_update.rowcount == 2 and result_update.lastrowid > 0: # update with change
|
||||
log.warning('Should we be here???')
|
||||
log.info('Update record with changes')
|
||||
record_id = result_update.lastrowid
|
||||
return record_id
|
||||
else:
|
||||
log.debug(result_update)
|
||||
log.debug(vars(result_update))
|
||||
log.debug(dir(result_update))
|
||||
log.debug(result_update.rowcount) # returns 1 on insert and 2 on update with change
|
||||
log.debug(result_update.lastrowid) # returns last row ID on insert and update with a change and returns 0 if nothing changed
|
||||
return False
|
||||
return False
|
||||
# ### END ### Core Help CRUD ### sql_update() ###
|
||||
|
||||
@@ -118,7 +185,7 @@ def sql_update(sql=None, data=None, table_name=None, rm_id_random=None, id_rando
|
||||
# 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, data:dict=None, table_name:str=None, rm_id_random:bool=None, id_random_length:int=None):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
#if sql: pass
|
||||
@@ -165,7 +232,7 @@ def sql_insert_or_update(sql:str=None, data:dict=None, table_name:str=None, rm_i
|
||||
|
||||
trans = db.begin()
|
||||
try:
|
||||
result_insert = db.execute(sql_insert_or_update, data)
|
||||
result_insert_or_update = db.execute(sql_insert_or_update, data)
|
||||
trans.commit()
|
||||
except Exception as e:
|
||||
trans.rollback()
|
||||
@@ -176,23 +243,25 @@ def sql_insert_or_update(sql:str=None, data:dict=None, table_name:str=None, rm_i
|
||||
log.exception('^^^ exception ^^^')
|
||||
return False
|
||||
else:
|
||||
if result_insert.rowcount == 1 and result_insert.lastrowid > 0: # insert
|
||||
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.lastrowid
|
||||
record_id = result_insert_or_update.lastrowid
|
||||
return record_id
|
||||
elif result_insert.rowcount == 1 and result_insert.lastrowid == 0: # update with no change
|
||||
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.rowcount == 2 and result_insert.lastrowid > 0: # update with change
|
||||
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.lastrowid
|
||||
record_id = result_insert_or_update.lastrowid
|
||||
return record_id
|
||||
else:
|
||||
log.debug(result_insert)
|
||||
log.debug(vars(result_insert))
|
||||
log.debug(dir(result_insert))
|
||||
log.debug(result_insert.rowcount) # returns 1 on insert and 2 on update with change
|
||||
log.debug(result_insert.lastrowid) # returns last row ID on insert and update with a change and returns 0 if nothing changed
|
||||
log.debug(result_insert_or_update)
|
||||
log.debug(vars(result_insert_or_update))
|
||||
log.debug(dir(result_insert_or_update))
|
||||
log.debug(result_insert_or_update.rowcount) # returns 1 on insert and 2 on update with change
|
||||
log.debug(result_insert_or_update.lastrowid) # returns last row ID on insert and update with a change and returns 0 if nothing changed
|
||||
return False
|
||||
return False
|
||||
# ### END ### Core Help CRUD ### sql_insert_or_update() ###
|
||||
@@ -407,7 +476,7 @@ def sql_select(table_name=None, record_id=None, record_id_random=None, field_nam
|
||||
# The catch all SQL DELETE function - STI 2021-02-17
|
||||
# This one does it all for SQL DELETE queries
|
||||
def sql_delete(table_name:str=None, record_id:int=None, record_id_random:str=None, field_name:str=None, field_value=None, sql:str=None, data:dict=None):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if table_name and (record_id or record_id_random) and not (field_name or field_value or sql or data):
|
||||
|
||||
Reference in New Issue
Block a user