def __perform_updates__(self, update_cursor, select_after_update=False): if len(self.__changed_columns__) == 0: return else: info = {} for column, datatypes in self.__changed_columns__.items(): for dt in datatypes: if not info.has_key(column): update_expression = dt.update_expression(self) if update_expression is not None: info[column] = update_expression statement = sql.update(self.__relation__, self.__primary_key__.where(), info) update_cursor.execute(statement) if select_after_update: need_select = [] for column, dbprops in self.__changed_columns__.items(): dbprops = filter(lambda d: d.__select_after_insert__(self), dbprops) if len(dbprops) > 0: need_select.append((column, dbprops)) if len(need_select) > 0: columns = map(lambda (c, d): c, need_select) query = sql.select(columns, self.__relation__, self.__primary_key__.where()) update_cursor.execute(query) tpl = update_cursor.fetchone() for (column, dbprops), value in zip(need_select, tpl): for dbprop in dbprops: dbprop.__set_from_result__(self.__ds__(), self, value) # Clear the list of changed columns self.__changed_columns__.clear()
def __setitem__(self, key, value): key_column = self._sqldict.child_key_column.column for validator in self._sqldict.child_key_column.validators: validator.check(self._dbobj, self._sqldict.child_key_column, key) key_literal = self._sqldict.child_key_column.sql_literal_class( self._sqldict.child_key_column.__convert__(key)) value_column = self._sqldict.child_value_column.column for validator in self._sqldict.child_value_column.validators: validator.check(self._dbobj, self._sqldict.child_value_column, value) value_literal = self._sqldict.child_value_column.sql_literal_class( self._sqldict.child_value_column.__convert__(value)) if self.has_key(key): if self[key] != value: where = sql.where.and_(self._sqldict.child_where(self._dbobj), sql.where(key_column, "=", key_literal)) command = sql.update(self._sqldict.child_relation, where, { str(value_column): value_literal }) else: command = None else: command = sql.insert( self._sqldict.child_relation, ( self._sqldict.child_key, key_column, value_column, ), ( self._dbobj.__primary_key__.sql_literal(), key_literal, value_literal, ) ) if command is not None: self._dbobj.__ds__().execute(command) dict.__setitem__(self, key, value)