From 71f92520191f3eb234a6072884586e9b4a96727c Mon Sep 17 00:00:00 2001 From: Pedro Date: Tue, 6 Jan 2015 19:47:59 +0100 Subject: [PATCH] removed List class wrapper and use simply std::vector throughout --- src/lib/List.h | 46 --------------------------- src/lib/Table.h | 4 +-- src/lib/sqlhelper.cpp | 8 ++--- src/lib/statements/CreateStatement.h | 2 +- src/lib/statements/ExecuteStatement.h | 5 ++- src/lib/statements/InsertStatement.h | 4 +-- src/lib/statements/SQLStatement.h | 14 +++++--- src/lib/statements/SelectStatement.h | 5 ++- src/lib/statements/UpdateStatement.h | 2 +- src/parser/bison_parser.y | 43 +++++++++++++------------ src/sql_tests.cpp | 38 +++++++++++----------- src/tests/helper.h | 6 ++-- 12 files changed, 67 insertions(+), 110 deletions(-) delete mode 100644 src/lib/List.h diff --git a/src/lib/List.h b/src/lib/List.h deleted file mode 100644 index 2f0621b..0000000 --- a/src/lib/List.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef __LIST_H__ -#define __LIST_H__ - -#include -#include - -namespace hsql { - -// TODO: try to replace the List wrapper by directly using std::vector - -/** - * @class List - * @brief Classed interface to std::vector (may be replaced with std::vector) - */ -template -class List { -public: - List() {} - - List(_T first_value) { - _vector.push_back(first_value); - } - - virtual ~List() { - for (_T e : _vector) delete e; - } - - inline size_t size() { return _vector.size(); }; - - inline _T at(int i) { return _vector[i]; } - inline _T &operator[](int i) { return _vector[i]; } - - inline void push_back(_T value) { _vector.push_back(value); } - - inline std::vector<_T> vector() { return _vector; }; - - -private: - std::vector<_T> _vector; - -}; - - -} // namespace hsql - -#endif diff --git a/src/lib/Table.h b/src/lib/Table.h index c34d3b9..b27c924 100644 --- a/src/lib/Table.h +++ b/src/lib/Table.h @@ -1,9 +1,9 @@ #ifndef __TABLEREF_H__ #define __TABLEREF_H__ -#include "List.h" #include "Expr.h" #include +#include namespace hsql { @@ -47,7 +47,7 @@ struct TableRef { char* alias; SelectStatement* select; - List* list; + std::vector* list; JoinDefinition* join; diff --git a/src/lib/sqlhelper.cpp b/src/lib/sqlhelper.cpp index 9d65e44..3b1ecae 100644 --- a/src/lib/sqlhelper.cpp +++ b/src/lib/sqlhelper.cpp @@ -33,7 +33,7 @@ void printTableRefInfo(TableRef* table, uint num_indent) { printExpression(table->join->condition, num_indent+2); break; case kTableCrossProduct: - for (TableRef* tbl : table->list->vector()) printTableRefInfo(tbl, num_indent); + for (TableRef* tbl : *table->list) printTableRefInfo(tbl, num_indent); break; } if (table->alias != NULL) { @@ -76,7 +76,7 @@ void printExpression(Expr* expr, uint num_indent) { void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) { inprint("SelectStatement", num_indent); inprint("Fields:", num_indent+1); - for (Expr* expr : stmt->select_list->vector()) printExpression(expr, num_indent+2); + for (Expr* expr : *stmt->select_list) printExpression(expr, num_indent+2); inprint("Sources:", num_indent+1); printTableRefInfo(stmt->from_table, num_indent+2); @@ -124,14 +124,14 @@ void printInsertStatementInfo(InsertStatement* stmt, uint num_indent) { inprint(stmt->table_name, num_indent+1); if (stmt->columns != NULL) { inprint("Columns", num_indent+1); - for (char* col_name : stmt->columns->vector()) { + for (char* col_name : *stmt->columns) { inprint(col_name, num_indent+2); } } switch (stmt->type) { case InsertStatement::kInsertValues: inprint("Values", num_indent+1); - for (Expr* expr : stmt->values->vector()) { + for (Expr* expr : *stmt->values) { printExpression(expr, num_indent+2); } break; diff --git a/src/lib/statements/CreateStatement.h b/src/lib/statements/CreateStatement.h index 886b9ed..348cb12 100644 --- a/src/lib/statements/CreateStatement.h +++ b/src/lib/statements/CreateStatement.h @@ -56,7 +56,7 @@ struct CreateStatement : SQLStatement { CreateType type; bool if_not_exists; - List* columns; + std::vector* columns; const char* file_path; const char* table_name; diff --git a/src/lib/statements/ExecuteStatement.h b/src/lib/statements/ExecuteStatement.h index 1ba4de5..ed6f1c9 100644 --- a/src/lib/statements/ExecuteStatement.h +++ b/src/lib/statements/ExecuteStatement.h @@ -13,8 +13,7 @@ namespace hsql { struct ExecuteStatement : SQLStatement { ExecuteStatement() : SQLStatement(kStmtExecute), - name(NULL), - parameters(NULL) {} + name(NULL) {} virtual ~ExecuteStatement() { delete name; @@ -22,7 +21,7 @@ struct ExecuteStatement : SQLStatement { } const char* name; - List* parameters; + std::vector* parameters; }; diff --git a/src/lib/statements/InsertStatement.h b/src/lib/statements/InsertStatement.h index 3dbce5c..01aa576 100644 --- a/src/lib/statements/InsertStatement.h +++ b/src/lib/statements/InsertStatement.h @@ -34,8 +34,8 @@ struct InsertStatement : SQLStatement { InsertType type; const char* table_name; - List* columns; - List* values; + std::vector* columns; + std::vector* values; SelectStatement* select; }; diff --git a/src/lib/statements/SQLStatement.h b/src/lib/statements/SQLStatement.h index 3ef79d0..79c6a5b 100644 --- a/src/lib/statements/SQLStatement.h +++ b/src/lib/statements/SQLStatement.h @@ -5,7 +5,6 @@ #ifndef __STATEMENT_H__ #define __STATEMENT_H__ -#include "List.h" #include "Expr.h" #include @@ -50,22 +49,27 @@ private: * @struct SQLStatementList * @brief Represents the result of the SQLParser. If parsing was successful it is a list of SQLStatement. */ -class SQLStatementList : public List { +struct SQLStatementList { public: SQLStatementList() : - List(), isValid(true), parser_msg(NULL) {}; SQLStatementList(SQLStatement* stmt) : - List(stmt), isValid(true), - parser_msg(NULL) {}; + parser_msg(NULL) { + addStatement(stmt); + }; virtual ~SQLStatementList() { delete parser_msg; } + + void addStatement(SQLStatement* stmt) { statements.push_back(stmt); } + SQLStatement* getStatement(int id) { return statements[id]; } + size_t numStatements() { return statements.size(); } + std::vector statements; bool isValid; const char* parser_msg; int error_line; diff --git a/src/lib/statements/SelectStatement.h b/src/lib/statements/SelectStatement.h index a9788a6..51ddf6e 100644 --- a/src/lib/statements/SelectStatement.h +++ b/src/lib/statements/SelectStatement.h @@ -3,7 +3,6 @@ #include "SQLStatement.h" #include "Expr.h" -#include "List.h" #include "Table.h" namespace hsql { @@ -62,7 +61,7 @@ struct GroupByDescription { delete having; } - List* columns; + std::vector* columns; Expr* having; }; @@ -94,7 +93,7 @@ struct SelectStatement : SQLStatement { TableRef* from_table; bool select_distinct; - List* select_list; + std::vector* select_list; Expr* where_clause; GroupByDescription* group_by; diff --git a/src/lib/statements/UpdateStatement.h b/src/lib/statements/UpdateStatement.h index d244fb6..9c88cdd 100644 --- a/src/lib/statements/UpdateStatement.h +++ b/src/lib/statements/UpdateStatement.h @@ -35,7 +35,7 @@ struct UpdateStatement : SQLStatement { // TODO: switch to char* instead of TableRef TableRef* table; - List* updates; + std::vector* updates; Expr* where; }; diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 2b5261b..531f082 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -124,11 +124,12 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c hsql::UpdateClause* update_t; hsql::SQLStatementList* stmt_list; - hsql::List* slist; - hsql::List* expr_list; - hsql::List* table_list; - hsql::List* column_list_t; - hsql::List* update_list_t; + + std::vector* str_vec; + std::vector* table_vec; + std::vector* column_vec; + std::vector* update_vec; + std::vector* expr_vec; } @@ -180,17 +181,18 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c %type expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias placeholder_expr %type column_name literal int_literal num_literal string_literal %type comp_expr opt_where join_condition opt_having -%type expr_list select_list literal_list -%type table_ref_commalist %type opt_order %type opt_limit %type opt_order_type -%type ident_commalist opt_column_list -%type column_def -%type column_def_commalist -%type update_clause -%type update_clause_commalist -%type opt_group +%type column_def +%type update_clause +%type opt_group + +%type ident_commalist opt_column_list +%type expr_list select_list literal_list +%type table_ref_commalist +%type update_clause_commalist +%type column_def_commalist /****************************** ** Token Precedence and Associativity @@ -234,7 +236,7 @@ input: statement_list: statement { $$ = new SQLStatementList($1); } - | statement_list ';' statement { $1->push_back($3); $$ = $1; } + | statement_list ';' statement { $1->addStatement($3); $$ = $1; } ; statement: @@ -270,7 +272,6 @@ execute_statement: EXECUTE IDENTIFIER { $$ = new ExecuteStatement(); $$->name = $2; - $$->parameters = NULL; } | EXECUTE IDENTIFIER '(' literal_list ')' { $$ = new ExecuteStatement(); @@ -326,7 +327,7 @@ opt_not_exists: ; column_def_commalist: - column_def { $$ = new List($1); } + column_def { $$ = new std::vector(); $$->push_back($1); } | column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; } ; @@ -417,7 +418,7 @@ update_statement: ; update_clause_commalist: - update_clause { $$ = new List($1); } + update_clause { $$ = new std::vector(); $$->push_back($1); } | update_clause_commalist ',' update_clause { $1->push_back($3); $$ = $1; } ; @@ -533,12 +534,12 @@ opt_limit: * Expressions ******************************/ expr_list: - expr_alias { $$ = new List($1); } + expr_alias { $$ = new std::vector(); $$->push_back($1); } | expr_list ',' expr_alias { $1->push_back($3); $$ = $1; } ; literal_list: - literal { $$ = new List($1); } + literal { $$ = new std::vector(); $$->push_back($1); } | literal_list ',' literal { $1->push_back($3); $$ = $1; } ; @@ -661,7 +662,7 @@ table_ref_atomic: table_ref_commalist: - table_ref_atomic { $$ = new List($1); } + table_ref_atomic { $$ = new std::vector(); $$->push_back($1); } | table_ref_commalist ',' table_ref_atomic { $1->push_back($3); $$ = $1; } ; @@ -752,7 +753,7 @@ opt_semicolon: ident_commalist: - IDENTIFIER { $$ = new List($1); } + IDENTIFIER { $$ = new std::vector(); $$->push_back($1); } | ident_commalist ',' IDENTIFIER { $1->push_back($3); $$ = $1; } ; diff --git a/src/sql_tests.cpp b/src/sql_tests.cpp index 4940d2e..7b4e32d 100644 --- a/src/sql_tests.cpp +++ b/src/sql_tests.cpp @@ -13,10 +13,10 @@ using namespace hsql; TEST(DeleteStatementTest) { SQLStatementList* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); ASSERT(stmt_list->isValid); - ASSERT_EQ(stmt_list->size(), 1); - ASSERT(stmt_list->at(0)->type() == kStmtDelete); + ASSERT_EQ(stmt_list->numStatements(), 1); + ASSERT(stmt_list->getStatement(0)->type() == kStmtDelete); - DeleteStatement* stmt = (DeleteStatement*) stmt_list->at(0); + DeleteStatement* stmt = (DeleteStatement*) stmt_list->getStatement(0); ASSERT_STREQ(stmt->table_name, "students"); ASSERT_NOTNULL(stmt->expr); ASSERT(stmt->expr->isType(kExprOperator)); @@ -27,10 +27,10 @@ TEST(DeleteStatementTest) { TEST(CreateStatementTest) { SQLStatementList* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); ASSERT(stmt_list->isValid); - ASSERT_EQ(stmt_list->size(), 1); - ASSERT_EQ(stmt_list->at(0)->type(), kStmtCreate); + ASSERT_EQ(stmt_list->numStatements(), 1); + ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtCreate); - CreateStatement* stmt = (CreateStatement*) stmt_list->at(0); + CreateStatement* stmt = (CreateStatement*) stmt_list->getStatement(0); ASSERT_EQ(stmt->type, CreateStatement::kTable); ASSERT_STREQ(stmt->table_name, "students"); ASSERT_NOTNULL(stmt->columns); @@ -49,10 +49,10 @@ TEST(CreateStatementTest) { TEST(UpdateStatementTest) { SQLStatementList* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); ASSERT(stmt_list->isValid); - ASSERT_EQ(stmt_list->size(), 1); - ASSERT_EQ(stmt_list->at(0)->type(), kStmtUpdate); + ASSERT_EQ(stmt_list->numStatements(), 1); + ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtUpdate); - UpdateStatement* stmt = (UpdateStatement*) stmt_list->at(0); + UpdateStatement* stmt = (UpdateStatement*) stmt_list->getStatement(0); ASSERT_NOTNULL(stmt->table); ASSERT_STREQ(stmt->table->name, "students"); @@ -77,8 +77,8 @@ TEST(UpdateStatementTest) { TEST(InsertStatementTest) { SQLStatementList* stmt_list = SQLParser::parseSQLString("INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)"); ASSERT(stmt_list->isValid); - ASSERT_EQ(stmt_list->size(), 1); - ASSERT_EQ(stmt_list->at(0)->type(), kStmtInsert); + ASSERT_EQ(stmt_list->numStatements(), 1); + ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtInsert); // TODO } @@ -87,10 +87,10 @@ TEST(InsertStatementTest) { TEST(DropTableStatementTest) { SQLStatementList* 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); + ASSERT_EQ(stmt_list->numStatements(), 1); + ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtDrop); - DropStatement* stmt = (DropStatement*) stmt_list->at(0); + DropStatement* stmt = (DropStatement*) stmt_list->getStatement(0); ASSERT_EQ(stmt->type, DropStatement::kTable); ASSERT_NOTNULL(stmt->name); ASSERT_STREQ(stmt->name, "students"); @@ -107,13 +107,13 @@ TEST(PrepareStatementTest) { ASSERT_EQ(prep_stmt->placeholders.size(), 3); - ASSERT_EQ(prep_stmt->query->size(), 2); - ASSERT_EQ(prep_stmt->query->at(0)->type(), kStmtInsert); - ASSERT_EQ(prep_stmt->query->at(1)->type(), kStmtSelect); + ASSERT_EQ(prep_stmt->query->numStatements(), 2); + ASSERT_EQ(prep_stmt->query->getStatement(0)->type(), kStmtInsert); + ASSERT_EQ(prep_stmt->query->getStatement(1)->type(), kStmtSelect); - InsertStatement* insert = (InsertStatement*) prep_stmt->query->at(0); - SelectStatement* select = (SelectStatement*) prep_stmt->query->at(1); + InsertStatement* insert = (InsertStatement*) prep_stmt->query->getStatement(0); + SelectStatement* select = (SelectStatement*) prep_stmt->query->getStatement(1); ASSERT(insert->values->at(0)->isType(kExprPlaceholder)); ASSERT(select->select_list->at(0)->isType(kExprPlaceholder)); diff --git a/src/tests/helper.h b/src/tests/helper.h index cc56a50..116b0d0 100644 --- a/src/tests/helper.h +++ b/src/tests/helper.h @@ -6,9 +6,9 @@ #define TEST_PARSE_SINGLE_SQL(query, stmt_type, stmt_class, output_var) \ SQLStatementList* stmt_list = SQLParser::parseSQLString(query); \ ASSERT(stmt_list->isValid); \ - ASSERT_EQ(stmt_list->size(), 1); \ - ASSERT_EQ(stmt_list->at(0)->type(), stmt_type); \ - stmt_class* output_var = (stmt_class*) stmt_list->at(0); + ASSERT_EQ(stmt_list->numStatements(), 1); \ + ASSERT_EQ(stmt_list->getStatement(0)->type(), stmt_type); \ + stmt_class* output_var = (stmt_class*) stmt_list->getStatement(0);