Skip to content

Commit

Permalink
feat: support select fields, see main2.c
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkinben committed Feb 1, 2022
1 parent eb27ec4 commit 6ba9f75
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 70 deletions.
6 changes: 0 additions & 6 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,4 @@ meta_command_result_t do_meta_command(buffer_t *input, table_t *table)
}
}

// 真正调用底层 API 执行 SQL, 相当于 sqlite 的 Core
execute_result_t execute_statement(statement_t *statement, table_t *table)
{
return execute_vm(statement, table);
}

#endif
26 changes: 7 additions & 19 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ int main(int argc, char *argv[])
}
}

statement_t statement;
statement_t statement = {
.conditions = NULL,
.schemas = NULL,
.table_name = filename
};
switch (parse_statement(input_buffer, &statement))
{
case PARSE_SUCCESS:
Expand All @@ -48,23 +52,7 @@ int main(int argc, char *argv[])
continue;
}

switch (execute_statement(&statement, table))
{
case EXECUTE_SUCCESS:
printf("Executed.\n");
break;
case EXECUTE_DUPLICATE_KEY:
printf("Execute Error: Duplicate key.\n");
break;
case EXECUTE_NO_SUCH_KEY:
printf("Execute Error: No such key %u\n", statement.row_value.id);
break;
case EXECUTE_TABLE_FULL:
printf("Execute Error: Table full.\n");
break;
case EXECUTE_UNKNOWN_STATEMENT:
printf("Execute Error: Unknown sql statement '%s' \n", input_buffer->buffer);
break;
}
execute_result_t result = vm_executor(&statement, table);
vm_logger(result, &statement, input_buffer);
}
}
8 changes: 4 additions & 4 deletions main2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int main(int argc, char *argv[])
char *filename = argv[1];
table_t *table = db_open(filename);
statement_t *stm = (statement_t *)malloc(sizeof(statement_t));
execute_result_t result;

while (1)
{
Expand All @@ -37,9 +38,8 @@ int main(int argc, char *argv[])
/* Parse the SQL, and put the result in statement_t `stm` */
statement_init(stm);
sql_parser(input->buffer, stm);
execute_vm(stm, table);

// printf("stm->type = %d \n", stm->type);
// printf("row = (%u, %s, %s) \n", stm->row_value.id, stm->row_value.username, stm->row_value.email);
result = vm_executor(stm, table);
vm_logger(result, stm, input);
statement_free(stm);
}
}
12 changes: 9 additions & 3 deletions sql-parser/condition.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include "schema.h"
#include "types.h"
#ifndef __CONDITION_H__
#define __CONDITION_H__
typedef enum
Expand Down Expand Up @@ -70,10 +71,15 @@ static inline void print_tree(condition_t *node, int depth)
}
}

static inline void destroy_tree(condition_t *node)
static bool test_condition(condition_t *cond, row_t *row)
{
printf("TODO: PLEASE implement me. \n");
assert(0);
return true;
}

static inline void destroy_condition_tree(condition_t *node)
{
printf("[destroy_condition_tree]: PLEASE implement me. \n");
// assert(0);
}


Expand Down
13 changes: 13 additions & 0 deletions sql-parser/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,18 @@ static inline schema_node_t *alloc_schema_node(const char *filedname, uint32_t w
return node;
}

static inline void free_schema_list(schema_node_t *schemas)
{
if (list_empty(&(schemas->entry)))
return;
list_node_t *pos, *next;
list_for_each_safe(pos, next, &schemas->entry)
{
list_del(pos);
free(list_entry(pos, schema_node_t, entry));
}
init_list_head(&(schemas->entry));
}


#endif
1 change: 0 additions & 1 deletion sql-parser/sqlex.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"where" |
"WHERE" { return WHERE; }

"table" |
"TABLE" { return TABLE; }

"int" |
Expand Down
33 changes: 4 additions & 29 deletions sql-parser/sqlparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,19 @@ statement:
selectsql:
SELECT '*' FROM IDNAME ';'
{
printf("TODO: please select * from [%s] \n", $4);
printf("IDNAME = %p\n", $4);
stm_ptr->table_name = $4;
stm_ptr->type = STATEMENT_SELECT;
statement_set(stm_ptr, STATEMENT_SELECT, $4, select_list, NULL);
}
| SELECT selectitemlist FROM IDNAME ';'
{
printf("TODO: please select ");
list_node_t *pos;
list_for_each(pos, &(select_list->entry))
{
const char *fieldname = list_entry(pos, schema_node_t, entry)->schema.fieldname;
printf("%s ", fieldname);
}
printf("from %s \n", $4);
init_list_head(&(select_list->entry));
statement_set(stm_ptr, STATEMENT_SELECT_FIELDS, $4, select_list, NULL);
}
| SELECT '*' FROM IDNAME WHERE conditions ';'
{
printf("[SELECT] cond ptr = %p, table name = %s \n", $6, $4);
print_tree($6, 0);
init_list_head(&(select_list->entry));
condition_tree = NULL;
statement_set(stm_ptr, STATEMENT_SELECT, $4, select_list, $6);
}
| SELECT selectitemlist FROM IDNAME WHERE conditions ';'
{
printf("---> TODO: please select ");
list_node_t *pos;
list_for_each(pos, &(select_list->entry))
{
const char *fieldname = list_entry(pos, schema_node_t, entry)->schema.fieldname;
printf("%s ", fieldname);
}
printf("from %s \n", $4);
printf("---> where condition is \n");
print_tree($6, 0);
init_list_head(&(select_list->entry));
condition_tree = NULL;
statement_set(stm_ptr, STATEMENT_SELECT_FIELDS, $4, select_list, $6);
}
;

Expand Down
42 changes: 35 additions & 7 deletions sql-statement/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef enum
{
STATEMENT_INSERT,
STATEMENT_SELECT,
STATEMENT_SELECT_FIELDS,
STATEMENT_UPDATE,
STATEMENT_DELETE,
STATEMENT_COMMIT,
Expand All @@ -23,19 +24,46 @@ typedef struct
condition_t *conditions; /* WHERE conditions */
} statement_t;

static inline void statement_init(statement_t *stm)
static inline void statement_set(
statement_t *stm,
statement_type_t type,
const char *table_name,
schema_node_t *schemas,
condition_t *conds
)
{
stm->type = -1;
stm->table_name = NULL;
stm->schemas = NULL;
stm->conditions = NULL;
stm->type = type;
stm->table_name = table_name;
stm->schemas = schemas;
stm->conditions = conds;
memset(&(stm->row_value), 0, sizeof(row_t));
}

static inline void statement_init(statement_t *stm)
{
statement_set(stm, -1, NULL, NULL, NULL);
}

/* Free the resource that were allocated in sql parser,
* and keep the `stm` struct.
*/
static inline void statement_free(statement_t *stm)
{
// TODO
assert(0);
if (stm->schemas)
{
free_schema_list(stm->schemas);
stm->schemas = NULL;
}
if (stm->table_name)
{
free((void *)(stm->table_name));
stm->table_name = NULL;
}
if (stm->conditions)
{
destroy_condition_tree(stm->conditions);
stm->conditions = NULL;
}
}

#endif
45 changes: 44 additions & 1 deletion sql-vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ execute_result_t execute_select(statement_t *statement, table_t *table)
return EXECUTE_SUCCESS;
}

execute_result_t execute_select_fields(statement_t *statement, table_t *table)
{
cursor_t *cursor = table_start(table);
row_t row;
uint32_t cnt = 0;
uint8_t flags = get_field_flags(statement->schemas);
while (!(cursor->end_of_table))
{
deserialize_row(cursor_value(cursor), &row);
cnt++;
print_fields(&row, flags);
cursor_advance(cursor);
}
printf("total %u rows\n", cnt);
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;
Expand Down Expand Up @@ -120,14 +138,16 @@ execute_result_t execute_rollback(statement_t *statement, table_t *table)
return EXECUTE_SUCCESS;
}

execute_result_t execute_vm(statement_t *statement, table_t *table)
execute_result_t vm_executor(statement_t *statement, table_t *table)
{
switch (statement->type)
{
case STATEMENT_INSERT:
return execute_insert(statement, table);
case STATEMENT_SELECT:
return execute_select(statement, table);
case STATEMENT_SELECT_FIELDS:
return execute_select_fields(statement, table);
case STATEMENT_UPDATE:
return execute_update(statement, table);
case STATEMENT_DELETE:
Expand All @@ -142,4 +162,27 @@ execute_result_t execute_vm(statement_t *statement, table_t *table)
return EXECUTE_UNKNOWN_STATEMENT;
}


void vm_logger(execute_result_t result, statement_t *statement, buffer_t *input)
{
switch (result)
{
case EXECUTE_SUCCESS:
printf("Executed.\n");
break;
case EXECUTE_DUPLICATE_KEY:
printf("Execute Error: Duplicate key.\n");
break;
case EXECUTE_NO_SUCH_KEY:
printf("Execute Error: No such key %u\n", statement->row_value.id);
break;
case EXECUTE_TABLE_FULL:
printf("Execute Error: Table full.\n");
break;
case EXECUTE_UNKNOWN_STATEMENT:
printf("Execute Error: Unknown sql statement '%s' \n", input->buffer);
break;
}
}

#endif
37 changes: 37 additions & 0 deletions table.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "types.h"
#include "pager.h"
#include "btree.h"
#include "sql-parser/schema.h"
#ifndef TABLE_H
#define TABLE_H

Expand All @@ -27,13 +28,49 @@ void print_row(row_t *row)
printf("(%u, %s, %s)\n", row->id, row->username, row->email);
}

uint8_t get_field_flags(schema_node_t *schemas)
{
if (schemas == NULL)
return 7;

uint8_t flags = 0;
list_node_t *pos;
list_for_each(pos, &(schemas->entry))
{
const char *field = list_entry(pos, schema_node_t, entry)->schema.fieldname;
if (strcmp(field, "id") == 0)
flags |= 1;
else if (strcmp(field, "username") == 0)
flags |= 2;
else if (strcmp(field, "email") == 0)
flags |= 4;
}
if (flags == 0) flags = 7;
return flags;
}

void print_fields(row_t *row, uint8_t flags)
{
if (flags & 1)
printf("id = %u ", row->id);

if (flags & 2)
printf("username = %s ", row->username);

if (flags & 4)
printf("email = %s ", row->email);

printf("\n");
}

// opening the database file
// initializing a pager data structure
// initializing a table data structure
table_t *db_open(const char *filename)
{
pager_t *pager = pager_open(filename);
table_t *table = (table_t *)malloc(sizeof(table_t));
table->table_name = filename;
table->pager = pager;
table->root_page_num = 0;
if (pager->num_pages == 0)
Expand Down
1 change: 1 addition & 0 deletions types.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct
{
uint32_t root_page_num;
pager_t *pager;
const char *table_name;
} table_t;

typedef struct
Expand Down

0 comments on commit 6ba9f75

Please sign in to comment.