-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a new REPL program for tinydb based on sql-parser
Here is some TODOs. - sql-parser: support insert, update, delete, commit, rollback keywords. - db-kernel: support select-where, update-where, create-table.
- Loading branch information
Showing
12 changed files
with
164 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @brief This is the REPL program of tinydb, which is based on a sql parser. | ||
*/ | ||
#include "sql-parser/sqlparser.h" | ||
#include "common.h" | ||
int main(int argc, char *argv[]) | ||
{ | ||
buffer_t *input = new_buffer_input(); | ||
|
||
if (argc < 2) | ||
{ | ||
printf("Must supply a database filename.\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
char *filename = argv[1]; | ||
table_t *table = db_open(filename); | ||
statement_t *stm = (statement_t *)malloc(sizeof(statement_t)); | ||
|
||
while (1) | ||
{ | ||
print_prompt(); | ||
read_input(input); | ||
|
||
/* If `input` is a meta command, then it will start with '.' */ | ||
if (input->buffer[0] == '.') | ||
{ | ||
switch (do_meta_command(input, table)) | ||
{ | ||
case META_COMMAND_SUCCESS: | ||
continue; | ||
case META_COMMAND_UNRECOGNIZED_COMMAND: | ||
printf("Unrecognized meta command '%s'\n", input->buffer); | ||
continue; | ||
} | ||
} | ||
|
||
/* 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
CC=clang -Wall -Wno-pointer-to-int-cast | ||
build: | ||
$(CC) main.c -o tinydb | ||
CC=clang -Wall -Wno-pointer-to-int-cast -Wno-unused-function | ||
|
||
PROJECT=$(shell pwd) | ||
SQLPARSER=$(PROJECT)/sql-parser | ||
SQLVM=$(PROJECT)/sql-v, | ||
|
||
INCLUDE=-I$(PROJECT) -I$(SQLPARSER) -I$(SQLVM) | ||
|
||
build: sqlparser | ||
$(CC) $(INCLUDE) sqlparser.tab.c lex.yy.c main.c -o tinydb | ||
|
||
clean: | ||
rm *.db | ||
rm *.db tinydb lex.yy.c *.tab.c *.tab.h | ||
|
||
run: | ||
make build | ||
./tinydb mydb.db | ||
|
||
sqlparser: | ||
flex $(SQLPARSER)/sqlex.l | ||
bison -d $(SQLPARSER)/sqlparser.y | ||
|
||
test: | ||
$(CC) test.c -o a.out | ||
./a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "sql-parser/condition.h" | ||
#include "types.h" | ||
#ifndef STATEMENT_H | ||
#define STATEMENT_H | ||
|
||
typedef enum | ||
{ | ||
STATEMENT_INSERT, | ||
STATEMENT_SELECT, | ||
STATEMENT_UPDATE, | ||
STATEMENT_DELETE, | ||
STATEMENT_COMMIT, | ||
STATEMENT_ROLLBACK | ||
} statement_type_t; | ||
|
||
|
||
typedef struct | ||
{ | ||
statement_type_t type; /* SELECT, UPDATE, DELETE, etc. */ | ||
row_t row_value; /* The row value of INSERT SQL */ | ||
const char *table_name; /* table name */ | ||
schema_node_t *schemas; /* SELECT column items list */ | ||
condition_t *conditions; /* WHERE conditions */ | ||
} statement_t; | ||
|
||
static inline void statement_init(statement_t *stm) | ||
{ | ||
stm->type = -1; | ||
stm->table_name = NULL; | ||
stm->schemas = NULL; | ||
stm->conditions = NULL; | ||
memset(&(stm->row_value), 0, sizeof(row_t)); | ||
} | ||
|
||
static inline void statement_free(statement_t *stm) | ||
{ | ||
// TODO | ||
assert(0); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters