aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/SQLAlchemy-0.7.0-py2.7-linux-x86_64.egg/sqlalchemy/orm/sync.py
blob: 5ebd44fb9719adff46375e9abc51c90a3c3dbe69 (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
# orm/sync.py
# Copyright (C) 2005-2011 the SQLAlchemy authors and contributors <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""private module containing functions used for copying data 
between instances based on join conditions.
"""

from sqlalchemy.orm import exc, util as mapperutil, attributes

def populate(source, source_mapper, dest, dest_mapper, 
                        synchronize_pairs, uowcommit, flag_cascaded_pks):
    source_dict = source.dict
    dest_dict = dest.dict

    for l, r in synchronize_pairs:
        try:
            # inline of source_mapper._get_state_attr_by_column
            prop = source_mapper._columntoproperty[l]
            value = source.manager[prop.key].impl.get(source, source_dict, 
                                                    attributes.PASSIVE_OFF)
        except exc.UnmappedColumnError:
            _raise_col_to_prop(False, source_mapper, l, dest_mapper, r)

        try:
            # inline of dest_mapper._set_state_attr_by_column
            prop = dest_mapper._columntoproperty[r]
            dest.manager[prop.key].impl.set(dest, dest_dict, value, None)
        except exc.UnmappedColumnError:
            _raise_col_to_prop(True, source_mapper, l, dest_mapper, r)

        # technically the "r.primary_key" check isn't
        # needed here, but we check for this condition to limit
        # how often this logic is invoked for memory/performance
        # reasons, since we only need this info for a primary key
        # destination.
        if flag_cascaded_pks and l.primary_key and \
                    r.primary_key and \
                    r.references(l):
            uowcommit.attributes[("pk_cascaded", dest, r)] = True

def clear(dest, dest_mapper, synchronize_pairs):
    for l, r in synchronize_pairs:
        if r.primary_key:
            raise AssertionError(
                                "Dependency rule tried to blank-out primary key "
                                "column '%s' on instance '%s'" % 
                                (r, mapperutil.state_str(dest))
                            )
        try:
            dest_mapper._set_state_attr_by_column(dest, dest.dict, r, None)
        except exc.UnmappedColumnError:
            _raise_col_to_prop(True, None, l, dest_mapper, r)

def update(source, source_mapper, dest, old_prefix, synchronize_pairs):
    for l, r in synchronize_pairs:
        try:
            oldvalue = source_mapper._get_committed_attr_by_column(source.obj(), l)
            value = source_mapper._get_state_attr_by_column(source, source.dict, l)
        except exc.UnmappedColumnError:
            _raise_col_to_prop(False, source_mapper, l, None, r)
        dest[r.key] = value
        dest[old_prefix + r.key] = oldvalue

def populate_dict(source, source_mapper, dict_, synchronize_pairs):
    for l, r in synchronize_pairs:
        try:
            value = source_mapper._get_state_attr_by_column(source, source.dict, l)
        except exc.UnmappedColumnError:
            _raise_col_to_prop(False, source_mapper, l, None, r)

        dict_[r.key] = value

def source_modified(uowcommit, source, source_mapper, synchronize_pairs):
    """return true if the source object has changes from an old to a 
    new value on the given synchronize pairs

    """
    for l, r in synchronize_pairs:
        try:
            prop = source_mapper._columntoproperty[l]
        except exc.UnmappedColumnError:
            _raise_col_to_prop(False, source_mapper, l, None, r)
        history = uowcommit.get_attribute_history(source, prop.key, 
                                        attributes.PASSIVE_NO_INITIALIZE)
        return bool(history.deleted)
    else:
        return False

def _raise_col_to_prop(isdest, source_mapper, source_column, dest_mapper, dest_column):
    if isdest:
        raise exc.UnmappedColumnError(
                                "Can't execute sync rule for destination column '%s'; "
                                "mapper '%s' does not map this column.  Try using an explicit"
                                " `foreign_keys` collection which does not include this column "
                                "(or use a viewonly=True relation)." % (dest_column, dest_mapper)
                                )
    else:
        raise exc.UnmappedColumnError(
                                "Can't execute sync rule for source column '%s'; mapper '%s' "
                                "does not map this column.  Try using an explicit `foreign_keys`"
                                " collection which does not include destination column '%s' (or "
                                "use a viewonly=True relation)." % 
                                (source_column, source_mapper, dest_column)
                                )