From 4e9eebb2afb0d863da15e6edf4357e2f4da7411f Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 3 Dec 2014 16:53:49 +0100 Subject: [PATCH] implemented drop statements --- src/lib/DropStatement.h | 38 ++++++++++++++++++++++++++++++++++++++ src/lib/sqllib.h | 1 + src/parser/bison_parser.y | 13 +++++++++++++ src/sql_tests.cpp | 12 ++++++++++++ test/valid_queries.sql | 4 +++- 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/lib/DropStatement.h diff --git a/src/lib/DropStatement.h b/src/lib/DropStatement.h new file mode 100644 index 0000000..4f4e45d --- /dev/null +++ b/src/lib/DropStatement.h @@ -0,0 +1,38 @@ +#ifndef __DROP_STATEMENT_H__ +#define __DROP_STATEMENT_H__ + +#include "Statement.h" + +namespace hsql { + +struct DropStatement : Statement { + enum ObjectType { + kTable, + kSchema, + kIndex, + kView + }; + + + DropStatement(ObjectType type) : + Statement(kStmtDrop), + obj_type(type), + name(NULL) {} + + + ObjectType obj_type; + const char* name; + + + + virtual ~DropStatement() { + delete name; + } +}; + + + + + +} // namespace hsql +#endif \ No newline at end of file diff --git a/src/lib/sqllib.h b/src/lib/sqllib.h index 31049ae..e2b1bc9 100644 --- a/src/lib/sqllib.h +++ b/src/lib/sqllib.h @@ -8,5 +8,6 @@ #include "InsertStatement.h" #include "UpdateStatement.h" #include "DeleteStatement.h" +#include "DropStatement.h" #endif \ No newline at end of file diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 238d36f..e0b9897 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -85,6 +85,7 @@ typedef void* yyscan_t; hsql::InsertStatement* insert_stmt; hsql::DeleteStatement* delete_stmt; hsql::UpdateStatement* update_stmt; + hsql::DropStatement* drop_stmt; hsql::TableRef* table; hsql::Expr* expr; @@ -139,6 +140,7 @@ typedef void* yyscan_t; %type insert_statement %type delete_statement truncate_statement %type update_statement +%type drop_statement %type table_name opt_alias alias file_path %type opt_not_exists %type import_file_type opt_join_type column_type @@ -205,6 +207,7 @@ statement: | delete_statement { $$ = $1; } | truncate_statement { $$ = $1; } | update_statement { $$ = $1; } + | drop_statement { $$ = $1; } ; @@ -276,6 +279,16 @@ column_type: | TEXT { $$ = ColumnDefinition::TEXT; } ; +/****************************** + * Drop Statement + * DROP TABLE students + ******************************/ + +drop_statement: + DROP TABLE table_name { + $$ = new DropStatement(DropStatement::kTable); + $$->name = $3; + } /****************************** * Delete Statement / Truncate statement diff --git a/src/sql_tests.cpp b/src/sql_tests.cpp index 60c9419..116e4f1 100644 --- a/src/sql_tests.cpp +++ b/src/sql_tests.cpp @@ -79,4 +79,16 @@ TEST(Insert) { ASSERT_EQ(stmt_list->at(0)->type, kStmtInsert); // TODO +} + + +TEST(DropTable) { + StatementList* stmt_list = SQLParser::parseSQLString("DROP TABLE students"); + ASSERT(stmt_list->isValid); + ASSERT_EQ(stmt_list->size(), 1); + ASSERT_EQ(stmt_list->at(0)->type, kStmtDrop); + + DropStatement* stmt = (DropStatement*) stmt_list->at(0); + ASSERT_NOTNULL(stmt->name); + ASSERT_STREQ(stmt->name, "students"); } \ No newline at end of file diff --git a/test/valid_queries.sql b/test/valid_queries.sql index 053354d..c3e5eb0 100644 --- a/test/valid_queries.sql +++ b/test/valid_queries.sql @@ -27,4 +27,6 @@ TRUNCATE students # UPDATE UPDATE students SET grade = 1.3 WHERE name = 'Max Mustermann'; UPDATE students SET grade = 1.3, name='Felix Fürstenberg' WHERE name = 'Max Mustermann'; -UPDATE students SET grade = 1.0; \ No newline at end of file +UPDATE students SET grade = 1.0; +# DROP +DROP TABLE students; \ No newline at end of file