# SQLAlchemy 0.5.7 Documentation

Search:
Version: 0.5.7 Last Updated: 12/26/2009 18:22:23

# Interfaces

Semi-private module containing various base classes used throughout the ORM.

Defines the extension classes `MapperExtension`, `SessionExtension`, and `AttributeExtension` as well as other user-subclassable extension objects.

class `sqlalchemy.orm.interfaces.``AttributeExtension`

  An event handler for individual attribute change events.

  AttributeExtension is assembled within the descriptors associated with a mapped class.

  `active_history`
    indicates that the set() method would like to receive the ‘old’ value, even if it means firing lazy callables.
  `append`(state, value, initiator)

    Receive a collection append event.

    The returned value will be used as the actual value to be appended.

  `remove`(state, value, initiator)

    Receive a remove event.

    No return value is defined.

  `set`(state, value, oldvalue, initiator)

    Receive a set event.

    The returned value will be used as the actual value to be set.

class `sqlalchemy.orm.interfaces.``InstrumentationManager`(class_)

  User-defined class instrumentation extension.

  The API for this class should be considered as semi-stable, and may change slightly with new releases.

  `__init__`(class_)
  `dict_getter`(class_)
  `dispose`(class_, manager)
  `get_instance_dict`(class_, instance)
  `initialize_instance_dict`(class_, instance)
  `install_descriptor`(class_, key, inst)
  `install_member`(class_, key, implementation)
  `install_state`(class_, instance, state)
  `instrument_attribute`(class_, key, inst)
  `instrument_collection_class`(class_, key, collection_class)
  `manage`(class_, manager)
  `manager_getter`(class_)
  `post_configure_attribute`(class_, key, inst)
  `remove_state`(class_, instance)
  `state_getter`(class_)
  `uninstall_descriptor`(class_, key)
  `uninstall_member`(class_, key)

class `sqlalchemy.orm.interfaces.``MapperExtension`

  Base implementation for customizing Mapper behavior.

  For each method in MapperExtension, returning a result of EXT_CONTINUE will allow processing to continue to the next MapperExtension in line or use the default functionality if there are no other extensions.

  Returning EXT_STOP will halt processing of further extensions handling that method. Some methods such as `load` have other return requirements, see the individual documentation for details. Other than these exception cases, any return value other than EXT_CONTINUE or EXT_STOP will be interpreted as equivalent to EXT_STOP.

  `after_delete`(mapper, connection, instance)
    Receive an object instance after that instance is DELETEed.
  `after_insert`(mapper, connection, instance)
    Receive an object instance after that instance is INSERTed.
  `after_update`(mapper, connection, instance)
    Receive an object instance after that instance is UPDATEed.
  `append_result`(mapper, selectcontext, row, instance, result, **flags)

    Receive an object instance before that instance is appended to a result list.

    If this method returns EXT_CONTINUE, result appending will proceed normally. if this method returns any other value or None, result appending will not proceed for this instance, giving this extension an opportunity to do the appending itself, if desired.

    mapper
      The mapper doing the operation.
    selectcontext
      SelectionContext corresponding to the instances() call.
    row
      The result row from the database.
    instance
      The object instance to be appended to the result.
    result
      List to which results are being appended.
    **flags
      extra information about the row, same as criterion in `create_row_processor()` method of `MapperProperty`
  `before_delete`(mapper, connection, instance)

    Receive an object instance before that instance is DELETEed.

    Note that no changes to the overall flush plan can be made here; this means any collection modification, save() or delete() operations which occur within this method will not take effect until the next flush call.

  `before_insert`(mapper, connection, instance)

    Receive an object instance before that instance is INSERTed into its table.

    This is a good place to set up primary key values and such that aren’t handled otherwise.

    Column-based attributes can be modified within this method which will result in the new value being inserted. However no changes to the overall flush plan can be made; this means any collection modification or save() operations which occur within this method will not take effect until the next flush call.

  `before_update`(mapper, connection, instance)

    Receive an object instance before that instance is UPDATEed.

    Note that this method is called for all instances that are marked as “dirty”, even those which have no net changes to their column-based attributes. An object is marked as dirty when any of its column-based attributes have a “set attribute” operation called or when any of its collections are modified. If, at update time, no column-based attributes have any net changes, no UPDATE statement will be issued. This means that an instance being sent to before_update is not a guarantee that an UPDATE statement will be issued (although you can affect the outcome here).

    To detect if the column-based attributes on the object have net changes, and will therefore generate an UPDATE statement, use `object_session(instance).is_modified(instance, include_collections=False)`.

    Column-based attributes can be modified within this method which will result in their being updated. However no changes to the overall flush plan can be made; this means any collection modification or save() operations which occur within this method will not take effect until the next flush call.

  `create_instance`(mapper, selectcontext, row, class_)

    Receive a row when a new object instance is about to be created from that row.

    The method can choose to create the instance itself, or it can return EXT_CONTINUE to indicate normal object creation should take place.

    mapper
      The mapper doing the operation
    selectcontext
      SelectionContext corresponding to the instances() call
    row
      The result row from the database
    class_
      The class we are mapping.
    return value
      A new object instance, or EXT_CONTINUE
  `init_failed`(mapper, class_, oldinit, instance, args, kwargs)
  `init_instance`(mapper, class_, oldinit, instance, args, kwargs)
  `instrument_class`(mapper, class_)
  `populate_instance`(mapper, selectcontext, row, instance, **flags)

    Receive an instance before that instance has its attributes populated.

    This usually corresponds to a newly loaded instance but may also correspond to an already-loaded instance which has unloaded attributes to be populated. The method may be called many times for a single instance, as multiple result rows are used to populate eagerly loaded collections.

    If this method returns EXT_CONTINUE, instance population will proceed normally. If any other value or None is returned, instance population will not proceed, giving this extension an opportunity to populate the instance itself, if desired.

    As of 0.5, most usages of this hook are obsolete. For a generic “object has been newly created from a row” hook, use `reconstruct_instance()`, or the `@orm.reconstructor` decorator.

  `reconstruct_instance`(mapper, instance)

    Receive an object instance after it has been created via `__new__`, and after initial attribute population has occurred.

    This typically occurs when the instance is created based on incoming result rows, and is only called once for that instance’s lifetime.

    Note that during a result-row load, this method is called upon the first row received for this instance. If eager loaders are set to further populate collections on the instance, those will not yet be completely loaded.

  `translate_row`(mapper, context, row)

    Perform pre-processing on the given result row and return a new row instance.

    This is called when the mapper first receives a row, before the object identity or the instance itself has been derived from that row.

class `sqlalchemy.orm.interfaces.``PropComparator`(prop, mapper, adapter=None)

  defines comparison operations for MapperProperty objects.

  PropComparator instances should also define an accessor ‘property’ which returns the MapperProperty associated with this PropComparator.

  `__init__`(prop, mapper, adapter=None)
  `adapted`(adapter)
    Return a copy of this PropComparator which will use the given adaption function on the local side of generated expressions.
  `any`(criterion=None, **kwargs)

    Return true if this collection contains any member that meets the given criterion.

    criterion
      an optional ClauseElement formulated against the member class’ table or attributes.
    **kwargs
      key/value pairs corresponding to member class attribute names which will be compared via equality to the corresponding values.
  static `any_op`(a, b, **kwargs)
  `has`(criterion=None, **kwargs)

    Return true if this element references a member which meets the given criterion.

    criterion
      an optional ClauseElement formulated against the member class’ table or attributes.
    **kwargs
      key/value pairs corresponding to member class attribute names which will be compared via equality to the corresponding values.
  static `has_op`(a, b, **kwargs)
  `of_type`(class_)

    Redefine this object in terms of a polymorphic subclass.

    Returns a new PropComparator from which further criterion can be evaluated.

    e.g.:

        query.join(Company.employees.of_type(Engineer)).\
           filter(Engineer.name=='foo')

    class_
      a class or mapper indicating that criterion will be against this specific subclass.
  static `of_type_op`(a, class_)

class `sqlalchemy.orm.interfaces.``SessionExtension`

  An extension hook object for Sessions. Subclasses may be installed into a Session (or sessionmaker) using the `extension` keyword argument.

  `after_attach`(session, instance)

    Execute after an instance is attached to a session.

    This is called after an add, delete or merge.

  `after_begin`(session, transaction, connection)

    Execute after a transaction is begun on a connection

    transaction is the SessionTransaction. This method is called after an engine level transaction is begun on a connection.

  `after_bulk_delete`(session, query, query_context, result)

    Execute after a bulk delete operation to the session.

    This is called after a session.query(...).delete()

    query is the query object that this delete operation was called on. query_context was the query context object. result is the result object returned from the bulk operation.

  `after_bulk_update`(session, query, query_context, result)

    Execute after a bulk update operation to the session.

    This is called after a session.query(...).update()

    query is the query object that this update operation was called on. query_context was the query context object. result is the result object returned from the bulk operation.

  `after_commit`(session)

    Execute after a commit has occured.

    Note that this may not be per-flush if a longer running transaction is ongoing.

  `after_flush`(session, flush_context)

    Execute after flush has completed, but before commit has been called.

    Note that the session’s state is still in pre-flush, i.e. ‘new’, ‘dirty’, and ‘deleted’ lists still show pre-flush state as well as the history settings on instance attributes.

  `after_flush_postexec`(session, flush_context)

    Execute after flush has completed, and after the post-exec state occurs.

    This will be when the ‘new’, ‘dirty’, and ‘deleted’ lists are in their final state. An actual commit() may or may not have occured, depending on whether or not the flush started its own transaction or participated in a larger transaction.

  `after_rollback`(session)

    Execute after a rollback has occured.

    Note that this may not be per-flush if a longer running transaction is ongoing.

  `before_commit`(session)

    Execute right before commit is called.

    Note that this may not be per-flush if a longer running transaction is ongoing.

  `before_flush`(session, flush_context, instances)

    Execute before flush process has started.

    instances is an optional list of objects which were passed to the `flush()` method.