Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/__init__.py
Show First 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | def __init__(self, conn, pool=None): | ||||
conn: psycopg2 connection to the SWH DB | conn: psycopg2 connection to the SWH DB | ||||
pool: psycopg2 pool of connections | pool: psycopg2 pool of connections | ||||
""" | """ | ||||
self.adapt_conn(conn) | self.adapt_conn(conn) | ||||
self.conn = conn | self.conn = conn | ||||
self.pool = pool | self.pool = pool | ||||
def __del__(self): | def put_conn(self): | ||||
if self.pool: | if self.pool: | ||||
self.pool.putconn(self.conn) | self.pool.putconn(self.conn) | ||||
def cursor(self, cur_arg=None): | def cursor(self, cur_arg=None): | ||||
"""get a cursor: from cur_arg if given, or a fresh one otherwise | """get a cursor: from cur_arg if given, or a fresh one otherwise | ||||
meant to avoid boilerplate if/then/else in methods that proxy stored | meant to avoid boilerplate if/then/else in methods that proxy stored | ||||
procedures | procedures | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | def copy_to(self, items, tblname, columns, | ||||
with open(write_file, 'w') as f: | with open(write_file, 'w') as f: | ||||
for d in items: | for d in items: | ||||
if item_cb is not None: | if item_cb is not None: | ||||
item_cb(d) | item_cb(d) | ||||
line = [escape(d.get(k, default_values.get(k))) | line = [escape(d.get(k, default_values.get(k))) | ||||
for k in columns] | for k in columns] | ||||
f.write(','.join(line)) | f.write(','.join(line)) | ||||
f.write('\n') | f.write('\n') | ||||
finally: | finally: | ||||
# No problem bubbling up exceptions, but we still need to make sure | # No problem bubbling up exceptions, but we still need to make sure | ||||
# we finish copying, even though we're probably going to cancel the | # we finish copying, even though we're probably going to cancel the | ||||
# transaction. | # transaction. | ||||
write_thread.join() | write_thread.join() | ||||
def mktemp(self, tblname, cur=None): | def mktemp(self, tblname, cur=None): | ||||
self.cursor(cur).execute('SELECT swh_mktemp(%s)', (tblname,)) | self.cursor(cur).execute('SELECT swh_mktemp(%s)', (tblname,)) |