コード例 #1
0
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)
コード例 #2
0
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)
コード例 #3
0
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'])
コード例 #4
0
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
コード例 #5
0
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()
コード例 #6
0
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()
コード例 #7
0
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)
コード例 #8
0
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
コード例 #9
0
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)