fixed leaks triggered by sql_tests.cpp

This commit is contained in:
Pedro 2017-02-08 04:10:26 +01:00
parent a362e86da1
commit 69e9673763
8 changed files with 84 additions and 25 deletions

View File

@ -34,8 +34,8 @@ namespace hsql {
CreateType type;
bool ifNotExists; // default: false
const char* filePath; // default: NULL
const char* tableName; // default: NULL
char* filePath; // default: NULL
char* tableName; // default: NULL
std::vector<ColumnDefinition*>* columns; // default: NULL
};

View File

@ -20,7 +20,7 @@ namespace hsql {
virtual ~DropStatement();
EntityType type;
const char* name;
char* name;
};
} // namespace hsql

View File

@ -12,7 +12,7 @@ namespace hsql {
ExecuteStatement();
virtual ~ExecuteStatement();
const char* name;
char* name;
std::vector<Expr*>* parameters;
};

View File

@ -124,7 +124,7 @@ namespace hsql {
char* substr(const char* source, int from, int to) {
int len = to - from;
char* copy = new char[len + 1];
char* copy = (char*) malloc(len + 1);;
strncpy(copy, source + from, len);
copy[len] = '\0';
return copy;

View File

@ -19,7 +19,7 @@ namespace hsql {
virtual ~InsertStatement();
InsertType type;
const char* tableName;
char* tableName;
std::vector<char*>* columns;
std::vector<Expr*>* values;
SelectStatement* select;

View File

@ -23,8 +23,12 @@ namespace hsql {
*/
void setPlaceholders(std::vector<void*> ph);
const char* name;
char* name;
SQLParserResult* query;
// The expressions are not owned by this statement.
// Rather they are owned by the query and destroyed, when
// the query is destroyed.
std::vector<Expr*> placeholders;
};

View File

@ -19,7 +19,7 @@ namespace hsql {
type(type) {};
ColumnDefinition::~ColumnDefinition() {
delete name;
free(name);
}
// CreateStatemnet
@ -32,9 +32,15 @@ namespace hsql {
columns(NULL) {};
CreateStatement::~CreateStatement() {
free(filePath);
free(tableName);
if (columns != NULL) {
for (ColumnDefinition* def : *columns) {
delete def;
}
delete columns;
delete filePath;
delete tableName;
}
}
// DeleteStatement
@ -44,7 +50,7 @@ namespace hsql {
expr(NULL) {};
DeleteStatement::~DeleteStatement() {
delete tableName;
free(tableName);
delete expr;
}
@ -55,7 +61,7 @@ namespace hsql {
name(NULL) {}
DropStatement::~DropStatement() {
delete name;
free(name);
}
// ExecuteStatement
@ -65,9 +71,15 @@ namespace hsql {
parameters(NULL) {}
ExecuteStatement::~ExecuteStatement() {
delete name;
free(name);
if (parameters != NULL) {
for (Expr* param : *parameters) {
delete param;
}
delete parameters;
}
}
// ImportStatement
ImportStatement::ImportStatement(ImportType type) :
@ -91,10 +103,22 @@ namespace hsql {
select(NULL) {}
InsertStatement::~InsertStatement() {
delete tableName;
delete columns;
delete values;
free(tableName);
delete select;
if (columns != NULL) {
for (char* column : *columns) {
free(column);
}
delete columns;
}
if (values != NULL) {
for (Expr* expr : *values) {
delete expr;
}
delete values;
}
}
// PrepareStatement
@ -105,7 +129,7 @@ namespace hsql {
PrepareStatement::~PrepareStatement() {
delete query;
delete name;
free(name);
}
void PrepareStatement::setPlaceholders(std::vector<void*> ph) {
@ -189,8 +213,16 @@ namespace hsql {
UpdateStatement::~UpdateStatement() {
delete table;
delete updates;
delete where;
if (updates != NULL) {
for (UpdateClause* update : *updates) {
free(update->column);
delete update->value;
delete update;
}
delete updates;
}
}
// TableRef

View File

@ -22,6 +22,8 @@ TEST(DeleteStatementTest) {
ASSERT(stmt->expr->isType(kExprOperator));
ASSERT_STREQ(stmt->expr->expr->name, "grade");
ASSERT_EQ(stmt->expr->expr2->fval, 2.0);
delete result;
}
TEST(CreateStatementTest) {
@ -43,6 +45,8 @@ TEST(CreateStatementTest) {
ASSERT_EQ(stmt->columns->at(1)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(2)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE);
delete result;
}
@ -69,25 +73,40 @@ TEST(UpdateStatementTest) {
ASSERT(stmt->where->isType(kExprOperator));
ASSERT(stmt->where->isSimpleOp('='));
ASSERT_STREQ(stmt->where->expr->name, "name");
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");\
delete result;
}
TEST(InsertStatementTest) {
TEST_PARSE_SINGLE_SQL("INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)", kStmtInsert, InsertStatement, stmt);
TEST_PARSE_SINGLE_SQL(
"INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)",
kStmtInsert,
InsertStatement,
result,
stmt);
ASSERT_EQ(stmt->values->size(), 4);
// TODO
delete result;
}
TEST(DropTableStatementTest) {
TEST_PARSE_SINGLE_SQL("DROP TABLE students", kStmtDrop, DropStatement, stmt);
TEST_PARSE_SINGLE_SQL(
"DROP TABLE students",
kStmtDrop,
DropStatement,
result,
stmt);
ASSERT_EQ(stmt->type, DropStatement::kTable);
ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students");
delete result;
}
@ -134,12 +153,16 @@ TEST(PrepareStatementTest) {
// Deallocate Statement
ASSERT_EQ(drop->type, DropStatement::kPreparedStatement);
ASSERT_STREQ(drop->name, "stmt");
delete result;
}
TEST(ExecuteStatementTest) {
TEST_PARSE_SINGLE_SQL("EXECUTE test(1, 2);", kStmtExecute, ExecuteStatement, stmt);
TEST_PARSE_SINGLE_SQL("EXECUTE test(1, 2);", kStmtExecute, ExecuteStatement, result, stmt);
ASSERT_STREQ(stmt->name, "test");
ASSERT_EQ(stmt->parameters->size(), 2);
delete result;
}