added destructors

This commit is contained in:
Pedro 2014-11-07 15:21:54 +01:00
parent c5d3a84395
commit 0b444c1955
11 changed files with 80 additions and 18 deletions

View File

@ -1,7 +1,7 @@
# Makefile
LIB_FILES = parser/bison_parser.cpp parser/flex_lexer.cpp parser/SQLParser.cpp lib/Expr.cpp lib/sqlhelper.cpp
LIB_FILES = parser/bison_parser.cpp parser/flex_lexer.cpp parser/SQLParser.cpp lib/Expr.cpp lib/sqlhelper.cpp lib/destructors.cpp
TESTS_MAIN = sql_tests.cpp
TESTS_BIN = bin/tests

View File

@ -9,15 +9,15 @@ make grammar_test
echo "\n\n"
./bin/grammar_test "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10"
./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;"
./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC"
./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5"
./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c LIKE 's%' LIMIT 10);"
./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);"
./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5"
./bin/grammar_test "SELECT a FROM foo WHERE a > 12 OR b > 3 AND NOT c LIMIT 10"
# ./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;"
./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC LIMIT 10; SELECT * AS table;"
# ./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5"
# ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c LIKE 's%' LIMIT 10);"
# ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);"
# ./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5"
./bin/grammar_test "IMPORT FROM TBL FILE 'students.tbl' INTO table"
# ./bin/grammar_test "IMPORT FROM TBL FILE 'students.tbl' INTO table"
# Error: Where clause in between join statement
# ./bin/grammar_test -f "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 ON a = b"
@ -32,8 +32,8 @@ echo "\n\n"
# ./bin/analysis "-- test
# SELECT * FROM table WHERE a NOT LIKE '%s' -- inline comment
# --my comment"
./bin/analysis "
IMPORT FROM TBL FILE 'students.tbl' INTO table;
SELECT * FROM table;"
# ./bin/analysis "
# IMPORT FROM TBL FILE 'students.tbl' INTO table;
# SELECT * FROM table;"
echo "\n\n"

View File

@ -86,8 +86,11 @@ Expr* Expr::makeFunctionRef(char* func_name, Expr* expr) {
return e;
}
// Expr::~Expr() {
// }
Expr::~Expr() {
delete expr;
delete expr2;
delete name;
delete table;
}
} // namespace hsql

View File

@ -2,6 +2,7 @@
#define __EXPRESSION_H__
#include <stdlib.h>
#include <memory>
namespace hsql {
@ -45,7 +46,10 @@ struct Expr {
Expr(ExprType type) : type(type) {};
// virtual ~Expr();
// Interesting side-effect:
// Making the destructor virtual causes segmentation faults
~Expr();
ExprType type;

View File

@ -20,6 +20,7 @@ typedef enum {
*/
struct ImportStatement : Statement {
ImportStatement() : Statement(kStmtImport) {};
virtual ~ImportStatement(); // defined in destructors.cpp
ImportFileType file_type;
const char* file_path;

View File

@ -17,6 +17,10 @@ public:
_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]; }

View File

@ -21,6 +21,8 @@ typedef enum {
struct OrderDescription {
OrderDescription(OrderType type, Expr* expr) : type(type), expr(expr) {}
virtual ~OrderDescription(); // defined in destructors.cpp
OrderType type;
Expr* expr;
};
@ -43,6 +45,7 @@ struct LimitDescription {
*/
struct SelectStatement : Statement {
SelectStatement() : Statement(kStmtSelect) {};
virtual ~SelectStatement(); // defined in destructors.cpp
TableRef* from_table;
List<Expr*>* select_list;

View File

@ -27,6 +27,7 @@ typedef enum {
struct Statement {
Statement(StatementType type) : type(type) {};
virtual ~Statement(); // defined in destructors.cpp
StatementType type;
};
@ -36,6 +37,7 @@ class StatementList : public List<Statement*> {
public:
StatementList() : List<Statement*>(), isValid(true) {};
StatementList(Statement* stmt) : List<Statement*>(stmt), isValid(true) {};
virtual ~StatementList(); // defined in destructors.cpp
bool isValid;
const char* parser_msg;

View File

@ -1,7 +1,7 @@
#ifndef __TABLEREF_H__
#define __TABLEREF_H__
#include <stdio.h>
namespace hsql {
class SelectStatement;
@ -30,6 +30,7 @@ typedef struct TableRef TableRef;
struct TableRef {
TableRef(TableRefType type) : type(type) {}
virtual ~TableRef(); // defined in destructors.cpp
TableRefType type;

43
src/lib/destructors.cpp Normal file
View File

@ -0,0 +1,43 @@
#include "sqllib.h"
namespace hsql {
Statement::~Statement() {
/* empty */
}
StatementList::~StatementList() {
delete parser_msg;
}
TableRef::~TableRef() {
delete name;
delete alias;
delete select;
delete list;
delete left;
delete right;
delete join_condition;
}
SelectStatement::~SelectStatement() {
delete from_table;
delete select_list;
delete where_clause;
delete group_by;
delete order;
delete limit;
}
OrderDescription::~OrderDescription() {
delete expr;
}
ImportStatement::~ImportStatement() {
delete file_path;
delete table_name;
}
} // namespace hsql

View File

@ -36,9 +36,10 @@ int main(int argc, char *argv[]) {
default:
fprintf(stderr, "\tStatement Type %u. No detailed print method available.\n", stmt->type);
break;
}
// delete stmt;
}
delete stmt_list;
}
return 0;