SQLAlchemy 0.5 Documentation

Multiple Pages | One Page
Version: 0.5.0rc2 Last Updated: 10/12/08 13:44:43

module sqlalchemy.orm.interfaces

Semi-private implementation objects which form the basis of ORM-mapped attributes, query options and mapper extension.

Defines the MapperExtension class, which can be end-user subclassed to add event-based functionality to mappers. The remainder of this module is generally private to the ORM.

Module Functions

def build_path(entity, key, prev=None)

class AttributeExtension(object)

An event handler for individual attribute change events.

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

def append(self, state, value, initiator)

Receive a collection append event.

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

def remove(self, state, value, initiator)

Receive a remove event.

No return value is defined.

def set(self, state, value, oldvalue, initiator)

Receive a set event.

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

back to section top

class ExtensionOption(MapperOption)

a MapperOption that applies a MapperExtension to a query operation.

def __init__(self, ext)

Construct a new ExtensionOption.

def process_query(self, query)
back to section top

class InstrumentationManager(object)

User-defined class instrumentation extension.

def __init__(self, class_)

Construct a new InstrumentationManager.

def dispose(self, class_, manager)
def get_instance_dict(self, class_, instance)
def initialize_instance_dict(self, class_, instance)
def install_descriptor(self, class_, key, inst)
def install_member(self, class_, key, implementation)
def install_state(self, class_, instance, state)
def instrument_attribute(self, class_, key, inst)
def instrument_collection_class(self, class_, key, collection_class)
def manage(self, class_, manager)
def manager_getter(self, class_)
def state_getter(self, class_)
def uninstall_descriptor(self, class_, key)
def uninstall_member(self, class_, key)
back to section top

class LoaderStrategy(object)

Describe the loading behavior of a StrategizedProperty object.

The LoaderStrategy interacts with the querying process in three ways:

  • it controls the configuration of the InstrumentedAttribute placed on a class to handle the behavior of the attribute. this may involve setting up class-level callable functions to fire off a select operation when the attribute is first accessed (i.e. a lazy load)
  • it processes the QueryContext at statement construction time, where it can modify the SQL statement that is being produced. simple column attributes may add their represented column to the list of selected columns, eager loading properties may add LEFT OUTER JOIN clauses to the statement.
  • it processes the SelectionContext at row-processing time. This includes straight population of attributes corresponding to rows, setting instance-level lazyloader callables on newly constructed instances, and appending child items to scalar/collection attributes in response to eagerly-loaded relations.
def __init__(self, parent)

Construct a new LoaderStrategy.

def create_row_processor(self, selectcontext, path, mapper, row, adapter)

Return row processing functions which fulfill the contract specified by MapperProperty.create_row_processor.

StrategizedProperty delegates its create_row_processor method directly to this method.

def debug_callable(self, fn, logger, announcement, logfn)
def init(self)
def init_class_attribute(self)
def setup_query(self, context, entity, path, adapter, **kwargs)
back to section top

class MapperExtension(object)

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.

def after_delete(self, mapper, connection, instance)

Receive an object instance after that instance is DELETEed.

def after_insert(self, mapper, connection, instance)

Receive an object instance after that instance is INSERTed.

def after_update(self, mapper, connection, instance)

Receive an object instance after that instance is UPDATEed.

def append_result(self, 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
def before_delete(self, 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.

def before_insert(self, 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.

def before_update(self, 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.

def create_instance(self, 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
def init_failed(self, mapper, class_, oldinit, instance, args, kwargs)
def init_instance(self, mapper, class_, oldinit, instance, args, kwargs)
def instrument_class(self, mapper, class_)
def populate_instance(self, 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.

def reconstruct_instance(self, mapper, instance)

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

This typicically 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.

def translate_row(self, 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.

back to section top

class MapperOption(object)

Describe a modification to a Query.

def process_query(self, query)
def process_query_conditionally(self, query)

same as process_query(), except that this option may not apply to the given query.

Used when secondary loaders resend existing options to a new Query.

back to section top

class MapperProperty(object)

Manage the relationship of a Mapper to a single class attribute, as well as that attribute as it appears on individual instances of the class, including attribute instrumentation, attribute access, loading behavior, and dependency calculations.

def cascade_iterator(self, type_, state, visited_instances=None, halt_on=None)

Iterate through instances related to the given instance for a particular 'cascade', starting with this MapperProperty.

See PropertyLoader for the related instance implementation.

def compare(self, operator, value)

Return a compare operation for the columns represented by this MapperProperty to the given value, which may be a column value or an instance. 'operator' is an operator from the operators module, or from sql.Comparator.

By default uses the PropComparator attached to this MapperProperty under the attribute name "comparator".

def create_row_processor(self, selectcontext, path, mapper, row, adapter)

Return a 2-tuple consiting of two row processing functions and an instance post-processing function.

Input arguments are the query.SelectionContext and the first applicable row of a result set obtained within query.Query.instances(), called only the first time a particular mapper's populate_instance() method is invoked for the overall result.

The settings contained within the SelectionContext as well as the columns present in the row (which will be the same columns present in all rows) are used to determine the presence and behavior of the returned callables. The callables will then be used to process all rows and instances.

Callables are of the following form:

def new_execute(state, row, **flags):
    # process incoming instance state and given row.  the instance is
    # "new" and was just created upon receipt of this row.
    # flags is a dictionary containing at least the following
    # attributes:
    #   isnew - indicates if the instance was newly created as a
    #           result of reading this row
    #   instancekey - identity key of the instance

def existing_execute(state, row, **flags):
    # process incoming instance state and given row.  the instance is
    # "existing" and was created based on a previous row.

return (new_execute, existing_execute)

Either of the three tuples can be None in which case no function is called.

def do_init(self)

Perform subclass-specific initialization steps.

This is a template method called by the MapperProperty object's init() method.

def init(self, key, parent)

Called after all mappers are compiled to assemble relationships between mappers, establish instrumented class attributes.

def is_primary(self)

Return True if this MapperProperty's mapper is the primary mapper for its class.

This flag is used to indicate that the MapperProperty can define attribute instrumentation for the class at the class level (as opposed to the individual instance level).

def merge(self, session, source, dest, dont_load, _recursive)

Merge the attribute represented by this MapperProperty from source to destination object

def register_dependencies(self, *args, **kwargs)

Called by the Mapper in response to the UnitOfWork calling the Mapper's register_dependencies operation. Should register with the UnitOfWork all inter-mapper dependencies as well as dependency processors (see UOW docs for more details).

def set_parent(self, parent)
def setup(self, context, entity, path, adapter, **kwargs)

Called by Query for the purposes of constructing a SQL statement.

Each MapperProperty associated with the target mapper processes the statement referenced by the query context, adding columns and/or criterion as appropriate.

back to section top

class PropComparator(ColumnOperators)

defines comparison operations for MapperProperty objects.

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

def __init__(self, prop, mapper)

Construct a new PropComparator.

def any(self, 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.
def has(self, 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.
def of_type(self, 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.
def __clause_element__(self)
back to section top

class PropertyOption(MapperOption)

A MapperOption that is applied to a property off the mapper or one of its child mappers, identified by a dot-separated key.

def __init__(self, key, mapper=None)

Construct a new PropertyOption.

def process_query(self, query)
def process_query_conditionally(self, query)
def process_query_property(self, query, paths)
back to section top

class SessionExtension(object)

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

def after_attach(self, session, instance)

Execute after an instance is attached to a session.

This is called after an add, delete or merge.

def after_begin(self, 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.

def after_commit(self, session)

Execute after a commit has occured.

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

def after_flush(self, 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.

def after_flush_postexec(self, 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.

def after_rollback(self, session)

Execute after a rollback has occured.

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

def before_commit(self, session)

Execute right before commit is called.

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

def before_flush(self, session, flush_context, instances)

Execute before flush process has started.

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

back to section top

class StrategizedOption(PropertyOption)

A MapperOption that affects which LoaderStrategy will be used for an operation by a StrategizedProperty.

def get_strategy_class(self)
def is_chained(self)
def process_query_property(self, query, paths)
back to section top

class StrategizedProperty(MapperProperty)

A MapperProperty which uses selectable strategies to affect loading behavior.

There is a single default strategy selected by default. Alternate strategies can be selected at Query time through the usage of StrategizedOption objects via the Query.options() method.

def create_row_processor(self, context, path, mapper, row, adapter)
def do_init(self)
def setup(self, context, entity, path, adapter, **kwargs)
back to section top
Up: API Documentation | Previous: module sqlalchemy.orm.collections | Next: module sqlalchemy.orm.mapper