implemented drop statements

This commit is contained in:
Pedro 2014-12-03 16:53:49 +01:00
parent 48b473e7c5
commit 4e9eebb2af
5 changed files with 67 additions and 1 deletions

38
src/lib/DropStatement.h Normal file
View File

@ -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

View File

@ -8,5 +8,6 @@
#include "InsertStatement.h"
#include "UpdateStatement.h"
#include "DeleteStatement.h"
#include "DropStatement.h"
#endif

View File

@ -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_stmt> insert_statement
%type <delete_stmt> delete_statement truncate_statement
%type <update_stmt> update_statement
%type <drop_stmt> drop_statement
%type <sval> table_name opt_alias alias file_path
%type <bval> opt_not_exists
%type <uval> 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

View File

@ -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");
}

View File

@ -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;
UPDATE students SET grade = 1.0;
# DROP
DROP TABLE students;