Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ typedef enum {
BTREE_IO_ERROR,
BTREE_DUPLICATE_KEY,
BTREE_TABLE_FULL,
BTREE_CORRUPT
BTREE_CORRUPT,
BTREE_ROW_NOT_FOUND
} BTreeResult;

typedef struct BTree BTree;
Expand Down
4 changes: 3 additions & 1 deletion src/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ BTreeResult btree_find(const BTree *tree, int id, Row *row)
deserialize_row(row, const_leaf_cell(page, index));
return BTREE_OK;
}
return BTREE_INVALID_ARGUMENT;
return BTREE_ROW_NOT_FOUND;
}

page_number = child_page_at(page, internal_child_index(page, id));
Expand Down Expand Up @@ -899,6 +899,8 @@ const char *btree_result_string(BTreeResult result)
return "database capacity reached";
case BTREE_CORRUPT:
return "database file is corrupt";
case BTREE_ROW_NOT_FOUND:
return "row not found";
default:
return "unknown error";
}
Expand Down
23 changes: 19 additions & 4 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ Table *table_open(const char *filename, SqliteResult *result)
btree_read_all(tree, table->rows, TABLE_ROW_CAPACITY, &table->size);
if (read_result != BTREE_OK) {
table_destroy(table);
*result = SQLITE_IO_ERROR;
switch (read_result) {
case BTREE_CORRUPT:
*result = SQLITE_CORRUPT;
break;
case BTREE_TABLE_FULL:
*result = SQLITE_TABLE_FULL;
break;
default:
*result = SQLITE_IO_ERROR;
break;
}
return NULL;
}

Expand Down Expand Up @@ -222,11 +232,16 @@ SqliteResult table_find(const Table *table, int id, Row *row)
return SQLITE_OK;
}

if (result == BTREE_INVALID_ARGUMENT) {
switch (result) {
case BTREE_ROW_NOT_FOUND:
return SQLITE_ROW_NOT_FOUND;
case BTREE_CORRUPT:
return SQLITE_CORRUPT;
case BTREE_TABLE_FULL:
return SQLITE_TABLE_FULL;
default:
return SQLITE_IO_ERROR;
}

return SQLITE_IO_ERROR;
}

const char *sqlite_result_string(SqliteResult result)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static void test_lookup(void)
assert(found.id == 23);
assert(strcmp(found.username, "user23") == 0);

assert(table_find(table, 99, &found) != SQLITE_OK);
assert(table_find(table, 99, &found) == SQLITE_ROW_NOT_FOUND);

table_destroy(table);
remove(filename);
Expand Down
Loading