Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#411 closed defect (fixed)

CursorDebugWrapper does not support pyformat paramstyle.

Reported by: imaurer@… Owned by: adrian
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: UI/UX:

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 Changed 10 years ago by anonymous

  • Component changed from Admin interface to Database wrapper

comment:2 Changed 10 years ago by imaurer@…

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 Changed 9 years ago by lalo.martins@…

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 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

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