removed List class wrapper and use simply std::vector throughout

This commit is contained in:
Pedro 2015-01-06 19:47:59 +01:00
parent b5aba646d8
commit 71f9252019
12 changed files with 67 additions and 110 deletions

View File

@ -1,46 +0,0 @@
#ifndef __LIST_H__
#define __LIST_H__
#include <vector>
#include <stdlib.h>
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 <typename _T>
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

View File

@ -1,9 +1,9 @@
#ifndef __TABLEREF_H__ #ifndef __TABLEREF_H__
#define __TABLEREF_H__ #define __TABLEREF_H__
#include "List.h"
#include "Expr.h" #include "Expr.h"
#include <stdio.h> #include <stdio.h>
#include <vector>
namespace hsql { namespace hsql {
@ -47,7 +47,7 @@ struct TableRef {
char* alias; char* alias;
SelectStatement* select; SelectStatement* select;
List<TableRef*>* list; std::vector<TableRef*>* list;
JoinDefinition* join; JoinDefinition* join;

View File

@ -33,7 +33,7 @@ void printTableRefInfo(TableRef* table, uint num_indent) {
printExpression(table->join->condition, num_indent+2); printExpression(table->join->condition, num_indent+2);
break; break;
case kTableCrossProduct: case kTableCrossProduct:
for (TableRef* tbl : table->list->vector()) printTableRefInfo(tbl, num_indent); for (TableRef* tbl : *table->list) printTableRefInfo(tbl, num_indent);
break; break;
} }
if (table->alias != NULL) { if (table->alias != NULL) {
@ -76,7 +76,7 @@ void printExpression(Expr* expr, uint num_indent) {
void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) { void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) {
inprint("SelectStatement", num_indent); inprint("SelectStatement", num_indent);
inprint("Fields:", num_indent+1); 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); inprint("Sources:", num_indent+1);
printTableRefInfo(stmt->from_table, num_indent+2); 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); inprint(stmt->table_name, num_indent+1);
if (stmt->columns != NULL) { if (stmt->columns != NULL) {
inprint("Columns", num_indent+1); inprint("Columns", num_indent+1);
for (char* col_name : stmt->columns->vector()) { for (char* col_name : *stmt->columns) {
inprint(col_name, num_indent+2); inprint(col_name, num_indent+2);
} }
} }
switch (stmt->type) { switch (stmt->type) {
case InsertStatement::kInsertValues: case InsertStatement::kInsertValues:
inprint("Values", num_indent+1); inprint("Values", num_indent+1);
for (Expr* expr : stmt->values->vector()) { for (Expr* expr : *stmt->values) {
printExpression(expr, num_indent+2); printExpression(expr, num_indent+2);
} }
break; break;

View File

@ -56,7 +56,7 @@ struct CreateStatement : SQLStatement {
CreateType type; CreateType type;
bool if_not_exists; bool if_not_exists;
List<ColumnDefinition*>* columns; std::vector<ColumnDefinition*>* columns;
const char* file_path; const char* file_path;
const char* table_name; const char* table_name;

View File

@ -13,8 +13,7 @@ namespace hsql {
struct ExecuteStatement : SQLStatement { struct ExecuteStatement : SQLStatement {
ExecuteStatement() : ExecuteStatement() :
SQLStatement(kStmtExecute), SQLStatement(kStmtExecute),
name(NULL), name(NULL) {}
parameters(NULL) {}
virtual ~ExecuteStatement() { virtual ~ExecuteStatement() {
delete name; delete name;
@ -22,7 +21,7 @@ struct ExecuteStatement : SQLStatement {
} }
const char* name; const char* name;
List<Expr*>* parameters; std::vector<Expr*>* parameters;
}; };

View File

@ -34,8 +34,8 @@ struct InsertStatement : SQLStatement {
InsertType type; InsertType type;
const char* table_name; const char* table_name;
List<char*>* columns; std::vector<char*>* columns;
List<Expr*>* values; std::vector<Expr*>* values;
SelectStatement* select; SelectStatement* select;
}; };

View File

@ -5,7 +5,6 @@
#ifndef __STATEMENT_H__ #ifndef __STATEMENT_H__
#define __STATEMENT_H__ #define __STATEMENT_H__
#include "List.h"
#include "Expr.h" #include "Expr.h"
#include <vector> #include <vector>
@ -50,22 +49,27 @@ private:
* @struct SQLStatementList * @struct SQLStatementList
* @brief Represents the result of the SQLParser. If parsing was successful it is a list of SQLStatement. * @brief Represents the result of the SQLParser. If parsing was successful it is a list of SQLStatement.
*/ */
class SQLStatementList : public List<SQLStatement*> { struct SQLStatementList {
public: public:
SQLStatementList() : SQLStatementList() :
List<SQLStatement*>(),
isValid(true), isValid(true),
parser_msg(NULL) {}; parser_msg(NULL) {};
SQLStatementList(SQLStatement* stmt) : SQLStatementList(SQLStatement* stmt) :
List<SQLStatement*>(stmt),
isValid(true), isValid(true),
parser_msg(NULL) {}; parser_msg(NULL) {
addStatement(stmt);
};
virtual ~SQLStatementList() { virtual ~SQLStatementList() {
delete parser_msg; 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<SQLStatement*> statements;
bool isValid; bool isValid;
const char* parser_msg; const char* parser_msg;
int error_line; int error_line;

View File

@ -3,7 +3,6 @@
#include "SQLStatement.h" #include "SQLStatement.h"
#include "Expr.h" #include "Expr.h"
#include "List.h"
#include "Table.h" #include "Table.h"
namespace hsql { namespace hsql {
@ -62,7 +61,7 @@ struct GroupByDescription {
delete having; delete having;
} }
List<Expr*>* columns; std::vector<Expr*>* columns;
Expr* having; Expr* having;
}; };
@ -94,7 +93,7 @@ struct SelectStatement : SQLStatement {
TableRef* from_table; TableRef* from_table;
bool select_distinct; bool select_distinct;
List<Expr*>* select_list; std::vector<Expr*>* select_list;
Expr* where_clause; Expr* where_clause;
GroupByDescription* group_by; GroupByDescription* group_by;

View File

@ -35,7 +35,7 @@ struct UpdateStatement : SQLStatement {
// TODO: switch to char* instead of TableRef // TODO: switch to char* instead of TableRef
TableRef* table; TableRef* table;
List<UpdateClause*>* updates; std::vector<UpdateClause*>* updates;
Expr* where; Expr* where;
}; };

View File

@ -124,11 +124,12 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c
hsql::UpdateClause* update_t; hsql::UpdateClause* update_t;
hsql::SQLStatementList* stmt_list; hsql::SQLStatementList* stmt_list;
hsql::List<char*>* slist;
hsql::List<hsql::Expr*>* expr_list; std::vector<char*>* str_vec;
hsql::List<hsql::TableRef*>* table_list; std::vector<hsql::TableRef*>* table_vec;
hsql::List<hsql::ColumnDefinition*>* column_list_t; std::vector<hsql::ColumnDefinition*>* column_vec;
hsql::List<hsql::UpdateClause*>* update_list_t; std::vector<hsql::UpdateClause*>* update_vec;
std::vector<hsql::Expr*>* expr_vec;
} }
@ -180,18 +181,19 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias placeholder_expr %type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias placeholder_expr
%type <expr> column_name literal int_literal num_literal string_literal %type <expr> column_name literal int_literal num_literal string_literal
%type <expr> comp_expr opt_where join_condition opt_having %type <expr> comp_expr opt_where join_condition opt_having
%type <expr_list> expr_list select_list literal_list
%type <table_list> table_ref_commalist
%type <order> opt_order %type <order> opt_order
%type <limit> opt_limit %type <limit> opt_limit
%type <order_type> opt_order_type %type <order_type> opt_order_type
%type <slist> ident_commalist opt_column_list
%type <column_t> column_def %type <column_t> column_def
%type <column_list_t> column_def_commalist
%type <update_t> update_clause %type <update_t> update_clause
%type <update_list_t> update_clause_commalist
%type <group_t> opt_group %type <group_t> opt_group
%type <str_vec> ident_commalist opt_column_list
%type <expr_vec> expr_list select_list literal_list
%type <table_vec> table_ref_commalist
%type <update_vec> update_clause_commalist
%type <column_vec> column_def_commalist
/****************************** /******************************
** Token Precedence and Associativity ** Token Precedence and Associativity
** Precedence: lowest to highest ** Precedence: lowest to highest
@ -234,7 +236,7 @@ input:
statement_list: statement_list:
statement { $$ = new SQLStatementList($1); } statement { $$ = new SQLStatementList($1); }
| statement_list ';' statement { $1->push_back($3); $$ = $1; } | statement_list ';' statement { $1->addStatement($3); $$ = $1; }
; ;
statement: statement:
@ -270,7 +272,6 @@ execute_statement:
EXECUTE IDENTIFIER { EXECUTE IDENTIFIER {
$$ = new ExecuteStatement(); $$ = new ExecuteStatement();
$$->name = $2; $$->name = $2;
$$->parameters = NULL;
} }
| EXECUTE IDENTIFIER '(' literal_list ')' { | EXECUTE IDENTIFIER '(' literal_list ')' {
$$ = new ExecuteStatement(); $$ = new ExecuteStatement();
@ -326,7 +327,7 @@ opt_not_exists:
; ;
column_def_commalist: column_def_commalist:
column_def { $$ = new List<ColumnDefinition*>($1); } column_def { $$ = new std::vector<ColumnDefinition*>(); $$->push_back($1); }
| column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; } | column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; }
; ;
@ -417,7 +418,7 @@ update_statement:
; ;
update_clause_commalist: update_clause_commalist:
update_clause { $$ = new List<UpdateClause*>($1); } update_clause { $$ = new std::vector<UpdateClause*>(); $$->push_back($1); }
| update_clause_commalist ',' update_clause { $1->push_back($3); $$ = $1; } | update_clause_commalist ',' update_clause { $1->push_back($3); $$ = $1; }
; ;
@ -533,12 +534,12 @@ opt_limit:
* Expressions * Expressions
******************************/ ******************************/
expr_list: expr_list:
expr_alias { $$ = new List<Expr*>($1); } expr_alias { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| expr_list ',' expr_alias { $1->push_back($3); $$ = $1; } | expr_list ',' expr_alias { $1->push_back($3); $$ = $1; }
; ;
literal_list: literal_list:
literal { $$ = new List<Expr*>($1); } literal { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| literal_list ',' literal { $1->push_back($3); $$ = $1; } | literal_list ',' literal { $1->push_back($3); $$ = $1; }
; ;
@ -661,7 +662,7 @@ table_ref_atomic:
table_ref_commalist: table_ref_commalist:
table_ref_atomic { $$ = new List<TableRef*>($1); } table_ref_atomic { $$ = new std::vector<TableRef*>(); $$->push_back($1); }
| table_ref_commalist ',' table_ref_atomic { $1->push_back($3); $$ = $1; } | table_ref_commalist ',' table_ref_atomic { $1->push_back($3); $$ = $1; }
; ;
@ -752,7 +753,7 @@ opt_semicolon:
ident_commalist: ident_commalist:
IDENTIFIER { $$ = new List<char*>($1); } IDENTIFIER { $$ = new std::vector<char*>(); $$->push_back($1); }
| ident_commalist ',' IDENTIFIER { $1->push_back($3); $$ = $1; } | ident_commalist ',' IDENTIFIER { $1->push_back($3); $$ = $1; }
; ;

View File

@ -13,10 +13,10 @@ using namespace hsql;
TEST(DeleteStatementTest) { TEST(DeleteStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); SQLStatementList* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->size(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT(stmt_list->at(0)->type() == kStmtDelete); 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_STREQ(stmt->table_name, "students");
ASSERT_NOTNULL(stmt->expr); ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator)); ASSERT(stmt->expr->isType(kExprOperator));
@ -27,10 +27,10 @@ TEST(DeleteStatementTest) {
TEST(CreateStatementTest) { TEST(CreateStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); SQLStatementList* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->size(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT_EQ(stmt_list->at(0)->type(), kStmtCreate); 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_EQ(stmt->type, CreateStatement::kTable);
ASSERT_STREQ(stmt->table_name, "students"); ASSERT_STREQ(stmt->table_name, "students");
ASSERT_NOTNULL(stmt->columns); ASSERT_NOTNULL(stmt->columns);
@ -49,10 +49,10 @@ TEST(CreateStatementTest) {
TEST(UpdateStatementTest) { TEST(UpdateStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); SQLStatementList* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->size(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT_EQ(stmt_list->at(0)->type(), kStmtUpdate); 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_NOTNULL(stmt->table);
ASSERT_STREQ(stmt->table->name, "students"); ASSERT_STREQ(stmt->table->name, "students");
@ -77,8 +77,8 @@ TEST(UpdateStatementTest) {
TEST(InsertStatementTest) { TEST(InsertStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)"); SQLStatementList* stmt_list = SQLParser::parseSQLString("INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->size(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT_EQ(stmt_list->at(0)->type(), kStmtInsert); ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtInsert);
// TODO // TODO
} }
@ -87,10 +87,10 @@ TEST(InsertStatementTest) {
TEST(DropTableStatementTest) { TEST(DropTableStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("DROP TABLE students"); SQLStatementList* stmt_list = SQLParser::parseSQLString("DROP TABLE students");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->size(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT_EQ(stmt_list->at(0)->type(), kStmtDrop); 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_EQ(stmt->type, DropStatement::kTable);
ASSERT_NOTNULL(stmt->name); ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students"); ASSERT_STREQ(stmt->name, "students");
@ -107,13 +107,13 @@ TEST(PrepareStatementTest) {
ASSERT_EQ(prep_stmt->placeholders.size(), 3); ASSERT_EQ(prep_stmt->placeholders.size(), 3);
ASSERT_EQ(prep_stmt->query->size(), 2); ASSERT_EQ(prep_stmt->query->numStatements(), 2);
ASSERT_EQ(prep_stmt->query->at(0)->type(), kStmtInsert); ASSERT_EQ(prep_stmt->query->getStatement(0)->type(), kStmtInsert);
ASSERT_EQ(prep_stmt->query->at(1)->type(), kStmtSelect); ASSERT_EQ(prep_stmt->query->getStatement(1)->type(), kStmtSelect);
InsertStatement* insert = (InsertStatement*) prep_stmt->query->at(0); InsertStatement* insert = (InsertStatement*) prep_stmt->query->getStatement(0);
SelectStatement* select = (SelectStatement*) prep_stmt->query->at(1); SelectStatement* select = (SelectStatement*) prep_stmt->query->getStatement(1);
ASSERT(insert->values->at(0)->isType(kExprPlaceholder)); ASSERT(insert->values->at(0)->isType(kExprPlaceholder));
ASSERT(select->select_list->at(0)->isType(kExprPlaceholder)); ASSERT(select->select_list->at(0)->isType(kExprPlaceholder));

View File

@ -6,9 +6,9 @@
#define TEST_PARSE_SINGLE_SQL(query, stmt_type, stmt_class, output_var) \ #define TEST_PARSE_SINGLE_SQL(query, stmt_type, stmt_class, output_var) \
SQLStatementList* stmt_list = SQLParser::parseSQLString(query); \ SQLStatementList* stmt_list = SQLParser::parseSQLString(query); \
ASSERT(stmt_list->isValid); \ ASSERT(stmt_list->isValid); \
ASSERT_EQ(stmt_list->size(), 1); \ ASSERT_EQ(stmt_list->numStatements(), 1); \
ASSERT_EQ(stmt_list->at(0)->type(), stmt_type); \ ASSERT_EQ(stmt_list->getStatement(0)->type(), stmt_type); \
stmt_class* output_var = (stmt_class*) stmt_list->at(0); stmt_class* output_var = (stmt_class*) stmt_list->getStatement(0);