def query_cursor(self, q, lazy_fetch=False, commit=True):
"""Execute a query and yield a cursor.
All execution performed by the Postgres object uses this method.
Args:
q (str): SQL query
lazy_fetch (bool): whether to use a server-side cursor (lazily fetches results).
"""
self.cursors_opened += 1
if self.verbose:
logging.debug(q)
if self.debug:
empty_cursor = Bunch()
empty_cursor.fetchmany = lambda size: []
empty_cursor.fetchall = lambda: []
yield empty_cursor
return
cursor_name = 'server_side_{}'.format(self.cursors_opened) if lazy_fetch else None
with self.connection.cursor(cursor_name, cursor_factory=RealDictCursor) as cursor:
cursor.execute(q)
yield cursor
if commit:
self.commit()
评论列表
文章目录