Changeset 347 for elixir/trunk/elixir/properties.py
- Timestamp:
- 06/19/08 15:35:21 (5 years ago)
- Files:
-
- 1 modified
-
elixir/trunk/elixir/properties.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/elixir/properties.py
r327 r347 1 1 ''' 2 2 This module provides support for defining properties on your entities. It both 3 provides, the `Property` class which acts as a building block for common 4 properties such as fields and relationships (for those, please consult the 5 corresponding modules), but also provides some more specialized properties, 6 such as `ColumnProperty` and `Synonym`. It also provides the GenericProperty 7 class which allows you to wrap any SQLAlchemy property, and its DSL-syntax 3 provides, the `Property` class which acts as a building block for common 4 properties such as fields and relationships (for those, please consult the 5 corresponding modules), but also provides some more specialized properties, 6 such as `ColumnProperty` and `Synonym`. It also provides the GenericProperty 7 class which allows you to wrap any SQLAlchemy property, and its DSL-syntax 8 8 equivalent: has_property_. 9 9 10 10 `has_property` 11 11 -------------- 12 The ``has_property`` statement allows you to define properties which rely on 12 The ``has_property`` statement allows you to define properties which rely on 13 13 their entity's table (and columns) being defined before they can be declared 14 14 themselves. The `has_property` statement takes two arguments: first the name of … … 16 16 lambda) taking one argument and returning the desired SQLAlchemy property. That 17 17 function will be called whenever the entity table is completely defined, and 18 will be given the .c attribute of the entity as argument (as a way to access 18 will be given the .c attribute of the entity as argument (as a way to access 19 19 the entity columns). 20 20 … … 26 26 has_field('quantity', Float) 27 27 has_field('unit_price', Float) 28 has_property('price', 28 has_property('price', 29 29 lambda c: column_property( 30 30 (c.quantity * c.unit_price).label('price'))) … … 34 34 from sqlalchemy.orm import column_property, synonym 35 35 36 __doc_all__ = ['EntityBuilder', 'Property', 'GenericProperty', 36 __doc_all__ = ['EntityBuilder', 'Property', 'GenericProperty', 37 37 'ColumnProperty'] 38 38 39 39 class EntityBuilder(object): 40 40 ''' 41 Abstract base class for all entity builders. An Entity builder is a class 42 of objects which can be added to an Entity (usually by using special 41 Abstract base class for all entity builders. An Entity builder is a class 42 of objects which can be added to an Entity (usually by using special 43 43 properties or statements) to "build" that entity. Building an entity, 44 44 meaning to add columns to its "main" table, create other tables, add … … 66 66 67 67 def create_properties(self): 68 pass 68 pass 69 69 70 70 def before_mapper(self): 71 pass 71 pass 72 72 73 73 def after_mapper(self): 74 pass 74 pass 75 75 76 76 def finalize(self): 77 pass 77 pass 78 78 79 79 … … 98 98 ''' 99 99 __metaclass__ = CounterMeta 100 100 101 101 def __init__(self, *args, **kwargs): 102 102 self.entity = None … … 135 135 (c.quantity * c.unit_price).label('price'))) 136 136 ''' 137 137 138 138 def __init__(self, prop): 139 139 super(GenericProperty, self).__init__() … … 154 154 class ColumnProperty(GenericProperty): 155 155 ''' 156 A specialized form of the GenericProperty to generate SQLAlchemy 157 ``column_property``'s. 158 159 It takes a single argument, which is a function (often 160 given as an anonymous lambda) taking one argument and returning the 156 A specialized form of the GenericProperty to generate SQLAlchemy 157 ``column_property``'s. 158 159 It takes a single argument, which is a function (often 160 given as an anonymous lambda) taking one argument and returning the 161 161 desired (scalar-returning) SQLAlchemy ClauseElement. That function will be 162 called whenever the entity table is completely defined, and will be given 163 the .c attribute of the entity as argument (as a way to access the entity 162 called whenever the entity table is completely defined, and will be given 163 the .c attribute of the entity as argument (as a way to access the entity 164 164 columns). The ColumnProperty will first wrap your ClauseElement in a label 165 165 with the same name as the property, then wrap that in a column_property. … … 172 172 price = ColumnProperty(lambda c: c.quantity * c.unit_price) 173 173 174 Please look at the `corresponding SQLAlchemy 174 Please look at the `corresponding SQLAlchemy 175 175 documentation <http://www.sqlalchemy.org/docs/04/mappers.html 176 176 #advdatamapping_mapper_expressions>`_ for details. … … 184 184 ''' 185 185 This class represents a synonym property of another property (column, ...) 186 of an entity. As opposed to the `synonym` kwarg to the Field class (which 187 share the same goal), this class can be used to define a synonym of a 186 of an entity. As opposed to the `synonym` kwarg to the Field class (which 187 share the same goal), this class can be used to define a synonym of a 188 188 property defined in a parent class (of the current class). On the other 189 hand, it cannot define a synonym for the purpose of using a standard python 189 hand, it cannot define a synonym for the purpose of using a standard python 190 190 property in queries. See the Field class for details on that usage. 191 191
