~edoardo-serra/storm/select-for-update-support

« back to all changes in this revision

Viewing changes to storm/store.py

  • Committer: Edoardo Serra
  • Date: 2010-09-09 15:07:13 UTC
  • Revision ID: eserra@barbera-20100909150713-3f8v0gxosfcsrv4d
SELECT ... FOR UPDATE implemented in Store.get

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
        self.invalidate()
138
138
        self._connection.rollback()
139
139
 
140
 
    def get(self, cls, key):
 
140
    def get(self, cls, key, for_update=False):
141
141
        """Get object of type cls with the given primary key from the database.
142
142
 
143
143
        If the object is alive the database won't be touched.
144
144
 
 
145
        If the FOR UPDATE clause is used, the query is always executed
 
146
 
145
147
        @param cls: Class of the object to be retrieved.
146
148
        @param key: Primary key of object. May be a tuple for composed keys.
 
149
        @param for_update: Indicates wether using the clause FOR UPDATE
147
150
 
148
151
        @return: The object found with the given primary key, or None
149
152
            if no object is found.
165
168
                variable = column.variable_factory(value=variable)
166
169
            primary_vars.append(variable)
167
170
 
168
 
        primary_values = tuple(var.get(to_db=True) for var in primary_vars)
169
 
        obj_info = self._alive.get((cls_info.cls, primary_values))
170
 
        if obj_info is not None:
171
 
            if obj_info.get("invalidated"):
172
 
                try:
173
 
                    self._validate_alive(obj_info)
174
 
                except LostObjectError:
175
 
                    return None
176
 
            return self._get_object(obj_info)
 
171
        if not for_update:
 
172
            primary_values = tuple(var.get(to_db=True) for var in primary_vars)
 
173
            obj_info = self._alive.get((cls_info.cls, primary_values))
 
174
            if obj_info is not None:
 
175
                if obj_info.get("invalidated"):
 
176
                    try:
 
177
                        self._validate_alive(obj_info)
 
178
                    except LostObjectError:
 
179
                        return None
 
180
                return self._get_object(obj_info)
177
181
 
178
182
        where = compare_columns(cls_info.primary_key, primary_vars)
179
183
 
180
184
        select = Select(cls_info.columns, where,
181
 
                        default_tables=cls_info.table, limit=1)
 
185
                        default_tables=cls_info.table, limit=1,
 
186
                        for_update=for_update)
182
187
 
183
188
        result = self._connection.execute(select)
184
189
        values = result.get_one()