summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/descriptor_props.py
blob: 4c4bc821fb86e3aae5e615dfe5e66bd354375c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# descriptor_props.py
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Michael Bayer
# mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""Descriptor proprerties are more "auxilliary" properties
that exist as configurational elements, but don't participate
as actively in the load/persist ORM loop.   They all
build on the "hybrid" extension to produce class descriptors.

"""

from sqlalchemy.orm.interfaces import \
    MapperProperty, PropComparator, StrategizedProperty
from sqlalchemy.orm import attributes
from sqlalchemy import util, sql, exc as sa_exc, event
from sqlalchemy.sql import expression
properties = util.importlater('sqlalchemy.orm', 'properties')

class DescriptorProperty(MapperProperty):
    """:class:`MapperProperty` which proxies access to a 
        user-defined descriptor."""

    def instrument_class(self, mapper):
        from sqlalchemy.ext import hybrid
        
        prop = self
        
        # hackety hack hack
        class _ProxyImpl(object):
            accepts_scalar_loader = False
            expire_missing = True

            def __init__(self, key):
                self.key = key
            
            if hasattr(prop, 'get_history'):
                def get_history(self, state, dict_, **kw):
                    return prop.get_history(state, dict_, **kw)
                
        if self.descriptor is None:
            desc = getattr(mapper.class_, self.key, None)
            if mapper._is_userland_descriptor(desc):
                self.descriptor = desc

        if self.descriptor is None:
            def fset(obj, value):
                setattr(obj, self.name, value)
            def fdel(obj):
                delattr(obj, self.name)
            def fget(obj):
                return getattr(obj, self.name)
            fget.__doc__ = self.doc

            descriptor = hybrid.property_(
                fget=fget,
                fset=fset,
                fdel=fdel,
            )
        elif isinstance(self.descriptor, property):
            descriptor = hybrid.property_(
                fget=self.descriptor.fget,
                fset=self.descriptor.fset,
                fdel=self.descriptor.fdel,
            )
        else:
            descriptor = hybrid.property_(
                fget=self.descriptor.__get__,
                fset=self.descriptor.__set__,
                fdel=self.descriptor.__delete__,
            )
        
        proxy_attr = attributes.\
                    create_proxied_attribute(self.descriptor or descriptor)\
                    (
                        self.parent.class_,
                        self.key, 
                        self.descriptor or descriptor,
                        lambda: self._comparator_factory(mapper)
                    )
        def get_comparator(owner):
            return util.update_wrapper(proxy_attr, descriptor)
        descriptor.expr = get_comparator
        descriptor.impl = _ProxyImpl(self.key)
        mapper.class_manager.instrument_attribute(self.key, descriptor)
    

class CompositeProperty(DescriptorProperty):
    
    def __init__(self, class_, *columns, **kwargs):
        self.columns = columns
        self.composite_class = class_
        self.active_history = kwargs.get('active_history', False)
        self.deferred = kwargs.get('deferred', False)
        self.group = kwargs.get('group', None)
        util.set_creation_order(self)
        self._create_descriptor()
        
    def do_init(self):
        """Initialization which occurs after the :class:`.CompositeProperty` 
        has been associated with its parent mapper.
        
        """
        self._setup_arguments_on_columns()
        self._setup_event_handlers()
    
    def _create_descriptor(self):
        """Create the actual Python descriptor that will serve as 
        the access point on the mapped class.
        
        """

        def fget(instance):
            dict_ = attributes.instance_dict(instance)
            if self.key in dict_:
                return dict_[self.key]
            else:
                dict_[self.key] = composite = self.composite_class(
                    *[getattr(instance, key) for key in self._attribute_keys]
            )
                return composite
                
        def fset(instance, value):
            if value is None:
                fdel(instance)
            else:
                dict_ = attributes.instance_dict(instance)
                dict_[self.key] = value
                for key, value in zip(
                        self._attribute_keys, 
                        value.__composite_values__()):
                    setattr(instance, key, value)
        
        def fdel(instance):
            for key in self._attribute_keys:
                setattr(instance, key, None)
        
        self.descriptor = property(fget, fset, fdel)
        
    def _setup_arguments_on_columns(self):
        """Propigate configuration arguments made on this composite
        to the target columns, for those that apply.
        
        """
        for col in self.columns:
            prop = self.parent._columntoproperty[col]
            prop.active_history = self.active_history
            if self.deferred:
                prop.deferred = self.deferred
                prop.strategy_class = strategies.DeferredColumnLoader
            prop.group = self.group

    def _setup_event_handlers(self):
        """Establish events that will clear out the composite value
        whenever changes in state occur on the target columns.
        
        """
        def load_handler(state):
            state.dict.pop(self.key, None)
            
        def expire_handler(state, keys):
            if keys is None or set(self._attribute_keys).intersection(keys):
                state.dict.pop(self.key, None)
        
        def insert_update_handler(mapper, connection, state):
            state.dict.pop(self.key, None)
            
        event.listen(self.parent, 'on_after_insert', 
                                    insert_update_handler, raw=True)
        event.listen(self.parent, 'on_after_update', 
                                    insert_update_handler, raw=True)
        event.listen(self.parent, 'on_load', load_handler, raw=True)
        event.listen(self.parent, 'on_refresh', load_handler, raw=True)
        event.listen(self.parent, "on_expire", expire_handler, raw=True)
        
        # TODO:  add listeners to the column attributes, which 
        # refresh the composite based on userland settings.
        
        # TODO: add a callable to the composite of the form
        # _on_change(self, attrname) which will send up a corresponding
        # refresh to the column attribute on all parents.  Basically
        # a specialization of the scalars.py example.
        
        
    @util.memoized_property
    def _attribute_keys(self):
        return [
            self.parent._columntoproperty[col].key
            for col in self.columns
        ]
        
    def get_history(self, state, dict_, **kw):
        """Provided for userland code that uses attributes.get_history()."""
        
        added = []
        deleted = []
        
        has_history = False
        for col in self.columns:
            key = self.parent._columntoproperty[col].key
            hist = state.manager[key].impl.get_history(state, dict_)
            if hist.has_changes():
                has_history = True
            
            added.extend(hist.non_deleted())
            if hist.deleted:
                deleted.extend(hist.deleted)
            else:
                deleted.append(None)
        
        if has_history:
            return attributes.History(
                [self.composite_class(*added)],
                (),
                [self.composite_class(*deleted)]
            )
        else:
            return attributes.History(
                (),[self.composite_class(*added)], ()
            )

    def _comparator_factory(self, mapper):
        return CompositeProperty.Comparator(self)

    class Comparator(PropComparator):
        def __init__(self, prop, adapter=None):
            self.prop = prop
            self.adapter = adapter
            
        def __clause_element__(self):
            if self.adapter:
                # TODO: test coverage for adapted composite comparison
                return expression.ClauseList(
                            *[self.adapter(x) for x in self.prop.columns])
            else:
                return expression.ClauseList(*self.prop.columns)
        
        __hash__ = None
        
        def __eq__(self, other):
            if other is None:
                values = [None] * len(self.prop.columns)
            else:
                values = other.__composite_values__()
            return sql.and_(
                    *[a==b for a, b in zip(self.prop.columns, values)])
            
        def __ne__(self, other):
            return sql.not_(self.__eq__(other))

    def __str__(self):
        return str(self.parent.class_.__name__) + "." + self.key

class ConcreteInheritedProperty(DescriptorProperty):
    """A 'do nothing' :class:`MapperProperty` that disables 
    an attribute on a concrete subclass that is only present
    on the inherited mapper, not the concrete classes' mapper.

    Cases where this occurs include:

    * When the superclass mapper is mapped against a 
      "polymorphic union", which includes all attributes from 
      all subclasses.
    * When a relationship() is configured on an inherited mapper,
      but not on the subclass mapper.  Concrete mappers require
      that relationship() is configured explicitly on each 
      subclass. 

    """

    def _comparator_factory(self, mapper):
        comparator_callable = None
        
        for m in self.parent.iterate_to_root():
            p = m._props[self.key]
            if not isinstance(p, ConcreteInheritedProperty):
                comparator_callable = p.comparator_factory
                break
        return comparator_callable
    
    def __init__(self):
        def warn():
            raise AttributeError("Concrete %s does not implement "
                "attribute %r at the instance level.  Add this "
                "property explicitly to %s." % 
                (self.parent, self.key, self.parent))

        class NoninheritedConcreteProp(object):
            def __set__(s, obj, value):
                warn()
            def __delete__(s, obj):
                warn()
            def __get__(s, obj, owner):
                if obj is None:
                    return self.descriptor
                warn()
        self.descriptor = NoninheritedConcreteProp()
        
        
class SynonymProperty(DescriptorProperty):

    def __init__(self, name, map_column=None, 
                            descriptor=None, comparator_factory=None,
                            doc=None):
        self.name = name
        self.map_column = map_column
        self.descriptor = descriptor
        self.comparator_factory = comparator_factory
        self.doc = doc or (descriptor and descriptor.__doc__) or None
        util.set_creation_order(self)

    def _comparator_factory(self, mapper):
        prop = getattr(mapper.class_, self.name).property

        if self.comparator_factory:
            comp = self.comparator_factory(prop, mapper)
        else:
            comp = prop.comparator_factory(prop, mapper)
        return comp

    def set_parent(self, parent, init):
        if self.map_column:
            # implement the 'map_column' option.
            if self.key not in parent.mapped_table.c:
                raise sa_exc.ArgumentError(
                    "Can't compile synonym '%s': no column on table "
                    "'%s' named '%s'" 
                     % (self.name, parent.mapped_table.description, self.key))
            elif parent.mapped_table.c[self.key] in \
                    parent._columntoproperty and \
                    parent._columntoproperty[
                                            parent.mapped_table.c[self.key]
                                        ].key == self.name:
                raise sa_exc.ArgumentError(
                    "Can't call map_column=True for synonym %r=%r, "
                    "a ColumnProperty already exists keyed to the name "
                    "%r for column %r" % 
                    (self.key, self.name, self.name, self.key)
                )
            p = properties.ColumnProperty(parent.mapped_table.c[self.key])
            parent._configure_property(
                                    self.name, p, 
                                    init=init, 
                                    setparent=True)
            p._mapped_by_synonym = self.key
    
        self.parent = parent
        
class ComparableProperty(DescriptorProperty):
    """Instruments a Python property for use in query expressions."""

    def __init__(self, comparator_factory, descriptor=None, doc=None):
        self.descriptor = descriptor
        self.comparator_factory = comparator_factory
        self.doc = doc or (descriptor and descriptor.__doc__) or None
        util.set_creation_order(self)

    def _comparator_factory(self, mapper):
        return self.comparator_factory(self, mapper)