add mieter, refactor to getOne and getMany
This commit is contained in:
34
dbpool.py
34
dbpool.py
@ -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()
|
||||
|
Reference in New Issue
Block a user