Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Modification of storing "sqlalchemy_queries" (debug) from deprecated Con... #79

Open
wants to merge 1 commit into from

2 participants

@djanku

...nectionProxy interface to recomended event listening "before_cursor_execute" and "after_cursos_execute".

Corrected dot "flask.ext.sqlalchemy" in source file header.

@djanku djanku Modification of storing "sqlalchemy_queries" (debug) from deprecated …
…ConnectionProxy interface to recomended event listening "before_cursor_execute" and "after_cursos_execute".


Corrected dot "flask.ext.sqlalchemy" in source file header.
1c8c3a1
@Koblaid

Typo: sqlalcehmy_queries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 28, 2012
  1. @djanku

    Modification of storing "sqlalchemy_queries" (debug) from deprecated …

    djanku authored
    …ConnectionProxy interface to recomended event listening "before_cursor_execute" and "after_cursos_execute".
    
    
    Corrected dot "flask.ext.sqlalchemy" in source file header.
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 27 deletions.
  1. +26 −27 flask_sqlalchemy.py
View
53 flask_sqlalchemy.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
- flaskext.sqlalchemy
+ flask.ext.sqlalchemy
~~~~~~~~~~~~~~~~~~~
Adds basic SQLAlchemy support to your application.
@@ -26,7 +26,6 @@
from sqlalchemy.orm.session import Session
from sqlalchemy.orm.interfaces import MapperExtension, SessionExtension, \
EXT_CONTINUE
-from sqlalchemy.interfaces import ConnectionProxy
from sqlalchemy.engine.url import make_url
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
from sqlalchemy.util import to_list
@@ -131,29 +130,6 @@ def _calling_context(app_path):
return '<unknown>'
-class _ConnectionDebugProxy(ConnectionProxy):
- """Helps debugging the database."""
-
- def __init__(self, import_name):
- self.app_package = import_name
-
- def cursor_execute(self, execute, cursor, statement, parameters,
- context, executemany):
- start = _timer()
- try:
- return execute(cursor, statement, parameters, context)
- finally:
- ctx = connection_stack.top
- if ctx is not None:
- queries = getattr(ctx, 'sqlalchemy_queries', None)
- if queries is None:
- queries = []
- setattr(ctx, 'sqlalchemy_queries', queries)
- queries.append(_DebugQueryTuple((
- statement, parameters, start, _timer(),
- _calling_context(self.app_package))))
-
-
class _SignalTrackingMapperExtension(MapperExtension):
def after_delete(self, mapper, connection, instance):
@@ -426,6 +402,26 @@ def get_uri(self):
'configuration variable' % self._bind
return binds[self._bind]
+ def _before_cursor_execute(self, connection, cursor, statement,
+ parameters, context, executemany):
+ ctx = connection_stack.top
+ if ctx is not None:
+ setattr(ctx, 'sqlalchemy_query_start', _timer())
+
+ def _after_cursor_execute(self, connection, cursor, statement,
+ parameters, context, executemany):
+ ctx = connection_stack.top
+ if ctx is not None:
+ end = _timer()
+ start = getattr(ctx, 'sqlalchemy_query_start', _timer())
+ queries = getattr(ctx, 'sqlalcehmy_queries', None)
+ if queries is None:
+ queries = []
+ setattr(ctx, 'sqlalchemy_queries', queries)
+ queries.append(_DebugQueryTuple((
+ statement, parameters, start, end,
+ _calling_context(self._app.import_name))))
+
def get_engine(self):
with self._lock:
uri = self.get_uri()
@@ -436,12 +432,15 @@ def get_engine(self):
options = {'convert_unicode': True}
self._sa.apply_pool_defaults(self._app, options)
self._sa.apply_driver_hacks(self._app, info, options)
- if _record_queries(self._app):
- options['proxy'] = _ConnectionDebugProxy(self._app.import_name)
if echo:
options['echo'] = True
self._engine = rv = sqlalchemy.create_engine(info, **options)
self._connected_for = (uri, echo)
+ if _record_queries(self._app):
+ sqlalchemy.event.listen(rv, 'before_cursor_execute',
+ self._before_cursor_execute)
+ sqlalchemy.event.listen(rv, 'after_cursor_execute',
+ self._after_cursor_execute)
return rv
Something went wrong with that request. Please try again.