Skip to content

Commit

Permalink
deduplicate cursor execution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
szampier committed Feb 8, 2024
1 parent 95974d1 commit e0cbe66
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/ngamsCore/ngamsLib/ngamsDbCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ def cleanSrvList(srvList):
return srvList


def _execute(cursor, query, args):
# Some drivers complain when an empty argument list/tuple is passed
# so let's avoid it
if args:
cursor.execute(query, args)
else:
cursor.execute(query)


class ngamsDbCursor(object):
"""
A class representing a cursor over which a query is run and where results
Expand All @@ -454,10 +463,7 @@ def __init__(self, pool, query, args):
try:
self.conn = pool.connection()
self.cursor = self.conn.cursor()
if args:
self.cursor.execute(query, args)
else:
self.cursor.execute(query)
_execute(self.cursor, query, args)
except:
self.close()
raise
Expand Down Expand Up @@ -566,13 +572,7 @@ def execute(self, sql, args=()):
cursor = self.cursor

with ngamsDbTimer(self.db_core, sql):

# Some drivers complain when an empty argument list/tuple is passed
# so let's avoid it
if not args:
cursor.execute(sql)
else:
cursor.execute(sql, args)
_execute(cursor, sql, args)

# From PEP-249, regarding .description:
# This attribute will be None for operations that do not return
Expand Down

0 comments on commit e0cbe66

Please sign in to comment.