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; CreateType type;
bool ifNotExists; // default: false bool ifNotExists; // default: false
const char* filePath; // default: NULL char* filePath; // default: NULL
const char* tableName; // default: NULL char* tableName; // default: NULL
std::vector<ColumnDefinition*>* columns; // default: NULL std::vector<ColumnDefinition*>* columns; // default: NULL
}; };

View File

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

View File

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

View File

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

View File

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

View File

@ -23,8 +23,12 @@ namespace hsql {
*/ */
void setPlaceholders(std::vector<void*> ph); void setPlaceholders(std::vector<void*> ph);
const char* name; char* name;
SQLParserResult* query; 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; std::vector<Expr*> placeholders;
}; };

View File

@ -19,7 +19,7 @@ namespace hsql {
type(type) {}; type(type) {};
ColumnDefinition::~ColumnDefinition() { ColumnDefinition::~ColumnDefinition() {
delete name; free(name);
} }
// CreateStatemnet // CreateStatemnet
@ -32,9 +32,15 @@ namespace hsql {
columns(NULL) {}; columns(NULL) {};
CreateStatement::~CreateStatement() { CreateStatement::~CreateStatement() {
delete columns; free(filePath);
delete filePath; free(tableName);
delete tableName;
if (columns != NULL) {
for (ColumnDefinition* def : *columns) {
delete def;
}
delete columns;
}
} }
// DeleteStatement // DeleteStatement
@ -44,7 +50,7 @@ namespace hsql {
expr(NULL) {}; expr(NULL) {};
DeleteStatement::~DeleteStatement() { DeleteStatement::~DeleteStatement() {
delete tableName; free(tableName);
delete expr; delete expr;
} }
@ -55,7 +61,7 @@ namespace hsql {
name(NULL) {} name(NULL) {}
DropStatement::~DropStatement() { DropStatement::~DropStatement() {
delete name; free(name);
} }
// ExecuteStatement // ExecuteStatement
@ -65,8 +71,14 @@ namespace hsql {
parameters(NULL) {} parameters(NULL) {}
ExecuteStatement::~ExecuteStatement() { ExecuteStatement::~ExecuteStatement() {
delete name; free(name);
delete parameters;
if (parameters != NULL) {
for (Expr* param : *parameters) {
delete param;
}
delete parameters;
}
} }
// ImportStatement // ImportStatement
@ -91,10 +103,22 @@ namespace hsql {
select(NULL) {} select(NULL) {}
InsertStatement::~InsertStatement() { InsertStatement::~InsertStatement() {
delete tableName; free(tableName);
delete columns;
delete values;
delete select; 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 // PrepareStatement
@ -105,7 +129,7 @@ namespace hsql {
PrepareStatement::~PrepareStatement() { PrepareStatement::~PrepareStatement() {
delete query; delete query;
delete name; free(name);
} }
void PrepareStatement::setPlaceholders(std::vector<void*> ph) { void PrepareStatement::setPlaceholders(std::vector<void*> ph) {
@ -114,7 +138,7 @@ namespace hsql {
placeholders.push_back((Expr*) e); placeholders.push_back((Expr*) e);
} }
// Sort by col-id // Sort by col-id
std::sort(placeholders.begin(), placeholders.end(), [](Expr * i, Expr * j) -> bool { return (i->ival < j->ival); }); std::sort(placeholders.begin(), placeholders.end(), [](Expr* i, Expr* j) -> bool { return (i->ival < j->ival); });
// Set the placeholder id on the Expr. This replaces the previously stored column id // Set the placeholder id on the Expr. This replaces the previously stored column id
for (uintmax_t i = 0; i < placeholders.size(); ++i) placeholders[i]->ival = i; for (uintmax_t i = 0; i < placeholders.size(); ++i) placeholders[i]->ival = i;
@ -189,8 +213,16 @@ namespace hsql {
UpdateStatement::~UpdateStatement() { UpdateStatement::~UpdateStatement() {
delete table; delete table;
delete updates;
delete where; delete where;
if (updates != NULL) {
for (UpdateClause* update : *updates) {
free(update->column);
delete update->value;
delete update;
}
delete updates;
}
} }
// TableRef // TableRef

View File

@ -22,6 +22,8 @@ TEST(DeleteStatementTest) {
ASSERT(stmt->expr->isType(kExprOperator)); ASSERT(stmt->expr->isType(kExprOperator));
ASSERT_STREQ(stmt->expr->expr->name, "grade"); ASSERT_STREQ(stmt->expr->expr->name, "grade");
ASSERT_EQ(stmt->expr->expr2->fval, 2.0); ASSERT_EQ(stmt->expr->expr2->fval, 2.0);
delete result;
} }
TEST(CreateStatementTest) { TEST(CreateStatementTest) {
@ -43,6 +45,8 @@ TEST(CreateStatementTest) {
ASSERT_EQ(stmt->columns->at(1)->type, ColumnDefinition::INT); ASSERT_EQ(stmt->columns->at(1)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(2)->type, ColumnDefinition::INT); ASSERT_EQ(stmt->columns->at(2)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE); 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->isType(kExprOperator));
ASSERT(stmt->where->isSimpleOp('=')); ASSERT(stmt->where->isSimpleOp('='));
ASSERT_STREQ(stmt->where->expr->name, "name"); 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(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); ASSERT_EQ(stmt->values->size(), 4);
// TODO // TODO
delete result;
} }
TEST(DropTableStatementTest) { 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_EQ(stmt->type, DropStatement::kTable);
ASSERT_NOTNULL(stmt->name); ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students"); ASSERT_STREQ(stmt->name, "students");
delete result;
} }
@ -134,12 +153,16 @@ TEST(PrepareStatementTest) {
// Deallocate Statement // Deallocate Statement
ASSERT_EQ(drop->type, DropStatement::kPreparedStatement); ASSERT_EQ(drop->type, DropStatement::kPreparedStatement);
ASSERT_STREQ(drop->name, "stmt"); ASSERT_STREQ(drop->name, "stmt");
delete result;
} }
TEST(ExecuteStatementTest) { 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_STREQ(stmt->name, "test");
ASSERT_EQ(stmt->parameters->size(), 2); ASSERT_EQ(stmt->parameters->size(), 2);
delete result;
} }