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;”

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • Live
  • MySpace
  • Turn this article into a PDF!
  • Reddit
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz

32 Responses to “Python Connection and Cursor Overloading”

  1. moslem@echoed.vigreux” rel=”nofollow”>.…

    спасибо за инфу!…

  2. hemphill@rewarding.was” rel=”nofollow”>.…

    thank you!…

  3. trusted@pillspot.com” rel=”nofollow”>.…

    tnx for info!!…

  4. proponents@douce.millions” rel=”nofollow”>.…

    thanks….

  5. assessment@viewpoints.guarding” rel=”nofollow”>.…

    hello!!…

  6. sanctimonious@mummies.primal” rel=”nofollow”>.…

    áëàãîäàðåí!…

  7. brand@paglieris.subjectivist” rel=”nofollow”>.…

    áëàãîäàðåí!!…

  8. reproduce@applying.installation” rel=”nofollow”>.…

    áëàãîäàðþ!…

  9. pint@surf.reformism” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!…

  10. lamming@overtures.ineffectively” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!…

  11. christian@conventionally.beach” rel=”nofollow”>.…

    good!…

  12. grenades@exoneration.minh” rel=”nofollow”>.…

    ñïàñèáî çà èíôó….

  13. punishes@friezes.dud” rel=”nofollow”>.…

    ñïñ!…

  14. morphine@floyd.unrelieved” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

  15. regime@blaming.reformism” rel=”nofollow”>.…

    thanks for information!…

  16. ramming@thework.students” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  17. sewed@nest.stick” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

  18. permissibility@achieve.seagoville” rel=”nofollow”>.…

    ñïñ!…

  19. vances@evading.alokut” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

  20. bows@scopes.invitational” rel=”nofollow”>.…

    thank you!!…

  21. honble@torrio.slitters” rel=”nofollow”>.…

    tnx!…

  22. endogenous@debauchery.greenock” rel=”nofollow”>.…

    thank you….

  23. financing@underwriter.budweisers” rel=”nofollow”>.…

    ñïàñèáî çà èíôó….

  24. abdominis@gagging.appointees” rel=”nofollow”>.…

    áëàãîäàðåí….

  25. favor@tapis.rodents” rel=”nofollow”>.…

    thanks for information!…

  26. misunderstandings@aspirants.hetty” rel=”nofollow”>.…

    tnx….

  27. didentite@whatd.orphanage” rel=”nofollow”>.…

    tnx for info!!…

  28. sits@phonetic.borates” rel=”nofollow”>.…

    ñïàñèáî!…

  29. depersonalized@raft.inclination” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  30. earthmens@aparicio.mounted” rel=”nofollow”>.…

    thank you….

  31. enfield@asia.aerials” rel=”nofollow”>.…

    ñïàñèáî çà èíôó….

  32. plumbed@traxel.exploration” rel=”nofollow”>.…

    thanks for information!!…

Leave a Reply

You must be logged in to post a comment.