[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
This commit is contained in:
parent
d0e614aeed
commit
654f03f72a
|
@ -83,6 +83,9 @@ class Cursor(object):
|
||||||
self.autocommit(False)
|
self.autocommit(False)
|
||||||
self.dbname = pool.dbname
|
self.dbname = pool.dbname
|
||||||
|
|
||||||
|
from inspect import stack
|
||||||
|
self.__caller = tuple(stack()[2][1:3])
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if hasattr(self, '_obj'):
|
if hasattr(self, '_obj'):
|
||||||
# Oops. 'self' has not been closed explicitly.
|
# 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
|
# but the database connection is not put back into the connection
|
||||||
# pool, preventing some operation on the database like dropping it.
|
# pool, preventing some operation on the database like dropping it.
|
||||||
# This can also lead to a server overload.
|
# 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()
|
self.close()
|
||||||
|
|
||||||
@check
|
@check
|
||||||
def execute(self, query, params=None):
|
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:
|
if '%d' in query or '%f' in query:
|
||||||
#import traceback
|
|
||||||
#traceback.print_stack()
|
|
||||||
log(query, netsvc.LOG_WARNING)
|
log(query, netsvc.LOG_WARNING)
|
||||||
log("SQL queries mustn't containt %d or %f anymore. Use only %s", 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')
|
query = query.replace('%d', '%s').replace('%f', '%s')
|
||||||
|
|
||||||
if self.sql_log:
|
if self.sql_log:
|
||||||
now = mdt.now()
|
now = mdt.now()
|
||||||
|
|
||||||
res = self._obj.execute(query, p or None)
|
res = self._obj.execute(query, params)
|
||||||
|
|
||||||
if self.sql_log:
|
if self.sql_log:
|
||||||
log("query: %s" % self._obj.query)
|
log("query: %s" % self._obj.query)
|
||||||
|
@ -158,6 +150,7 @@ class Cursor(object):
|
||||||
process('from')
|
process('from')
|
||||||
process('into')
|
process('into')
|
||||||
self.count = 0
|
self.count = 0
|
||||||
|
self.sql_log = False
|
||||||
|
|
||||||
@check
|
@check
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
Loading…
Reference in New Issue