def select_one(conn, item_name, table_name): table = conn.load_table(table_name) result = table.find_one(name=item_name) # type: OrderedDict if result is None: raise ItemNotStoredError(f'can\'t read "{item_name}" becase it\'s not' f' stored in table "{table_name}"') return Item(**result)
def delete_one(conn, item_name, table_name): table = conn.load_table(table_name) result = table.find_one(name=item_name) if result is None: raise ItemNotStoredError( f'can\'t delete {item_name} because it\'s not stored' f' in table "{table_name}"') table.delete(name=item_name)
def update_one(conn, item, table_name): table = conn.load_table(table_name) result = table.find_one(name=item.name) if result is None: raise ItemNotStoredError( f'can\'t update {item.name} because it\'s not stored' f' in table "{table_name}"') table.update(asdict(item), keys=['name'])
def select_one(conn, item_name, table_name): sql = ''' SELECT * FROM {} WHERE name=? '''.format(table_name) cursor = conn.execute(sql, (item_name, )) result = cursor.fetchone() if result is None: raise ItemNotStoredError(f'can\'t read "{item_name}" becase it\'s not' f' stored in table "{table_name}"') return Item(*result[1:]) # exclude row id
def delete_one(conn, item_name, table_name): sql_check = ''' SELECT EXISTS(SELECT 1 FROM {} WHERE name=? LIMIT 1) '''.format(table_name) sql_delete = ''' DELETE FROM {} WHERE name=? '''.format(table_name) cursor = conn.execute(sql_check, (item_name, )) result = cursor.fetchone() if result[0] == 0: raise ItemNotStoredError( f'can\'t delete {item_name} because it\'s not stored' f' in table "{table_name}"') cursor.execute(sql_delete, (item_name, )) conn.commit()
def update_one(conn, item, table_name): sql_check = ''' SELECT EXISTS(SELECT 1 FROM {} WHERE name=? LIMIT 1) '''.format(table_name) sql_update = ''' UPDATE {} SET price=?, quantity=? WHERE name=? '''.format(table_name) cursor = conn.execute(sql_check, (item.name, )) result = cursor.fetchone() if result[0] == 0: raise ItemNotStoredError( f'can\'t update {item.name} because it\'s not stored' f' in table "{table_name}"') cursor.execute(sql_update, (item.price, item.quantity, item.name)) conn.commit()
def delete_item(name): item = _find_item(name) if item is None: raise ItemNotStoredError( f'Can\'t delete "{name}" because it\'s not stored') _item_base.remove(item)
def update_item(item): old_item = _find_item(item.name) if old_item is None: raise ItemNotStoredError( f'Can\'t update "{item.name}" because it\'s not stored') old_item.price, old_item.quantity = item.price, item.quantity
def read_item(name): item = _find_item(name) if item is None: raise ItemNotStoredError( f'Can\'t read "{name}" because it\'s not stored') return deepcopy(item)