Django

Code

Ticket #411 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

CursorDebugWrapper does not support pyformat paramstyle.

Reported by: imaurer@gmail.com Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

According to the Python Database API Specification v2.0, the paramters provided to the cursor.execute() method can be sequences or mappings.

http://www.python.org/peps/pep-0249.html

Right now, the CursorDebugWrapper? is converting all params to tuples before appending it to the queries list. This is causing my calls that use the pyformat paramstyle to fail during DEBUG mode. Below I removed the 'tuple()' function call which fixes my error. Of course, if there is a reason for this explicit conversion to tuple type that I do not see then a better work around would be needed.

class CursorDebugWrapper:
    def __init__(self, cursor, db):
        self.cursor = cursor
        self.db = db

    def execute(self, sql, params=[]):
        start = time()
        result = self.cursor.execute(sql, params)
        stop = time()
        self.db.queries.append({
            'sql': sql % params,
            'time': "%.3f" % (stop - start),
        })
        return result

Attachments

Change History

08/23/05 19:03:17 changed by anonymous

  • component changed from Admin interface to Database wrapper.

08/23/05 19:23:13 changed by imaurer@gmail.com

The tuple function is needed for lists of 1 item. Below is a very simple-mided check for DictType? which I think is acceptable.

class CursorDebugWrapper:
    def __init__(self, cursor, db):
        self.cursor = cursor
        self.db = db

    def execute(self, sql, params=[]):
        start = time()
        result = self.cursor.execute(sql, params)
        stop = time()
        
        if type(params) != dict:
            params = tuple(params)
        
        self.db.queries.append({
            'sql': sql % params,
            'time': "%.3f" % (stop - start),
        })
        return result

04/27/06 23:17:33 changed by lalo.martins@gmail.com

what I did was:

    def execute(self, sql, params=()):
        start = time()
        try:
            return self.cursor.execute(sql, params)
        finally:
            if not isinstance(params, (tuple, dict)):
                params = tuple(params)
            stop = time()
            self.db.queries.append({
                'sql': sql % params,
                'time': "%.3f" % (stop - start),
            })

05/31/06 22:57:08 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [3038]) Fixed #411 -- CursorDebugWrapper? now supports pyformat paramstyle


Add/Change #411 (CursorDebugWrapper does not support pyformat paramstyle.)




Change Properties
Action