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

View File

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

View File

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

View File

@ -9,15 +9,8 @@ namespace hsql {
* Example: "EXECUTE ins_prep(100, "test", 2.3);" * Example: "EXECUTE ins_prep(100, "test", 2.3);"
*/ */
struct ExecuteStatement : SQLStatement { struct ExecuteStatement : SQLStatement {
ExecuteStatement() : ExecuteStatement();
SQLStatement(kStmtExecute), virtual ~ExecuteStatement();
name(NULL),
parameters(NULL) {}
virtual ~ExecuteStatement() {
delete name;
delete parameters;
}
const char* name; const char* name;
std::vector<Expr*>* parameters; 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* Expr::makeOpUnary(OperatorType op, Expr* expr) {
Expr* e = new Expr(kExprOperator); Expr* e = new Expr(kExprOperator);
@ -92,11 +106,4 @@ namespace hsql {
return e; return e;
} }
Expr::~Expr() {
delete expr;
delete expr2;
delete name;
delete table;
}
} // namespace hsql } // namespace hsql

View File

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

View File

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

View File

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

View File

@ -12,15 +12,8 @@ namespace hsql {
* Example: "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?" * Example: "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?"
*/ */
struct PrepareStatement : SQLStatement { struct PrepareStatement : SQLStatement {
PrepareStatement() : PrepareStatement();
SQLStatement(kStmtPrepare), virtual ~PrepareStatement();
name(NULL),
query(NULL) {}
virtual ~PrepareStatement() {
delete query;
delete name;
}
/** /**
* When setting the placeholders we need to make sure that they are in the correct order. * 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 * @param vector of placeholders that the parser found
*/ */
void setPlaceholders(std::vector<void*> ph) { 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;
}
const char* name; const char* name;
SQLParserResult* query; SQLParserResult* query;

View File

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

View File

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

View File

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

View File

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