Logo Search packages:      
Sourcecode: sqlalchemy version File versions  Download package

def sqlalchemy::orm::mapper::Mapper::__init__ (   self,
  class_,
  local_table,
  properties = None,
  primary_key = None,
  non_primary = False,
  inherits = None,
  inherit_condition = None,
  extension = None,
  order_by = False,
  allow_column_override = False,
  entity_name = None,
  always_refresh = False,
  version_id_col = None,
  polymorphic_on = None,
  _polymorphic_map = None,
  polymorphic_identity = None,
  concrete = False,
  select_table = None,
  allow_null_pks = False,
  batch = True,
  column_prefix = None 
)

construct a new mapper.  

All arguments may be sent to the sqlalchemy.orm.mapper() function where they are
passed through to here.

class_ - the class to be mapped.

local_table - the table to which the class is mapped, or None if this mapper inherits
from another mapper using concrete table inheritance.

properties - a dictionary mapping the string names of object attributes to MapperProperty 
instances, which define the persistence behavior of that attribute.  Note that the columns in the 
mapped table are automatically converted into ColumnProperty instances based on the "key" 
property of each Column (although they can be overridden using this dictionary).

primary_key - a list of Column objects which define the "primary key" to be used against this mapper's
selectable unit.  This is normally simply the primary key of the "local_table", but can be overridden here.

non_primary - construct a Mapper that will define only the selection of instances, not their persistence.

inherits - another Mapper for which this Mapper will have an inheritance relationship with.

inherit_condition - for joined table inheritance, a SQL expression (constructed ClauseElement) which 
will define how the two tables are joined; defaults to a natural join between the two tables.

extension - a MapperExtension instance or list of MapperExtension instances which will be applied to
all operations by this Mapper.

order_by - a single Column or list of Columns for which selection operations should use as the default
ordering for entities.  Defaults to the OID/ROWID of the table if any, or the first primary key column of the table.

allow_column_override - if True, allows association relationships to be set up which override the usage of 
a column that is on the table (based on key/attribute name).

entity_name - a name to be associated with the class, to allow alternate mappings for a single class.

always_refresh - if True, all query operations for this mapped class will overwrite all data
within object instances that already exist within the session, erasing any in-memory changes with whatever
information was loaded from the database.

version_id_col - a Column which must have an integer type that will be used to keep a running "version id" of
mapped entities in the database.  this is used during save operations to insure that no other thread or process
has updated the instance during the lifetime of the entity, else a ConcurrentModificationError exception is thrown.

polymorphic_on - used with mappers in an inheritance relationship, a Column which will identify the class/mapper
combination to be used with a particular row.  requires the polymorphic_identity value to be set for all mappers
in the inheritance hierarchy.

_polymorphic_map - used internally to propigate the full map of polymorphic identifiers to surrogate mappers.

polymorphic_identity - a value which will be stored in the Column denoted by polymorphic_on, corresponding to the
"class identity" of this mapper.

concrete - if True, indicates this mapper should use concrete table inheritance with its parent mapper.

select_table - a Table or (more commonly) Selectable which will be used to select instances of this mapper's class.
usually used to provide polymorphic loading among several classes in an inheritance hierarchy.

allow_null_pks - indicates that composite primary keys where one or more (but not all) columns contain NULL is a valid
primary key.  Primary keys which contain NULL values usually indicate that a result row does not contain an entity
and should be skipped.

batch - indicates that save operations of multiple entities can be batched together for efficiency.  
setting to False indicates that an instance will be fully saved before saving the next instance, which 
includes inserting/updating all table rows corresponding to the entity as well as calling all MapperExtension 
methods corresponding to the save operation.

column_prefix - a string which will be prepended to the "key" name of all Columns when creating column-based
properties from the given Table.  does not affect explicitly specified column-based properties

Definition at line 34 of file mapper.py.

                                   :
        """construct a new mapper.  
        
        All arguments may be sent to the sqlalchemy.orm.mapper() function where they are
        passed through to here.
        
        class_ - the class to be mapped.
        
        local_table - the table to which the class is mapped, or None if this mapper inherits
        from another mapper using concrete table inheritance.
        
        properties - a dictionary mapping the string names of object attributes to MapperProperty 
        instances, which define the persistence behavior of that attribute.  Note that the columns in the 
        mapped table are automatically converted into ColumnProperty instances based on the "key" 
        property of each Column (although they can be overridden using this dictionary).
        
        primary_key - a list of Column objects which define the "primary key" to be used against this mapper's
        selectable unit.  This is normally simply the primary key of the "local_table", but can be overridden here.
        
        non_primary - construct a Mapper that will define only the selection of instances, not their persistence.
        
        inherits - another Mapper for which this Mapper will have an inheritance relationship with.
        
        inherit_condition - for joined table inheritance, a SQL expression (constructed ClauseElement) which 
        will define how the two tables are joined; defaults to a natural join between the two tables.
        
        extension - a MapperExtension instance or list of MapperExtension instances which will be applied to
        all operations by this Mapper.
        
        order_by - a single Column or list of Columns for which selection operations should use as the default
        ordering for entities.  Defaults to the OID/ROWID of the table if any, or the first primary key column of the table.
        
        allow_column_override - if True, allows association relationships to be set up which override the usage of 
        a column that is on the table (based on key/attribute name).
        
        entity_name - a name to be associated with the class, to allow alternate mappings for a single class.
        
        always_refresh - if True, all query operations for this mapped class will overwrite all data
        within object instances that already exist within the session, erasing any in-memory changes with whatever
        information was loaded from the database.
        
        version_id_col - a Column which must have an integer type that will be used to keep a running "version id" of
        mapped entities in the database.  this is used during save operations to insure that no other thread or process
        has updated the instance during the lifetime of the entity, else a ConcurrentModificationError exception is thrown.
        
        polymorphic_on - used with mappers in an inheritance relationship, a Column which will identify the class/mapper
        combination to be used with a particular row.  requires the polymorphic_identity value to be set for all mappers
        in the inheritance hierarchy.
        
        _polymorphic_map - used internally to propigate the full map of polymorphic identifiers to surrogate mappers.
        
        polymorphic_identity - a value which will be stored in the Column denoted by polymorphic_on, corresponding to the
        "class identity" of this mapper.
        
        concrete - if True, indicates this mapper should use concrete table inheritance with its parent mapper.
        
        select_table - a Table or (more commonly) Selectable which will be used to select instances of this mapper's class.
        usually used to provide polymorphic loading among several classes in an inheritance hierarchy.
        
        allow_null_pks - indicates that composite primary keys where one or more (but not all) columns contain NULL is a valid
        primary key.  Primary keys which contain NULL values usually indicate that a result row does not contain an entity
        and should be skipped.
        
        batch - indicates that save operations of multiple entities can be batched together for efficiency.  
        setting to False indicates that an instance will be fully saved before saving the next instance, which 
        includes inserting/updating all table rows corresponding to the entity as well as calling all MapperExtension 
        methods corresponding to the save operation.

        column_prefix - a string which will be prepended to the "key" name of all Columns when creating column-based
        properties from the given Table.  does not affect explicitly specified column-based properties
        """
        if not issubclass(class_, object):
            raise exceptions.ArgumentError("Class '%s' is not a new-style class" % class_.__name__)

        for table in (local_table, select_table):
            if table is not None and isinstance(table, sql._SelectBaseMixin):
                # some db's, noteably postgres, dont want to select from a select
                # without an alias.  also if we make our own alias internally, then
                # the configured properties on the mapper are not matched against the alias 
                # we make, theres workarounds but it starts to get really crazy (its crazy enough
                # the SQL that gets generated) so just require an alias
                raise exceptions.ArgumentError("Mapping against a Select object requires that it has a name.  Use an alias to give it a name, i.e. s = select(...).alias('myselect')")

        self.class_ = class_
        self.entity_name = entity_name
        self.class_key = ClassKey(class_, entity_name)
        self.primary_key = primary_key
        self.non_primary = non_primary
        self.order_by = order_by
        self.always_refresh = always_refresh
        self.version_id_col = version_id_col
        self.concrete = concrete
        self.single = False
        self.inherits = inherits
        self.select_table = select_table
        self.local_table = local_table
        self.inherit_condition = inherit_condition
        self.extension = extension
        self.properties = properties or {}
        self.allow_column_override = allow_column_override
        self.allow_null_pks = allow_null_pks
        self.delete_orphans = []
        self.batch = batch
        self.column_prefix = column_prefix
        # a Column which is used during a select operation to retrieve the 
        # "polymorphic identity" of the row, which indicates which Mapper should be used
        # to construct a new object instance from that row.
        self.polymorphic_on = polymorphic_on
        self._eager_loaders = util.Set()
        
        # our 'polymorphic identity', a string name that when located in a result set row
        # indicates this Mapper should be used to construct the object instance for that row.
        self.polymorphic_identity = polymorphic_identity
        
        # a dictionary of 'polymorphic identity' names, associating those names with
        # Mappers that will be used to construct object instances upon a select operation.
        if _polymorphic_map is None:
            self.polymorphic_map = {}
        else:
            self.polymorphic_map = _polymorphic_map

        class LOrderedProp(util.OrderedProperties):
            """this extends OrderedProperties to trigger a compile() before the
            members of the object are accessed."""
            def _get_data(s):
                self.compile()
                return s.__dict__['_OrderedProperties__data']
            _OrderedProperties__data = property(_get_data)
                
        self.columns = LOrderedProp()
        self.c = self.columns
                
        # each time the options() method is called, the resulting Mapper is
        # stored in this dictionary based on the given options for fast re-access
        self._options = {}
        
        # a set of all mappers which inherit from this one.
        self._inheriting_mappers = util.Set()
        
        # a second mapper that is used for selecting, if the "select_table" argument
        # was sent to this mapper.
        self.__surrogate_mapper = None
        
        # whether or not our compile() method has been called already.
        self.__is_compiled = False

        # if this mapper is to be a primary mapper (i.e. the non_primary flag is not set),
        # associate this Mapper with the given class_ and entity name.  subsequent
        # calls to class_mapper() for the class_/entity name combination will return this 
        # mapper.
        self._compile_class()

        self.__log("constructed")
        
        # uncomment to compile at construction time (the old way)
        # this will break mapper setups that arent declared in the order
        # of dependency
        #self.compile()
    
    def __log(self, msg):


Generated by  Doxygen 1.6.0   Back to index