~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/engine/interfaces.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# engine/interfaces.py
 
2
# Copyright (C) 2005-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
 
3
#
 
4
# This module is part of SQLAlchemy and is released under
 
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
 
6
 
 
7
"""Define core interfaces used by the engine system."""
 
8
 
 
9
from .. import util, event, events
 
10
 
 
11
 
 
12
class Dialect(object):
 
13
    """Define the behavior of a specific database and DB-API combination.
 
14
 
 
15
    Any aspect of metadata definition, SQL query generation,
 
16
    execution, result-set handling, or anything else which varies
 
17
    between databases is defined under the general category of the
 
18
    Dialect.  The Dialect acts as a factory for other
 
19
    database-specific object implementations including
 
20
    ExecutionContext, Compiled, DefaultGenerator, and TypeEngine.
 
21
 
 
22
    All Dialects implement the following attributes:
 
23
 
 
24
    name
 
25
      identifying name for the dialect from a DBAPI-neutral point of view
 
26
      (i.e. 'sqlite')
 
27
 
 
28
    driver
 
29
      identifying name for the dialect's DBAPI
 
30
 
 
31
    positional
 
32
      True if the paramstyle for this Dialect is positional.
 
33
 
 
34
    paramstyle
 
35
      the paramstyle to be used (some DB-APIs support multiple
 
36
      paramstyles).
 
37
 
 
38
    convert_unicode
 
39
      True if Unicode conversion should be applied to all ``str``
 
40
      types.
 
41
 
 
42
    encoding
 
43
      type of encoding to use for unicode, usually defaults to
 
44
      'utf-8'.
 
45
 
 
46
    statement_compiler
 
47
      a :class:`.Compiled` class used to compile SQL statements
 
48
 
 
49
    ddl_compiler
 
50
      a :class:`.Compiled` class used to compile DDL statements
 
51
 
 
52
    server_version_info
 
53
      a tuple containing a version number for the DB backend in use.
 
54
      This value is only available for supporting dialects, and is
 
55
      typically populated during the initial connection to the database.
 
56
 
 
57
    default_schema_name
 
58
     the name of the default schema.  This value is only available for
 
59
     supporting dialects, and is typically populated during the
 
60
     initial connection to the database.
 
61
 
 
62
    execution_ctx_cls
 
63
      a :class:`.ExecutionContext` class used to handle statement execution
 
64
 
 
65
    execute_sequence_format
 
66
      either the 'tuple' or 'list' type, depending on what cursor.execute()
 
67
      accepts for the second argument (they vary).
 
68
 
 
69
    preparer
 
70
      a :class:`~sqlalchemy.sql.compiler.IdentifierPreparer` class used to
 
71
      quote identifiers.
 
72
 
 
73
    supports_alter
 
74
      ``True`` if the database supports ``ALTER TABLE``.
 
75
 
 
76
    max_identifier_length
 
77
      The maximum length of identifier names.
 
78
 
 
79
    supports_unicode_statements
 
80
      Indicate whether the DB-API can receive SQL statements as Python
 
81
      unicode strings
 
82
 
 
83
    supports_unicode_binds
 
84
      Indicate whether the DB-API can receive string bind parameters
 
85
      as Python unicode strings
 
86
 
 
87
    supports_sane_rowcount
 
88
      Indicate whether the dialect properly implements rowcount for
 
89
      ``UPDATE`` and ``DELETE`` statements.
 
90
 
 
91
    supports_sane_multi_rowcount
 
92
      Indicate whether the dialect properly implements rowcount for
 
93
      ``UPDATE`` and ``DELETE`` statements when executed via
 
94
      executemany.
 
95
 
 
96
    preexecute_autoincrement_sequences
 
97
      True if 'implicit' primary key functions must be executed separately
 
98
      in order to get their value.   This is currently oriented towards
 
99
      Postgresql.
 
100
 
 
101
    implicit_returning
 
102
      use RETURNING or equivalent during INSERT execution in order to load
 
103
      newly generated primary keys and other column defaults in one execution,
 
104
      which are then available via inserted_primary_key.
 
105
      If an insert statement has returning() specified explicitly,
 
106
      the "implicit" functionality is not used and inserted_primary_key
 
107
      will not be available.
 
108
 
 
109
    dbapi_type_map
 
110
      A mapping of DB-API type objects present in this Dialect's
 
111
      DB-API implementation mapped to TypeEngine implementations used
 
112
      by the dialect.
 
113
 
 
114
      This is used to apply types to result sets based on the DB-API
 
115
      types present in cursor.description; it only takes effect for
 
116
      result sets against textual statements where no explicit
 
117
      typemap was present.
 
118
 
 
119
    colspecs
 
120
      A dictionary of TypeEngine classes from sqlalchemy.types mapped
 
121
      to subclasses that are specific to the dialect class.  This
 
122
      dictionary is class-level only and is not accessed from the
 
123
      dialect instance itself.
 
124
 
 
125
    supports_default_values
 
126
      Indicates if the construct ``INSERT INTO tablename DEFAULT
 
127
      VALUES`` is supported
 
128
 
 
129
    supports_sequences
 
130
      Indicates if the dialect supports CREATE SEQUENCE or similar.
 
131
 
 
132
    sequences_optional
 
133
      If True, indicates if the "optional" flag on the Sequence() construct
 
134
      should signal to not generate a CREATE SEQUENCE. Applies only to
 
135
      dialects that support sequences. Currently used only to allow Postgresql
 
136
      SERIAL to be used on a column that specifies Sequence() for usage on
 
137
      other backends.
 
138
 
 
139
    supports_native_enum
 
140
      Indicates if the dialect supports a native ENUM construct.
 
141
      This will prevent types.Enum from generating a CHECK
 
142
      constraint when that type is used.
 
143
 
 
144
    supports_native_boolean
 
145
      Indicates if the dialect supports a native boolean construct.
 
146
      This will prevent types.Boolean from generating a CHECK
 
147
      constraint when that type is used.
 
148
 
 
149
    """
 
150
 
 
151
    def create_connect_args(self, url):
 
152
        """Build DB-API compatible connection arguments.
 
153
 
 
154
        Given a :class:`~sqlalchemy.engine.url.URL` object, returns a tuple
 
155
        consisting of a `*args`/`**kwargs` suitable to send directly
 
156
        to the dbapi's connect function.
 
157
 
 
158
        """
 
159
 
 
160
        raise NotImplementedError()
 
161
 
 
162
    @classmethod
 
163
    def type_descriptor(cls, typeobj):
 
164
        """Transform a generic type to a dialect-specific type.
 
165
 
 
166
        Dialect classes will usually use the
 
167
        :func:`.types.adapt_type` function in the types module to
 
168
        accomplish this.
 
169
 
 
170
        The returned result is cached *per dialect class* so can
 
171
        contain no dialect-instance state.
 
172
 
 
173
        """
 
174
 
 
175
        raise NotImplementedError()
 
176
 
 
177
    def initialize(self, connection):
 
178
        """Called during strategized creation of the dialect with a
 
179
        connection.
 
180
 
 
181
        Allows dialects to configure options based on server version info or
 
182
        other properties.
 
183
 
 
184
        The connection passed here is a SQLAlchemy Connection object,
 
185
        with full capabilities.
 
186
 
 
187
        The initalize() method of the base dialect should be called via
 
188
        super().
 
189
 
 
190
        """
 
191
 
 
192
        pass
 
193
 
 
194
    def reflecttable(self, connection, table, include_columns=None):
 
195
        """Load table description from the database.
 
196
 
 
197
        Given a :class:`.Connection` and a
 
198
        :class:`~sqlalchemy.schema.Table` object, reflect its columns and
 
199
        properties from the database.  If include_columns (a list or
 
200
        set) is specified, limit the autoload to the given column
 
201
        names.
 
202
 
 
203
        The default implementation uses the
 
204
        :class:`~sqlalchemy.engine.reflection.Inspector` interface to
 
205
        provide the output, building upon the granular table/column/
 
206
        constraint etc. methods of :class:`.Dialect`.
 
207
 
 
208
        """
 
209
 
 
210
        raise NotImplementedError()
 
211
 
 
212
    def get_columns(self, connection, table_name, schema=None, **kw):
 
213
        """Return information about columns in `table_name`.
 
214
 
 
215
        Given a :class:`.Connection`, a string
 
216
        `table_name`, and an optional string `schema`, return column
 
217
        information as a list of dictionaries with these keys:
 
218
 
 
219
        name
 
220
          the column's name
 
221
 
 
222
        type
 
223
          [sqlalchemy.types#TypeEngine]
 
224
 
 
225
        nullable
 
226
          boolean
 
227
 
 
228
        default
 
229
          the column's default value
 
230
 
 
231
        autoincrement
 
232
          boolean
 
233
 
 
234
        sequence
 
235
          a dictionary of the form
 
236
              {'name' : str, 'start' :int, 'increment': int}
 
237
 
 
238
        Additional column attributes may be present.
 
239
        """
 
240
 
 
241
        raise NotImplementedError()
 
242
 
 
243
    def get_primary_keys(self, connection, table_name, schema=None, **kw):
 
244
        """Return information about primary keys in `table_name`.
 
245
 
 
246
 
 
247
        Deprecated.  This method is only called by the default
 
248
        implementation of :meth:`.Dialect.get_pk_constraint`.  Dialects should
 
249
        instead implement this method directly.
 
250
 
 
251
        """
 
252
 
 
253
        raise NotImplementedError()
 
254
 
 
255
    def get_pk_constraint(self, connection, table_name, schema=None, **kw):
 
256
        """Return information about the primary key constraint on
 
257
        table_name`.
 
258
 
 
259
        Given a :class:`.Connection`, a string
 
260
        `table_name`, and an optional string `schema`, return primary
 
261
        key information as a dictionary with these keys:
 
262
 
 
263
        constrained_columns
 
264
          a list of column names that make up the primary key
 
265
 
 
266
        name
 
267
          optional name of the primary key constraint.
 
268
 
 
269
        """
 
270
        raise NotImplementedError()
 
271
 
 
272
    def get_foreign_keys(self, connection, table_name, schema=None, **kw):
 
273
        """Return information about foreign_keys in `table_name`.
 
274
 
 
275
        Given a :class:`.Connection`, a string
 
276
        `table_name`, and an optional string `schema`, return foreign
 
277
        key information as a list of dicts with these keys:
 
278
 
 
279
        name
 
280
          the constraint's name
 
281
 
 
282
        constrained_columns
 
283
          a list of column names that make up the foreign key
 
284
 
 
285
        referred_schema
 
286
          the name of the referred schema
 
287
 
 
288
        referred_table
 
289
          the name of the referred table
 
290
 
 
291
        referred_columns
 
292
          a list of column names in the referred table that correspond to
 
293
          constrained_columns
 
294
        """
 
295
 
 
296
        raise NotImplementedError()
 
297
 
 
298
    def get_table_names(self, connection, schema=None, **kw):
 
299
        """Return a list of table names for `schema`."""
 
300
 
 
301
        raise NotImplementedError
 
302
 
 
303
    def get_view_names(self, connection, schema=None, **kw):
 
304
        """Return a list of all view names available in the database.
 
305
 
 
306
        schema:
 
307
          Optional, retrieve names from a non-default schema.
 
308
        """
 
309
 
 
310
        raise NotImplementedError()
 
311
 
 
312
    def get_view_definition(self, connection, view_name, schema=None, **kw):
 
313
        """Return view definition.
 
314
 
 
315
        Given a :class:`.Connection`, a string
 
316
        `view_name`, and an optional string `schema`, return the view
 
317
        definition.
 
318
        """
 
319
 
 
320
        raise NotImplementedError()
 
321
 
 
322
    def get_indexes(self, connection, table_name, schema=None, **kw):
 
323
        """Return information about indexes in `table_name`.
 
324
 
 
325
        Given a :class:`.Connection`, a string
 
326
        `table_name` and an optional string `schema`, return index
 
327
        information as a list of dictionaries with these keys:
 
328
 
 
329
        name
 
330
          the index's name
 
331
 
 
332
        column_names
 
333
          list of column names in order
 
334
 
 
335
        unique
 
336
          boolean
 
337
        """
 
338
 
 
339
        raise NotImplementedError()
 
340
 
 
341
    def normalize_name(self, name):
 
342
        """convert the given name to lowercase if it is detected as
 
343
        case insensitive.
 
344
 
 
345
        this method is only used if the dialect defines
 
346
        requires_name_normalize=True.
 
347
 
 
348
        """
 
349
        raise NotImplementedError()
 
350
 
 
351
    def denormalize_name(self, name):
 
352
        """convert the given name to a case insensitive identifier
 
353
        for the backend if it is an all-lowercase name.
 
354
 
 
355
        this method is only used if the dialect defines
 
356
        requires_name_normalize=True.
 
357
 
 
358
        """
 
359
        raise NotImplementedError()
 
360
 
 
361
    def has_table(self, connection, table_name, schema=None):
 
362
        """Check the existence of a particular table in the database.
 
363
 
 
364
        Given a :class:`.Connection` object and a string
 
365
        `table_name`, return True if the given table (possibly within
 
366
        the specified `schema`) exists in the database, False
 
367
        otherwise.
 
368
        """
 
369
 
 
370
        raise NotImplementedError()
 
371
 
 
372
    def has_sequence(self, connection, sequence_name, schema=None):
 
373
        """Check the existence of a particular sequence in the database.
 
374
 
 
375
        Given a :class:`.Connection` object and a string
 
376
        `sequence_name`, return True if the given sequence exists in
 
377
        the database, False otherwise.
 
378
        """
 
379
 
 
380
        raise NotImplementedError()
 
381
 
 
382
    def _get_server_version_info(self, connection):
 
383
        """Retrieve the server version info from the given connection.
 
384
 
 
385
        This is used by the default implementation to populate the
 
386
        "server_version_info" attribute and is called exactly
 
387
        once upon first connect.
 
388
 
 
389
        """
 
390
 
 
391
        raise NotImplementedError()
 
392
 
 
393
    def _get_default_schema_name(self, connection):
 
394
        """Return the string name of the currently selected schema from
 
395
        the given connection.
 
396
 
 
397
        This is used by the default implementation to populate the
 
398
        "default_schema_name" attribute and is called exactly
 
399
        once upon first connect.
 
400
 
 
401
        """
 
402
 
 
403
        raise NotImplementedError()
 
404
 
 
405
    def do_begin(self, dbapi_connection):
 
406
        """Provide an implementation of ``connection.begin()``, given a
 
407
        DB-API connection.
 
408
 
 
409
        The DBAPI has no dedicated "begin" method and it is expected
 
410
        that transactions are implicit.  This hook is provided for those
 
411
        DBAPIs that might need additional help in this area.
 
412
 
 
413
        Note that :meth:`.Dialect.do_begin` is not called unless a
 
414
        :class:`.Transaction` object is in use.  The
 
415
        :meth:`.Dialect.do_autocommit`
 
416
        hook is provided for DBAPIs that need some extra commands emitted
 
417
        after a commit in order to enter the next transaction, when the
 
418
        SQLAlchemy :class:`.Connection` is used in it's default "autocommit"
 
419
        mode.
 
420
 
 
421
        :param dbapi_connection: a DBAPI connection, typically
 
422
         proxied within a :class:`.ConnectionFairy`.
 
423
 
 
424
         """
 
425
 
 
426
        raise NotImplementedError()
 
427
 
 
428
    def do_rollback(self, dbapi_connection):
 
429
        """Provide an implementation of ``connection.rollback()``, given
 
430
        a DB-API connection.
 
431
 
 
432
        :param dbapi_connection: a DBAPI connection, typically
 
433
         proxied within a :class:`.ConnectionFairy`.
 
434
 
 
435
         """
 
436
 
 
437
        raise NotImplementedError()
 
438
 
 
439
 
 
440
    def do_commit(self, dbapi_connection):
 
441
        """Provide an implementation of ``connection.commit()``, given a
 
442
        DB-API connection.
 
443
 
 
444
        :param dbapi_connection: a DBAPI connection, typically
 
445
         proxied within a :class:`.ConnectionFairy`.
 
446
 
 
447
        """
 
448
 
 
449
        raise NotImplementedError()
 
450
 
 
451
    def do_close(self, dbapi_connection):
 
452
        """Provide an implementation of ``connection.close()``, given a DBAPI
 
453
        connection.
 
454
 
 
455
        This hook is called by the :class:`.Pool` when a connection has been
 
456
        detached from the pool, or is being returned beyond the normal
 
457
        capacity of the pool.
 
458
 
 
459
        .. versionadded:: 0.8
 
460
 
 
461
        """
 
462
 
 
463
        raise NotImplementedError()
 
464
 
 
465
    def create_xid(self):
 
466
        """Create a two-phase transaction ID.
 
467
 
 
468
        This id will be passed to do_begin_twophase(),
 
469
        do_rollback_twophase(), do_commit_twophase().  Its format is
 
470
        unspecified.
 
471
        """
 
472
 
 
473
        raise NotImplementedError()
 
474
 
 
475
    def do_savepoint(self, connection, name):
 
476
        """Create a savepoint with the given name.
 
477
 
 
478
        :param connection: a :class:`.Connection`.
 
479
        :param name: savepoint name.
 
480
 
 
481
        """
 
482
 
 
483
        raise NotImplementedError()
 
484
 
 
485
    def do_rollback_to_savepoint(self, connection, name):
 
486
        """Rollback a connection to the named savepoint.
 
487
 
 
488
        :param connection: a :class:`.Connection`.
 
489
        :param name: savepoint name.
 
490
 
 
491
        """
 
492
 
 
493
        raise NotImplementedError()
 
494
 
 
495
    def do_release_savepoint(self, connection, name):
 
496
        """Release the named savepoint on a connection.
 
497
 
 
498
        :param connection: a :class:`.Connection`.
 
499
        :param name: savepoint name.
 
500
        """
 
501
 
 
502
        raise NotImplementedError()
 
503
 
 
504
    def do_begin_twophase(self, connection, xid):
 
505
        """Begin a two phase transaction on the given connection.
 
506
 
 
507
        :param connection: a :class:`.Connection`.
 
508
        :param xid: xid
 
509
 
 
510
        """
 
511
 
 
512
        raise NotImplementedError()
 
513
 
 
514
    def do_prepare_twophase(self, connection, xid):
 
515
        """Prepare a two phase transaction on the given connection.
 
516
 
 
517
        :param connection: a :class:`.Connection`.
 
518
        :param xid: xid
 
519
 
 
520
        """
 
521
 
 
522
        raise NotImplementedError()
 
523
 
 
524
    def do_rollback_twophase(self, connection, xid, is_prepared=True,
 
525
                            recover=False):
 
526
        """Rollback a two phase transaction on the given connection.
 
527
 
 
528
        :param connection: a :class:`.Connection`.
 
529
        :param xid: xid
 
530
        :param is_prepared: whether or not
 
531
         :meth:`.TwoPhaseTransaction.prepare` was called.
 
532
        :param recover: if the recover flag was passed.
 
533
 
 
534
        """
 
535
 
 
536
        raise NotImplementedError()
 
537
 
 
538
    def do_commit_twophase(self, connection, xid, is_prepared=True,
 
539
                            recover=False):
 
540
        """Commit a two phase transaction on the given connection.
 
541
 
 
542
 
 
543
        :param connection: a :class:`.Connection`.
 
544
        :param xid: xid
 
545
        :param is_prepared: whether or not
 
546
         :meth:`.TwoPhaseTransaction.prepare` was called.
 
547
        :param recover: if the recover flag was passed.
 
548
 
 
549
        """
 
550
 
 
551
        raise NotImplementedError()
 
552
 
 
553
    def do_recover_twophase(self, connection):
 
554
        """Recover list of uncommited prepared two phase transaction
 
555
        identifiers on the given connection.
 
556
 
 
557
        :param connection: a :class:`.Connection`.
 
558
 
 
559
        """
 
560
 
 
561
        raise NotImplementedError()
 
562
 
 
563
    def do_executemany(self, cursor, statement, parameters, context=None):
 
564
        """Provide an implementation of ``cursor.executemany(statement,
 
565
        parameters)``."""
 
566
 
 
567
        raise NotImplementedError()
 
568
 
 
569
    def do_execute(self, cursor, statement, parameters, context=None):
 
570
        """Provide an implementation of ``cursor.execute(statement,
 
571
        parameters)``."""
 
572
 
 
573
        raise NotImplementedError()
 
574
 
 
575
    def do_execute_no_params(self, cursor, statement, parameters,
 
576
                             context=None):
 
577
        """Provide an implementation of ``cursor.execute(statement)``.
 
578
 
 
579
        The parameter collection should not be sent.
 
580
 
 
581
        """
 
582
 
 
583
        raise NotImplementedError()
 
584
 
 
585
    def is_disconnect(self, e, connection, cursor):
 
586
        """Return True if the given DB-API error indicates an invalid
 
587
        connection"""
 
588
 
 
589
        raise NotImplementedError()
 
590
 
 
591
    def connect(self):
 
592
        """return a callable which sets up a newly created DBAPI connection.
 
593
 
 
594
        The callable accepts a single argument "conn" which is the
 
595
        DBAPI connection itself.  It has no return value.
 
596
 
 
597
        This is used to set dialect-wide per-connection options such as
 
598
        isolation modes, unicode modes, etc.
 
599
 
 
600
        If a callable is returned, it will be assembled into a pool listener
 
601
        that receives the direct DBAPI connection, with all wrappers removed.
 
602
 
 
603
        If None is returned, no listener will be generated.
 
604
 
 
605
        """
 
606
        return None
 
607
 
 
608
    def reset_isolation_level(self, dbapi_conn):
 
609
        """Given a DBAPI connection, revert its isolation to the default."""
 
610
 
 
611
        raise NotImplementedError()
 
612
 
 
613
    def set_isolation_level(self, dbapi_conn, level):
 
614
        """Given a DBAPI connection, set its isolation level."""
 
615
 
 
616
        raise NotImplementedError()
 
617
 
 
618
    def get_isolation_level(self, dbapi_conn):
 
619
        """Given a DBAPI connection, return its isolation level."""
 
620
 
 
621
        raise NotImplementedError()
 
622
 
 
623
 
 
624
class ExecutionContext(object):
 
625
    """A messenger object for a Dialect that corresponds to a single
 
626
    execution.
 
627
 
 
628
    ExecutionContext should have these data members:
 
629
 
 
630
    connection
 
631
      Connection object which can be freely used by default value
 
632
      generators to execute SQL.  This Connection should reference the
 
633
      same underlying connection/transactional resources of
 
634
      root_connection.
 
635
 
 
636
    root_connection
 
637
      Connection object which is the source of this ExecutionContext.  This
 
638
      Connection may have close_with_result=True set, in which case it can
 
639
      only be used once.
 
640
 
 
641
    dialect
 
642
      dialect which created this ExecutionContext.
 
643
 
 
644
    cursor
 
645
      DB-API cursor procured from the connection,
 
646
 
 
647
    compiled
 
648
      if passed to constructor, sqlalchemy.engine.base.Compiled object
 
649
      being executed,
 
650
 
 
651
    statement
 
652
      string version of the statement to be executed.  Is either
 
653
      passed to the constructor, or must be created from the
 
654
      sql.Compiled object by the time pre_exec() has completed.
 
655
 
 
656
    parameters
 
657
      bind parameters passed to the execute() method.  For compiled
 
658
      statements, this is a dictionary or list of dictionaries.  For
 
659
      textual statements, it should be in a format suitable for the
 
660
      dialect's paramstyle (i.e. dict or list of dicts for non
 
661
      positional, list or list of lists/tuples for positional).
 
662
 
 
663
    isinsert
 
664
      True if the statement is an INSERT.
 
665
 
 
666
    isupdate
 
667
      True if the statement is an UPDATE.
 
668
 
 
669
    should_autocommit
 
670
      True if the statement is a "committable" statement.
 
671
 
 
672
    prefetch_cols
 
673
      a list of Column objects for which a client-side default
 
674
      was fired off.  Applies to inserts and updates.
 
675
 
 
676
    postfetch_cols
 
677
      a list of Column objects for which a server-side default or
 
678
      inline SQL expression value was fired off.  Applies to inserts
 
679
      and updates.
 
680
    """
 
681
 
 
682
    def create_cursor(self):
 
683
        """Return a new cursor generated from this ExecutionContext's
 
684
        connection.
 
685
 
 
686
        Some dialects may wish to change the behavior of
 
687
        connection.cursor(), such as postgresql which may return a PG
 
688
        "server side" cursor.
 
689
        """
 
690
 
 
691
        raise NotImplementedError()
 
692
 
 
693
    def pre_exec(self):
 
694
        """Called before an execution of a compiled statement.
 
695
 
 
696
        If a compiled statement was passed to this ExecutionContext,
 
697
        the `statement` and `parameters` datamembers must be
 
698
        initialized after this statement is complete.
 
699
        """
 
700
 
 
701
        raise NotImplementedError()
 
702
 
 
703
    def post_exec(self):
 
704
        """Called after the execution of a compiled statement.
 
705
 
 
706
        If a compiled statement was passed to this ExecutionContext,
 
707
        the `last_insert_ids`, `last_inserted_params`, etc.
 
708
        datamembers should be available after this method completes.
 
709
        """
 
710
 
 
711
        raise NotImplementedError()
 
712
 
 
713
    def result(self):
 
714
        """Return a result object corresponding to this ExecutionContext.
 
715
 
 
716
        Returns a ResultProxy.
 
717
        """
 
718
 
 
719
        raise NotImplementedError()
 
720
 
 
721
    def handle_dbapi_exception(self, e):
 
722
        """Receive a DBAPI exception which occurred upon execute, result
 
723
        fetch, etc."""
 
724
 
 
725
        raise NotImplementedError()
 
726
 
 
727
    def should_autocommit_text(self, statement):
 
728
        """Parse the given textual statement and return True if it refers to
 
729
        a "committable" statement"""
 
730
 
 
731
        raise NotImplementedError()
 
732
 
 
733
    def lastrow_has_defaults(self):
 
734
        """Return True if the last INSERT or UPDATE row contained
 
735
        inlined or database-side defaults.
 
736
        """
 
737
 
 
738
        raise NotImplementedError()
 
739
 
 
740
    def get_rowcount(self):
 
741
        """Return the DBAPI ``cursor.rowcount`` value, or in some
 
742
        cases an interpreted value.
 
743
 
 
744
        See :attr:`.ResultProxy.rowcount` for details on this.
 
745
 
 
746
        """
 
747
 
 
748
        raise NotImplementedError()
 
749
 
 
750
 
 
751
class Compiled(object):
 
752
    """Represent a compiled SQL or DDL expression.
 
753
 
 
754
    The ``__str__`` method of the ``Compiled`` object should produce
 
755
    the actual text of the statement.  ``Compiled`` objects are
 
756
    specific to their underlying database dialect, and also may
 
757
    or may not be specific to the columns referenced within a
 
758
    particular set of bind parameters.  In no case should the
 
759
    ``Compiled`` object be dependent on the actual values of those
 
760
    bind parameters, even though it may reference those values as
 
761
    defaults.
 
762
    """
 
763
 
 
764
    def __init__(self, dialect, statement, bind=None,
 
765
                compile_kwargs=util.immutabledict()):
 
766
        """Construct a new ``Compiled`` object.
 
767
 
 
768
        :param dialect: ``Dialect`` to compile against.
 
769
 
 
770
        :param statement: ``ClauseElement`` to be compiled.
 
771
 
 
772
        :param bind: Optional Engine or Connection to compile this
 
773
          statement against.
 
774
 
 
775
        :param compile_kwargs: additional kwargs that will be
 
776
         passed to the initial call to :meth:`.Compiled.process`.
 
777
 
 
778
         .. versionadded:: 0.8
 
779
 
 
780
        """
 
781
 
 
782
        self.dialect = dialect
 
783
        self.bind = bind
 
784
        if statement is not None:
 
785
            self.statement = statement
 
786
            self.can_execute = statement.supports_execution
 
787
            self.string = self.process(self.statement, **compile_kwargs)
 
788
 
 
789
    @util.deprecated("0.7", ":class:`.Compiled` objects now compile "
 
790
                        "within the constructor.")
 
791
    def compile(self):
 
792
        """Produce the internal string representation of this element."""
 
793
        pass
 
794
 
 
795
    @property
 
796
    def sql_compiler(self):
 
797
        """Return a Compiled that is capable of processing SQL expressions.
 
798
 
 
799
        If this compiler is one, it would likely just return 'self'.
 
800
 
 
801
        """
 
802
 
 
803
        raise NotImplementedError()
 
804
 
 
805
    def process(self, obj, **kwargs):
 
806
        return obj._compiler_dispatch(self, **kwargs)
 
807
 
 
808
    def __str__(self):
 
809
        """Return the string text of the generated SQL or DDL."""
 
810
 
 
811
        return self.string or ''
 
812
 
 
813
    def construct_params(self, params=None):
 
814
        """Return the bind params for this compiled object.
 
815
 
 
816
        :param params: a dict of string/object pairs whose values will
 
817
                       override bind values compiled in to the
 
818
                       statement.
 
819
        """
 
820
 
 
821
        raise NotImplementedError()
 
822
 
 
823
    @property
 
824
    def params(self):
 
825
        """Return the bind params for this compiled object."""
 
826
        return self.construct_params()
 
827
 
 
828
    def execute(self, *multiparams, **params):
 
829
        """Execute this compiled object."""
 
830
 
 
831
        e = self.bind
 
832
        if e is None:
 
833
            raise exc.UnboundExecutionError(
 
834
                        "This Compiled object is not bound to any Engine "
 
835
                        "or Connection.")
 
836
        return e._execute_compiled(self, multiparams, params)
 
837
 
 
838
    def scalar(self, *multiparams, **params):
 
839
        """Execute this compiled object and return the result's
 
840
        scalar value."""
 
841
 
 
842
        return self.execute(*multiparams, **params).scalar()
 
843
 
 
844
 
 
845
class TypeCompiler(object):
 
846
    """Produces DDL specification for TypeEngine objects."""
 
847
 
 
848
    def __init__(self, dialect):
 
849
        self.dialect = dialect
 
850
 
 
851
    def process(self, type_):
 
852
        return type_._compiler_dispatch(self)
 
853
 
 
854
 
 
855
class Connectable(object):
 
856
    """Interface for an object which supports execution of SQL constructs.
 
857
 
 
858
    The two implementations of :class:`.Connectable` are
 
859
    :class:`.Connection` and :class:`.Engine`.
 
860
 
 
861
    Connectable must also implement the 'dialect' member which references a
 
862
    :class:`.Dialect` instance.
 
863
 
 
864
    """
 
865
 
 
866
    dispatch = event.dispatcher(events.ConnectionEvents)
 
867
 
 
868
    def connect(self, **kwargs):
 
869
        """Return a :class:`.Connection` object.
 
870
 
 
871
        Depending on context, this may be ``self`` if this object
 
872
        is already an instance of :class:`.Connection`, or a newly
 
873
        procured :class:`.Connection` if this object is an instance
 
874
        of :class:`.Engine`.
 
875
 
 
876
        """
 
877
 
 
878
    def contextual_connect(self):
 
879
        """Return a :class:`.Connection` object which may be part of an ongoing
 
880
        context.
 
881
 
 
882
        Depending on context, this may be ``self`` if this object
 
883
        is already an instance of :class:`.Connection`, or a newly
 
884
        procured :class:`.Connection` if this object is an instance
 
885
        of :class:`.Engine`.
 
886
 
 
887
        """
 
888
 
 
889
        raise NotImplementedError()
 
890
 
 
891
    @util.deprecated("0.7",
 
892
                     "Use the create() method on the given schema "
 
893
                     "object directly, i.e. :meth:`.Table.create`, "
 
894
                     ":meth:`.Index.create`, :meth:`.MetaData.create_all`")
 
895
    def create(self, entity, **kwargs):
 
896
        """Emit CREATE statements for the given schema entity."""
 
897
 
 
898
        raise NotImplementedError()
 
899
 
 
900
    @util.deprecated("0.7",
 
901
                     "Use the drop() method on the given schema "
 
902
                     "object directly, i.e. :meth:`.Table.drop`, "
 
903
                     ":meth:`.Index.drop`, :meth:`.MetaData.drop_all`")
 
904
    def drop(self, entity, **kwargs):
 
905
        """Emit DROP statements for the given schema entity."""
 
906
 
 
907
        raise NotImplementedError()
 
908
 
 
909
    def execute(self, object, *multiparams, **params):
 
910
        """Executes the given construct and returns a :class:`.ResultProxy`."""
 
911
        raise NotImplementedError()
 
912
 
 
913
    def scalar(self, object, *multiparams, **params):
 
914
        """Executes and returns the first column of the first row.
 
915
 
 
916
        The underlying cursor is closed after execution.
 
917
        """
 
918
        raise NotImplementedError()
 
919
 
 
920
    def _run_visitor(self, visitorcallable, element,
 
921
                                    **kwargs):
 
922
        raise NotImplementedError()
 
923
 
 
924
    def _execute_clauseelement(self, elem, multiparams=None, params=None):
 
925
        raise NotImplementedError()