add mieter, refactor to getOne and getMany

This commit is contained in:
2021-01-17 00:20:42 +01:00
parent 9a907c3a8e
commit 878ff31a52
5 changed files with 153 additions and 89 deletions

View File

@ -26,3 +26,37 @@ def createConnectionPool():
def getConnection():
global pool
return pool.get_connection()
def getMany(stmt):
try:
dbh = getConnection()
objs = []
cur = dbh.cursor(dictionary=True)
cur.execute(stmt)
for obj in cur:
objs.append(obj)
return objs
except Exception as err:
return str(err), 500
finally:
dbh.close()
def getOne(stmt, params, objName):
try:
dbh = getConnection()
cur = dbh.cursor(dictionary=True)
cur.execute(stmt, params)
obj = cur.next()
if not obj:
return "{} not found".format(objName), 404
invObj = cur.next()
if invObj:
return "More than one {} by that id ({}, {})".format(objName, id, invObj), 500
return obj
except Exception as err:
return str(err), 500
finally:
dbh.close()