# SQLAlchemy 0.7 Documentation

Search:
Release: 0.7.3 | Release Date: October 16, 2011

# Core Internals

Some key internal constructs are listed here.

class `sqlalchemy.engine.base.``Compiled`(dialect, statement, bind=None)

  Bases: `object`

  Represent a compiled SQL or DDL expression.

  The `__str__` method of the `Compiled` object should produce the actual text of the statement. `Compiled` objects are specific to their underlying database dialect, and also may or may not be specific to the columns referenced within a particular set of bind parameters. In no case should the `Compiled` object be dependent on the actual values of those bind parameters, even though it may reference those values as defaults.

  `__init__`(dialect, statement, bind=None)

    Construct a new `Compiled` object.

    Parameters:

    - dialect – `Dialect` to compile against.
    - statement – `ClauseElement` to be compiled.
    - bind – Optional Engine or Connection to compile this statement against.

  `compile`()

    Produce the internal string representation of this element.

    Deprecated since version 0.7: `Compiled` objects now compile within the constructor.

  `construct_params`(params=None)

    Return the bind params for this compiled object.

    Parameters: | params – a dict of string/object pairs whos values will override bind values compiled in to the statement.

  `execute`(*multiparams, **params)

    Execute this compiled object.

  `params`

    Return the bind params for this compiled object.

  `process`(obj, **kwargs)
  `scalar`(*multiparams, **params)

    Execute this compiled object and return the result’s scalar value.

  `sql_compiler`

    Return a Compiled that is capable of processing SQL expressions.

    If this compiler is one, it would likely just return ‘self’.

class `sqlalchemy.sql.compiler.``DDLCompiler`(dialect, statement, bind=None)

  Bases: `sqlalchemy.engine.base.Compiled`

  `define_constraint_remote_table`(constraint, table, preparer)

    Format the remote table clause of a CREATE CONSTRAINT clause.

class `sqlalchemy.engine.default.``DefaultDialect`(convert_unicode=False, assert_unicode=False, encoding='utf-8', paramstyle=None, dbapi=None, implicit_returning=None, label_length=None, **kwargs)

  Bases: `sqlalchemy.engine.base.Dialect`

  Default implementation of Dialect

  `connect`(*cargs, **cparams)
  `create_connect_args`(url)
  `create_xid`()

    Create a random two-phase transaction ID.

    This id will be passed to do_begin_twophase(), do_rollback_twophase(), do_commit_twophase(). Its format is unspecified.

  `ddl_compiler`

    alias of `DDLCompiler`

  `dialect_description`
  `do_begin`(connection)

    Implementations might want to put logic here for turning autocommit on/off, etc.

  `do_commit`(connection)

    Implementations might want to put logic here for turning autocommit on/off, etc.

  `do_execute`(cursor, statement, parameters, context=None)
  `do_executemany`(cursor, statement, parameters, context=None)
  `do_release_savepoint`(connection, name)
  `do_rollback`(connection)

    Implementations might want to put logic here for turning autocommit on/off, etc.

  `do_rollback_to_savepoint`(connection, name)
  `do_savepoint`(connection, name)
  `execute_sequence_format`

    alias of `tuple`

  `execution_ctx_cls`

    alias of `DefaultExecutionContext`

  `get_pk_constraint`(conn, table_name, schema=None, **kw)

    Compatiblity method, adapts the result of get_primary_keys() for those dialects which don’t implement get_pk_constraint().

  classmethod `get_pool_class`(url)
  `initialize`(connection)
  `is_disconnect`(e, connection, cursor)
  `on_connect`()

    return a callable which sets up a newly created DBAPI connection.

    This is used to set dialect-wide per-connection options such as isolation modes, unicode modes, etc.

    If a callable is returned, it will be assembled into a pool listener that receives the direct DBAPI connection, with all wrappers removed.

    If None is returned, no listener will be generated.

  `preparer`

    alias of `IdentifierPreparer`

  `reflecttable`(connection, table, include_columns)
  `reset_isolation_level`(dbapi_conn)
  `statement_compiler`

    alias of `SQLCompiler`

  `type_compiler`

    alias of `GenericTypeCompiler`

  `type_descriptor`(typeobj)

    Provide a database-specific `TypeEngine` object, given the generic object which comes from the types module.

    This method looks for a dictionary called `colspecs` as a class or instance-level variable, and passes on to `types.adapt_type()`.

  `validate_identifier`(ident)

class `sqlalchemy.engine.base.``Dialect`

  Bases: `object`

  Define the behavior of a specific database and DB-API combination.

  Any aspect of metadata definition, SQL query generation, execution, result-set handling, or anything else which varies between databases is defined under the general category of the Dialect. The Dialect acts as a factory for other database-specific object implementations including ExecutionContext, Compiled, DefaultGenerator, and TypeEngine.

  All Dialects implement the following attributes:

  name
    identifying name for the dialect from a DBAPI-neutral point of view (i.e. ‘sqlite’)
  driver
    identifying name for the dialect’s DBAPI
  positional
    True if the paramstyle for this Dialect is positional.
  paramstyle
    the paramstyle to be used (some DB-APIs support multiple paramstyles).
  convert_unicode
    True if Unicode conversion should be applied to all `str` types.
  encoding
    type of encoding to use for unicode, usually defaults to ‘utf-8’.
  statement_compiler
    a `Compiled` class used to compile SQL statements
  ddl_compiler
    a `Compiled` class used to compile DDL statements
  server_version_info
    a tuple containing a version number for the DB backend in use. This value is only available for supporting dialects, and is typically populated during the initial connection to the database.
  default_schema_name
    the name of the default schema. This value is only available for supporting dialects, and is typically populated during the initial connection to the database.
  execution_ctx_cls
    a `ExecutionContext` class used to handle statement execution
  execute_sequence_format
    either the ‘tuple’ or ‘list’ type, depending on what cursor.execute() accepts for the second argument (they vary).
  preparer
    a `IdentifierPreparer` class used to quote identifiers.
  supports_alter
    `True` if the database supports `ALTER TABLE`.
  max_identifier_length
    The maximum length of identifier names.
  supports_unicode_statements
    Indicate whether the DB-API can receive SQL statements as Python unicode strings
  supports_unicode_binds
    Indicate whether the DB-API can receive string bind parameters as Python unicode strings
  supports_sane_rowcount
    Indicate whether the dialect properly implements rowcount for `UPDATE` and `DELETE` statements.
  supports_sane_multi_rowcount
    Indicate whether the dialect properly implements rowcount for `UPDATE` and `DELETE` statements when executed via executemany.
  preexecute_autoincrement_sequences
    True if ‘implicit’ primary key functions must be executed separately in order to get their value. This is currently oriented towards Postgresql.
  implicit_returning
    use RETURNING or equivalent during INSERT execution in order to load newly generated primary keys and other column defaults in one execution, which are then available via inserted_primary_key. If an insert statement has returning() specified explicitly, the “implicit” functionality is not used and inserted_primary_key will not be available.
  dbapi_type_map

    A mapping of DB-API type objects present in this Dialect’s DB-API implementation mapped to TypeEngine implementations used by the dialect.

    This is used to apply types to result sets based on the DB-API types present in cursor.description; it only takes effect for result sets against textual statements where no explicit typemap was present.

  colspecs
    A dictionary of TypeEngine classes from sqlalchemy.types mapped to subclasses that are specific to the dialect class. This dictionary is class-level only and is not accessed from the dialect instance itself.
  supports_default_values
    Indicates if the construct `INSERT INTO tablename DEFAULT VALUES` is supported
  supports_sequences
    Indicates if the dialect supports CREATE SEQUENCE or similar.
  sequences_optional
    If True, indicates if the “optional” flag on the Sequence() construct should signal to not generate a CREATE SEQUENCE. Applies only to dialects that support sequences. Currently used only to allow Postgresql SERIAL to be used on a column that specifies Sequence() for usage on other backends.
  supports_native_enum
    Indicates if the dialect supports a native ENUM construct. This will prevent types.Enum from generating a CHECK constraint when that type is used.
  supports_native_boolean
    Indicates if the dialect supports a native boolean construct. This will prevent types.Boolean from generating a CHECK constraint when that type is used.
  `connect`()

    return a callable which sets up a newly created DBAPI connection.

    The callable accepts a single argument “conn” which is the DBAPI connection itself. It has no return value.

    This is used to set dialect-wide per-connection options such as isolation modes, unicode modes, etc.

    If a callable is returned, it will be assembled into a pool listener that receives the direct DBAPI connection, with all wrappers removed.

    If None is returned, no listener will be generated.

  `create_connect_args`(url)

    Build DB-API compatible connection arguments.

    Given a `URL` object, returns a tuple consisting of a *args/**kwargs suitable to send directly to the dbapi’s connect function.

  `create_xid`()

    Create a two-phase transaction ID.

    This id will be passed to do_begin_twophase(), do_rollback_twophase(), do_commit_twophase(). Its format is unspecified.

  `denormalize_name`(name)

    convert the given name to a case insensitive identifier for the backend if it is an all-lowercase name.

    this method is only used if the dialect defines requires_name_normalize=True.

  `do_begin`(connection)

    Provide an implementation of connection.begin(), given a DB-API connection.

  `do_begin_twophase`(connection, xid)

    Begin a two phase transaction on the given connection.

  `do_commit`(connection)

    Provide an implementation of connection.commit(), given a DB-API connection.

  `do_commit_twophase`(connection, xid, is_prepared=True, recover=False)

    Commit a two phase transaction on the given connection.

  `do_execute`(cursor, statement, parameters, context=None)

    Provide an implementation of cursor.execute(statement, parameters).

  `do_executemany`(cursor, statement, parameters, context=None)

    Provide an implementation of cursor.executemany(statement, parameters).

  `do_prepare_twophase`(connection, xid)

    Prepare a two phase transaction on the given connection.

  `do_recover_twophase`(connection)

    Recover list of uncommited prepared two phase transaction identifiers on the given connection.

  `do_release_savepoint`(connection, name)

    Release the named savepoint on a SQL Alchemy connection.

  `do_rollback`(connection)

    Provide an implementation of connection.rollback(), given a DB-API connection.

  `do_rollback_to_savepoint`(connection, name)

    Rollback a SQL Alchemy connection to the named savepoint.

  `do_rollback_twophase`(connection, xid, is_prepared=True, recover=False)

    Rollback a two phase transaction on the given connection.

  `do_savepoint`(connection, name)

    Create a savepoint with the given name on a SQLAlchemy connection.

  `get_columns`(connection, table_name, schema=None, **kw)

    Return information about columns in table_name.

    Given a `Connection`, a string table_name, and an optional string schema, return column information as a list of dictionaries with these keys:

    name
      the column’s name
    type
      [sqlalchemy.types#TypeEngine]
    nullable
      boolean
    default
      the column’s default value
    autoincrement
      boolean
    sequence
      a dictionary of the form
        {‘name’ : str, ‘start’ :int, ‘increment’: int}

    Additional column attributes may be present.

  `get_foreign_keys`(connection, table_name, schema=None, **kw)

    Return information about foreign_keys in table_name.

    Given a `Connection`, a string table_name, and an optional string schema, return foreign key information as a list of dicts with these keys:

    name
      the constraint’s name
    constrained_columns
      a list of column names that make up the foreign key
    referred_schema
      the name of the referred schema
    referred_table
      the name of the referred table
    referred_columns
      a list of column names in the referred table that correspond to constrained_columns
  `get_indexes`(connection, table_name, schema=None, **kw)

    Return information about indexes in table_name.

    Given a `Connection`, a string table_name and an optional string schema, return index information as a list of dictionaries with these keys:

    name
      the index’s name
    column_names
      list of column names in order
    unique
      boolean
  `get_isolation_level`(dbapi_conn)

    Given a DBAPI connection, return its isolation level.

  `get_pk_constraint`(table_name, schema=None, **kw)

    Return information about the primary key constraint on table_name`.

    Given a string table_name, and an optional string schema, return primary key information as a dictionary with these keys:

    constrained_columns
      a list of column names that make up the primary key
    name
      optional name of the primary key constraint.
  `get_primary_keys`(connection, table_name, schema=None, **kw)

    Return information about primary keys in table_name.

    Given a `Connection`, a string table_name, and an optional string schema, return primary key information as a list of column names.

  `get_table_names`(connection, schema=None, **kw)

    Return a list of table names for schema.

  `get_view_definition`(connection, view_name, schema=None, **kw)

    Return view definition.

    Given a `Connection`, a string view_name, and an optional string schema, return the view definition.

  `get_view_names`(connection, schema=None, **kw)

    Return a list of all view names available in the database.

    schema:
      Optional, retrieve names from a non-default schema.
  `has_sequence`(connection, sequence_name, schema=None)

    Check the existence of a particular sequence in the database.

    Given a `Connection` object and a string sequence_name, return True if the given sequence exists in the database, False otherwise.

  `has_table`(connection, table_name, schema=None)

    Check the existence of a particular table in the database.

    Given a `Connection` object and a string table_name, return True if the given table (possibly within the specified schema) exists in the database, False otherwise.

  `initialize`(connection)

    Called during strategized creation of the dialect with a connection.

    Allows dialects to configure options based on server version info or other properties.

    The connection passed here is a SQLAlchemy Connection object, with full capabilities.

    The initalize() method of the base dialect should be called via super().

  `is_disconnect`(e, connection, cursor)

    Return True if the given DB-API error indicates an invalid connection

  `normalize_name`(name)

    convert the given name to lowercase if it is detected as case insensitive.

    this method is only used if the dialect defines requires_name_normalize=True.

  `reflecttable`(connection, table, include_columns=None)

    Load table description from the database.

    Given a `Connection` and a `Table` object, reflect its columns and properties from the database. If include_columns (a list or set) is specified, limit the autoload to the given column names.

    The default implementation uses the `Inspector` interface to provide the output, building upon the granular table/column/ constraint etc. methods of `Dialect`.

  `reset_isolation_level`(dbapi_conn)

    Given a DBAPI connection, revert its isolation to the default.

  `set_isolation_level`(dbapi_conn, level)

    Given a DBAPI connection, set its isolation level.

  classmethod `type_descriptor`(typeobj)

    Transform a generic type to a dialect-specific type.

    Dialect classes will usually use the `adapt_type()` function in the types module to make this job easy.

    The returned result is cached per dialect class so can contain no dialect-instance state.

class `sqlalchemy.engine.default.``DefaultExecutionContext`

  Bases: `sqlalchemy.engine.base.ExecutionContext`

  `connection`
  `create_cursor`()
  `get_insert_default`(column)
  `get_lastrowid`()

    return self.cursor.lastrowid, or equivalent, after an INSERT.

    This may involve calling special cursor functions, issuing a new SELECT on the cursor (or a new one), or returning a stored value that was calculated within post_exec().

    This function will only be called for dialects which support “implicit” primary key generation, keep preexecute_autoincrement_sequences set to False, and when no explicit id value was bound to the statement.

    The function is called once, directly after post_exec() and before the transaction is committed or ResultProxy is generated. If the post_exec() method assigns a value to self._lastrowid, the value is used in place of calling get_lastrowid().

    Note that this method is not equivalent to the `lastrowid` method on `ResultProxy`, which is a direct proxy to the DBAPI `lastrowid` accessor in all cases.

  `get_result_proxy`()
  `get_update_default`(column)
  `handle_dbapi_exception`(e)
  `is_crud`
  `lastrow_has_defaults`()
  `post_exec`()
  `post_insert`()
  `pre_exec`()
  `rowcount`
  `set_input_sizes`(translate=None, exclude_types=None)

    Given a cursor and ClauseParameters, call the appropriate style of `setinputsizes()` on the cursor, using DB-API types from the bind parameter’s `TypeEngine` objects.

    This method only called by those dialects which require it, currently cx_oracle.

  `should_autocommit`
  `should_autocommit_text`(statement)
  `supports_sane_multi_rowcount`()
  `supports_sane_rowcount`()

class `sqlalchemy.engine.base.``ExecutionContext`

  Bases: `object`

  A messenger object for a Dialect that corresponds to a single execution.

  ExecutionContext should have these data members:

  connection
    Connection object which can be freely used by default value generators to execute SQL. This Connection should reference the same underlying connection/transactional resources of root_connection.
  root_connection
    Connection object which is the source of this ExecutionContext. This Connection may have close_with_result=True set, in which case it can only be used once.
  dialect
    dialect which created this ExecutionContext.
  cursor
    DB-API cursor procured from the connection,
  compiled
    if passed to constructor, sqlalchemy.engine.base.Compiled object being executed,
  statement
    string version of the statement to be executed. Is either passed to the constructor, or must be created from the sql.Compiled object by the time pre_exec() has completed.
  parameters
    bind parameters passed to the execute() method. For compiled statements, this is a dictionary or list of dictionaries. For textual statements, it should be in a format suitable for the dialect’s paramstyle (i.e. dict or list of dicts for non positional, list or list of lists/tuples for positional).
  isinsert
    True if the statement is an INSERT.
  isupdate
    True if the statement is an UPDATE.
  should_autocommit
    True if the statement is a “committable” statement.
  postfetch_cols
    a list of Column objects for which a server-side default or inline SQL expression value was fired off. Applies to inserts and updates.
  `create_cursor`()

    Return a new cursor generated from this ExecutionContext’s connection.

    Some dialects may wish to change the behavior of connection.cursor(), such as postgresql which may return a PG “server side” cursor.

  `get_rowcount`()

    Return the number of rows produced (by a SELECT query) or affected (by an INSERT/UPDATE/DELETE statement).

    Note that this row count may not be properly implemented in some dialects; this is indicated by the `supports_sane_rowcount` and `supports_sane_multi_rowcount` dialect attributes.

  `handle_dbapi_exception`(e)

    Receive a DBAPI exception which occurred upon execute, result fetch, etc.

  `lastrow_has_defaults`()

    Return True if the last INSERT or UPDATE row contained inlined or database-side defaults.

  `post_exec`()

    Called after the execution of a compiled statement.

    If a compiled statement was passed to this ExecutionContext, the last_insert_ids, last_inserted_params, etc. datamembers should be available after this method completes.

  `pre_exec`()

    Called before an execution of a compiled statement.

    If a compiled statement was passed to this ExecutionContext, the statement and parameters datamembers must be initialized after this statement is complete.

  `result`()

    Return a result object corresponding to this ExecutionContext.

    Returns a ResultProxy.

  `should_autocommit_text`(statement)

    Parse the given textual statement and return True if it refers to a “committable” statement

class `sqlalchemy.sql.compiler.``IdentifierPreparer`(dialect, initial_quote='"', final_quote=None, escape_quote='"', omit_schema=False)

  Bases: `object`

  Handle quoting and case-folding of identifiers based on options.

  `__init__`(dialect, initial_quote='"', final_quote=None, escape_quote='"', omit_schema=False)

    Construct a new `IdentifierPreparer` object.

    initial_quote
      Character that begins a delimited identifier.
    final_quote
      Character that ends a delimited identifier. Defaults to initial_quote.
    omit_schema
      Prevent prepending schema name. Useful for databases that do not support schemae.
  `format_column`(column, use_table=False, name=None, table_name=None)

    Prepare a quoted column name.

  `format_table`(table, use_schema=True, name=None)

    Prepare a quoted table and schema name.

  `format_table_seq`(table, use_schema=True)

    Format table name and schema as a tuple.

  `quote_identifier`(value)

    Quote an identifier.

    Subclasses should override this to provide database-dependent quoting behavior.

  `quote_schema`(schema, force)

    Quote a schema.

    Subclasses should override this to provide database-dependent quoting behavior.

  `unformat_identifiers`(identifiers)

    Unpack ‘schema.table.column’-like strings into components.

class `sqlalchemy.sql.compiler.``SQLCompiler`(dialect, statement, column_keys=None, inline=False, **kwargs)

  Bases: `sqlalchemy.engine.base.Compiled`

  Default implementation of Compiled.

  Compiles ClauseElements into SQL strings. Uses a similar visit paradigm as visitors.ClauseVisitor but implements its own traversal.

  `__init__`(dialect, statement, column_keys=None, inline=False, **kwargs)

    Construct a new `DefaultCompiler` object.

    dialect
      Dialect to be used
    statement
      ClauseElement to be compiled
    column_keys
      a list of column names to be compiled into an INSERT or UPDATE statement.
  `construct_params`(params=None, _group_number=None)

    return a dictionary of bind parameter keys and values

  `default_from`()

    Called when a SELECT statement has no froms, and no FROM clause is to be appended.

    Gives Oracle a chance to tack on a `FROM DUAL` to the string output.

  `escape_literal_column`(text)

    provide escaping for the literal_column() construct.

  `get_select_precolumns`(select)

    Called when building a `SELECT` statement, position is just before column list.

  `label_select_column`(select, column, asfrom)

    label columns present in a select().

  `params`

    Return the bind params for this compiled object.

  `render_literal_value`(value, type_)

    Render the value of a bind parameter as a quoted literal.

    This is used for statement sections that do not accept bind paramters on the target driver/database.

    This should be implemented by subclasses using the quoting services of the DBAPI.