-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.cpp
More file actions
88 lines (75 loc) · 2.69 KB
/
statement.cpp
File metadata and controls
88 lines (75 loc) · 2.69 KB
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
#include "setup.h"
#include "input_buffer.h"
#include "meta_command.h"
#include "statement.h"
PrepareResult prepare_statement(InputBuffer* input_buffer, Statement* statement) {
std::cout << "this is the statement: " << input_buffer->buffer << "\n";
if (input_buffer->buffer.substr(0, 6) == "insert") {
statement->type = STATEMENT_INSERT;
int assigned = std::sscanf(input_buffer->buffer.c_str(), "insert %d %s %s",
&(statement->row_to_insert.id),
statement->row_to_insert.username,
statement->row_to_insert.email);
if (assigned < 3) {
return PREPARE_SYNTAX_ERROR;
}
return PREPARE_SUCCESS;
}
if (input_buffer->buffer.substr(0, 6) == "select") {
statement->type = STATEMENT_SELECT;
return PREPARE_SUCCESS;
}
return PREPARE_UNRECOGNIZED_STATEMENT;
}
ExecuteResult execute_insert(Statement* statement, Table* table) {
if (table->num_rows >= TABLE_MAX_ROWS) {
return EXECUTE_TABLE_FULL;
}
Row* row_to_insert = &(statement->row_to_insert);
serialize_row(row_to_insert, row_slot(table, table->num_rows));
table->num_rows += 1;
return EXECUTE_SUCCESS;
}
ExecuteResult execute_select(Statement* statement, Table* table) {
Row row;
for (uint32_t i = 0; i < table->num_rows; ++i) {
deserialize_row(row_slot(table, i), &row);
print_row(&row);
}
return EXECUTE_SUCCESS;
}
ExecuteResult execute_statement(Statement* statement, Table* table) {
switch (statement->type) {
case (STATEMENT_INSERT):
return execute_insert(statement, table);
case (STATEMENT_SELECT):
return execute_select(statement, table);
}
}
void serialize_row(Row* source, void* destination) {
memcpy((char*)destination + ID_OFFSET, &(source->id), ID_SIZE);
memcpy((char*)destination + USERNAME_OFFSET, &(source->username), USERNAME_SIZE);
memcpy((char*)destination + EMAIL_OFFSET, &(source->email), EMAIL_SIZE);
}
void deserialize_row(void* source, Row* destination) {
memcpy(&(destination->id), (char*)source + ID_OFFSET, ID_SIZE);
memcpy(&(destination->username), (char*)source + USERNAME_OFFSET, USERNAME_SIZE);
memcpy(&(destination->email), (char*)source + EMAIL_OFFSET, EMAIL_SIZE);
}
Table::Table() {
num_rows = 0;
}
void print_row(Row* row) {
std::cout << "(" << row->id << ", " << row->username << ", " << row->email << ")\n";
}
void* row_slot(Table* table, uint32_t row_num) {
uint32_t page_num = row_num / ROWS_PER_PAGE;
void* page = table->pages[page_num];
if (!page) {
// Allocate memory only when we try to access page
page = table->pages[page_num] = malloc(PAGE_SIZE);
}
uint32_t row_offset = row_num % ROWS_PER_PAGE;
uint32_t byte_offset = row_offset * ROW_SIZE;
return (void*)((char*)page + byte_offset);
}