~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/orm/sync.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# orm/sync.py
2
 
# Copyright (C) 2005-2012 the SQLAlchemy authors and contributors <see AUTHORS file>
 
2
# Copyright (C) 2005-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
3
3
#
4
4
# This module is part of SQLAlchemy and is released under
5
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
9
9
 
10
10
"""
11
11
 
12
 
from sqlalchemy.orm import exc, util as mapperutil, attributes
 
12
from . import exc, util as orm_util, attributes
 
13
 
13
14
 
14
15
def populate(source, source_mapper, dest, dest_mapper,
15
16
                        synchronize_pairs, uowcommit, flag_cascaded_pks):
42
43
                    r.references(l):
43
44
            uowcommit.attributes[("pk_cascaded", dest, r)] = True
44
45
 
 
46
 
45
47
def clear(dest, dest_mapper, synchronize_pairs):
46
48
    for l, r in synchronize_pairs:
47
49
        if r.primary_key:
48
50
            raise AssertionError(
49
 
                                "Dependency rule tried to blank-out primary key "
50
 
                                "column '%s' on instance '%s'" %
51
 
                                (r, mapperutil.state_str(dest))
52
 
                            )
 
51
                "Dependency rule tried to blank-out primary key "
 
52
                "column '%s' on instance '%s'" %
 
53
                (r, orm_util.state_str(dest))
 
54
            )
53
55
        try:
54
56
            dest_mapper._set_state_attr_by_column(dest, dest.dict, r, None)
55
57
        except exc.UnmappedColumnError:
56
58
            _raise_col_to_prop(True, None, l, dest_mapper, r)
57
59
 
 
60
 
58
61
def update(source, source_mapper, dest, old_prefix, synchronize_pairs):
59
62
    for l, r in synchronize_pairs:
60
63
        try:
61
 
            oldvalue = source_mapper._get_committed_attr_by_column(source.obj(), l)
62
 
            value = source_mapper._get_state_attr_by_column(source, source.dict, l)
 
64
            oldvalue = source_mapper._get_committed_attr_by_column(
 
65
                source.obj(), l)
 
66
            value = source_mapper._get_state_attr_by_column(
 
67
                source, source.dict, l)
63
68
        except exc.UnmappedColumnError:
64
69
            _raise_col_to_prop(False, source_mapper, l, None, r)
65
70
        dest[r.key] = value
66
71
        dest[old_prefix + r.key] = oldvalue
67
72
 
 
73
 
68
74
def populate_dict(source, source_mapper, dict_, synchronize_pairs):
69
75
    for l, r in synchronize_pairs:
70
76
        try:
71
 
            value = source_mapper._get_state_attr_by_column(source, source.dict, l)
 
77
            value = source_mapper._get_state_attr_by_column(
 
78
                source, source.dict, l)
72
79
        except exc.UnmappedColumnError:
73
80
            _raise_col_to_prop(False, source_mapper, l, None, r)
74
81
 
75
82
        dict_[r.key] = value
76
83
 
 
84
 
77
85
def source_modified(uowcommit, source, source_mapper, synchronize_pairs):
78
86
    """return true if the source object has changes from an old to a
79
87
    new value on the given synchronize pairs
86
94
            _raise_col_to_prop(False, source_mapper, l, None, r)
87
95
        history = uowcommit.get_attribute_history(source, prop.key,
88
96
                                        attributes.PASSIVE_NO_INITIALIZE)
89
 
        return bool(history.deleted)
 
97
        if bool(history.deleted):
 
98
            return True
90
99
    else:
91
100
        return False
92
101
 
93
 
def _raise_col_to_prop(isdest, source_mapper, source_column, dest_mapper, dest_column):
 
102
 
 
103
def _raise_col_to_prop(isdest, source_mapper, source_column,
 
104
                       dest_mapper, dest_column):
94
105
    if isdest:
95
 
        raise exc.UnmappedColumnError(
96
 
                                "Can't execute sync rule for destination column '%s'; "
97
 
                                "mapper '%s' does not map this column.  Try using an explicit"
98
 
                                " `foreign_keys` collection which does not include this column "
99
 
                                "(or use a viewonly=True relation)." % (dest_column, dest_mapper)
100
 
                                )
 
106
        raise exc.UnmappedColumnError("Can't execute sync rule for "
 
107
                "destination column '%s'; mapper '%s' does not map "
 
108
                "this column.  Try using an explicit `foreign_keys` "
 
109
                "collection which does not include this column (or use "
 
110
                "a viewonly=True relation)." % (dest_column,
 
111
                dest_mapper))
101
112
    else:
102
 
        raise exc.UnmappedColumnError(
103
 
                                "Can't execute sync rule for source column '%s'; mapper '%s' "
104
 
                                "does not map this column.  Try using an explicit `foreign_keys`"
105
 
                                " collection which does not include destination column '%s' (or "
106
 
                                "use a viewonly=True relation)." %
107
 
                                (source_column, source_mapper, dest_column)
108
 
                                )
 
113
        raise exc.UnmappedColumnError("Can't execute sync rule for "
 
114
                "source column '%s'; mapper '%s' does not map this "
 
115
                "column.  Try using an explicit `foreign_keys` "
 
116
                "collection which does not include destination column "
 
117
                "'%s' (or use a viewonly=True relation)."
 
118
                % (source_column, source_mapper, dest_column))