moved implementations from header files to statements.cpp

This commit is contained in:
Pedro Flemming 2017-02-03 16:50:18 +01:00
parent eb262d1bde
commit 0946624d54
15 changed files with 284 additions and 218 deletions

View File

@ -3,10 +3,9 @@
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
/**
* Represents definition of a table column
*/
// Represents definition of a table column
struct ColumnDefinition {
enum DataType {
TEXT,
@ -14,48 +13,30 @@ namespace hsql {
DOUBLE
};
ColumnDefinition(char* name, DataType type) :
name(name),
type(type) {}
virtual ~ColumnDefinition() {
delete name;
}
ColumnDefinition(char* name, DataType type);
virtual ~ColumnDefinition();
char* name;
DataType type;
};
/**
* Represents SQL Create statements.
* Example: "CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)"
*/
// Represents SQL Create statements.
// Example: "CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)"
struct CreateStatement : SQLStatement {
enum CreateType {
kTable,
kTableFromTbl // Hyrise file format
};
CreateStatement(CreateType type) :
SQLStatement(kStmtCreate),
type(type),
ifNotExists(false),
filePath(NULL),
tableName(NULL),
columns(NULL) {};
virtual ~CreateStatement() {
delete columns;
delete filePath;
delete tableName;
}
CreateStatement(CreateType type);
virtual ~CreateStatement();
CreateType type;
bool ifNotExists;
const char* filePath;
const char* tableName;
std::vector<ColumnDefinition*>* columns;
bool ifNotExists; // default: false
const char* filePath; // default: NULL
const char* tableName; // default: NULL
std::vector<ColumnDefinition*>* columns; // default: NULL
};
} // namespace hsql

View File

@ -3,23 +3,14 @@
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
/**
* Represents SQL Delete statements.
* Example: "DELETE FROM students WHERE grade > 3.0"
*
* Note: if (expr == NULL) => delete all rows (truncate)
*/
// Represents SQL Delete statements.
// Example: "DELETE FROM students WHERE grade > 3.0"
// Note: if (expr == NULL) => delete all rows (truncate)
struct DeleteStatement : SQLStatement {
DeleteStatement() :
SQLStatement(kStmtDelete),
tableName(NULL),
expr(NULL) {};
virtual ~DeleteStatement() {
delete tableName;
delete expr;
}
DeleteStatement();
virtual ~DeleteStatement();
char* tableName;
Expr* expr;

View File

@ -3,11 +3,10 @@
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
/**
* Represents SQL Delete statements.
* Example "DROP TABLE students;"
*/
// Represents SQL Delete statements.
// Example "DROP TABLE students;"
struct DropStatement : SQLStatement {
enum EntityType {
kTable,
@ -17,14 +16,8 @@ namespace hsql {
kPreparedStatement
};
DropStatement(EntityType type) :
SQLStatement(kStmtDrop),
type(type),
name(NULL) {}
virtual ~DropStatement() {
delete name;
}
DropStatement(EntityType type);
virtual ~DropStatement();
EntityType type;
const char* name;

View File

@ -9,15 +9,8 @@ namespace hsql {
* Example: "EXECUTE ins_prep(100, "test", 2.3);"
*/
struct ExecuteStatement : SQLStatement {
ExecuteStatement() :
SQLStatement(kStmtExecute),
name(NULL),
parameters(NULL) {}
virtual ~ExecuteStatement() {
delete name;
delete parameters;
}
ExecuteStatement();
virtual ~ExecuteStatement();
const char* name;
std::vector<Expr*>* parameters;

View File

@ -15,6 +15,20 @@ namespace hsql {
Expr::Expr(ExprType type) :
type(type),
expr(NULL),
expr2(NULL),
name(NULL),
table(NULL),
alias(NULL) {};
Expr::~Expr() {
delete expr;
delete expr2;
delete name;
delete table;
}
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
Expr* e = new Expr(kExprOperator);
@ -92,11 +106,4 @@ namespace hsql {
return e;
}
Expr::~Expr() {
delete expr;
delete expr2;
delete name;
delete table;
}
} // namespace hsql

View File

@ -56,13 +56,7 @@ namespace hsql {
Expr(ExprType type) :
type(type),
expr(NULL),
expr2(NULL),
name(NULL),
table(NULL),
alias(NULL) {};
Expr(ExprType type);
// Interesting side-effect:
// Making the destructor virtual used to cause segmentation faults

View File

@ -13,16 +13,8 @@ namespace hsql {
kImportTbl, // Hyrise file format
};
ImportStatement(ImportType type) :
SQLStatement(kStmtImport),
type(type),
filePath(NULL),
tableName(NULL) {};
virtual ~ImportStatement() {
delete filePath;
delete tableName;
}
ImportStatement(ImportType type);
virtual ~ImportStatement();
ImportType type;
const char* filePath;

View File

@ -15,20 +15,8 @@ namespace hsql {
kInsertSelect
};
InsertStatement(InsertType type) :
SQLStatement(kStmtInsert),
type(type),
tableName(NULL),
columns(NULL),
values(NULL),
select(NULL) {}
virtual ~InsertStatement() {
delete tableName;
delete columns;
delete values;
delete select;
}
InsertStatement(InsertType type);
virtual ~InsertStatement();
InsertType type;
const char* tableName;

View File

@ -12,15 +12,8 @@ namespace hsql {
* Example: "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?"
*/
struct PrepareStatement : SQLStatement {
PrepareStatement() :
SQLStatement(kStmtPrepare),
name(NULL),
query(NULL) {}
virtual ~PrepareStatement() {
delete query;
delete name;
}
PrepareStatement();
virtual ~PrepareStatement();
/**
* When setting the placeholders we need to make sure that they are in the correct order.
@ -28,17 +21,7 @@ namespace hsql {
*
* @param vector of placeholders that the parser found
*/
void setPlaceholders(std::vector<void*> ph) {
for (void* e : ph) {
if (e != NULL)
placeholders.push_back((Expr*) e);
}
// Sort by col-id
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
for (uintmax_t i = 0; i < placeholders.size(); ++i) placeholders[i]->ival = i;
}
void setPlaceholders(std::vector<void*> ph);
const char* name;
SQLParserResult* query;

View File

@ -25,14 +25,11 @@ namespace hsql {
* Base struct for every SQL statement
*/
struct SQLStatement {
SQLStatement(StatementType type) :
_type(type) {};
SQLStatement(StatementType type);
virtual ~SQLStatement() {}
virtual ~SQLStatement();
virtual StatementType type() {
return _type;
}
virtual StatementType type();
private:
StatementType _type;

View File

@ -16,13 +16,8 @@ namespace hsql {
* TODO: hold multiple expressions to be sorted by
*/
struct OrderDescription {
OrderDescription(OrderType type, Expr* expr) :
type(type),
expr(expr) {}
virtual ~OrderDescription() {
delete expr;
}
OrderDescription(OrderType type, Expr* expr);
virtual ~OrderDescription();
OrderType type;
Expr* expr;
@ -35,9 +30,7 @@ namespace hsql {
* Description of the limit clause within a select statement
*/
struct LimitDescription {
LimitDescription(int64_t limit, int64_t offset) :
limit(limit),
offset(offset) {}
LimitDescription(int64_t limit, int64_t offset);
int64_t limit;
int64_t offset;
@ -47,14 +40,9 @@ namespace hsql {
* Description of the group-by clause within a select statement
*/
struct GroupByDescription {
GroupByDescription() :
columns(NULL),
having(NULL) {}
~GroupByDescription() {
delete columns;
delete having;
}
GroupByDescription();
// TODO: make virtual
~GroupByDescription();
std::vector<Expr*>* columns;
Expr* having;
@ -65,25 +53,8 @@ namespace hsql {
* TODO: add union_order and union_limit
*/
struct SelectStatement : SQLStatement {
SelectStatement() :
SQLStatement(kStmtSelect),
fromTable(NULL),
selectDistinct(false),
selectList(NULL),
whereClause(NULL),
groupBy(NULL),
unionSelect(NULL),
order(NULL),
limit(NULL) {};
virtual ~SelectStatement() {
delete fromTable;
delete selectList;
delete whereClause;
delete groupBy;
delete order;
delete limit;
}
SelectStatement();
virtual ~SelectStatement();
TableRef* fromTable;
bool selectDistinct;

View File

@ -29,15 +29,7 @@ namespace hsql {
* @brief Holds reference to tables. Can be either table names or a select statement.
*/
struct TableRef {
TableRef(TableRefType type) :
type(type),
schema(NULL),
name(NULL),
alias(NULL),
select(NULL),
list(NULL),
join(NULL) {}
TableRef(TableRefType type);
virtual ~TableRef();
TableRefType type;
@ -82,17 +74,8 @@ namespace hsql {
* @brief Definition of a join table
*/
struct JoinDefinition {
JoinDefinition() :
left(NULL),
right(NULL),
condition(NULL),
type(kJoinInner) {}
virtual ~JoinDefinition() {
delete left;
delete right;
delete condition;
}
JoinDefinition();
virtual ~JoinDefinition();
TableRef* left;
TableRef* right;

View File

@ -16,17 +16,8 @@ namespace hsql {
* Represents SQL Update statements.
*/
struct UpdateStatement : SQLStatement {
UpdateStatement() :
SQLStatement(kStmtUpdate),
table(NULL),
updates(NULL),
where(NULL) {}
virtual ~UpdateStatement() {
delete table;
delete updates;
delete where;
}
UpdateStatement();
virtual ~UpdateStatement();
// TODO: switch to char* instead of TableRef
TableRef* table;

View File

@ -1,16 +0,0 @@
#include "Table.h"
#include "SelectStatement.h"
namespace hsql {
TableRef::~TableRef() {
delete name;
delete alias;
delete select;
delete list;
}
} // namespace hsql

218
src/sql/statements.cpp Normal file
View File

@ -0,0 +1,218 @@
#include "statements.h"
namespace hsql {
// SQLStatement
SQLStatement::SQLStatement(StatementType type) :
_type(type) {};
SQLStatement::~SQLStatement() {}
StatementType SQLStatement::type() {
return _type;
}
// ColumnDefinition
ColumnDefinition::ColumnDefinition(char* name, DataType type) :
name(name),
type(type) {};
ColumnDefinition::~ColumnDefinition() {
delete name;
}
// CreateStatemnet
CreateStatement::CreateStatement(CreateType type) :
SQLStatement(kStmtCreate),
type(type),
ifNotExists(false),
filePath(NULL),
tableName(NULL),
columns(NULL) {};
CreateStatement::~CreateStatement() {
delete columns;
delete filePath;
delete tableName;
}
// DeleteStatement
DeleteStatement::DeleteStatement() :
SQLStatement(kStmtDelete),
tableName(NULL),
expr(NULL) {};
DeleteStatement::~DeleteStatement() {
delete tableName;
delete expr;
}
// DropStatament
DropStatement::DropStatement(EntityType type) :
SQLStatement(kStmtDrop),
type(type),
name(NULL) {}
DropStatement::~DropStatement() {
delete name;
}
// ExecuteStatement
ExecuteStatement::ExecuteStatement() :
SQLStatement(kStmtExecute),
name(NULL),
parameters(NULL) {}
ExecuteStatement::~ExecuteStatement() {
delete name;
delete parameters;
}
// ImportStatement
ImportStatement::ImportStatement(ImportType type) :
SQLStatement(kStmtImport),
type(type),
filePath(NULL),
tableName(NULL) {};
ImportStatement::~ImportStatement() {
delete filePath;
delete tableName;
}
// InsertStatement
InsertStatement::InsertStatement(InsertType type) :
SQLStatement(kStmtInsert),
type(type),
tableName(NULL),
columns(NULL),
values(NULL),
select(NULL) {}
InsertStatement::~InsertStatement() {
delete tableName;
delete columns;
delete values;
delete select;
}
// PrepareStatement
PrepareStatement::PrepareStatement() :
SQLStatement(kStmtPrepare),
name(NULL),
query(NULL) {}
PrepareStatement::~PrepareStatement() {
delete query;
delete name;
}
void PrepareStatement::setPlaceholders(std::vector<void*> ph) {
for (void* e : ph) {
if (e != NULL)
placeholders.push_back((Expr*) e);
}
// Sort by col-id
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
for (uintmax_t i = 0; i < placeholders.size(); ++i) placeholders[i]->ival = i;
}
// SelectStatement.h
// OrderDescription
OrderDescription::OrderDescription(OrderType type, Expr* expr) :
type(type),
expr(expr) {}
OrderDescription::~OrderDescription() {
delete expr;
}
// LimitDescription
LimitDescription::LimitDescription(int64_t limit, int64_t offset) :
limit(limit),
offset(offset) {}
// GroypByDescription
GroupByDescription::GroupByDescription() :
columns(NULL),
having(NULL) {}
GroupByDescription::~GroupByDescription() {
delete columns;
delete having;
}
// SelectStatement
SelectStatement::SelectStatement() :
SQLStatement(kStmtSelect),
fromTable(NULL),
selectDistinct(false),
selectList(NULL),
whereClause(NULL),
groupBy(NULL),
unionSelect(NULL),
order(NULL),
limit(NULL) {};
SelectStatement::~SelectStatement() {
delete fromTable;
delete selectList;
delete whereClause;
delete groupBy;
delete order;
delete limit;
}
// UpdateStatement
UpdateStatement::UpdateStatement() :
SQLStatement(kStmtUpdate),
table(NULL),
updates(NULL),
where(NULL) {}
UpdateStatement::~UpdateStatement() {
delete table;
delete updates;
delete where;
}
// TableRef
TableRef::TableRef(TableRefType type) :
type(type),
schema(NULL),
name(NULL),
alias(NULL),
select(NULL),
list(NULL),
join(NULL) {}
TableRef::~TableRef() {
delete name;
delete alias;
delete select;
delete list;
}
// JoinDefinition
JoinDefinition::JoinDefinition() :
left(NULL),
right(NULL),
condition(NULL),
type(kJoinInner) {}
JoinDefinition::~JoinDefinition() {
delete left;
delete right;
delete condition;
}
} // namespace hsql