diff options
| author | Christophe Simonis <christophe@cobalt> | 2008-12-19 20:21:49 +0100 |
|---|---|---|
| committer | Christophe Simonis <christophe@cobalt> | 2008-12-19 20:21:49 +0100 |
| commit | 654f03f72a2537904e89683f89203916f91b860c (patch) | |
| tree | 98ca4b982124b0ed7902d3e520e517227b797a96 /bin/sql_db.py | |
| parent | d0e614aeedc36cab7f67f7f089ab4cb8e574312f (diff) | |
[IMP] Track where a cursor is created when this cursor is not closed explicitly
[IMP] do not reformat the parameters, allowing to pass dict as parameters
bzr revid: christophe@cobalt-20081219192149-pzg3xaygvbhc111x
Diffstat (limited to 'bin/sql_db.py')
| -rw-r--r-- | bin/sql_db.py | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/bin/sql_db.py b/bin/sql_db.py index 1d5bfce89e7..4003ec64875 100644 --- a/bin/sql_db.py +++ b/bin/sql_db.py @@ -82,7 +82,10 @@ class Cursor(object): self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor) self.autocommit(False) self.dbname = pool.dbname - + + from inspect import stack + self.__caller = tuple(stack()[2][1:3]) + def __del__(self): if hasattr(self, '_obj'): # Oops. 'self' has not been closed explicitly. @@ -90,35 +93,24 @@ class Cursor(object): # but the database connection is not put back into the connection # pool, preventing some operation on the database like dropping it. # This can also lead to a server overload. - log('Cursor not closed explicitly', netsvc.LOG_WARNING) + msg = "Cursor not closed explicitly\n" \ + "Cursor was created at %s:%s" % self.__caller + + log(msg, netsvc.LOG_WARNING) self.close() @check def execute(self, query, params=None): - if params is None: - params=() - if not isinstance(params, (tuple, list)): - params = (params,) - - def base_string(s): - if isinstance(s, unicode): - return s.encode('utf-8') - return s - p=map(base_string, params) - query = base_string(query) - if '%d' in query or '%f' in query: - #import traceback - #traceback.print_stack() log(query, netsvc.LOG_WARNING) log("SQL queries mustn't containt %d or %f anymore. Use only %s", netsvc.LOG_WARNING) - if p: + if params: query = query.replace('%d', '%s').replace('%f', '%s') if self.sql_log: now = mdt.now() - res = self._obj.execute(query, p or None) + res = self._obj.execute(query, params) if self.sql_log: log("query: %s" % self._obj.query) @@ -158,6 +150,7 @@ class Cursor(object): process('from') process('into') self.count = 0 + self.sql_log = False @check def close(self): |
