From 0946624d546cebbcfe7625a7635919c3d6895cb6 Mon Sep 17 00:00:00 2001 From: Pedro Flemming Date: Fri, 3 Feb 2017 16:50:18 +0100 Subject: [PATCH] moved implementations from header files to statements.cpp --- src/sql/CreateStatement.h | 47 +++----- src/sql/DeleteStatement.h | 23 ++-- src/sql/DropStatement.h | 17 +-- src/sql/ExecuteStatement.h | 11 +- src/sql/Expr.cpp | 21 ++-- src/sql/Expr.h | 8 +- src/sql/ImportStatement.h | 12 +- src/sql/InsertStatement.h | 16 +-- src/sql/PrepareStatement.h | 23 +--- src/sql/SQLStatement.h | 9 +- src/sql/SelectStatement.h | 45 ++------ src/sql/Table.h | 23 +--- src/sql/UpdateStatement.h | 13 +-- src/sql/destruct.cpp | 16 --- src/sql/statements.cpp | 218 +++++++++++++++++++++++++++++++++++++ 15 files changed, 284 insertions(+), 218 deletions(-) delete mode 100644 src/sql/destruct.cpp create mode 100644 src/sql/statements.cpp diff --git a/src/sql/CreateStatement.h b/src/sql/CreateStatement.h index 6c9dbf7..0874818 100644 --- a/src/sql/CreateStatement.h +++ b/src/sql/CreateStatement.h @@ -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,49 +13,31 @@ 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* columns; + bool ifNotExists; // default: false + const char* filePath; // default: NULL + const char* tableName; // default: NULL + std::vector* columns; // default: NULL }; } // namespace hsql -#endif \ No newline at end of file +#endif diff --git a/src/sql/DeleteStatement.h b/src/sql/DeleteStatement.h index 202672c..bbb67aa 100644 --- a/src/sql/DeleteStatement.h +++ b/src/sql/DeleteStatement.h @@ -3,27 +3,18 @@ #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; }; } // namespace hsql -#endif \ No newline at end of file +#endif diff --git a/src/sql/DropStatement.h b/src/sql/DropStatement.h index 6c2aa10..4f091d8 100644 --- a/src/sql/DropStatement.h +++ b/src/sql/DropStatement.h @@ -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; diff --git a/src/sql/ExecuteStatement.h b/src/sql/ExecuteStatement.h index 1896bbb..c1cb567 100644 --- a/src/sql/ExecuteStatement.h +++ b/src/sql/ExecuteStatement.h @@ -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* parameters; diff --git a/src/sql/Expr.cpp b/src/sql/Expr.cpp index 04b1c79..df8a259 100644 --- a/src/sql/Expr.cpp +++ b/src/sql/Expr.cpp @@ -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 \ No newline at end of file diff --git a/src/sql/Expr.h b/src/sql/Expr.h index 9301ea6..84a1d6f 100644 --- a/src/sql/Expr.h +++ b/src/sql/Expr.h @@ -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 diff --git a/src/sql/ImportStatement.h b/src/sql/ImportStatement.h index 7087f57..3cc4593 100644 --- a/src/sql/ImportStatement.h +++ b/src/sql/ImportStatement.h @@ -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; diff --git a/src/sql/InsertStatement.h b/src/sql/InsertStatement.h index 9d23995..f62036c 100644 --- a/src/sql/InsertStatement.h +++ b/src/sql/InsertStatement.h @@ -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; diff --git a/src/sql/PrepareStatement.h b/src/sql/PrepareStatement.h index 8da35d2..925ae6f 100644 --- a/src/sql/PrepareStatement.h +++ b/src/sql/PrepareStatement.h @@ -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 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 ph); const char* name; SQLParserResult* query; diff --git a/src/sql/SQLStatement.h b/src/sql/SQLStatement.h index 634e606..264eb64 100644 --- a/src/sql/SQLStatement.h +++ b/src/sql/SQLStatement.h @@ -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; diff --git a/src/sql/SelectStatement.h b/src/sql/SelectStatement.h index d4ca6f5..eb0cfe2 100644 --- a/src/sql/SelectStatement.h +++ b/src/sql/SelectStatement.h @@ -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* 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; diff --git a/src/sql/Table.h b/src/sql/Table.h index ec778a8..15bf219 100644 --- a/src/sql/Table.h +++ b/src/sql/Table.h @@ -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; diff --git a/src/sql/UpdateStatement.h b/src/sql/UpdateStatement.h index ad9fe5a..88222f4 100644 --- a/src/sql/UpdateStatement.h +++ b/src/sql/UpdateStatement.h @@ -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; diff --git a/src/sql/destruct.cpp b/src/sql/destruct.cpp deleted file mode 100644 index 5e04742..0000000 --- a/src/sql/destruct.cpp +++ /dev/null @@ -1,16 +0,0 @@ - -#include "Table.h" -#include "SelectStatement.h" - -namespace hsql { - - - TableRef::~TableRef() { - delete name; - delete alias; - delete select; - delete list; - } - - -} // namespace hsql \ No newline at end of file diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp new file mode 100644 index 0000000..06c468e --- /dev/null +++ b/src/sql/statements.cpp @@ -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 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 \ No newline at end of file