~jbaker/storm/nose_plugin

« back to all changes in this revision

Viewing changes to storm/store.py

  • Committer: Gustavo Niemeyer
  • Date: 2006-06-01 02:42:53 UTC
  • Revision ID: gustavo@niemeyer.net-20060601024253-a75ab5c2391b237d
- Created exception hierarchy, and patched DBAPI2 modules to include
  Storm exceptions as bases for their exceptions.
- Added new finer-grained exceptions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
from storm.expr import Column, Count, Max, Min, Avg, Sum, Eq, Expr, And
15
15
from storm.expr import compile_python, compare_columns, CompileError
16
16
from storm.variables import Variable
 
17
from storm.exceptions import (
 
18
    InStoreError, NotInStoreError, NotFlushedError, OrderLoopError, SetError)
17
19
from storm import Undef
18
20
 
19
21
 
20
 
__all__ = ["Store", "StoreError", "ResultSet"]
21
 
 
22
 
 
23
 
class StoreError(Exception):
24
 
    pass
 
22
__all__ = ["Store"]
25
23
 
26
24
 
27
25
PENDING_ADD = 1
130
128
 
131
129
        store = obj_info.get("store")
132
130
        if store is not None and store is not self:
133
 
            raise StoreError("%r is part of another store" % obj)
 
131
            raise InStoreError("%r is part of another store" % obj)
134
132
 
135
133
        pending = obj_info.get("pending")
136
134
 
137
135
        if pending is PENDING_ADD:
138
 
            raise StoreError("%r is already scheduled to be added" % obj)
 
136
            raise InStoreError("%r is already scheduled to be added" % obj)
139
137
        elif pending is PENDING_REMOVE:
140
138
            del obj_info["pending"]
141
139
        else:
144
142
                obj_info["store"] = self
145
143
            else:
146
144
                if not self._is_ghost(obj):
147
 
                    raise StoreError("%r is already in the store" % obj)
 
145
                    raise InStoreError("%r is already in the store" % obj)
148
146
                self._set_alive(obj)
149
147
 
150
148
            obj_info["pending"] = PENDING_ADD
154
152
        obj_info = get_obj_info(obj)
155
153
 
156
154
        if obj_info.get("store") is not self:
157
 
            raise StoreError("%r is not in this store" % obj)
 
155
            raise NotInStoreError("%r is not in this store" % obj)
158
156
 
159
157
        pending = obj_info.get("pending")
160
158
 
161
159
        if pending is PENDING_REMOVE:
162
 
            raise StoreError("%r is already scheduled to be removed" % obj)
 
160
            raise NotInStoreError("%r is already scheduled to be removed"%obj)
163
161
        elif pending is PENDING_ADD:
164
162
            del obj_info["pending"]
165
163
            self._set_ghost(obj)
170
168
 
171
169
    def reload(self, obj):
172
170
        obj_info, cls_info = get_info(obj)
173
 
        if obj_info.get("store") is not self:
174
 
            raise StoreError("%r is not in this store" % obj)
 
171
        if obj_info.get("store") is not self or self._is_ghost(obj):
 
172
            raise NotInStoreError("%r is not in this store" % obj)
175
173
        if "primary_vars" not in obj_info:
176
 
            raise StoreError("Can't reload an object if it was never flushed")
 
174
            raise NotFlushedError("Can't reload an object if it was "
 
175
                                  "never flushed")
177
176
        where = compare_columns(cls_info.primary_key,
178
177
                                obj_info["primary_vars"])
179
178
        select = Select(cls_info.columns, where,
216
215
                else:
217
216
                    break # Found an item without dirty predecessors.
218
217
            else:
219
 
                raise StoreError("Can't flush due to ordering loop")
 
218
                raise OrderLoopError("Can't flush due to ordering loop")
220
219
            self._flush_one(obj)
221
220
 
222
221
        self._order.clear()
471
470
            if (not isinstance(expr, Eq) or
472
471
                not isinstance(expr.expr1, Column) or
473
472
                not isinstance(expr.expr2, (Column, Variable))):
474
 
                raise StoreError("Unsupported set expression: %r" % repr(expr))
 
473
                raise SetError("Unsupported set expression: %r" % repr(expr))
475
474
            changes[expr.expr1] = expr.expr2
476
475
 
477
476
        for key, value in kwargs.items():
480
479
                changes[column] = None
481
480
            elif isinstance(value, Expr):
482
481
                if not isinstance(value, Column):
483
 
                    raise StoreError("Unsupported set expression: %r" %
 
482
                    raise SetError("Unsupported set expression: %r" %
484
483
                                     repr(value))
485
484
                changes[column] = value
486
485
            else: