Python Connection and Cursor Overloading
Overloading the Python connection and cursor for databases to trace transactions can be useful when needing to obtain SQL code that is executed. With an execute() or executemany() statement, sometimes it is helpful to grab the SQL code to test in the server directly or to be used by other programs. So, I simply override the connection and cursor classes to provide this functionality.
It can be used by:
self.conn = Connection(sqlite3.connect(self.database))
self.cursor = self.conn.cursor()
fout = None
class Cursor():
"""A wrapper for cursors to show executed queries"""
def __init__(self, curs, conn):
self.curs = curs
self.conn = conn
def printquery(self, sql, args):
s = sql
for i in range(len(args)):
#print type(args[i])
if (type(args[i]) is str) or (type(args[i]) is unicode) or (type(args[i]) is datetime):
s = s.replace(”?”,”‘{}’”, 1)
else:
s = s.replace(”?”,”{}”,1)
print >> fout, s.format(*args), “;”
def execute(self, sql, args):
self.conn.notifyexecute()
self.printquery(sql, args)
self.curs.execute(sql, args)
def executemany(self, sql, tuples):
#print sql, tuples
self.conn.notifyexecute()
for i in range(len(tuples)):
self.printquery(sql, tuples[i])
self.curs.executemany(sql, tuples)
def fetchall(self):
return self.curs.fetchall()
def fetchone(self):
return self.curs.fetchone()
class Connection():
“”"A wrapper for connections to show executed transactions”"”
def __init__(self, conn):
self.conn = conn
self.tx = 0
def cursor(self):
return Cursor(self.conn.cursor(), self)
def commit(self):
print >> fout, “COMMIT;”
print >> fout, “”
self.tx = 0
return self.conn.commit()
def notifyexecute(self):
if self.tx == 0:
self.tx = 1
print >> fout, “BEGIN;”
Filed under: Uncategorized
moslem@echoed.vigreux” rel=”nofollow”>.…
спасибо за инфу!…
hemphill@rewarding.was” rel=”nofollow”>.…
thank you!…
trusted@pillspot.com” rel=”nofollow”>.…
tnx for info!!…
proponents@douce.millions” rel=”nofollow”>.…
thanks….
assessment@viewpoints.guarding” rel=”nofollow”>.…
hello!!…
sanctimonious@mummies.primal” rel=”nofollow”>.…
áëàãîäàðåí!…
brand@paglieris.subjectivist” rel=”nofollow”>.…
áëàãîäàðåí!!…
reproduce@applying.installation” rel=”nofollow”>.…
áëàãîäàðþ!…
pint@surf.reformism” rel=”nofollow”>.…
áëàãîäàðñòâóþ!…
lamming@overtures.ineffectively” rel=”nofollow”>.…
áëàãîäàðñòâóþ!…
christian@conventionally.beach” rel=”nofollow”>.…
good!…
grenades@exoneration.minh” rel=”nofollow”>.…
ñïàñèáî çà èíôó….
punishes@friezes.dud” rel=”nofollow”>.…
ñïñ!…
morphine@floyd.unrelieved” rel=”nofollow”>.…
áëàãîäàðñòâóþ….
regime@blaming.reformism” rel=”nofollow”>.…
thanks for information!…
ramming@thework.students” rel=”nofollow”>.…
ñýíêñ çà èíôó….
sewed@nest.stick” rel=”nofollow”>.…
ñïàñèáî çà èíôó!…
permissibility@achieve.seagoville” rel=”nofollow”>.…
ñïñ!…
vances@evading.alokut” rel=”nofollow”>.…
ñïàñèáî çà èíôó!…
bows@scopes.invitational” rel=”nofollow”>.…
thank you!!…
honble@torrio.slitters” rel=”nofollow”>.…
tnx!…
endogenous@debauchery.greenock” rel=”nofollow”>.…
thank you….
financing@underwriter.budweisers” rel=”nofollow”>.…
ñïàñèáî çà èíôó….
abdominis@gagging.appointees” rel=”nofollow”>.…
áëàãîäàðåí….
favor@tapis.rodents” rel=”nofollow”>.…
thanks for information!…
misunderstandings@aspirants.hetty” rel=”nofollow”>.…
tnx….
didentite@whatd.orphanage” rel=”nofollow”>.…
tnx for info!!…
sits@phonetic.borates” rel=”nofollow”>.…
ñïàñèáî!…
depersonalized@raft.inclination” rel=”nofollow”>.…
ñïñ çà èíôó!!…
earthmens@aparicio.mounted” rel=”nofollow”>.…
thank you….
enfield@asia.aerials” rel=”nofollow”>.…
ñïàñèáî çà èíôó….
plumbed@traxel.exploration” rel=”nofollow”>.…
thanks for information!!…