summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/events.py
blob: a1313de632630c6ddd6b0a798f0ed1c9e5c74ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""Core event interfaces."""

from sqlalchemy import event, exc

class DDLEvents(event.Events):
    """
    Define create/drop event listers for schema objects.
    
    These events currently apply to :class:`.Table`
    and :class:`.MetaData` objects as targets.
    
    e.g.::
    
        from sqlalchemy import event
        from sqlalchemy import Table, Column, Metadata, Integer
        
        m = MetaData()
        some_table = Table('some_table', m, Column('data', Integer))
        
        def on_after_create(target, connection, **kw):
            connection.execute("ALTER TABLE %s SET name=foo_%s" % 
                                    (target.name, target.name))
                                    
        event.listen(on_after_create, "on_after_create", some_table)
    
    DDL events integrate closely with the 
    :class:`.DDL` class and the :class:`.DDLElement` hierarchy
    of DDL clause constructs, which are themselves appropriate 
    as listener callables::
    
        from sqlalchemy import DDL
        event.listen(
            DDL("ALTER TABLE %(table)s SET name=foo_%(table)s"),
            "on_after_create",
            some_table
        )
    
    The methods here define the name of an event as well
    as the names of members that are passed to listener
    functions.
    
    See also:

        :ref:`event_toplevel`
        
        :class:`.DDLElement`
        
        :class:`.DDL`
        
        :ref:`schema_ddl_sequences`
    
    """
    
    def on_before_create(self, target, connection, **kw):
        """Called before CREATE statments are emitted.
        
        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         CREATE statement or statements will be emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  Currently this includes the ``tables``
         argument in the case of a :class:`.MetaData` object,
         which is the list of :class:`.Table` objects for which
         CREATE will be emitted.
           
        """

    def on_after_create(self, target, connection, **kw):
        """Called after CREATE statments are emitted.
        
        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         CREATE statement or statements have been emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  Currently this includes the ``tables``
         argument in the case of a :class:`.MetaData` object,
         which is the list of :class:`.Table` objects for which
         CREATE has been emitted.
           
        """

    def on_before_drop(self, target, connection, **kw):
        """Called before DROP statments are emitted.
        
        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         DROP statement or statements will be emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  Currently this includes the ``tables``
         argument in the case of a :class:`.MetaData` object,
         which is the list of :class:`.Table` objects for which
         DROP will be emitted.
           
        """
    
    def on_after_drop(self, target, connection, **kw):
        """Called after DROP statments are emitted.
        
        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         DROP statement or statements have been emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  Currently this includes the ``tables``
         argument in the case of a :class:`.MetaData` object,
         which is the list of :class:`.Table` objects for which
         DROP has been emitted.
           
        """
    

class PoolEvents(event.Events):
    """Available events for :class:`.Pool`.
    
    The methods here define the name of an event as well
    as the names of members that are passed to listener
    functions.
    
    e.g.::
    
        from sqlalchemy import event
        
        def my_on_checkout(dbapi_conn, connection_rec, connection_proxy):
            "handle an on checkout event"
            
        events.listen(my_on_checkout, 'on_checkout', Pool)

    In addition to accepting the :class:`.Pool` class and :class:`.Pool` instances,
    :class:`.PoolEvents` also accepts :class:`.Engine` objects and
    the :class:`.Engine` class as targets, which will be resolved
    to the ``.pool`` attribute of the given engine or the :class:`.Pool`
    class::
        
        engine = create_engine("postgresql://scott:tiger@localhost/test")
        
        # will associate with engine.pool
        events.listen(my_on_checkout, 'on_checkout', engine)

    """
    
    @classmethod
    def accept_with(cls, target):
        from sqlalchemy.engine import Engine
        from sqlalchemy.pool import Pool
        
        if isinstance(target, type):
            if issubclass(target, Engine):
                return Pool
            elif issubclass(target, Pool):
                return target
        elif isinstance(target, Engine):
            return target.pool
        else:
            return target
    
    def on_connect(self, dbapi_connection, connection_record):
        """Called once for each new DB-API connection or Pool's ``creator()``.

        :param dbapi_con:
          A newly connected raw DB-API connection (not a SQLAlchemy
          ``Connection`` wrapper).

        :param con_record:
          The ``_ConnectionRecord`` that persistently manages the connection

        """

    def on_first_connect(self, dbapi_connection, connection_record):
        """Called exactly once for the first DB-API connection.

        :param dbapi_con:
          A newly connected raw DB-API connection (not a SQLAlchemy
          ``Connection`` wrapper).

        :param con_record:
          The ``_ConnectionRecord`` that persistently manages the connection

        """

    def on_checkout(self, dbapi_connection, connection_record, connection_proxy):
        """Called when a connection is retrieved from the Pool.

        :param dbapi_con:
          A raw DB-API connection

        :param con_record:
          The ``_ConnectionRecord`` that persistently manages the connection

        :param con_proxy:
          The ``_ConnectionFairy`` which manages the connection for the span of
          the current checkout.

        If you raise an ``exc.DisconnectionError``, the current
        connection will be disposed and a fresh connection retrieved.
        Processing of all checkout listeners will abort and restart
        using the new connection.
        """

    def on_checkin(self, dbapi_connection, connection_record):
        """Called when a connection returns to the pool.

        Note that the connection may be closed, and may be None if the
        connection has been invalidated.  ``checkin`` will not be called
        for detached connections.  (They do not return to the pool.)

        :param dbapi_con:
          A raw DB-API connection

        :param con_record:
          The ``_ConnectionRecord`` that persistently manages the connection

        """

class EngineEvents(event.Events):
    """Available events for :class:`.Engine`.
    
    The methods here define the name of an event as well as the names of members that are passed to listener functions.
    
    e.g.::
    
        from sqlalchemy import event, create_engine
        
        def on_before_execute(conn, clauseelement, multiparams, params):
            log.info("Received statement: %s" % clauseelement)
        
        engine = create_engine('postgresql://scott:tiger@localhost/test')
        event.listen(on_before_execute, "on_before_execute", engine)
    
    Some events allow modifiers to the listen() function.
    
    :param retval=False: Applies to the :meth:`.on_before_execute` and 
      :meth:`.on_before_cursor_execute` events only.  When True, the
      user-defined event function must have a return value, which
      is a tuple of parameters that replace the given statement 
      and parameters.  See those methods for a description of
      specific return arguments.
    
    """
    
    @classmethod
    def listen(cls, fn, identifier, target, retval=False):
        from sqlalchemy.engine.base import Connection, \
            _listener_connection_cls
        if target.Connection is Connection:
            target.Connection = _listener_connection_cls(
                                        Connection, 
                                        target.dispatch)
        
        if not retval:
            if identifier == 'on_before_execute':
                orig_fn = fn
                def wrap(conn, clauseelement, multiparams, params):
                    orig_fn(conn, clauseelement, multiparams, params)
                    return clauseelement, multiparams, params
                fn = wrap
            elif identifier == 'on_before_cursor_execute':
                orig_fn = fn
                def wrap(conn, cursor, statement, 
                        parameters, context, executemany):
                    orig_fn(conn, cursor, statement, 
                        parameters, context, executemany)
                    return statement, parameters
                fn = wrap
                    
        elif retval and identifier not in ('on_before_execute', 'on_before_cursor_execute'):
            raise exc.ArgumentError(
                    "Only the 'on_before_execute' and "
                    "'on_before_cursor_execute' engine "
                    "event listeners accept the 'retval=True' "
                    "argument.")
        event.Events.listen(fn, identifier, target)

    def on_before_execute(self, conn, clauseelement, multiparams, params):
        """Intercept high level execute() events."""

    def on_after_execute(self, conn, clauseelement, multiparams, params, result):
        """Intercept high level execute() events."""
        
    def on_before_cursor_execute(self, conn, cursor, statement, 
                        parameters, context, executemany):
        """Intercept low-level cursor execute() events."""

    def on_after_cursor_execute(self, conn, cursor, statement, 
                        parameters, context, executemany):
        """Intercept low-level cursor execute() events."""

    def on_begin(self, conn):
        """Intercept begin() events."""
        
    def on_rollback(self, conn):
        """Intercept rollback() events."""
        
    def on_commit(self, conn):
        """Intercept commit() events."""
        
    def on_savepoint(self, conn, name=None):
        """Intercept savepoint() events."""
        
    def on_rollback_savepoint(self, conn, name, context):
        """Intercept rollback_savepoint() events."""
        
    def on_release_savepoint(self, conn, name, context):
        """Intercept release_savepoint() events."""
        
    def on_begin_twophase(self, conn, xid):
        """Intercept begin_twophase() events."""
        
    def on_prepare_twophase(self, conn, xid):
        """Intercept prepare_twophase() events."""
        
    def on_rollback_twophase(self, conn, xid, is_prepared):
        """Intercept rollback_twophase() events."""
        
    def on_commit_twophase(self, conn, xid, is_prepared):
        """Intercept commit_twophase() events."""