-
Notifications
You must be signed in to change notification settings - Fork 1
/
statement.h
121 lines (111 loc) · 3.29 KB
/
statement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "types.h"
#include "cursor.h"
#include "transaction.h"
#ifndef STATEMENT_H
#define STATEMENT_H
/**
* The parsing result of a SQL statement sentence is stored in statement_t.
* These `execute_xxx` functions are equal to the "Vitrual Machine" in sqlite.
* SQL Statement:
* - insert id username email
* - select
* - update id username email
**/
execute_result_t execute_insert(statement_t *statement, table_t *table)
{
row_t *row_to_insert = &(statement->row_value);
uint32_t key_to_insert = row_to_insert->id;
cursor_t *cursor = table_find(table, key_to_insert);
void *node = get_page(table->pager, cursor->page_num);
uint32_t num_cells = *leaf_node_num_cells(node);
if (cursor->cell_num < num_cells)
{
uint32_t key_at_index = *leaf_node_key(node, cursor->cell_num);
// printf("[execute insert] key_at_index = %d\n", key_at_index);
if (key_at_index == key_to_insert)
{
free(cursor);
return EXECUTE_DUPLICATE_KEY;
}
}
leaf_node_insert(cursor, row_to_insert->id, row_to_insert);
#ifdef OPEN_TRANSACTION
transaction_push(cursor, row_to_insert, statement->type);
#endif
free(cursor);
return EXECUTE_SUCCESS;
}
execute_result_t execute_select(statement_t *statement, table_t *table)
{
cursor_t *cursor = table_start(table);
row_t row;
uint32_t i = 0;
while (!(cursor->end_of_table))
{
deserialize_row(cursor_value(cursor), &row);
i++;
print_row(&row);
cursor_advance(cursor);
}
printf("total %u rows\n", i);
free(cursor);
return EXECUTE_SUCCESS;
}
execute_result_t execute_update(statement_t *statement, table_t *table)
{
uint32_t key_to_update = statement->row_value.id;
cursor_t *cursor = table_exists(table, key_to_update);
if (cursor != NULL)
{
void *row = cursor_value(cursor);
#ifdef OPEN_TRANSACTION
row_t temp;
deserialize_row(row, &temp);
transaction_push(cursor, &temp, statement->type);
#endif
serialize_row(&(statement->row_value), row);
free(cursor);
return EXECUTE_SUCCESS;
}
return EXECUTE_NO_SUCH_KEY;
}
execute_result_t execute_delete(statement_t *statement, table_t *table)
{
uint32_t key_to_delete = statement->row_value.id;
cursor_t *cursor = table_exists(table, key_to_delete);
if (cursor != NULL)
{
// printf("[execute_delete] page num: %u\n", cursor->page_num);
// printf("[execute_delete] cell num: %u\n", cursor->cell_num);
leaf_node_fake_delete(cursor, key_to_delete);
#ifdef OPEN_TRANSACTION
row_t temp;
deserialize_row(cursor_value(cursor), &temp);
transaction_push(cursor, &temp, statement->type);
#endif
free(cursor);
return EXECUTE_SUCCESS;
}
return EXECUTE_NO_SUCH_KEY;
}
execute_result_t execute_commit(statement_t *statement, table_t *table)
{
assert(statement->type == STATEMENT_COMMIT);
#ifdef OPEN_TRANSACTION
transaction_commit();
#else
transaction_fatal();
#endif
return EXECUTE_SUCCESS;
}
execute_result_t execute_rollback(statement_t *statement, table_t *table)
{
assert(statement->type == STATEMENT_ROLLBACK);
#ifdef OPEN_TRANSACTION
transaction_rollback();
#else
transaction_fatal();
#endif
return EXECUTE_SUCCESS;
}
#endif