Creating templates different types of objects or resources.
This commit is contained in:
24
app/db.py
24
app/db.py
@@ -1,3 +1,5 @@
|
||||
import secrets
|
||||
|
||||
from app.config import settings
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
@@ -19,16 +21,20 @@ db = engine.connect()
|
||||
|
||||
|
||||
# Insert a new record with values given.
|
||||
def sql_insert(table_name=None, record=None, sql=None, data=None):
|
||||
def sql_insert(table_name=None, record=None, sql=None, data=None, id_random_length=None):
|
||||
print('** sql_insert() ***')
|
||||
|
||||
if table_name and record:
|
||||
#record.pop('id')
|
||||
if id_random_length in [8, 16]:
|
||||
record['id_random'] = secrets.token_urlsafe(id_random_length)
|
||||
|
||||
fields = []
|
||||
values = []
|
||||
for key, value in record.items():
|
||||
if key != 'id': # A special exception for the id auto increment field.
|
||||
fields.append('`'+str(key)+'`')
|
||||
values.append(':'+str(key))
|
||||
#if key != 'id': # A special exception for the id auto increment field.
|
||||
fields.append('`'+str(key)+'`')
|
||||
values.append(':'+str(key))
|
||||
fields_string = ', '.join(fields)
|
||||
values_string = ', '.join(values)
|
||||
|
||||
@@ -157,15 +163,17 @@ def sql_select(sql=None, data=None, table_name=None, record_id=None, record_id_r
|
||||
else:
|
||||
if result.rowcount == 1 and as_list:
|
||||
print('Single as list')
|
||||
record = result.fetchall()
|
||||
return record
|
||||
record = dict(result.fetchone())
|
||||
return [record]
|
||||
elif result.rowcount == 1 and not as_list:
|
||||
print('Single as single')
|
||||
record = result.fetchone()
|
||||
#record = result.fetchone()
|
||||
record = dict(result.fetchone())
|
||||
return record
|
||||
elif result.rowcount > 1:
|
||||
print('List as list')
|
||||
records = result.fetchall()
|
||||
#records = result.fetchall()
|
||||
records = [dict(u) for u in result.fetchall()]
|
||||
return records
|
||||
else:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user