Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#411 closed defect (fixed)

CursorDebugWrapper does not support pyformat paramstyle.

Reported by: imaurer@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: trivial Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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

Change History (4)

comment:1 by anonymous, 19 years ago

Component: Admin interfaceDatabase wrapper

comment:2 by imaurer@…, 19 years ago

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

comment:3 by lalo.martins@…, 19 years ago

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),
            })

comment:4 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top