def update(self, item): item_id = self._get_column_value(item, self._id_column) item_name = self._get_column_value(item, self._name_column) if not item_id: msg = 'Cannot update %s with no id' % self._type_name raise ValueError(msg) if not item_id in self._by_id: msg = 'Cannot update non-existent %s with id %s' msg = msg % (self._type_name, item_id) raise ValueError(msg) old_item_name = self._by_id[item_id].name values = dict((x, self._get_column_value(item, x)) \ for x in self._columns[1:]) id_criteria = { self._id_column : item_id } execute_update(self._db, self._table_name, values, id_criteria) with execute_select(self._db, self._table_name, self._columns, id_criteria, self._create_item) as results: new_item = next(results) for (n, item) in enumerate(self._all): if self._get_column_value(item, self._id_column) == item_id: self._all[n] = new_item break self._by_id[item_id] = new_item if item_name != old_item_name: del self._by_name[old_item_name] self._by_name[item_name] = new_item
def add(self, item): item_id = self._get_column_value(item, self._id_column) item_name = self._get_column_value(item, self._name_column) if item_id: msg = '%s with id %s already exists' % (self._type_name, item_id) raise ValueError(msg) if item_name in self._by_name: print [repr(x) for x in self._by_name] msg = '%s with name "%s" already exists' % (self._type_name, item_name) raise ValueError(msg) values = dict((x, self._get_column_value(item, x)) \ for x in self._columns[1:]) item_id = next_item_id(self._db, self._table_name) values[self._id_column] = item_id execute_insert(self._db, self._table_name, values) with execute_select(self._db, self._table_name, self._columns, { self._id_column : item_id }, self._create_item) as results: new_item = next(results) self._all.append(new_item) self._by_id[item_id] = new_item item_name = self._get_column_value(new_item, self._name_column) self._by_name[item_name] = new_item return new_item
def with_id(self, id): with execute_select(self._db, self._table_name, self._columns, { self._id_column : id }, self._create_item) as results: try: return next(results) except StopIteration: return None
def __init__(self, type_name, db, table_name, columns, item_constructor, column_value_extractor): def id_for(x): return self._get_column_value(x, columns[0]) def name_for(x): return self._get_column_value(x, columns[1]) self._type_name = type_name self._db = db self._table_name = table_name self._columns = columns self._id_column = columns[0] self._name_column = columns[1] self._create_item = item_constructor self._get_column_value = column_value_extractor with execute_select(db, table_name, columns, { }, item_constructor) as results: self._all = [ x for x in results ] self._by_id = dict((id_for(x), x) for x in self._all) self._by_name = dict((name_for(x), x) for x in self._all)
def retrieve(self, **criteria): return execute_select(self._db, self._table_name, self._columns, self._normalize_criteria(criteria), self._create_item)
def all(self): with execute_select(self._db, self._table_name, self._columns, { }, self._create_item) as items: return [ x for x in items ]