implemented create with column def

This commit is contained in:
Pedro 2014-11-26 18:20:10 +01:00
parent 382522db76
commit 2f76183586
6 changed files with 103 additions and 40 deletions

View File

@ -16,6 +16,12 @@ struct ColumnDefinition {
DOUBLE
};
ColumnDefinition(char* name, DataType type) :
name(name),
type(type) {}
virtual ~ColumnDefinition(); // defined in destructors.cpp
char* name;
DataType type;
};
@ -34,6 +40,7 @@ struct CreateStatement : Statement {
CreateStatement() :
Statement(kStmtCreate),
if_not_exists(false),
columns(NULL),
file_path(NULL),
table_name(NULL) {};
@ -41,6 +48,9 @@ struct CreateStatement : Statement {
CreateType create_type;
bool if_not_exists;
List<ColumnDefinition*>* columns;
const char* file_path;
const char* table_name;
};

View File

@ -16,6 +16,7 @@ namespace hsql {
struct DeleteStatement : Statement {
DeleteStatement() :
Statement(kStmtDelete),
table_name(NULL),
expr(NULL) {};
virtual ~DeleteStatement(); // defined in destructors.cpp

View File

@ -4,14 +4,43 @@
namespace hsql {
Statement::~Statement() {
/* empty */
}
/**
* Statement.h
*/
Statement::~Statement() { /* empty */ }
StatementList::~StatementList() {
delete parser_msg;
}
/**
* ImportStatement.h
*/
ImportStatement::~ImportStatement() {
delete file_path;
delete table_name;
}
/**
* InsertStatement.h
*/
InsertStatement::~InsertStatement() {
delete table_name;
delete columns;
delete values;
delete select;
}
/**
* DeleteStatement.h
*/
DeleteStatement::~DeleteStatement() {
delete table_name;
delete expr;
}
/**
* SelectStatement.h
*/
SelectStatement::~SelectStatement() {
delete from_table;
delete select_list;
@ -20,43 +49,38 @@ SelectStatement::~SelectStatement() {
delete order;
delete limit;
}
ImportStatement::~ImportStatement() {
delete file_path;
delete table_name;
}
CreateStatement::~CreateStatement() {
delete file_path;
delete table_name;
}
InsertStatement::~InsertStatement() {
delete table_name;
delete select;
}
DeleteStatement::~DeleteStatement() {
delete expr;
}
OrderDescription::~OrderDescription() {
delete expr;
}
/**
* CreateStatement.h
*/
CreateStatement::~CreateStatement() {
delete columns;
delete file_path;
delete table_name;
}
ColumnDefinition::~ColumnDefinition() {
delete name;
}
/**
* Table.h
*/
TableRef::~TableRef() {
delete name;
delete alias;
delete select;
delete list;
}
JoinDefinition::~JoinDefinition() {
delete left;
delete right;
delete condition;
}
} // namespace hsql

View File

@ -90,11 +90,13 @@ typedef void* yyscan_t;
hsql::OrderDescription* order;
hsql::OrderType order_type;
hsql::LimitDescription* limit;
hsql::ColumnDefinition* column_t;
hsql::StatementList* stmt_list;
hsql::List<char*>* slist;
hsql::List<hsql::Expr*>* expr_list;
hsql::List<hsql::TableRef*>* table_list;
hsql::List<hsql::ColumnDefinition*>* column_list_t;
}
@ -135,6 +137,7 @@ typedef void* yyscan_t;
%type <delete_stmt> delete_statement truncate_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
%type <table> from_clause table_ref table_ref_atomic table_ref_name
%type <table> join_clause join_table
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias
@ -145,8 +148,9 @@ typedef void* yyscan_t;
%type <order> opt_order
%type <limit> opt_limit
%type <order_type> opt_order_type
%type <uval> import_file_type opt_join_type
%type <slist> ident_commalist opt_column_list
%type <column_t> column_def
%type <column_list_t> column_def_commalist
/******************************
** Token Precedence and Associativity
@ -237,7 +241,7 @@ create_statement:
$$->create_type = CreateStatement::kTable;
$$->if_not_exists = $3;
$$->table_name = $4;
// TODO: build into object
$$->columns = $6;
}
;
@ -247,18 +251,20 @@ opt_not_exists:
;
column_def_commalist:
column_def
| column_def_commalist ',' column_def
column_def { $$ = new List<ColumnDefinition*>($1); }
| column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; }
;
column_def:
IDENTIFIER column_type
IDENTIFIER column_type {
$$ = new ColumnDefinition($1, (ColumnDefinition::DataType) $2);
}
;
column_type:
INTEGER
| DOUBLE
| TEXT
INTEGER { $$ = ColumnDefinition::INT; }
| DOUBLE { $$ = ColumnDefinition::DOUBLE; }
| TEXT { $$ = ColumnDefinition::TEXT; }
;

View File

@ -13,25 +13,41 @@ using namespace hsql;
TEST(SelectTest) {
StatementList* stmt_list = SQLParser::parseSQLString("SELECT * FROM students;");
ASSERT(stmt_list->isValid);
ASSERT(stmt_list->size() == 1);
ASSERT_EQ(stmt_list->size(), 1);
ASSERT(stmt_list->at(0)->type == kStmtSelect);
SelectStatement* stmt = (SelectStatement*) stmt_list->at(0);
ASSERT(stmt->where_clause == NULL);
ASSERT_NULL(stmt->where_clause);
}
TEST(DeleteTest) {
StatementList* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
ASSERT(stmt_list->isValid);
ASSERT(stmt_list->size() == 1);
ASSERT_EQ(stmt_list->size(), 1);
ASSERT(stmt_list->at(0)->type == kStmtDelete);
DeleteStatement* stmt = (DeleteStatement*) stmt_list->at(0);
ASSERT_STREQ(stmt->table_name, "students");
ASSERT(stmt->expr != NULL);
ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator));
ASSERT_STREQ(stmt->expr->expr->name, "grade");
ASSERT_EQ(stmt->expr->expr2->fval, 2.0);
}
TEST(CreateTable) {
StatementList* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)");
ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->size(), 1);
ASSERT_EQ(stmt_list->at(0)->type, kStmtCreate);
CreateStatement* stmt = (CreateStatement*) stmt_list->at(0);
ASSERT_EQ(stmt->create_type, CreateStatement::kTable);
ASSERT_STREQ(stmt->table_name, "students");
ASSERT_NOTNULL(stmt->columns);
ASSERT_EQ(stmt->columns->size(), 4);
ASSERT_STREQ(stmt->columns->at(0)->name, "name");
ASSERT_EQ(stmt->columns->at(0)->type, ColumnDefinition::TEXT);
ASSERT_STREQ(stmt->columns->at(3)->name, "grade");
ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE);
}

View File

@ -7,13 +7,19 @@
void name()
#define ASSERT(cond) if (!(cond)) throw AssertionFailedException(#cond);
#define ASSERT_TRUE(cond) ASSERT(cond);
#define ASSERT_FALSE(cond) if (cond) throw AssertionFailedException(#cond);
#define ASSERT_NULL(value) ASSERT_TRUE(value == NULL);
#define ASSERT_NOTNULL(value) ASSERT_TRUE(value != NULL);
#define ASSERT_STREQ(a, b) \
if (std::string(a).compare(std::string(b)) != 0) throw AssertionFailedException(#a " == " #b)
#define ASSERT_EQ(a, b) \
ASSERT(a == b);
class AssertionFailedException: public std::exception {
public:
AssertionFailedException(std::string msg) :