From 0946624d546cebbcfe7625a7635919c3d6895cb6 Mon Sep 17 00:00:00 2001 From: Pedro Flemming Date: Fri, 3 Feb 2017 16:50:18 +0100 Subject: [PATCH 1/7] 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 From 02b7b880edcbd457d5458642411988974c132e07 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 01:55:59 +0100 Subject: [PATCH 2/7] remove inline functions from Expr --- src/sql/Expr.cpp | 52 +++++++++++++++++++-------- src/sql/Expr.h | 91 +++++++++++++++++++++--------------------------- 2 files changed, 77 insertions(+), 66 deletions(-) diff --git a/src/sql/Expr.cpp b/src/sql/Expr.cpp index df8a259..f4c36e8 100644 --- a/src/sql/Expr.cpp +++ b/src/sql/Expr.cpp @@ -5,16 +5,6 @@ namespace hsql { - char* substr(const char* source, int from, int to) { - int len = to-from; - char* copy = new char[len+1]; - strncpy(copy, source+from, len); - copy[len] = '\0'; - return copy; - } - - - Expr::Expr(ExprType type) : type(type), expr(NULL), @@ -38,8 +28,6 @@ namespace hsql { return e; } - - Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { Expr* e = new Expr(kExprOperator); e->op_type = op; @@ -58,8 +46,6 @@ namespace hsql { return e; } - - Expr* Expr::makeLiteral(int64_t val) { Expr* e = new Expr(kExprLiteralInt); e->ival = val; @@ -106,4 +92,40 @@ namespace hsql { return e; } -} // namespace hsql \ No newline at end of file + bool Expr::isType(ExprType e_type) { + return e_type == type; + } + + bool Expr::isLiteral() { + return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder); + } + + bool Expr::hasAlias() { + return alias != NULL; + } + + bool Expr::hasTable() { + return table != NULL; + } + + char* Expr::getName() { + if (alias != NULL) return alias; + else return name; + } + + bool Expr::isSimpleOp() { + return op_type == SIMPLE_OP; + } + + bool Expr::isSimpleOp(char op) { + return isSimpleOp() && op_char == op; + } + + char* substr(const char* source, int from, int to) { + int len = to-from; + char* copy = new char[len+1]; + strncpy(copy, source+from, len); + copy[len] = '\0'; + return copy; + } +} // namespace hsql diff --git a/src/sql/Expr.h b/src/sql/Expr.h index 84a1d6f..d594e0e 100644 --- a/src/sql/Expr.h +++ b/src/sql/Expr.h @@ -6,12 +6,11 @@ namespace hsql { -// Helper function + // Helper function used by the lexer. + // TODO: move to more appropriate place. char* substr(const char* source, int from, int to); - - - typedef enum { + enum ExprType { kExprLiteralFloat, kExprLiteralString, kExprLiteralInt, @@ -20,27 +19,21 @@ namespace hsql { kExprColumnRef, kExprFunctionRef, kExprOperator - } ExprType; - + }; typedef struct Expr Expr; - /** - * Represents SQL expressions (i.e. literals, operators, column_refs) - * - * TODO: When destructing a placeholder expression, we might need to alter the placeholder_list - */ + // Represents SQL expressions (i.e. literals, operators, column_refs). + // TODO: When destructing a placeholder expression, we might need to alter the placeholder_list. struct Expr { - /** - * Operator types. These are important for expressions of type kExprOperator - * Trivial types are those that can be described by a single character e.g: - * + - * / < > = % - * Non-trivial are: - * <> <= >= LIKE ISNULL NOT - */ - typedef enum { + // Operator types. These are important for expressions of type kExprOperator. + // Trivial types are those that can be described by a single character e.g: + // + - * / < > = % + // Non-trivial are: <> <= >= LIKE ISNULL NOT + enum OperatorType { SIMPLE_OP, - // Binary + + // Binary operators. NOT_EQUALS, LESS_EQ, GREATER_EQ, @@ -48,18 +41,20 @@ namespace hsql { NOT_LIKE, AND, OR, - // Unary + + // Unary operators. NOT, UMINUS, ISNULL - } OperatorType; + }; Expr(ExprType type); // Interesting side-effect: - // Making the destructor virtual used to cause segmentation faults + // Making the destructor virtual used to cause segmentation faults. + // TODO: inspect. ~Expr(); ExprType type; @@ -77,47 +72,41 @@ namespace hsql { char op_char; bool distinct; + // Convenience accessor methods. - /** - * Convenience accessor methods - */ - inline bool isType(ExprType e_type) { - return e_type == type; - } - inline bool isLiteral() { - return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder); - } - inline bool hasAlias() { - return alias != NULL; - } - inline bool hasTable() { - return table != NULL; - } - inline char* getName() { - if (alias != NULL) return alias; - else return name; - } - inline bool isSimpleOp() { - return op_type == SIMPLE_OP; - } - inline bool isSimpleOp(char op) { - return isSimpleOp() && op_char == op; - } + bool isType(ExprType e_type); + + bool isLiteral(); + + bool hasAlias(); + + bool hasTable(); + + char* getName(); + + bool isSimpleOp(); + + bool isSimpleOp(char op); - /** - * Static expression constructors - */ + // Static constructors. + static Expr* makeOpUnary(OperatorType op, Expr* expr); + static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2); + static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2); static Expr* makeLiteral(int64_t val); + static Expr* makeLiteral(double val); + static Expr* makeLiteral(char* val); static Expr* makeColumnRef(char* name); + static Expr* makeColumnRef(char* table, char* name); + static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct); static Expr* makePlaceholder(int id); From 8582c7f901ede3e7200d881d7f271252a54669e4 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 02:02:30 +0100 Subject: [PATCH 3/7] remove typedef enums and inline methods. --- src/sql/SQLStatement.h | 4 ++-- src/sql/SelectStatement.h | 4 ++-- src/sql/Table.h | 49 ++++++++++----------------------------- src/sql/statements.cpp | 16 ++++++++----- 4 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/sql/SQLStatement.h b/src/sql/SQLStatement.h index 264eb64..56704ca 100644 --- a/src/sql/SQLStatement.h +++ b/src/sql/SQLStatement.h @@ -5,7 +5,7 @@ #include namespace hsql { - typedef enum { + enum StatementType { kStmtError, // unused kStmtSelect, kStmtImport, @@ -19,7 +19,7 @@ namespace hsql { kStmtExport, kStmtRename, kStmtAlter - } StatementType; + }; /** * Base struct for every SQL statement diff --git a/src/sql/SelectStatement.h b/src/sql/SelectStatement.h index eb0cfe2..6086438 100644 --- a/src/sql/SelectStatement.h +++ b/src/sql/SelectStatement.h @@ -6,10 +6,10 @@ #include "Table.h" namespace hsql { - typedef enum { + enum OrderType { kOrderAsc, kOrderDesc - } OrderType; + }; /** * Description of the order by clause within a select statement diff --git a/src/sql/Table.h b/src/sql/Table.h index 15bf219..2e7ac29 100644 --- a/src/sql/Table.h +++ b/src/sql/Table.h @@ -11,23 +11,15 @@ namespace hsql { struct JoinDefinition; struct TableRef; - - /** - * @enum TableRefType - * Types table references - */ - typedef enum { + // Possible table reference types. + enum TableRefType { kTableName, kTableSelect, kTableJoin, kTableCrossProduct - } TableRefType; + }; - - /** - * @struct TableRef - * @brief Holds reference to tables. Can be either table names or a select statement. - */ + // Holds reference to tables. Can be either table names or a select statement. struct TableRef { TableRef(TableRefType type); virtual ~TableRef(); @@ -42,37 +34,22 @@ namespace hsql { std::vector* list; JoinDefinition* join; + // Returns true if a schema is set. + bool hasSchema(); - /** - * Convenience accessor methods - */ - inline bool hasSchema() { - return schema != NULL; - } - - inline char* getName() { - if (alias != NULL) return alias; - else return name; - } + // Returns the alias, if it is set. Otherwise the name. + char* getName(); }; - - /** - * @enum JoinType - * Types of joins - */ - typedef enum { + // Possible types of joins. + enum JoinType { kJoinInner, kJoinOuter, kJoinLeft, kJoinRight, - } JoinType; + }; - - /** - * @struct JoinDefinition - * @brief Definition of a join table - */ + // Definition of a join construct. struct JoinDefinition { JoinDefinition(); virtual ~JoinDefinition(); @@ -84,7 +61,5 @@ namespace hsql { JoinType type; }; - - } // namespace hsql #endif diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 06c468e..1e76737 100644 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -180,9 +180,6 @@ namespace hsql { delete where; } - - - // TableRef TableRef::TableRef(TableRefType type) : type(type), @@ -200,6 +197,15 @@ namespace hsql { delete list; } + bool TableRef::hasSchema() { + return schema != NULL; + } + + char* TableRef::getName() { + if (alias != NULL) return alias; + else return name; + } + // JoinDefinition JoinDefinition::JoinDefinition() : left(NULL), @@ -213,6 +219,4 @@ namespace hsql { delete condition; } - - -} // namespace hsql \ No newline at end of file +} // namespace hsql From 28214e80432635920f1fd8862472b0b17453691c Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 02:06:15 +0100 Subject: [PATCH 4/7] change indent to spaces=2 and indent access modifiers. minor style changes --- astyle.options | 10 +- src/SQLParser.cpp | 54 ++--- src/SQLParser.h | 20 +- src/SQLParserResult.cpp | 48 ++--- src/SQLParserResult.h | 38 ++-- src/sql/CreateStatement.h | 54 ++--- src/sql/DeleteStatement.h | 18 +- src/sql/DropStatement.h | 32 +-- src/sql/ExecuteStatement.h | 20 +- src/sql/Expr.cpp | 208 ++++++++++---------- src/sql/Expr.h | 152 +++++++-------- src/sql/ImportStatement.h | 30 +-- src/sql/InsertStatement.h | 36 ++-- src/sql/PrepareStatement.h | 34 ++-- src/sql/SQLStatement.h | 50 ++--- src/sql/SelectStatement.h | 100 +++++----- src/sql/Table.h | 84 ++++---- src/sql/UpdateStatement.h | 36 ++-- src/sql/statements.cpp | 390 ++++++++++++++++++------------------- src/sqlhelper.cpp | 354 ++++++++++++++++----------------- src/sqlhelper.h | 12 +- test/lib/select.cpp | 38 ++-- test/lib/test.cpp | 68 +++---- test/lib/test.h | 18 +- 24 files changed, 953 insertions(+), 951 deletions(-) diff --git a/astyle.options b/astyle.options index 58989d5..073bfaf 100644 --- a/astyle.options +++ b/astyle.options @@ -1,8 +1,10 @@ +--style=google + # indentation ---indent=spaces=4 +--indent=spaces=2 --indent-namespaces ---style=java ---style=attach --A2 \ No newline at end of file +--align-reference=type +--align-pointer=type +--pad-oper diff --git a/src/SQLParser.cpp b/src/SQLParser.cpp index fa90168..9270745 100644 --- a/src/SQLParser.cpp +++ b/src/SQLParser.cpp @@ -8,39 +8,39 @@ namespace hsql { - SQLParser::SQLParser() { - fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); + SQLParser::SQLParser() { + fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); + } + + + SQLParserResult* SQLParser::parseSQLString(const char* text) { + SQLParserResult* result = NULL; + yyscan_t scanner; + YY_BUFFER_STATE state; + + if (hsql_lex_init(&scanner)) { + // couldn't initialize + fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); + return NULL; } + state = hsql__scan_string(text, scanner); - SQLParserResult* SQLParser::parseSQLString(const char *text) { - SQLParserResult* result = NULL; - yyscan_t scanner; - YY_BUFFER_STATE state; - - if (hsql_lex_init(&scanner)) { - // couldn't initialize - fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); - return NULL; - } - - state = hsql__scan_string(text, scanner); - - if (hsql_parse(&result, scanner)) { - // Returns an error stmt object - return result; - } - - hsql__delete_buffer(state, scanner); - - hsql_lex_destroy(scanner); - return result; + if (hsql_parse(&result, scanner)) { + // Returns an error stmt object + return result; } + hsql__delete_buffer(state, scanner); - SQLParserResult* SQLParser::parseSQLString(const std::string& text) { - return parseSQLString(text.c_str()); - } + hsql_lex_destroy(scanner); + return result; + } + + + SQLParserResult* SQLParser::parseSQLString(const std::string& text) { + return parseSQLString(text.c_str()); + } } // namespace hsql \ No newline at end of file diff --git a/src/SQLParser.h b/src/SQLParser.h index 0fa414d..50c3f18 100644 --- a/src/SQLParser.h +++ b/src/SQLParser.h @@ -5,17 +5,17 @@ #include "sql/statements.h" namespace hsql { - /** - * Main class for parsing SQL strings - */ - class SQLParser { - public: - static SQLParserResult* parseSQLString(const char* sql); - static SQLParserResult* parseSQLString(const std::string& sql); + /** + * Main class for parsing SQL strings + */ + class SQLParser { + public: + static SQLParserResult* parseSQLString(const char* sql); + static SQLParserResult* parseSQLString(const std::string& sql); - private: - SQLParser(); - }; + private: + SQLParser(); + }; } // namespace hsql diff --git a/src/SQLParserResult.cpp b/src/SQLParserResult.cpp index 5ca2180..52c9f02 100644 --- a/src/SQLParserResult.cpp +++ b/src/SQLParserResult.cpp @@ -3,39 +3,39 @@ namespace hsql { - SQLParserResult::SQLParserResult() : - isValid(true), - errorMsg(NULL) {}; + SQLParserResult::SQLParserResult() : + isValid(true), + errorMsg(NULL) {}; - SQLParserResult::SQLParserResult(SQLStatement* stmt) : - isValid(true), - errorMsg(NULL) { - addStatement(stmt); - }; + SQLParserResult::SQLParserResult(SQLStatement* stmt) : + isValid(true), + errorMsg(NULL) { + addStatement(stmt); + }; - SQLParserResult::~SQLParserResult() { - for (std::vector::iterator it = statements.begin(); it != statements.end(); ++it) { - delete *it; - } - - delete errorMsg; + SQLParserResult::~SQLParserResult() { + for (std::vector::iterator it = statements.begin(); it != statements.end(); ++it) { + delete *it; } - - void SQLParserResult::addStatement(SQLStatement* stmt) { - statements.push_back(stmt); - } + delete errorMsg; + } - SQLStatement* SQLParserResult::getStatement(int id) { - return statements[id]; - } + void SQLParserResult::addStatement(SQLStatement* stmt) { + statements.push_back(stmt); + } - size_t SQLParserResult::size() { - return statements.size(); - } + SQLStatement* SQLParserResult::getStatement(int id) { + return statements[id]; + } + + + size_t SQLParserResult::size() { + return statements.size(); + } } // namespace hsql \ No newline at end of file diff --git a/src/SQLParserResult.h b/src/SQLParserResult.h index 285befc..f06bd27 100644 --- a/src/SQLParserResult.h +++ b/src/SQLParserResult.h @@ -4,31 +4,31 @@ #include "sql/SQLStatement.h" namespace hsql { - /** - * Represents the result of the SQLParser. - * If parsing was successful it contains a list of SQLStatement. - */ - class SQLParserResult { - public: + /** + * Represents the result of the SQLParser. + * If parsing was successful it contains a list of SQLStatement. + */ + class SQLParserResult { + public: - SQLParserResult(); - SQLParserResult(SQLStatement* stmt); - virtual ~SQLParserResult(); + SQLParserResult(); + SQLParserResult(SQLStatement* stmt); + virtual ~SQLParserResult(); - void addStatement(SQLStatement* stmt); + void addStatement(SQLStatement* stmt); - SQLStatement* getStatement(int id); + SQLStatement* getStatement(int id); - size_t size(); + size_t size(); - // public properties - std::vector statements; - bool isValid; + // public properties + std::vector statements; + bool isValid; - const char* errorMsg; - int errorLine; - int errorColumn; - }; + const char* errorMsg; + int errorLine; + int errorColumn; + }; } // namespace hsql diff --git a/src/sql/CreateStatement.h b/src/sql/CreateStatement.h index 0874818..350d5a4 100644 --- a/src/sql/CreateStatement.h +++ b/src/sql/CreateStatement.h @@ -5,39 +5,39 @@ // Note: Implementations of constructors and destructors can be found in statements.cpp. namespace hsql { - // Represents definition of a table column - struct ColumnDefinition { - enum DataType { - TEXT, - INT, - DOUBLE - }; - - ColumnDefinition(char* name, DataType type); - virtual ~ColumnDefinition(); - - char* name; - DataType type; + // Represents definition of a table column + struct ColumnDefinition { + enum DataType { + TEXT, + INT, + DOUBLE }; + ColumnDefinition(char* name, DataType type); + virtual ~ColumnDefinition(); - // 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 - }; + char* name; + DataType type; + }; - CreateStatement(CreateType type); - virtual ~CreateStatement(); - CreateType type; - bool ifNotExists; // default: false - const char* filePath; // default: NULL - const char* tableName; // default: NULL - std::vector* columns; // default: NULL + // 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); + virtual ~CreateStatement(); + + CreateType type; + bool ifNotExists; // default: false + const char* filePath; // default: NULL + const char* tableName; // default: NULL + std::vector* columns; // default: NULL + }; + } // namespace hsql #endif diff --git a/src/sql/DeleteStatement.h b/src/sql/DeleteStatement.h index bbb67aa..ed4d77d 100644 --- a/src/sql/DeleteStatement.h +++ b/src/sql/DeleteStatement.h @@ -5,16 +5,16 @@ // 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) - struct DeleteStatement : SQLStatement { - DeleteStatement(); - virtual ~DeleteStatement(); + // Represents SQL Delete statements. + // Example: "DELETE FROM students WHERE grade > 3.0" + // Note: if (expr == NULL) => delete all rows (truncate) + struct DeleteStatement : SQLStatement { + DeleteStatement(); + virtual ~DeleteStatement(); - char* tableName; - Expr* expr; - }; + char* tableName; + Expr* expr; + }; } // namespace hsql #endif diff --git a/src/sql/DropStatement.h b/src/sql/DropStatement.h index 4f091d8..a7886d2 100644 --- a/src/sql/DropStatement.h +++ b/src/sql/DropStatement.h @@ -5,23 +5,23 @@ // Note: Implementations of constructors and destructors can be found in statements.cpp. namespace hsql { - // Represents SQL Delete statements. - // Example "DROP TABLE students;" - struct DropStatement : SQLStatement { - enum EntityType { - kTable, - kSchema, - kIndex, - kView, - kPreparedStatement - }; - - DropStatement(EntityType type); - virtual ~DropStatement(); - - EntityType type; - const char* name; + // Represents SQL Delete statements. + // Example "DROP TABLE students;" + struct DropStatement : SQLStatement { + enum EntityType { + kTable, + kSchema, + kIndex, + kView, + kPreparedStatement }; + DropStatement(EntityType type); + virtual ~DropStatement(); + + EntityType type; + const char* name; + }; + } // namespace hsql #endif \ No newline at end of file diff --git a/src/sql/ExecuteStatement.h b/src/sql/ExecuteStatement.h index c1cb567..7a1ba86 100644 --- a/src/sql/ExecuteStatement.h +++ b/src/sql/ExecuteStatement.h @@ -4,17 +4,17 @@ #include "SQLStatement.h" namespace hsql { - /** - * Represents SQL Execute statements. - * Example: "EXECUTE ins_prep(100, "test", 2.3);" - */ - struct ExecuteStatement : SQLStatement { - ExecuteStatement(); - virtual ~ExecuteStatement(); + /** + * Represents SQL Execute statements. + * Example: "EXECUTE ins_prep(100, "test", 2.3);" + */ + struct ExecuteStatement : SQLStatement { + ExecuteStatement(); + virtual ~ExecuteStatement(); - const char* name; - std::vector* parameters; - }; + const char* name; + std::vector* parameters; + }; } // namsepace hsql #endif \ No newline at end of file diff --git a/src/sql/Expr.cpp b/src/sql/Expr.cpp index f4c36e8..fd3ee46 100644 --- a/src/sql/Expr.cpp +++ b/src/sql/Expr.cpp @@ -5,127 +5,127 @@ namespace hsql { - Expr::Expr(ExprType type) : - type(type), - expr(NULL), - expr2(NULL), - name(NULL), - table(NULL), - alias(NULL) {}; + 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() { + delete expr; + delete expr2; + delete name; + delete table; + } - Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { - Expr* e = new Expr(kExprOperator); - e->op_type = op; - e->expr = expr; - e->expr2 = NULL; - return e; - } + Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { + Expr* e = new Expr(kExprOperator); + e->op_type = op; + e->expr = expr; + e->expr2 = NULL; + return e; + } - Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { - Expr* e = new Expr(kExprOperator); - e->op_type = op; - e->op_char = 0; - e->expr = expr1; - e->expr2 = expr2; - return e; - } + Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { + Expr* e = new Expr(kExprOperator); + e->op_type = op; + e->op_char = 0; + e->expr = expr1; + e->expr2 = expr2; + return e; + } - Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) { - Expr* e = new Expr(kExprOperator); - e->op_type = SIMPLE_OP; - e->op_char = op; - e->expr = expr1; - e->expr2 = expr2; - return e; - } + Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) { + Expr* e = new Expr(kExprOperator); + e->op_type = SIMPLE_OP; + e->op_char = op; + e->expr = expr1; + e->expr2 = expr2; + return e; + } - Expr* Expr::makeLiteral(int64_t val) { - Expr* e = new Expr(kExprLiteralInt); - e->ival = val; - return e; - } + Expr* Expr::makeLiteral(int64_t val) { + Expr* e = new Expr(kExprLiteralInt); + e->ival = val; + return e; + } - Expr* Expr::makeLiteral(double value) { - Expr* e = new Expr(kExprLiteralFloat); - e->fval = value; - return e; - } + Expr* Expr::makeLiteral(double value) { + Expr* e = new Expr(kExprLiteralFloat); + e->fval = value; + return e; + } - Expr* Expr::makeLiteral(char* string) { - Expr* e = new Expr(kExprLiteralString); - e->name = string; - return e; - } + Expr* Expr::makeLiteral(char* string) { + Expr* e = new Expr(kExprLiteralString); + e->name = string; + return e; + } - Expr* Expr::makeColumnRef(char* name) { - Expr* e = new Expr(kExprColumnRef); - e->name = name; - return e; - } + Expr* Expr::makeColumnRef(char* name) { + Expr* e = new Expr(kExprColumnRef); + e->name = name; + return e; + } - Expr* Expr::makeColumnRef(char* table, char* name) { - Expr* e = new Expr(kExprColumnRef); - e->name = name; - e->table = table; - return e; - } + Expr* Expr::makeColumnRef(char* table, char* name) { + Expr* e = new Expr(kExprColumnRef); + e->name = name; + e->table = table; + return e; + } - Expr* Expr::makeFunctionRef(char* func_name, Expr* expr, bool distinct) { - Expr* e = new Expr(kExprFunctionRef); - e->name = func_name; - e->expr = expr; - e->distinct = distinct; - return e; - } + Expr* Expr::makeFunctionRef(char* func_name, Expr* expr, bool distinct) { + Expr* e = new Expr(kExprFunctionRef); + e->name = func_name; + e->expr = expr; + e->distinct = distinct; + return e; + } - Expr* Expr::makePlaceholder(int id) { - Expr* e = new Expr(kExprPlaceholder); - e->ival = id; - return e; - } + Expr* Expr::makePlaceholder(int id) { + Expr* e = new Expr(kExprPlaceholder); + e->ival = id; + return e; + } - bool Expr::isType(ExprType e_type) { - return e_type == type; - } + bool Expr::isType(ExprType e_type) { + return e_type == type; + } - bool Expr::isLiteral() { - return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder); - } + bool Expr::isLiteral() { + return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder); + } - bool Expr::hasAlias() { - return alias != NULL; - } + bool Expr::hasAlias() { + return alias != NULL; + } - bool Expr::hasTable() { - return table != NULL; - } + bool Expr::hasTable() { + return table != NULL; + } - char* Expr::getName() { - if (alias != NULL) return alias; - else return name; - } + char* Expr::getName() { + if (alias != NULL) return alias; + else return name; + } - bool Expr::isSimpleOp() { - return op_type == SIMPLE_OP; - } + bool Expr::isSimpleOp() { + return op_type == SIMPLE_OP; + } - bool Expr::isSimpleOp(char op) { - return isSimpleOp() && op_char == op; - } - - char* substr(const char* source, int from, int to) { - int len = to-from; - char* copy = new char[len+1]; - strncpy(copy, source+from, len); - copy[len] = '\0'; - return copy; - } + bool Expr::isSimpleOp(char op) { + return isSimpleOp() && op_char == op; + } + + char* substr(const char* source, int from, int to) { + int len = to - from; + char* copy = new char[len + 1]; + strncpy(copy, source + from, len); + copy[len] = '\0'; + return copy; + } } // namespace hsql diff --git a/src/sql/Expr.h b/src/sql/Expr.h index d594e0e..74d475e 100644 --- a/src/sql/Expr.h +++ b/src/sql/Expr.h @@ -6,111 +6,111 @@ namespace hsql { - // Helper function used by the lexer. - // TODO: move to more appropriate place. - char* substr(const char* source, int from, int to); + // Helper function used by the lexer. + // TODO: move to more appropriate place. + char* substr(const char* source, int from, int to); - enum ExprType { - kExprLiteralFloat, - kExprLiteralString, - kExprLiteralInt, - kExprStar, - kExprPlaceholder, - kExprColumnRef, - kExprFunctionRef, - kExprOperator + enum ExprType { + kExprLiteralFloat, + kExprLiteralString, + kExprLiteralInt, + kExprStar, + kExprPlaceholder, + kExprColumnRef, + kExprFunctionRef, + kExprOperator + }; + + typedef struct Expr Expr; + + // Represents SQL expressions (i.e. literals, operators, column_refs). + // TODO: When destructing a placeholder expression, we might need to alter the placeholder_list. + struct Expr { + // Operator types. These are important for expressions of type kExprOperator. + // Trivial types are those that can be described by a single character e.g: + // + - * / < > = % + // Non-trivial are: <> <= >= LIKE ISNULL NOT + enum OperatorType { + SIMPLE_OP, + + // Binary operators. + NOT_EQUALS, + LESS_EQ, + GREATER_EQ, + LIKE, + NOT_LIKE, + AND, + OR, + + // Unary operators. + NOT, + UMINUS, + ISNULL }; - typedef struct Expr Expr; - - // Represents SQL expressions (i.e. literals, operators, column_refs). - // TODO: When destructing a placeholder expression, we might need to alter the placeholder_list. - struct Expr { - // Operator types. These are important for expressions of type kExprOperator. - // Trivial types are those that can be described by a single character e.g: - // + - * / < > = % - // Non-trivial are: <> <= >= LIKE ISNULL NOT - enum OperatorType { - SIMPLE_OP, - - // Binary operators. - NOT_EQUALS, - LESS_EQ, - GREATER_EQ, - LIKE, - NOT_LIKE, - AND, - OR, - - // Unary operators. - NOT, - UMINUS, - ISNULL - }; + Expr(ExprType type); - Expr(ExprType type); + // Interesting side-effect: + // Making the destructor virtual used to cause segmentation faults. + // TODO: inspect. + ~Expr(); - // Interesting side-effect: - // Making the destructor virtual used to cause segmentation faults. - // TODO: inspect. - ~Expr(); + ExprType type; - ExprType type; + Expr* expr; + Expr* expr2; + char* name; + char* table; + char* alias; + float fval; + int64_t ival; + int64_t ival2; - Expr* expr; - Expr* expr2; - char* name; - char* table; - char* alias; - float fval; - int64_t ival; - int64_t ival2; + OperatorType op_type; + char op_char; + bool distinct; - OperatorType op_type; - char op_char; - bool distinct; + // Convenience accessor methods. - // Convenience accessor methods. + bool isType(ExprType e_type); - bool isType(ExprType e_type); + bool isLiteral(); - bool isLiteral(); + bool hasAlias(); - bool hasAlias(); + bool hasTable(); - bool hasTable(); + char* getName(); - char* getName(); + bool isSimpleOp(); - bool isSimpleOp(); - - bool isSimpleOp(char op); + bool isSimpleOp(char op); - // Static constructors. + // Static constructors. - static Expr* makeOpUnary(OperatorType op, Expr* expr); + static Expr* makeOpUnary(OperatorType op, Expr* expr); - static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2); + static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2); - static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2); + static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2); - static Expr* makeLiteral(int64_t val); + static Expr* makeLiteral(int64_t val); - static Expr* makeLiteral(double val); + static Expr* makeLiteral(double val); - static Expr* makeLiteral(char* val); + static Expr* makeLiteral(char* val); - static Expr* makeColumnRef(char* name); + static Expr* makeColumnRef(char* name); - static Expr* makeColumnRef(char* table, char* name); + static Expr* makeColumnRef(char* table, char* name); - static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct); + static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct); - static Expr* makePlaceholder(int id); - }; + static Expr* makePlaceholder(int id); + }; // Zero initializes an Expr object and assigns it to a space in the heap // For Hyrise we still had to put in the explicit NULL constructor diff --git a/src/sql/ImportStatement.h b/src/sql/ImportStatement.h index 3cc4593..4fb3e3e 100644 --- a/src/sql/ImportStatement.h +++ b/src/sql/ImportStatement.h @@ -4,23 +4,23 @@ #include "SQLStatement.h" namespace hsql { - /** - * Represents SQL Import statements. - */ - struct ImportStatement : SQLStatement { - enum ImportType { - kImportCSV, - kImportTbl, // Hyrise file format - }; - - ImportStatement(ImportType type); - virtual ~ImportStatement(); - - ImportType type; - const char* filePath; - const char* tableName; + /** + * Represents SQL Import statements. + */ + struct ImportStatement : SQLStatement { + enum ImportType { + kImportCSV, + kImportTbl, // Hyrise file format }; + ImportStatement(ImportType type); + virtual ~ImportStatement(); + + ImportType type; + const char* filePath; + const char* tableName; + }; + } // namespace hsql diff --git a/src/sql/InsertStatement.h b/src/sql/InsertStatement.h index f62036c..94aa726 100644 --- a/src/sql/InsertStatement.h +++ b/src/sql/InsertStatement.h @@ -5,25 +5,25 @@ #include "SelectStatement.h" namespace hsql { - /** - * Represents SQL Insert statements. - * Example: "INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)" - */ - struct InsertStatement : SQLStatement { - enum InsertType { - kInsertValues, - kInsertSelect - }; - - InsertStatement(InsertType type); - virtual ~InsertStatement(); - - InsertType type; - const char* tableName; - std::vector* columns; - std::vector* values; - SelectStatement* select; + /** + * Represents SQL Insert statements. + * Example: "INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)" + */ + struct InsertStatement : SQLStatement { + enum InsertType { + kInsertValues, + kInsertSelect }; + InsertStatement(InsertType type); + virtual ~InsertStatement(); + + InsertType type; + const char* tableName; + std::vector* columns; + std::vector* values; + SelectStatement* select; + }; + } // namsepace hsql #endif \ No newline at end of file diff --git a/src/sql/PrepareStatement.h b/src/sql/PrepareStatement.h index 925ae6f..eb1fb67 100644 --- a/src/sql/PrepareStatement.h +++ b/src/sql/PrepareStatement.h @@ -7,26 +7,26 @@ #include namespace hsql { + /** + * Represents SQL Prepare statements. + * Example: "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?" + */ + struct PrepareStatement : SQLStatement { + PrepareStatement(); + virtual ~PrepareStatement(); + /** - * Represents SQL Prepare statements. - * Example: "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?" + * When setting the placeholders we need to make sure that they are in the correct order. + * To ensure that, during parsing we store the character position use that to sort the list here. + * + * @param vector of placeholders that the parser found */ - struct PrepareStatement : SQLStatement { - PrepareStatement(); - virtual ~PrepareStatement(); + void setPlaceholders(std::vector ph); - /** - * When setting the placeholders we need to make sure that they are in the correct order. - * To ensure that, during parsing we store the character position use that to sort the list here. - * - * @param vector of placeholders that the parser found - */ - void setPlaceholders(std::vector ph); - - const char* name; - SQLParserResult* query; - std::vector placeholders; - }; + const char* name; + SQLParserResult* query; + std::vector placeholders; + }; } // namsepace hsql #endif \ No newline at end of file diff --git a/src/sql/SQLStatement.h b/src/sql/SQLStatement.h index 56704ca..ffbe049 100644 --- a/src/sql/SQLStatement.h +++ b/src/sql/SQLStatement.h @@ -5,35 +5,35 @@ #include namespace hsql { - enum StatementType { - kStmtError, // unused - kStmtSelect, - kStmtImport, - kStmtInsert, - kStmtUpdate, - kStmtDelete, - kStmtCreate, - kStmtDrop, - kStmtPrepare, - kStmtExecute, - kStmtExport, - kStmtRename, - kStmtAlter - }; + enum StatementType { + kStmtError, // unused + kStmtSelect, + kStmtImport, + kStmtInsert, + kStmtUpdate, + kStmtDelete, + kStmtCreate, + kStmtDrop, + kStmtPrepare, + kStmtExecute, + kStmtExport, + kStmtRename, + kStmtAlter + }; - /** - * Base struct for every SQL statement - */ - struct SQLStatement { - SQLStatement(StatementType type); + /** + * Base struct for every SQL statement + */ + struct SQLStatement { + SQLStatement(StatementType type); - virtual ~SQLStatement(); + virtual ~SQLStatement(); - virtual StatementType type(); + virtual StatementType type(); - private: - StatementType _type; - }; + private: + StatementType _type; + }; } // namespace hsql #endif // __SQLSTATEMENT_H__ diff --git a/src/sql/SelectStatement.h b/src/sql/SelectStatement.h index 6086438..a9d169d 100644 --- a/src/sql/SelectStatement.h +++ b/src/sql/SelectStatement.h @@ -6,66 +6,66 @@ #include "Table.h" namespace hsql { - enum OrderType { - kOrderAsc, - kOrderDesc - }; + enum OrderType { + kOrderAsc, + kOrderDesc + }; - /** - * Description of the order by clause within a select statement - * TODO: hold multiple expressions to be sorted by - */ - struct OrderDescription { - OrderDescription(OrderType type, Expr* expr); - virtual ~OrderDescription(); + /** + * Description of the order by clause within a select statement + * TODO: hold multiple expressions to be sorted by + */ + struct OrderDescription { + OrderDescription(OrderType type, Expr* expr); + virtual ~OrderDescription(); - OrderType type; - Expr* expr; - }; + OrderType type; + Expr* expr; + }; - const int64_t kNoLimit = -1; - const int64_t kNoOffset = -1; + const int64_t kNoLimit = -1; + const int64_t kNoOffset = -1; - /** - * Description of the limit clause within a select statement - */ - struct LimitDescription { - LimitDescription(int64_t limit, int64_t offset); + /** + * Description of the limit clause within a select statement + */ + struct LimitDescription { + LimitDescription(int64_t limit, int64_t offset); - int64_t limit; - int64_t offset; - }; + int64_t limit; + int64_t offset; + }; - /** - * Description of the group-by clause within a select statement - */ - struct GroupByDescription { - GroupByDescription(); - // TODO: make virtual - ~GroupByDescription(); + /** + * Description of the group-by clause within a select statement + */ + struct GroupByDescription { + GroupByDescription(); + // TODO: make virtual + ~GroupByDescription(); - std::vector* columns; - Expr* having; - }; + std::vector* columns; + Expr* having; + }; - /** - * Representation of a full SQL select statement. - * TODO: add union_order and union_limit - */ - struct SelectStatement : SQLStatement { - SelectStatement(); - virtual ~SelectStatement(); + /** + * Representation of a full SQL select statement. + * TODO: add union_order and union_limit + */ + struct SelectStatement : SQLStatement { + SelectStatement(); + virtual ~SelectStatement(); - TableRef* fromTable; - bool selectDistinct; - std::vector* selectList; - Expr* whereClause; - GroupByDescription* groupBy; + TableRef* fromTable; + bool selectDistinct; + std::vector* selectList; + Expr* whereClause; + GroupByDescription* groupBy; - SelectStatement* unionSelect; - OrderDescription* order; - LimitDescription* limit; - }; + SelectStatement* unionSelect; + OrderDescription* order; + LimitDescription* limit; + }; } // namespace hsql #endif \ No newline at end of file diff --git a/src/sql/Table.h b/src/sql/Table.h index 2e7ac29..83e1cb1 100644 --- a/src/sql/Table.h +++ b/src/sql/Table.h @@ -7,59 +7,59 @@ namespace hsql { - struct SelectStatement; - struct JoinDefinition; - struct TableRef; + struct SelectStatement; + struct JoinDefinition; + struct TableRef; - // Possible table reference types. - enum TableRefType { - kTableName, - kTableSelect, - kTableJoin, - kTableCrossProduct - }; + // Possible table reference types. + enum TableRefType { + kTableName, + kTableSelect, + kTableJoin, + kTableCrossProduct + }; - // Holds reference to tables. Can be either table names or a select statement. - struct TableRef { - TableRef(TableRefType type); - virtual ~TableRef(); + // Holds reference to tables. Can be either table names or a select statement. + struct TableRef { + TableRef(TableRefType type); + virtual ~TableRef(); - TableRefType type; + TableRefType type; - char* schema; - char* name; - char* alias; + char* schema; + char* name; + char* alias; - SelectStatement* select; - std::vector* list; - JoinDefinition* join; + SelectStatement* select; + std::vector* list; + JoinDefinition* join; - // Returns true if a schema is set. - bool hasSchema(); + // Returns true if a schema is set. + bool hasSchema(); - // Returns the alias, if it is set. Otherwise the name. - char* getName(); - }; + // Returns the alias, if it is set. Otherwise the name. + char* getName(); + }; - // Possible types of joins. - enum JoinType { - kJoinInner, - kJoinOuter, - kJoinLeft, - kJoinRight, - }; + // Possible types of joins. + enum JoinType { + kJoinInner, + kJoinOuter, + kJoinLeft, + kJoinRight, + }; - // Definition of a join construct. - struct JoinDefinition { - JoinDefinition(); - virtual ~JoinDefinition(); + // Definition of a join construct. + struct JoinDefinition { + JoinDefinition(); + virtual ~JoinDefinition(); - TableRef* left; - TableRef* right; - Expr* condition; + TableRef* left; + TableRef* right; + Expr* condition; - JoinType type; - }; + JoinType type; + }; } // namespace hsql #endif diff --git a/src/sql/UpdateStatement.h b/src/sql/UpdateStatement.h index 88222f4..b4d6e28 100644 --- a/src/sql/UpdateStatement.h +++ b/src/sql/UpdateStatement.h @@ -4,26 +4,26 @@ #include "SQLStatement.h" namespace hsql { - /** - * Represents "column = value" expressions - */ - struct UpdateClause { - char* column; - Expr* value; - }; + /** + * Represents "column = value" expressions + */ + struct UpdateClause { + char* column; + Expr* value; + }; - /** - * Represents SQL Update statements. - */ - struct UpdateStatement : SQLStatement { - UpdateStatement(); - virtual ~UpdateStatement(); + /** + * Represents SQL Update statements. + */ + struct UpdateStatement : SQLStatement { + UpdateStatement(); + virtual ~UpdateStatement(); - // TODO: switch to char* instead of TableRef - TableRef* table; - std::vector* updates; - Expr* where; - }; + // TODO: switch to char* instead of TableRef + TableRef* table; + std::vector* updates; + Expr* where; + }; } // namsepace hsql #endif \ No newline at end of file diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 1e76737..45da79d 100644 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -3,220 +3,220 @@ namespace hsql { - // SQLStatement - SQLStatement::SQLStatement(StatementType type) : - _type(type) {}; + // SQLStatement + SQLStatement::SQLStatement(StatementType type) : + _type(type) {}; - SQLStatement::~SQLStatement() {} + SQLStatement::~SQLStatement() {} - StatementType SQLStatement::type() { - return _type; + 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); }); - // ColumnDefinition - ColumnDefinition::ColumnDefinition(char* name, DataType type) : - name(name), - type(type) {}; + // 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; + } - ColumnDefinition::~ColumnDefinition() { - delete name; - } + // SelectStatement.h - // CreateStatemnet - CreateStatement::CreateStatement(CreateType type) : - SQLStatement(kStmtCreate), - type(type), - ifNotExists(false), - filePath(NULL), - tableName(NULL), - columns(NULL) {}; + // OrderDescription + OrderDescription::OrderDescription(OrderType type, Expr* expr) : + type(type), + expr(expr) {} - CreateStatement::~CreateStatement() { - delete columns; - delete filePath; - delete tableName; - } + OrderDescription::~OrderDescription() { + delete expr; + } - // DeleteStatement - DeleteStatement::DeleteStatement() : - SQLStatement(kStmtDelete), - tableName(NULL), - expr(NULL) {}; + // LimitDescription + LimitDescription::LimitDescription(int64_t limit, int64_t offset) : + limit(limit), + offset(offset) {} - DeleteStatement::~DeleteStatement() { - delete tableName; - delete expr; - } + // GroypByDescription + GroupByDescription::GroupByDescription() : + columns(NULL), + having(NULL) {} - // DropStatament - DropStatement::DropStatement(EntityType type) : - SQLStatement(kStmtDrop), - type(type), - name(NULL) {} + GroupByDescription::~GroupByDescription() { + delete columns; + delete having; + } - DropStatement::~DropStatement() { - delete name; - } + // SelectStatement + SelectStatement::SelectStatement() : + SQLStatement(kStmtSelect), + fromTable(NULL), + selectDistinct(false), + selectList(NULL), + whereClause(NULL), + groupBy(NULL), + unionSelect(NULL), + order(NULL), + limit(NULL) {}; - // ExecuteStatement - ExecuteStatement::ExecuteStatement() : - SQLStatement(kStmtExecute), - name(NULL), - parameters(NULL) {} + SelectStatement::~SelectStatement() { + delete fromTable; + delete selectList; + delete whereClause; + delete groupBy; + delete order; + delete limit; + } - ExecuteStatement::~ExecuteStatement() { - delete name; - delete parameters; - } + // UpdateStatement + UpdateStatement::UpdateStatement() : + SQLStatement(kStmtUpdate), + table(NULL), + updates(NULL), + where(NULL) {} - // ImportStatement - ImportStatement::ImportStatement(ImportType type) : - SQLStatement(kStmtImport), - type(type), - filePath(NULL), - tableName(NULL) {}; + UpdateStatement::~UpdateStatement() { + delete table; + delete updates; + delete where; + } - ImportStatement::~ImportStatement() { - delete filePath; - delete tableName; - } + // TableRef + TableRef::TableRef(TableRefType type) : + type(type), + schema(NULL), + name(NULL), + alias(NULL), + select(NULL), + list(NULL), + join(NULL) {} - // InsertStatement - InsertStatement::InsertStatement(InsertType type) : - SQLStatement(kStmtInsert), - type(type), - tableName(NULL), - columns(NULL), - values(NULL), - select(NULL) {} + TableRef::~TableRef() { + delete name; + delete alias; + delete select; + delete list; + } - InsertStatement::~InsertStatement() { - delete tableName; - delete columns; - delete values; - delete select; - } + bool TableRef::hasSchema() { + return schema != NULL; + } - // PrepareStatement - PrepareStatement::PrepareStatement() : - SQLStatement(kStmtPrepare), - name(NULL), - query(NULL) {} + char* TableRef::getName() { + if (alias != NULL) return alias; + else return name; + } - PrepareStatement::~PrepareStatement() { - delete query; - delete name; - } + // JoinDefinition + JoinDefinition::JoinDefinition() : + left(NULL), + right(NULL), + condition(NULL), + type(kJoinInner) {} - 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; - } - - bool TableRef::hasSchema() { - return schema != NULL; - } - - char* TableRef::getName() { - if (alias != NULL) return alias; - else return name; - } - - // JoinDefinition - JoinDefinition::JoinDefinition() : - left(NULL), - right(NULL), - condition(NULL), - type(kJoinInner) {} - - JoinDefinition::~JoinDefinition() { - delete left; - delete right; - delete condition; - } + JoinDefinition::~JoinDefinition() { + delete left; + delete right; + delete condition; + } } // namespace hsql diff --git a/src/sqlhelper.cpp b/src/sqlhelper.cpp index 29cafab..7a67c3d 100644 --- a/src/sqlhelper.cpp +++ b/src/sqlhelper.cpp @@ -5,204 +5,204 @@ namespace hsql { - void printOperatorExpression(Expr* expr, uintmax_t numIndent); + void printOperatorExpression(Expr* expr, uintmax_t numIndent); - std::string indent(uintmax_t numIndent) { - return std::string(numIndent, '\t'); + std::string indent(uintmax_t numIndent) { + return std::string(numIndent, '\t'); + } + void inprint(int64_t val, uintmax_t numIndent) { + printf("%s%lld \n", indent(numIndent).c_str(), val); + } + void inprint(float val, uintmax_t numIndent) { + printf("%s%f\n", indent(numIndent).c_str(), val); + } + void inprint(const char* val, uintmax_t numIndent) { + printf("%s%s\n", indent(numIndent).c_str(), val); + } + void inprint(const char* val, const char* val2, uintmax_t numIndent) { + printf("%s%s->%s\n", indent(numIndent).c_str(), val, val2); + } + void inprintC(char val, uintmax_t numIndent) { + printf("%s%c\n", indent(numIndent).c_str(), val); + } + void inprintU(uint64_t val, uintmax_t numIndent) { + printf("%s%llu\n", indent(numIndent).c_str(), val); + } + + void printTableRefInfo(TableRef* table, uintmax_t numIndent) { + switch (table->type) { + case kTableName: + inprint(table->name, numIndent); + break; + case kTableSelect: + printSelectStatementInfo(table->select, numIndent); + break; + case kTableJoin: + inprint("Join Table", numIndent); + inprint("Left", numIndent + 1); + printTableRefInfo(table->join->left, numIndent + 2); + inprint("Right", numIndent + 1); + printTableRefInfo(table->join->right, numIndent + 2); + inprint("Join Condition", numIndent + 1); + printExpression(table->join->condition, numIndent + 2); + break; + case kTableCrossProduct: + for (TableRef* tbl : *table->list) printTableRefInfo(tbl, numIndent); + break; } - void inprint(int64_t val, uintmax_t numIndent) { - printf("%s%lld \n", indent(numIndent).c_str(), val); + if (table->alias != NULL) { + inprint("Alias", numIndent + 1); + inprint(table->alias, numIndent + 2); } - void inprint(float val, uintmax_t numIndent) { - printf("%s%f\n", indent(numIndent).c_str(), val); - } - void inprint(const char* val, uintmax_t numIndent) { - printf("%s%s\n", indent(numIndent).c_str(), val); - } - void inprint(const char* val, const char* val2, uintmax_t numIndent) { - printf("%s%s->%s\n", indent(numIndent).c_str(), val, val2); - } - void inprintC(char val, uintmax_t numIndent) { - printf("%s%c\n", indent(numIndent).c_str(), val); - } - void inprintU(uint64_t val, uintmax_t numIndent) { - printf("%s%llu\n", indent(numIndent).c_str(), val); + } + + void printOperatorExpression(Expr* expr, uintmax_t numIndent) { + if (expr == NULL) { + inprint("null", numIndent); + return; } - void printTableRefInfo(TableRef* table, uintmax_t numIndent) { - switch (table->type) { - case kTableName: - inprint(table->name, numIndent); - break; - case kTableSelect: - printSelectStatementInfo(table->select, numIndent); - break; - case kTableJoin: - inprint("Join Table", numIndent); - inprint("Left", numIndent+1); - printTableRefInfo(table->join->left, numIndent+2); - inprint("Right", numIndent+1); - printTableRefInfo(table->join->right, numIndent+2); - inprint("Join Condition", numIndent+1); - printExpression(table->join->condition, numIndent+2); - break; - case kTableCrossProduct: - for (TableRef* tbl : *table->list) printTableRefInfo(tbl, numIndent); - break; - } - if (table->alias != NULL) { - inprint("Alias", numIndent+1); - inprint(table->alias, numIndent+2); - } + switch (expr->op_type) { + case Expr::SIMPLE_OP: + inprintC(expr->op_char, numIndent); + break; + case Expr::AND: + inprint("AND", numIndent); + break; + case Expr::OR: + inprint("OR", numIndent); + break; + case Expr::NOT: + inprint("NOT", numIndent); + break; + default: + inprintU(expr->op_type, numIndent); + break; } + printExpression(expr->expr, numIndent + 1); + if (expr->expr2 != NULL) printExpression(expr->expr2, numIndent + 1); + } - void printOperatorExpression(Expr* expr, uintmax_t numIndent) { - if (expr == NULL) { - inprint("null", numIndent); - return; - } - - switch (expr->op_type) { - case Expr::SIMPLE_OP: - inprintC(expr->op_char, numIndent); - break; - case Expr::AND: - inprint("AND", numIndent); - break; - case Expr::OR: - inprint("OR", numIndent); - break; - case Expr::NOT: - inprint("NOT", numIndent); - break; - default: - inprintU(expr->op_type, numIndent); - break; - } - printExpression(expr->expr, numIndent+1); - if (expr->expr2 != NULL) printExpression(expr->expr2, numIndent+1); + void printExpression(Expr* expr, uintmax_t numIndent) { + switch (expr->type) { + case kExprStar: + inprint("*", numIndent); + break; + case kExprColumnRef: + inprint(expr->name, numIndent); + break; + // case kExprTableColumnRef: inprint(expr->table, expr->name, numIndent); break; + case kExprLiteralFloat: + inprint(expr->fval, numIndent); + break; + case kExprLiteralInt: + inprint(expr->ival, numIndent); + break; + case kExprLiteralString: + inprint(expr->name, numIndent); + break; + case kExprFunctionRef: + inprint(expr->name, numIndent); + inprint(expr->expr->name, numIndent + 1); + break; + case kExprOperator: + printOperatorExpression(expr, numIndent); + break; + default: + fprintf(stderr, "Unrecognized expression type %d\n", expr->type); + return; } - - void printExpression(Expr* expr, uintmax_t numIndent) { - switch (expr->type) { - case kExprStar: - inprint("*", numIndent); - break; - case kExprColumnRef: - inprint(expr->name, numIndent); - break; - // case kExprTableColumnRef: inprint(expr->table, expr->name, numIndent); break; - case kExprLiteralFloat: - inprint(expr->fval, numIndent); - break; - case kExprLiteralInt: - inprint(expr->ival, numIndent); - break; - case kExprLiteralString: - inprint(expr->name, numIndent); - break; - case kExprFunctionRef: - inprint(expr->name, numIndent); - inprint(expr->expr->name, numIndent+1); - break; - case kExprOperator: - printOperatorExpression(expr, numIndent); - break; - default: - fprintf(stderr, "Unrecognized expression type %d\n", expr->type); - return; - } - if (expr->alias != NULL) { - inprint("Alias", numIndent+1); - inprint(expr->alias, numIndent+2); - } + if (expr->alias != NULL) { + inprint("Alias", numIndent + 1); + inprint(expr->alias, numIndent + 2); } + } - void printSelectStatementInfo(SelectStatement* stmt, uintmax_t numIndent) { - inprint("SelectStatement", numIndent); - inprint("Fields:", numIndent+1); - for (Expr* expr : *stmt->selectList) printExpression(expr, numIndent+2); + void printSelectStatementInfo(SelectStatement* stmt, uintmax_t numIndent) { + inprint("SelectStatement", numIndent); + inprint("Fields:", numIndent + 1); + for (Expr* expr : *stmt->selectList) printExpression(expr, numIndent + 2); - inprint("Sources:", numIndent+1); - printTableRefInfo(stmt->fromTable, numIndent+2); + inprint("Sources:", numIndent + 1); + printTableRefInfo(stmt->fromTable, numIndent + 2); - if (stmt->whereClause != NULL) { - inprint("Search Conditions:", numIndent+1); - printExpression(stmt->whereClause, numIndent+2); - } - - - if (stmt->unionSelect != NULL) { - inprint("Union:", numIndent+1); - printSelectStatementInfo(stmt->unionSelect, numIndent+2); - } - - if (stmt->order != NULL) { - inprint("OrderBy:", numIndent+1); - printExpression(stmt->order->expr, numIndent+2); - if (stmt->order->type == kOrderAsc) inprint("ascending", numIndent+2); - else inprint("descending", numIndent+2); - } - - if (stmt->limit != NULL) { - inprint("Limit:", numIndent+1); - inprint(stmt->limit->limit, numIndent+2); - } + if (stmt->whereClause != NULL) { + inprint("Search Conditions:", numIndent + 1); + printExpression(stmt->whereClause, numIndent + 2); } - - void printImportStatementInfo(ImportStatement* stmt, uintmax_t numIndent) { - inprint("ImportStatment", numIndent); - inprint(stmt->filePath, numIndent+1); - inprint(stmt->tableName, numIndent+1); + if (stmt->unionSelect != NULL) { + inprint("Union:", numIndent + 1); + printSelectStatementInfo(stmt->unionSelect, numIndent + 2); } - void printCreateStatementInfo(CreateStatement* stmt, uintmax_t numIndent) { - inprint("CreateStatment", numIndent); - inprint(stmt->tableName, numIndent+1); - inprint(stmt->filePath, numIndent+1); + if (stmt->order != NULL) { + inprint("OrderBy:", numIndent + 1); + printExpression(stmt->order->expr, numIndent + 2); + if (stmt->order->type == kOrderAsc) inprint("ascending", numIndent + 2); + else inprint("descending", numIndent + 2); } - void printInsertStatementInfo(InsertStatement* stmt, uintmax_t numIndent) { - inprint("InsertStatment", numIndent); - inprint(stmt->tableName, numIndent+1); - if (stmt->columns != NULL) { - inprint("Columns", numIndent+1); - for (char* col_name : *stmt->columns) { - inprint(col_name, numIndent+2); - } - } - switch (stmt->type) { - case InsertStatement::kInsertValues: - inprint("Values", numIndent+1); - for (Expr* expr : *stmt->values) { - printExpression(expr, numIndent+2); - } - break; - case InsertStatement::kInsertSelect: - printSelectStatementInfo(stmt->select, numIndent+1); - break; - } + if (stmt->limit != NULL) { + inprint("Limit:", numIndent + 1); + inprint(stmt->limit->limit, numIndent + 2); } + } - void printStatementInfo(SQLStatement* stmt) { - switch (stmt->type()) { - case kStmtSelect: - printSelectStatementInfo((SelectStatement*) stmt, 0); - break; - case kStmtInsert: - printInsertStatementInfo((InsertStatement*) stmt, 0); - break; - case kStmtCreate: - printCreateStatementInfo((CreateStatement*) stmt, 0); - break; - case kStmtImport: - printImportStatementInfo((ImportStatement*) stmt, 0); - break; - default: - break; - } + + + void printImportStatementInfo(ImportStatement* stmt, uintmax_t numIndent) { + inprint("ImportStatment", numIndent); + inprint(stmt->filePath, numIndent + 1); + inprint(stmt->tableName, numIndent + 1); + } + + void printCreateStatementInfo(CreateStatement* stmt, uintmax_t numIndent) { + inprint("CreateStatment", numIndent); + inprint(stmt->tableName, numIndent + 1); + inprint(stmt->filePath, numIndent + 1); + } + + void printInsertStatementInfo(InsertStatement* stmt, uintmax_t numIndent) { + inprint("InsertStatment", numIndent); + inprint(stmt->tableName, numIndent + 1); + if (stmt->columns != NULL) { + inprint("Columns", numIndent + 1); + for (char* col_name : *stmt->columns) { + inprint(col_name, numIndent + 2); + } } + switch (stmt->type) { + case InsertStatement::kInsertValues: + inprint("Values", numIndent + 1); + for (Expr* expr : *stmt->values) { + printExpression(expr, numIndent + 2); + } + break; + case InsertStatement::kInsertSelect: + printSelectStatementInfo(stmt->select, numIndent + 1); + break; + } + } + + void printStatementInfo(SQLStatement* stmt) { + switch (stmt->type()) { + case kStmtSelect: + printSelectStatementInfo((SelectStatement*) stmt, 0); + break; + case kStmtInsert: + printInsertStatementInfo((InsertStatement*) stmt, 0); + break; + case kStmtCreate: + printCreateStatementInfo((CreateStatement*) stmt, 0); + break; + case kStmtImport: + printImportStatementInfo((ImportStatement*) stmt, 0); + break; + default: + break; + } + } } // namespace hsql \ No newline at end of file diff --git a/src/sqlhelper.h b/src/sqlhelper.h index 7be3663..f76919f 100644 --- a/src/sqlhelper.h +++ b/src/sqlhelper.h @@ -5,12 +5,12 @@ namespace hsql { - void printStatementInfo(SQLStatement* stmt); - void printSelectStatementInfo(SelectStatement* stmt, uintmax_t num_indent); - void printImportStatementInfo(ImportStatement* stmt, uintmax_t num_indent); - void printInsertStatementInfo(InsertStatement* stmt, uintmax_t num_indent); - void printCreateStatementInfo(CreateStatement* stmt, uintmax_t num_indent); - void printExpression(Expr* expr, uintmax_t num_indent); + void printStatementInfo(SQLStatement* stmt); + void printSelectStatementInfo(SelectStatement* stmt, uintmax_t num_indent); + void printImportStatementInfo(ImportStatement* stmt, uintmax_t num_indent); + void printInsertStatementInfo(InsertStatement* stmt, uintmax_t num_indent); + void printCreateStatementInfo(CreateStatement* stmt, uintmax_t num_indent); + void printExpression(Expr* expr, uintmax_t num_indent); } // namespace hsql diff --git a/test/lib/select.cpp b/test/lib/select.cpp index 04af2a6..2fc4767 100644 --- a/test/lib/select.cpp +++ b/test/lib/select.cpp @@ -7,40 +7,40 @@ using namespace hsql; TEST(SelectTest) { - TEST_PARSE_SINGLE_SQL("SELECT * FROM students;", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL("SELECT * FROM students;", kStmtSelect, SelectStatement, stmt); - ASSERT_NULL(stmt->whereClause); - ASSERT_NULL(stmt->groupBy); + ASSERT_NULL(stmt->whereClause); + ASSERT_NULL(stmt->groupBy); } TEST(SelectHavingTest) { - TEST_PARSE_SINGLE_SQL("SELECT city, AVG(grade) AS avg_grade FROM students GROUP BY city HAVING AVG(grade) < 2.0", kStmtSelect, SelectStatement, stmt); - ASSERT_FALSE(stmt->selectDistinct); + TEST_PARSE_SINGLE_SQL("SELECT city, AVG(grade) AS avg_grade FROM students GROUP BY city HAVING AVG(grade) < 2.0", kStmtSelect, SelectStatement, stmt); + ASSERT_FALSE(stmt->selectDistinct); - GroupByDescription* group = stmt->groupBy; - ASSERT_NOTNULL(group); - ASSERT_EQ(group->columns->size(), 1); - ASSERT(group->having->isSimpleOp('<')); - ASSERT(group->having->expr->isType(kExprFunctionRef)); - ASSERT(group->having->expr2->isType(kExprLiteralFloat)); + GroupByDescription* group = stmt->groupBy; + ASSERT_NOTNULL(group); + ASSERT_EQ(group->columns->size(), 1); + ASSERT(group->having->isSimpleOp('<')); + ASSERT(group->having->expr->isType(kExprFunctionRef)); + ASSERT(group->having->expr2->isType(kExprLiteralFloat)); } TEST(SelectDistinctTest) { - TEST_PARSE_SINGLE_SQL("SELECT DISTINCT grade, city FROM students;", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL("SELECT DISTINCT grade, city FROM students;", kStmtSelect, SelectStatement, stmt); - ASSERT(stmt->selectDistinct); - ASSERT_NULL(stmt->whereClause); + ASSERT(stmt->selectDistinct); + ASSERT_NULL(stmt->whereClause); } TEST(SelectGroupDistinctTest) { - TEST_PARSE_SINGLE_SQL("SELECT city, COUNT(name), COUNT(DISTINCT grade) FROM students GROUP BY city;", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL("SELECT city, COUNT(name), COUNT(DISTINCT grade) FROM students GROUP BY city;", kStmtSelect, SelectStatement, stmt); - ASSERT_FALSE(stmt->selectDistinct); - ASSERT_EQ(stmt->selectList->size(), 3); - ASSERT(!stmt->selectList->at(1)->distinct); - ASSERT(stmt->selectList->at(2)->distinct); + ASSERT_FALSE(stmt->selectDistinct); + ASSERT_EQ(stmt->selectList->size(), 3); + ASSERT(!stmt->selectList->at(1)->distinct); + ASSERT(stmt->selectList->at(2)->distinct); } diff --git a/test/lib/test.cpp b/test/lib/test.cpp index c2be54e..b39f8fc 100644 --- a/test/lib/test.cpp +++ b/test/lib/test.cpp @@ -3,57 +3,57 @@ class TestsManager { - // Note: static initialization fiasco - // http://www.parashift.com/c++-faq-lite/static-init-order.html - // http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html -public: - static std::vector& testNames() { - static std::vector* _testNames = new std::vector; - return *_testNames; - } + // Note: static initialization fiasco + // http://www.parashift.com/c++-faq-lite/static-init-order.html + // http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html + public: + static std::vector& testNames() { + static std::vector* _testNames = new std::vector; + return *_testNames; + } - static std::vector& tests() { - static std::vector* tests = new std::vector; - return *tests; - } + static std::vector& tests() { + static std::vector* tests = new std::vector; + return *tests; + } }; int AddTest(void (*foo)(void), std::string name) { - TestsManager::tests().push_back(foo); - TestsManager::testNames().push_back(name); - return 0; + TestsManager::tests().push_back(foo); + TestsManager::testNames().push_back(name); + return 0; } size_t RunTests() { - size_t numFailed = 0; - for (size_t i = 0; i < TestsManager::tests().size(); ++i) { - printf("\033[0;32m{ running}\033[0m %s\n", TestsManager::testNames()[i].c_str()); + size_t numFailed = 0; + for (size_t i = 0; i < TestsManager::tests().size(); ++i) { + printf("\033[0;32m{ running}\033[0m %s\n", TestsManager::testNames()[i].c_str()); - try { - // Run test - (*TestsManager::tests()[i])(); - printf("\033[0;32m{ ok}\033[0m %s\n", TestsManager::testNames()[i].c_str()); + try { + // Run test + (*TestsManager::tests()[i])(); + printf("\033[0;32m{ ok}\033[0m %s\n", TestsManager::testNames()[i].c_str()); - } catch (AssertionFailedException& e) { - printf("\033[1;31m{ failed} %s\n", TestsManager::testNames()[i].c_str()); - printf("\tAssertion failed: %s\n\033[0m", e.what()); - numFailed++; - } + } catch (AssertionFailedException& e) { + printf("\033[1;31m{ failed} %s\n", TestsManager::testNames()[i].c_str()); + printf("\tAssertion failed: %s\n\033[0m", e.what()); + numFailed++; } - return numFailed; + } + return numFailed; } int main() { - size_t numFailed = RunTests(); - if (numFailed == 0) { - return 0; - } else { - return -1; - } + size_t numFailed = RunTests(); + if (numFailed == 0) { + return 0; + } else { + return -1; + } } \ No newline at end of file diff --git a/test/lib/test.h b/test/lib/test.h index 19efae6..19fe70e 100644 --- a/test/lib/test.h +++ b/test/lib/test.h @@ -31,17 +31,17 @@ class AssertionFailedException: public std::exception { -public: - AssertionFailedException(std::string msg) : - std::exception(), - _msg(msg) {}; + public: + AssertionFailedException(std::string msg) : + std::exception(), + _msg(msg) {}; - virtual const char* what() const throw() { - return _msg.c_str(); - } + virtual const char* what() const throw() { + return _msg.c_str(); + } -protected: - std::string _msg; + protected: + std::string _msg; }; int AddTest(void (*foo)(void), std::string name); From 5cf62f6b1d1847d20639ee8df18a0c8049f6c962 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 02:33:42 +0100 Subject: [PATCH 5/7] add documentation to parser and parsing result --- src/SQLParser.cpp | 6 +++--- src/SQLParser.h | 9 ++++++--- src/SQLParserResult.cpp | 5 ----- src/SQLParserResult.h | 32 ++++++++++++++++++++++---------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/SQLParser.cpp b/src/SQLParser.cpp index 9270745..217db47 100644 --- a/src/SQLParser.cpp +++ b/src/SQLParser.cpp @@ -12,22 +12,22 @@ namespace hsql { fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); } - SQLParserResult* SQLParser::parseSQLString(const char* text) { SQLParserResult* result = NULL; yyscan_t scanner; YY_BUFFER_STATE state; if (hsql_lex_init(&scanner)) { - // couldn't initialize + // Couldn't initialize the lexer. fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); return NULL; } state = hsql__scan_string(text, scanner); + // Parser and return early if it failed. if (hsql_parse(&result, scanner)) { - // Returns an error stmt object + // Returns an error stmt object. return result; } diff --git a/src/SQLParser.h b/src/SQLParser.h index 50c3f18..5bb2a4a 100644 --- a/src/SQLParser.h +++ b/src/SQLParser.h @@ -5,15 +5,18 @@ #include "sql/statements.h" namespace hsql { - /** - * Main class for parsing SQL strings - */ + + // Static methods used to parse SQL strings. class SQLParser { public: + // Parses a given constant character SQL string. static SQLParserResult* parseSQLString(const char* sql); + + // Parses an SQL std::string. static SQLParserResult* parseSQLString(const std::string& sql); private: + // Static class can't be instatiated. SQLParser(); }; diff --git a/src/SQLParserResult.cpp b/src/SQLParserResult.cpp index 52c9f02..ed7e1b5 100644 --- a/src/SQLParserResult.cpp +++ b/src/SQLParserResult.cpp @@ -7,14 +7,12 @@ namespace hsql { isValid(true), errorMsg(NULL) {}; - SQLParserResult::SQLParserResult(SQLStatement* stmt) : isValid(true), errorMsg(NULL) { addStatement(stmt); }; - SQLParserResult::~SQLParserResult() { for (std::vector::iterator it = statements.begin(); it != statements.end(); ++it) { delete *it; @@ -23,17 +21,14 @@ namespace hsql { delete errorMsg; } - void SQLParserResult::addStatement(SQLStatement* stmt) { statements.push_back(stmt); } - SQLStatement* SQLParserResult::getStatement(int id) { return statements[id]; } - size_t SQLParserResult::size() { return statements.size(); } diff --git a/src/SQLParserResult.h b/src/SQLParserResult.h index f06bd27..5622dda 100644 --- a/src/SQLParserResult.h +++ b/src/SQLParserResult.h @@ -4,29 +4,41 @@ #include "sql/SQLStatement.h" namespace hsql { - /** - * Represents the result of the SQLParser. - * If parsing was successful it contains a list of SQLStatement. - */ + // Represents the result of the SQLParser. + // If parsing was successful it contains a list of SQLStatement. class SQLParserResult { public: - + // Initialize with empty statement list. SQLParserResult(); + + // Initialize with a single statement. SQLParserResult(SQLStatement* stmt); + + // Deletes all statements in the resul. virtual ~SQLParserResult(); - void addStatement(SQLStatement* stmt); - - SQLStatement* getStatement(int id); - + // Returns the number of statements in the result. size_t size(); - // public properties + // Gets the SQL statement with the given index. + SQLStatement* getStatement(int id); + + // Adds a statement to the result list of statements. + void addStatement(SQLStatement* stmt); + + // List of statements within the result. std::vector statements; + + // Flag indicating the parsing was successful. bool isValid; + // Error message, if an error occurred. const char* errorMsg; + + // Line number of the occurrance of the error in the query. int errorLine; + + // Column number of the occurrance of the error in the query. int errorColumn; }; From ec46b28f323fb41b70ad7867bfb86ec95343d192 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 02:59:07 +0100 Subject: [PATCH 6/7] improved interface of SQLParserResult --- src/SQLParserResult.cpp | 44 ++++++++++++++++++++++++++++----------- src/SQLParserResult.h | 32 +++++++++++++++++++++------- src/sql/SQLStatement.h | 2 +- src/sql/statements.cpp | 2 +- test/lib/helper.h | 10 ++++----- test/sql_grammar_test.cpp | 6 +++--- test/sql_tests.cpp | 20 +++++++++--------- 7 files changed, 77 insertions(+), 39 deletions(-) diff --git a/src/SQLParserResult.cpp b/src/SQLParserResult.cpp index ed7e1b5..94cd5e0 100644 --- a/src/SQLParserResult.cpp +++ b/src/SQLParserResult.cpp @@ -4,33 +4,53 @@ namespace hsql { SQLParserResult::SQLParserResult() : - isValid(true), - errorMsg(NULL) {}; + isValid_(true), + errorMsg_(NULL) {}; SQLParserResult::SQLParserResult(SQLStatement* stmt) : - isValid(true), - errorMsg(NULL) { + isValid_(true), + errorMsg_(NULL) { addStatement(stmt); }; SQLParserResult::~SQLParserResult() { - for (std::vector::iterator it = statements.begin(); it != statements.end(); ++it) { - delete *it; + for (SQLStatement* statement : statements_) { + delete statement; } - delete errorMsg; + delete errorMsg_; } void SQLParserResult::addStatement(SQLStatement* stmt) { - statements.push_back(stmt); + statements_.push_back(stmt); } - SQLStatement* SQLParserResult::getStatement(int id) { - return statements[id]; + const SQLStatement* SQLParserResult::getStatement(int index) const { + return statements_[index]; } - size_t SQLParserResult::size() { - return statements.size(); + SQLStatement* SQLParserResult::getMutableStatement(int index) { + return statements_[index]; + } + + size_t SQLParserResult::size() const { + return statements_.size(); + } + + bool SQLParserResult::isValid() const { + return isValid_; + } + + const char* SQLParserResult::errorMsg() const { + return errorMsg_; + } + + int SQLParserResult::errorLine() const { + return errorLine_; + } + + int SQLParserResult::errorColumn() const { + return errorColumn_; } } // namespace hsql \ No newline at end of file diff --git a/src/SQLParserResult.h b/src/SQLParserResult.h index 5622dda..cbc815a 100644 --- a/src/SQLParserResult.h +++ b/src/SQLParserResult.h @@ -12,34 +12,52 @@ namespace hsql { SQLParserResult(); // Initialize with a single statement. + // Takes ownership of the statement. SQLParserResult(SQLStatement* stmt); // Deletes all statements in the resul. virtual ~SQLParserResult(); + // Returns true if parsing was successful. + bool isValid() const; + // Returns the number of statements in the result. - size_t size(); + size_t size() const; + + // Returns the error message, if an error occurred. + const char* errorMsg() const; + + // Returns the line number of the occurrance of the error in the query. + int errorLine() const; + + // Returns the column number of the occurrance of the error in the query. + int errorColumn() const; // Gets the SQL statement with the given index. - SQLStatement* getStatement(int id); + const SQLStatement* getStatement(int index) const; + + // Gets the non const SQL statement with the given index. + SQLStatement* getMutableStatement(int index); // Adds a statement to the result list of statements. + // Takes ownership of the statement. void addStatement(SQLStatement* stmt); + private: // List of statements within the result. - std::vector statements; + std::vector statements_; // Flag indicating the parsing was successful. - bool isValid; + bool isValid_; // Error message, if an error occurred. - const char* errorMsg; + const char* errorMsg_; // Line number of the occurrance of the error in the query. - int errorLine; + int errorLine_; // Column number of the occurrance of the error in the query. - int errorColumn; + int errorColumn_; }; } // namespace hsql diff --git a/src/sql/SQLStatement.h b/src/sql/SQLStatement.h index ffbe049..9aa9434 100644 --- a/src/sql/SQLStatement.h +++ b/src/sql/SQLStatement.h @@ -29,7 +29,7 @@ namespace hsql { virtual ~SQLStatement(); - virtual StatementType type(); + virtual StatementType type() const; private: StatementType _type; diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 45da79d..36467b8 100644 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -9,7 +9,7 @@ namespace hsql { SQLStatement::~SQLStatement() {} - StatementType SQLStatement::type() { + StatementType SQLStatement::type() const { return _type; } diff --git a/test/lib/helper.h b/test/lib/helper.h index 5e74da9..143e725 100644 --- a/test/lib/helper.h +++ b/test/lib/helper.h @@ -3,20 +3,20 @@ #define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \ - SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ - ASSERT(outputVar->isValid); \ + const SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ + ASSERT(outputVar->isValid()); \ ASSERT_EQ(outputVar->size(), numStatements); #define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \ TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \ ASSERT_EQ(stmt_list->getStatement(0)->type(), stmtType); \ - stmtClass* outputVar = (stmtClass*) stmt_list->getStatement(0); + const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(0); #define TEST_CAST_STMT(stmt_list, stmt_index, stmtType, stmtClass, outputVar) \ ASSERT_EQ(stmt_list->getStatement(stmt_index)->type(), stmtType); \ - stmtClass* outputVar = (stmtClass*) stmt_list->getStatement(stmt_index); + const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(stmt_index); -#endif \ No newline at end of file +#endif diff --git a/test/sql_grammar_test.cpp b/test/sql_grammar_test.cpp index e4ca602..177bfea 100644 --- a/test/sql_grammar_test.cpp +++ b/test/sql_grammar_test.cpp @@ -67,15 +67,15 @@ int main(int argc, char *argv[]) { start = std::chrono::system_clock::now(); // Parsing - SQLParserResult* stmt_list = SQLParser::parseSQLString(sql.c_str()); + SQLParserResult* result = SQLParser::parseSQLString(sql.c_str()); end = std::chrono::system_clock::now(); std::chrono::duration elapsed_seconds = end-start; double us = elapsed_seconds.count() * 1000 * 1000; - if (expectFalse == stmt_list->isValid) { + if (expectFalse == result->isValid()) { printf("\033[0;31m{ failed}\033[0m\n"); - printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", stmt_list->errorMsg, stmt_list->errorLine, stmt_list->errorColumn); + printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", result->errorMsg(), result->errorLine(), result->errorColumn()); printf("\t%s\n", sql.c_str()); numFailed++; } else { diff --git a/test/sql_tests.cpp b/test/sql_tests.cpp index 8550e00..c7248e3 100644 --- a/test/sql_tests.cpp +++ b/test/sql_tests.cpp @@ -11,12 +11,12 @@ using namespace hsql; TEST(DeleteStatementTest) { - SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); - ASSERT(result->isValid); + const SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); + ASSERT(result->isValid()); ASSERT_EQ(result->size(), 1); ASSERT(result->getStatement(0)->type() == kStmtDelete); - DeleteStatement* stmt = (DeleteStatement*) result->getStatement(0); + const DeleteStatement* stmt = (const DeleteStatement*) result->getStatement(0); ASSERT_STREQ(stmt->tableName, "students"); ASSERT_NOTNULL(stmt->expr); ASSERT(stmt->expr->isType(kExprOperator)); @@ -25,12 +25,12 @@ TEST(DeleteStatementTest) { } TEST(CreateStatementTest) { - SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); - ASSERT(result->isValid); + const SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); + ASSERT(result->isValid()); ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->getStatement(0)->type(), kStmtCreate); - CreateStatement* stmt = (CreateStatement*) result->getStatement(0); + const CreateStatement* stmt = (const CreateStatement*) result->getStatement(0); ASSERT_EQ(stmt->type, CreateStatement::kTable); ASSERT_STREQ(stmt->tableName, "students"); ASSERT_NOTNULL(stmt->columns); @@ -47,12 +47,12 @@ TEST(CreateStatementTest) { TEST(UpdateStatementTest) { - SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); - ASSERT(result->isValid); + const SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); + ASSERT(result->isValid()); ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->getStatement(0)->type(), kStmtUpdate); - UpdateStatement* stmt = (UpdateStatement*) result->getStatement(0); + const UpdateStatement* stmt = (const UpdateStatement*) result->getStatement(0); ASSERT_NOTNULL(stmt->table); ASSERT_STREQ(stmt->table->name, "students"); @@ -142,4 +142,4 @@ TEST(ExecuteStatementTest) { ASSERT_STREQ(stmt->name, "test"); ASSERT_EQ(stmt->parameters->size(), 2); -} \ No newline at end of file +} From 4aca7d035f85737296fd9e28cab11f58559adc05 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 03:07:51 +0100 Subject: [PATCH 7/7] fixed inconsistend member naming. fixed building of error result in parser --- src/SQLParserResult.cpp | 10 + src/SQLParserResult.h | 7 + src/parser/bison_parser.cpp | 512 ++++++++++++++++++------------------ src/parser/bison_parser.h | 4 +- src/parser/bison_parser.y | 6 +- src/sql/Expr.cpp | 14 +- src/sql/Expr.h | 4 +- src/sql/SQLStatement.h | 2 +- src/sql/statements.cpp | 4 +- src/sqlhelper.cpp | 6 +- 10 files changed, 291 insertions(+), 278 deletions(-) diff --git a/src/SQLParserResult.cpp b/src/SQLParserResult.cpp index 94cd5e0..9481e13 100644 --- a/src/SQLParserResult.cpp +++ b/src/SQLParserResult.cpp @@ -53,4 +53,14 @@ namespace hsql { return errorColumn_; } + void SQLParserResult::setIsValid(bool isValid) { + isValid_ = isValid; + } + + void SQLParserResult::setErrorDetails(const char* errorMsg, int errorLine, int errorColumn) { + errorMsg_ = errorMsg; + errorLine_ = errorLine; + errorColumn_ = errorColumn; + } + } // namespace hsql \ No newline at end of file diff --git a/src/SQLParserResult.h b/src/SQLParserResult.h index cbc815a..7cc3006 100644 --- a/src/SQLParserResult.h +++ b/src/SQLParserResult.h @@ -43,6 +43,13 @@ namespace hsql { // Takes ownership of the statement. void addStatement(SQLStatement* stmt); + // Set whether parsing was successful. + void setIsValid(bool isValid); + + // Set the details of the error, if available. + void setErrorDetails(const char* errorMsg, int errorLine, int errorColumn); + + private: // List of statements within the result. std::vector statements_; diff --git a/src/parser/bison_parser.cpp b/src/parser/bison_parser.cpp index c8fc3d6..f790a65 100644 --- a/src/parser/bison_parser.cpp +++ b/src/parser/bison_parser.cpp @@ -94,10 +94,8 @@ using namespace hsql; int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const char *msg) { SQLParserResult* list = new SQLParserResult(); - list->isValid = false; - list->errorMsg = strdup(msg); - list->errorLine = llocp->first_line; - list->errorColumn = llocp->first_column; + list->setIsValid(false); + list->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column); *result = list; return 0; @@ -106,7 +104,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch -#line 110 "bison_parser.cpp" /* yacc.c:339 */ +#line 108 "bison_parser.cpp" /* yacc.c:339 */ # ifndef YY_NULLPTR # if defined __cplusplus && 201103L <= __cplusplus @@ -144,7 +142,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch extern int hsql_debug; #endif /* "%code requires" blocks. */ -#line 42 "bison_parser.y" /* yacc.c:355 */ +#line 40 "bison_parser.y" /* yacc.c:355 */ // %code requires block @@ -167,7 +165,7 @@ extern int hsql_debug; } \ } -#line 171 "bison_parser.cpp" /* yacc.c:355 */ +#line 169 "bison_parser.cpp" /* yacc.c:355 */ /* Token type. */ #ifndef HSQL_TOKENTYPE @@ -305,7 +303,7 @@ extern int hsql_debug; typedef union HSQL_STYPE HSQL_STYPE; union HSQL_STYPE { -#line 101 "bison_parser.y" /* yacc.c:355 */ +#line 99 "bison_parser.y" /* yacc.c:355 */ double fval; int64_t ival; @@ -341,7 +339,7 @@ union HSQL_STYPE std::vector* update_vec; std::vector* expr_vec; -#line 345 "bison_parser.cpp" /* yacc.c:355 */ +#line 343 "bison_parser.cpp" /* yacc.c:355 */ }; # define HSQL_STYPE_IS_TRIVIAL 1 # define HSQL_STYPE_IS_DECLARED 1 @@ -369,7 +367,7 @@ int hsql_parse (hsql::SQLParserResult** result, yyscan_t scanner); /* Copy the second part of user declarations. */ -#line 373 "bison_parser.cpp" /* yacc.c:358 */ +#line 371 "bison_parser.cpp" /* yacc.c:358 */ #ifdef short # undef short @@ -681,21 +679,21 @@ static const yytype_uint8 yytranslate[] = /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ static const yytype_uint16 yyrline[] = { - 0, 229, 229, 236, 237, 241, 246, 251, 252, 253, - 254, 255, 256, 257, 258, 259, 267, 272, 280, 284, - 296, 304, 308, 318, 324, 333, 334, 338, 339, 343, - 350, 351, 352, 353, 363, 367, 379, 387, 399, 405, - 415, 416, 426, 435, 436, 440, 452, 453, 457, 458, - 462, 467, 479, 480, 481, 485, 496, 497, 501, 506, - 511, 512, 516, 521, 525, 526, 529, 530, 534, 535, - 536, 541, 542, 543, 550, 551, 555, 556, 560, 567, - 568, 569, 570, 571, 575, 576, 577, 581, 582, 586, - 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, - 601, 602, 603, 604, 605, 606, 610, 614, 615, 619, - 620, 621, 625, 630, 631, 635, 639, 644, 655, 656, - 666, 667, 673, 678, 679, 684, 694, 702, 703, 708, - 709, 713, 714, 722, 734, 735, 736, 737, 738, 744, - 750, 754, 763, 764, 769, 770 + 0, 227, 227, 234, 235, 239, 244, 249, 250, 251, + 252, 253, 254, 255, 256, 257, 265, 270, 278, 282, + 294, 302, 306, 316, 322, 331, 332, 336, 337, 341, + 348, 349, 350, 351, 361, 365, 377, 385, 397, 403, + 413, 414, 424, 433, 434, 438, 450, 451, 455, 456, + 460, 465, 477, 478, 479, 483, 494, 495, 499, 504, + 509, 510, 514, 519, 523, 524, 527, 528, 532, 533, + 534, 539, 540, 541, 548, 549, 553, 554, 558, 565, + 566, 567, 568, 569, 573, 574, 575, 579, 580, 584, + 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, + 599, 600, 601, 602, 603, 604, 608, 612, 613, 617, + 618, 619, 623, 628, 629, 633, 637, 642, 653, 654, + 664, 665, 671, 676, 677, 682, 692, 700, 701, 706, + 707, 711, 712, 720, 732, 733, 734, 735, 736, 742, + 748, 752, 761, 762, 767, 768 }; #endif @@ -1631,7 +1629,7 @@ YYLTYPE yylloc = yyloc_default; yychar = YYEMPTY; /* Cause a token to be read. */ /* User initialization code. */ -#line 79 "bison_parser.y" /* yacc.c:1429 */ +#line 77 "bison_parser.y" /* yacc.c:1429 */ { // Initialize yylloc.first_column = 0; @@ -1642,7 +1640,7 @@ YYLTYPE yylloc = yyloc_default; yylloc.placeholder_id = 0; } -#line 1646 "bison_parser.cpp" /* yacc.c:1429 */ +#line 1644 "bison_parser.cpp" /* yacc.c:1429 */ yylsp[0] = yylloc; goto yysetstate; @@ -1829,356 +1827,356 @@ yyreduce: switch (yyn) { case 2: -#line 229 "bison_parser.y" /* yacc.c:1646 */ +#line 227 "bison_parser.y" /* yacc.c:1646 */ { *result = (yyvsp[-1].stmt_list); } -#line 1837 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1835 "bison_parser.cpp" /* yacc.c:1646 */ break; case 3: -#line 236 "bison_parser.y" /* yacc.c:1646 */ +#line 234 "bison_parser.y" /* yacc.c:1646 */ { (yyval.stmt_list) = new SQLParserResult((yyvsp[0].statement)); } -#line 1843 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1841 "bison_parser.cpp" /* yacc.c:1646 */ break; case 4: -#line 237 "bison_parser.y" /* yacc.c:1646 */ +#line 235 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].stmt_list)->addStatement((yyvsp[0].statement)); (yyval.stmt_list) = (yyvsp[-2].stmt_list); } -#line 1849 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1847 "bison_parser.cpp" /* yacc.c:1646 */ break; case 5: -#line 241 "bison_parser.y" /* yacc.c:1646 */ +#line 239 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[0].prep_stmt)->setPlaceholders(yyloc.placeholder_list); yyloc.placeholder_list.clear(); (yyval.statement) = (yyvsp[0].prep_stmt); } -#line 1859 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1857 "bison_parser.cpp" /* yacc.c:1646 */ break; case 7: -#line 251 "bison_parser.y" /* yacc.c:1646 */ +#line 249 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].select_stmt); } -#line 1865 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1863 "bison_parser.cpp" /* yacc.c:1646 */ break; case 8: -#line 252 "bison_parser.y" /* yacc.c:1646 */ +#line 250 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].import_stmt); } -#line 1871 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1869 "bison_parser.cpp" /* yacc.c:1646 */ break; case 9: -#line 253 "bison_parser.y" /* yacc.c:1646 */ +#line 251 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].create_stmt); } -#line 1877 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1875 "bison_parser.cpp" /* yacc.c:1646 */ break; case 10: -#line 254 "bison_parser.y" /* yacc.c:1646 */ +#line 252 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].insert_stmt); } -#line 1883 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1881 "bison_parser.cpp" /* yacc.c:1646 */ break; case 11: -#line 255 "bison_parser.y" /* yacc.c:1646 */ +#line 253 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].delete_stmt); } -#line 1889 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1887 "bison_parser.cpp" /* yacc.c:1646 */ break; case 12: -#line 256 "bison_parser.y" /* yacc.c:1646 */ +#line 254 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].delete_stmt); } -#line 1895 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1893 "bison_parser.cpp" /* yacc.c:1646 */ break; case 13: -#line 257 "bison_parser.y" /* yacc.c:1646 */ +#line 255 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].update_stmt); } -#line 1901 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1899 "bison_parser.cpp" /* yacc.c:1646 */ break; case 14: -#line 258 "bison_parser.y" /* yacc.c:1646 */ +#line 256 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].drop_stmt); } -#line 1907 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1905 "bison_parser.cpp" /* yacc.c:1646 */ break; case 15: -#line 259 "bison_parser.y" /* yacc.c:1646 */ +#line 257 "bison_parser.y" /* yacc.c:1646 */ { (yyval.statement) = (yyvsp[0].exec_stmt); } -#line 1913 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1911 "bison_parser.cpp" /* yacc.c:1646 */ break; case 16: -#line 267 "bison_parser.y" /* yacc.c:1646 */ +#line 265 "bison_parser.y" /* yacc.c:1646 */ { (yyval.prep_stmt) = new PrepareStatement(); (yyval.prep_stmt)->name = (yyvsp[-2].sval); (yyval.prep_stmt)->query = new SQLParserResult((yyvsp[0].statement)); } -#line 1923 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1921 "bison_parser.cpp" /* yacc.c:1646 */ break; case 17: -#line 272 "bison_parser.y" /* yacc.c:1646 */ +#line 270 "bison_parser.y" /* yacc.c:1646 */ { (yyval.prep_stmt) = new PrepareStatement(); (yyval.prep_stmt)->name = (yyvsp[-4].sval); (yyval.prep_stmt)->query = (yyvsp[-2].stmt_list); } -#line 1933 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1931 "bison_parser.cpp" /* yacc.c:1646 */ break; case 18: -#line 280 "bison_parser.y" /* yacc.c:1646 */ +#line 278 "bison_parser.y" /* yacc.c:1646 */ { (yyval.exec_stmt) = new ExecuteStatement(); (yyval.exec_stmt)->name = (yyvsp[0].sval); } -#line 1942 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1940 "bison_parser.cpp" /* yacc.c:1646 */ break; case 19: -#line 284 "bison_parser.y" /* yacc.c:1646 */ +#line 282 "bison_parser.y" /* yacc.c:1646 */ { (yyval.exec_stmt) = new ExecuteStatement(); (yyval.exec_stmt)->name = (yyvsp[-3].sval); (yyval.exec_stmt)->parameters = (yyvsp[-1].expr_vec); } -#line 1952 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1950 "bison_parser.cpp" /* yacc.c:1646 */ break; case 20: -#line 296 "bison_parser.y" /* yacc.c:1646 */ +#line 294 "bison_parser.y" /* yacc.c:1646 */ { (yyval.import_stmt) = new ImportStatement((ImportStatement::ImportType) (yyvsp[-4].uval)); (yyval.import_stmt)->filePath = (yyvsp[-2].sval); (yyval.import_stmt)->tableName = (yyvsp[0].sval); } -#line 1962 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1960 "bison_parser.cpp" /* yacc.c:1646 */ break; case 21: -#line 304 "bison_parser.y" /* yacc.c:1646 */ +#line 302 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = ImportStatement::kImportCSV; } -#line 1968 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1966 "bison_parser.cpp" /* yacc.c:1646 */ break; case 22: -#line 308 "bison_parser.y" /* yacc.c:1646 */ +#line 306 "bison_parser.y" /* yacc.c:1646 */ { (yyval.sval) = (yyvsp[0].expr)->name; } -#line 1974 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1972 "bison_parser.cpp" /* yacc.c:1646 */ break; case 23: -#line 318 "bison_parser.y" /* yacc.c:1646 */ +#line 316 "bison_parser.y" /* yacc.c:1646 */ { (yyval.create_stmt) = new CreateStatement(CreateStatement::kTableFromTbl); (yyval.create_stmt)->ifNotExists = (yyvsp[-5].bval); (yyval.create_stmt)->tableName = (yyvsp[-4].sval); (yyval.create_stmt)->filePath = (yyvsp[0].sval); } -#line 1985 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1983 "bison_parser.cpp" /* yacc.c:1646 */ break; case 24: -#line 324 "bison_parser.y" /* yacc.c:1646 */ +#line 322 "bison_parser.y" /* yacc.c:1646 */ { (yyval.create_stmt) = new CreateStatement(CreateStatement::kTable); (yyval.create_stmt)->ifNotExists = (yyvsp[-4].bval); (yyval.create_stmt)->tableName = (yyvsp[-3].sval); (yyval.create_stmt)->columns = (yyvsp[-1].column_vec); } -#line 1996 "bison_parser.cpp" /* yacc.c:1646 */ +#line 1994 "bison_parser.cpp" /* yacc.c:1646 */ break; case 25: -#line 333 "bison_parser.y" /* yacc.c:1646 */ +#line 331 "bison_parser.y" /* yacc.c:1646 */ { (yyval.bval) = true; } -#line 2002 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2000 "bison_parser.cpp" /* yacc.c:1646 */ break; case 26: -#line 334 "bison_parser.y" /* yacc.c:1646 */ +#line 332 "bison_parser.y" /* yacc.c:1646 */ { (yyval.bval) = false; } -#line 2008 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2006 "bison_parser.cpp" /* yacc.c:1646 */ break; case 27: -#line 338 "bison_parser.y" /* yacc.c:1646 */ +#line 336 "bison_parser.y" /* yacc.c:1646 */ { (yyval.column_vec) = new std::vector(); (yyval.column_vec)->push_back((yyvsp[0].column_t)); } -#line 2014 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2012 "bison_parser.cpp" /* yacc.c:1646 */ break; case 28: -#line 339 "bison_parser.y" /* yacc.c:1646 */ +#line 337 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].column_vec)->push_back((yyvsp[0].column_t)); (yyval.column_vec) = (yyvsp[-2].column_vec); } -#line 2020 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2018 "bison_parser.cpp" /* yacc.c:1646 */ break; case 29: -#line 343 "bison_parser.y" /* yacc.c:1646 */ +#line 341 "bison_parser.y" /* yacc.c:1646 */ { (yyval.column_t) = new ColumnDefinition((yyvsp[-1].sval), (ColumnDefinition::DataType) (yyvsp[0].uval)); } -#line 2028 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2026 "bison_parser.cpp" /* yacc.c:1646 */ break; case 30: -#line 350 "bison_parser.y" /* yacc.c:1646 */ +#line 348 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = ColumnDefinition::INT; } -#line 2034 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2032 "bison_parser.cpp" /* yacc.c:1646 */ break; case 31: -#line 351 "bison_parser.y" /* yacc.c:1646 */ +#line 349 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = ColumnDefinition::INT; } -#line 2040 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2038 "bison_parser.cpp" /* yacc.c:1646 */ break; case 32: -#line 352 "bison_parser.y" /* yacc.c:1646 */ +#line 350 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = ColumnDefinition::DOUBLE; } -#line 2046 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2044 "bison_parser.cpp" /* yacc.c:1646 */ break; case 33: -#line 353 "bison_parser.y" /* yacc.c:1646 */ +#line 351 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = ColumnDefinition::TEXT; } -#line 2052 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2050 "bison_parser.cpp" /* yacc.c:1646 */ break; case 34: -#line 363 "bison_parser.y" /* yacc.c:1646 */ +#line 361 "bison_parser.y" /* yacc.c:1646 */ { (yyval.drop_stmt) = new DropStatement(DropStatement::kTable); (yyval.drop_stmt)->name = (yyvsp[0].sval); } -#line 2061 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2059 "bison_parser.cpp" /* yacc.c:1646 */ break; case 35: -#line 367 "bison_parser.y" /* yacc.c:1646 */ +#line 365 "bison_parser.y" /* yacc.c:1646 */ { (yyval.drop_stmt) = new DropStatement(DropStatement::kPreparedStatement); (yyval.drop_stmt)->name = (yyvsp[0].sval); } -#line 2070 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2068 "bison_parser.cpp" /* yacc.c:1646 */ break; case 36: -#line 379 "bison_parser.y" /* yacc.c:1646 */ +#line 377 "bison_parser.y" /* yacc.c:1646 */ { (yyval.delete_stmt) = new DeleteStatement(); (yyval.delete_stmt)->tableName = (yyvsp[-1].sval); (yyval.delete_stmt)->expr = (yyvsp[0].expr); } -#line 2080 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2078 "bison_parser.cpp" /* yacc.c:1646 */ break; case 37: -#line 387 "bison_parser.y" /* yacc.c:1646 */ +#line 385 "bison_parser.y" /* yacc.c:1646 */ { (yyval.delete_stmt) = new DeleteStatement(); (yyval.delete_stmt)->tableName = (yyvsp[0].sval); } -#line 2089 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2087 "bison_parser.cpp" /* yacc.c:1646 */ break; case 38: -#line 399 "bison_parser.y" /* yacc.c:1646 */ +#line 397 "bison_parser.y" /* yacc.c:1646 */ { (yyval.insert_stmt) = new InsertStatement(InsertStatement::kInsertValues); (yyval.insert_stmt)->tableName = (yyvsp[-5].sval); (yyval.insert_stmt)->columns = (yyvsp[-4].str_vec); (yyval.insert_stmt)->values = (yyvsp[-1].expr_vec); } -#line 2100 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2098 "bison_parser.cpp" /* yacc.c:1646 */ break; case 39: -#line 405 "bison_parser.y" /* yacc.c:1646 */ +#line 403 "bison_parser.y" /* yacc.c:1646 */ { (yyval.insert_stmt) = new InsertStatement(InsertStatement::kInsertSelect); (yyval.insert_stmt)->tableName = (yyvsp[-2].sval); (yyval.insert_stmt)->columns = (yyvsp[-1].str_vec); (yyval.insert_stmt)->select = (yyvsp[0].select_stmt); } -#line 2111 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2109 "bison_parser.cpp" /* yacc.c:1646 */ break; case 40: -#line 415 "bison_parser.y" /* yacc.c:1646 */ +#line 413 "bison_parser.y" /* yacc.c:1646 */ { (yyval.str_vec) = (yyvsp[-1].str_vec); } -#line 2117 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2115 "bison_parser.cpp" /* yacc.c:1646 */ break; case 41: -#line 416 "bison_parser.y" /* yacc.c:1646 */ +#line 414 "bison_parser.y" /* yacc.c:1646 */ { (yyval.str_vec) = NULL; } -#line 2123 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2121 "bison_parser.cpp" /* yacc.c:1646 */ break; case 42: -#line 426 "bison_parser.y" /* yacc.c:1646 */ +#line 424 "bison_parser.y" /* yacc.c:1646 */ { (yyval.update_stmt) = new UpdateStatement(); (yyval.update_stmt)->table = (yyvsp[-3].table); (yyval.update_stmt)->updates = (yyvsp[-1].update_vec); (yyval.update_stmt)->where = (yyvsp[0].expr); } -#line 2134 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2132 "bison_parser.cpp" /* yacc.c:1646 */ break; case 43: -#line 435 "bison_parser.y" /* yacc.c:1646 */ +#line 433 "bison_parser.y" /* yacc.c:1646 */ { (yyval.update_vec) = new std::vector(); (yyval.update_vec)->push_back((yyvsp[0].update_t)); } -#line 2140 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2138 "bison_parser.cpp" /* yacc.c:1646 */ break; case 44: -#line 436 "bison_parser.y" /* yacc.c:1646 */ +#line 434 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].update_vec)->push_back((yyvsp[0].update_t)); (yyval.update_vec) = (yyvsp[-2].update_vec); } -#line 2146 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2144 "bison_parser.cpp" /* yacc.c:1646 */ break; case 45: -#line 440 "bison_parser.y" /* yacc.c:1646 */ +#line 438 "bison_parser.y" /* yacc.c:1646 */ { (yyval.update_t) = new UpdateClause(); (yyval.update_t)->column = (yyvsp[-2].sval); (yyval.update_t)->value = (yyvsp[0].expr); } -#line 2156 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2154 "bison_parser.cpp" /* yacc.c:1646 */ break; case 48: -#line 457 "bison_parser.y" /* yacc.c:1646 */ +#line 455 "bison_parser.y" /* yacc.c:1646 */ { (yyval.select_stmt) = (yyvsp[-1].select_stmt); } -#line 2162 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2160 "bison_parser.cpp" /* yacc.c:1646 */ break; case 49: -#line 458 "bison_parser.y" /* yacc.c:1646 */ +#line 456 "bison_parser.y" /* yacc.c:1646 */ { (yyval.select_stmt) = (yyvsp[-1].select_stmt); } -#line 2168 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2166 "bison_parser.cpp" /* yacc.c:1646 */ break; case 50: -#line 462 "bison_parser.y" /* yacc.c:1646 */ +#line 460 "bison_parser.y" /* yacc.c:1646 */ { (yyval.select_stmt) = (yyvsp[-2].select_stmt); (yyval.select_stmt)->order = (yyvsp[-1].order); (yyval.select_stmt)->limit = (yyvsp[0].limit); } -#line 2178 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2176 "bison_parser.cpp" /* yacc.c:1646 */ break; case 51: -#line 467 "bison_parser.y" /* yacc.c:1646 */ +#line 465 "bison_parser.y" /* yacc.c:1646 */ { // TODO: allow multiple unions (through linked list) // TODO: capture type of set_operator @@ -2188,11 +2186,11 @@ yyreduce: (yyval.select_stmt)->order = (yyvsp[-1].order); (yyval.select_stmt)->limit = (yyvsp[0].limit); } -#line 2192 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2190 "bison_parser.cpp" /* yacc.c:1646 */ break; case 55: -#line 485 "bison_parser.y" /* yacc.c:1646 */ +#line 483 "bison_parser.y" /* yacc.c:1646 */ { (yyval.select_stmt) = new SelectStatement(); (yyval.select_stmt)->selectDistinct = (yyvsp[-4].bval); @@ -2201,381 +2199,381 @@ yyreduce: (yyval.select_stmt)->whereClause = (yyvsp[-1].expr); (yyval.select_stmt)->groupBy = (yyvsp[0].group_t); } -#line 2205 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2203 "bison_parser.cpp" /* yacc.c:1646 */ break; case 56: -#line 496 "bison_parser.y" /* yacc.c:1646 */ +#line 494 "bison_parser.y" /* yacc.c:1646 */ { (yyval.bval) = true; } -#line 2211 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2209 "bison_parser.cpp" /* yacc.c:1646 */ break; case 57: -#line 497 "bison_parser.y" /* yacc.c:1646 */ +#line 495 "bison_parser.y" /* yacc.c:1646 */ { (yyval.bval) = false; } -#line 2217 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2215 "bison_parser.cpp" /* yacc.c:1646 */ break; case 59: -#line 506 "bison_parser.y" /* yacc.c:1646 */ +#line 504 "bison_parser.y" /* yacc.c:1646 */ { (yyval.table) = (yyvsp[0].table); } -#line 2223 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2221 "bison_parser.cpp" /* yacc.c:1646 */ break; case 60: -#line 511 "bison_parser.y" /* yacc.c:1646 */ +#line 509 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = (yyvsp[0].expr); } -#line 2229 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2227 "bison_parser.cpp" /* yacc.c:1646 */ break; case 61: -#line 512 "bison_parser.y" /* yacc.c:1646 */ +#line 510 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = NULL; } -#line 2235 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2233 "bison_parser.cpp" /* yacc.c:1646 */ break; case 62: -#line 516 "bison_parser.y" /* yacc.c:1646 */ +#line 514 "bison_parser.y" /* yacc.c:1646 */ { (yyval.group_t) = new GroupByDescription(); (yyval.group_t)->columns = (yyvsp[-1].expr_vec); (yyval.group_t)->having = (yyvsp[0].expr); } -#line 2245 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2243 "bison_parser.cpp" /* yacc.c:1646 */ break; case 63: -#line 521 "bison_parser.y" /* yacc.c:1646 */ +#line 519 "bison_parser.y" /* yacc.c:1646 */ { (yyval.group_t) = NULL; } -#line 2251 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2249 "bison_parser.cpp" /* yacc.c:1646 */ break; case 64: -#line 525 "bison_parser.y" /* yacc.c:1646 */ +#line 523 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = (yyvsp[0].expr); } -#line 2257 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2255 "bison_parser.cpp" /* yacc.c:1646 */ break; case 65: -#line 526 "bison_parser.y" /* yacc.c:1646 */ +#line 524 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = NULL; } -#line 2263 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2261 "bison_parser.cpp" /* yacc.c:1646 */ break; case 66: -#line 529 "bison_parser.y" /* yacc.c:1646 */ +#line 527 "bison_parser.y" /* yacc.c:1646 */ { (yyval.order) = new OrderDescription((yyvsp[0].order_type), (yyvsp[-1].expr)); } -#line 2269 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2267 "bison_parser.cpp" /* yacc.c:1646 */ break; case 67: -#line 530 "bison_parser.y" /* yacc.c:1646 */ +#line 528 "bison_parser.y" /* yacc.c:1646 */ { (yyval.order) = NULL; } -#line 2275 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2273 "bison_parser.cpp" /* yacc.c:1646 */ break; case 68: -#line 534 "bison_parser.y" /* yacc.c:1646 */ +#line 532 "bison_parser.y" /* yacc.c:1646 */ { (yyval.order_type) = kOrderAsc; } -#line 2281 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2279 "bison_parser.cpp" /* yacc.c:1646 */ break; case 69: -#line 535 "bison_parser.y" /* yacc.c:1646 */ +#line 533 "bison_parser.y" /* yacc.c:1646 */ { (yyval.order_type) = kOrderDesc; } -#line 2287 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2285 "bison_parser.cpp" /* yacc.c:1646 */ break; case 70: -#line 536 "bison_parser.y" /* yacc.c:1646 */ +#line 534 "bison_parser.y" /* yacc.c:1646 */ { (yyval.order_type) = kOrderAsc; } -#line 2293 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2291 "bison_parser.cpp" /* yacc.c:1646 */ break; case 71: -#line 541 "bison_parser.y" /* yacc.c:1646 */ +#line 539 "bison_parser.y" /* yacc.c:1646 */ { (yyval.limit) = new LimitDescription((yyvsp[0].expr)->ival, kNoOffset); delete (yyvsp[0].expr); } -#line 2299 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2297 "bison_parser.cpp" /* yacc.c:1646 */ break; case 72: -#line 542 "bison_parser.y" /* yacc.c:1646 */ +#line 540 "bison_parser.y" /* yacc.c:1646 */ { (yyval.limit) = new LimitDescription((yyvsp[-2].expr)->ival, (yyvsp[0].expr)->ival); delete (yyvsp[-2].expr); delete (yyvsp[0].expr); } -#line 2305 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2303 "bison_parser.cpp" /* yacc.c:1646 */ break; case 73: -#line 543 "bison_parser.y" /* yacc.c:1646 */ +#line 541 "bison_parser.y" /* yacc.c:1646 */ { (yyval.limit) = NULL; } -#line 2311 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2309 "bison_parser.cpp" /* yacc.c:1646 */ break; case 74: -#line 550 "bison_parser.y" /* yacc.c:1646 */ +#line 548 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr_vec) = new std::vector(); (yyval.expr_vec)->push_back((yyvsp[0].expr)); } -#line 2317 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2315 "bison_parser.cpp" /* yacc.c:1646 */ break; case 75: -#line 551 "bison_parser.y" /* yacc.c:1646 */ +#line 549 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].expr_vec)->push_back((yyvsp[0].expr)); (yyval.expr_vec) = (yyvsp[-2].expr_vec); } -#line 2323 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2321 "bison_parser.cpp" /* yacc.c:1646 */ break; case 76: -#line 555 "bison_parser.y" /* yacc.c:1646 */ +#line 553 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr_vec) = new std::vector(); (yyval.expr_vec)->push_back((yyvsp[0].expr)); } -#line 2329 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2327 "bison_parser.cpp" /* yacc.c:1646 */ break; case 77: -#line 556 "bison_parser.y" /* yacc.c:1646 */ +#line 554 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].expr_vec)->push_back((yyvsp[0].expr)); (yyval.expr_vec) = (yyvsp[-2].expr_vec); } -#line 2335 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2333 "bison_parser.cpp" /* yacc.c:1646 */ break; case 78: -#line 560 "bison_parser.y" /* yacc.c:1646 */ +#line 558 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = (yyvsp[-1].expr); (yyval.expr)->alias = (yyvsp[0].sval); } -#line 2344 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2342 "bison_parser.cpp" /* yacc.c:1646 */ break; case 79: -#line 567 "bison_parser.y" /* yacc.c:1646 */ +#line 565 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = (yyvsp[-1].expr); } -#line 2350 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2348 "bison_parser.cpp" /* yacc.c:1646 */ break; case 87: -#line 581 "bison_parser.y" /* yacc.c:1646 */ +#line 579 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpUnary(Expr::UMINUS, (yyvsp[0].expr)); } -#line 2356 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2354 "bison_parser.cpp" /* yacc.c:1646 */ break; case 88: -#line 582 "bison_parser.y" /* yacc.c:1646 */ +#line 580 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpUnary(Expr::NOT, (yyvsp[0].expr)); } -#line 2362 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2360 "bison_parser.cpp" /* yacc.c:1646 */ break; case 90: -#line 587 "bison_parser.y" /* yacc.c:1646 */ +#line 585 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '-', (yyvsp[0].expr)); } -#line 2368 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2366 "bison_parser.cpp" /* yacc.c:1646 */ break; case 91: -#line 588 "bison_parser.y" /* yacc.c:1646 */ +#line 586 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '+', (yyvsp[0].expr)); } -#line 2374 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2372 "bison_parser.cpp" /* yacc.c:1646 */ break; case 92: -#line 589 "bison_parser.y" /* yacc.c:1646 */ +#line 587 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '/', (yyvsp[0].expr)); } -#line 2380 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2378 "bison_parser.cpp" /* yacc.c:1646 */ break; case 93: -#line 590 "bison_parser.y" /* yacc.c:1646 */ +#line 588 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '*', (yyvsp[0].expr)); } -#line 2386 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2384 "bison_parser.cpp" /* yacc.c:1646 */ break; case 94: -#line 591 "bison_parser.y" /* yacc.c:1646 */ +#line 589 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '%', (yyvsp[0].expr)); } -#line 2392 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2390 "bison_parser.cpp" /* yacc.c:1646 */ break; case 95: -#line 592 "bison_parser.y" /* yacc.c:1646 */ +#line 590 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '^', (yyvsp[0].expr)); } -#line 2398 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2396 "bison_parser.cpp" /* yacc.c:1646 */ break; case 96: -#line 593 "bison_parser.y" /* yacc.c:1646 */ +#line 591 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), Expr::AND, (yyvsp[0].expr)); } -#line 2404 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2402 "bison_parser.cpp" /* yacc.c:1646 */ break; case 97: -#line 594 "bison_parser.y" /* yacc.c:1646 */ +#line 592 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), Expr::OR, (yyvsp[0].expr)); } -#line 2410 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2408 "bison_parser.cpp" /* yacc.c:1646 */ break; case 98: -#line 595 "bison_parser.y" /* yacc.c:1646 */ +#line 593 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), Expr::LIKE, (yyvsp[0].expr)); } -#line 2416 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2414 "bison_parser.cpp" /* yacc.c:1646 */ break; case 99: -#line 596 "bison_parser.y" /* yacc.c:1646 */ +#line 594 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-3].expr), Expr::NOT_LIKE, (yyvsp[0].expr)); } -#line 2422 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2420 "bison_parser.cpp" /* yacc.c:1646 */ break; case 100: -#line 601 "bison_parser.y" /* yacc.c:1646 */ +#line 599 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '=', (yyvsp[0].expr)); } -#line 2428 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2426 "bison_parser.cpp" /* yacc.c:1646 */ break; case 101: -#line 602 "bison_parser.y" /* yacc.c:1646 */ +#line 600 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), Expr::NOT_EQUALS, (yyvsp[0].expr)); } -#line 2434 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2432 "bison_parser.cpp" /* yacc.c:1646 */ break; case 102: -#line 603 "bison_parser.y" /* yacc.c:1646 */ +#line 601 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '<', (yyvsp[0].expr)); } -#line 2440 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2438 "bison_parser.cpp" /* yacc.c:1646 */ break; case 103: -#line 604 "bison_parser.y" /* yacc.c:1646 */ +#line 602 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), '>', (yyvsp[0].expr)); } -#line 2446 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2444 "bison_parser.cpp" /* yacc.c:1646 */ break; case 104: -#line 605 "bison_parser.y" /* yacc.c:1646 */ +#line 603 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), Expr::LESS_EQ, (yyvsp[0].expr)); } -#line 2452 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2450 "bison_parser.cpp" /* yacc.c:1646 */ break; case 105: -#line 606 "bison_parser.y" /* yacc.c:1646 */ +#line 604 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeOpBinary((yyvsp[-2].expr), Expr::GREATER_EQ, (yyvsp[0].expr)); } -#line 2458 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2456 "bison_parser.cpp" /* yacc.c:1646 */ break; case 106: -#line 610 "bison_parser.y" /* yacc.c:1646 */ +#line 608 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeFunctionRef((yyvsp[-4].sval), (yyvsp[-1].expr), (yyvsp[-2].bval)); } -#line 2464 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2462 "bison_parser.cpp" /* yacc.c:1646 */ break; case 107: -#line 614 "bison_parser.y" /* yacc.c:1646 */ +#line 612 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeColumnRef((yyvsp[0].sval)); } -#line 2470 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2468 "bison_parser.cpp" /* yacc.c:1646 */ break; case 108: -#line 615 "bison_parser.y" /* yacc.c:1646 */ +#line 613 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeColumnRef((yyvsp[-2].sval), (yyvsp[0].sval)); } -#line 2476 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2474 "bison_parser.cpp" /* yacc.c:1646 */ break; case 112: -#line 625 "bison_parser.y" /* yacc.c:1646 */ +#line 623 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeLiteral((yyvsp[0].sval)); } -#line 2482 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2480 "bison_parser.cpp" /* yacc.c:1646 */ break; case 113: -#line 630 "bison_parser.y" /* yacc.c:1646 */ +#line 628 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeLiteral((yyvsp[0].fval)); } -#line 2488 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2486 "bison_parser.cpp" /* yacc.c:1646 */ break; case 115: -#line 635 "bison_parser.y" /* yacc.c:1646 */ +#line 633 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makeLiteral((yyvsp[0].ival)); } -#line 2494 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2492 "bison_parser.cpp" /* yacc.c:1646 */ break; case 116: -#line 639 "bison_parser.y" /* yacc.c:1646 */ +#line 637 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = new Expr(kExprStar); } -#line 2500 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2498 "bison_parser.cpp" /* yacc.c:1646 */ break; case 117: -#line 644 "bison_parser.y" /* yacc.c:1646 */ +#line 642 "bison_parser.y" /* yacc.c:1646 */ { (yyval.expr) = Expr::makePlaceholder(yylloc.total_column); yyloc.placeholder_list.push_back((yyval.expr)); } -#line 2509 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2507 "bison_parser.cpp" /* yacc.c:1646 */ break; case 119: -#line 656 "bison_parser.y" /* yacc.c:1646 */ +#line 654 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[0].table_vec)->push_back((yyvsp[-2].table)); auto tbl = new TableRef(kTableCrossProduct); tbl->list = (yyvsp[0].table_vec); (yyval.table) = tbl; } -#line 2520 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2518 "bison_parser.cpp" /* yacc.c:1646 */ break; case 121: -#line 667 "bison_parser.y" /* yacc.c:1646 */ +#line 665 "bison_parser.y" /* yacc.c:1646 */ { auto tbl = new TableRef(kTableSelect); tbl->select = (yyvsp[-2].select_stmt); tbl->alias = (yyvsp[0].sval); (yyval.table) = tbl; } -#line 2531 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2529 "bison_parser.cpp" /* yacc.c:1646 */ break; case 123: -#line 678 "bison_parser.y" /* yacc.c:1646 */ +#line 676 "bison_parser.y" /* yacc.c:1646 */ { (yyval.table_vec) = new std::vector(); (yyval.table_vec)->push_back((yyvsp[0].table)); } -#line 2537 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2535 "bison_parser.cpp" /* yacc.c:1646 */ break; case 124: -#line 679 "bison_parser.y" /* yacc.c:1646 */ +#line 677 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].table_vec)->push_back((yyvsp[0].table)); (yyval.table_vec) = (yyvsp[-2].table_vec); } -#line 2543 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2541 "bison_parser.cpp" /* yacc.c:1646 */ break; case 125: -#line 684 "bison_parser.y" /* yacc.c:1646 */ +#line 682 "bison_parser.y" /* yacc.c:1646 */ { auto tbl = new TableRef(kTableName); tbl->name = (yyvsp[-1].sval); tbl->alias = (yyvsp[0].sval); (yyval.table) = tbl; } -#line 2554 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2552 "bison_parser.cpp" /* yacc.c:1646 */ break; case 126: -#line 694 "bison_parser.y" /* yacc.c:1646 */ +#line 692 "bison_parser.y" /* yacc.c:1646 */ { (yyval.table) = new TableRef(kTableName); (yyval.table)->name = (yyvsp[0].sval); } -#line 2563 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2561 "bison_parser.cpp" /* yacc.c:1646 */ break; case 129: -#line 708 "bison_parser.y" /* yacc.c:1646 */ +#line 706 "bison_parser.y" /* yacc.c:1646 */ { (yyval.sval) = (yyvsp[0].sval); } -#line 2569 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2567 "bison_parser.cpp" /* yacc.c:1646 */ break; case 132: -#line 714 "bison_parser.y" /* yacc.c:1646 */ +#line 712 "bison_parser.y" /* yacc.c:1646 */ { (yyval.sval) = NULL; } -#line 2575 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2573 "bison_parser.cpp" /* yacc.c:1646 */ break; case 133: -#line 723 "bison_parser.y" /* yacc.c:1646 */ +#line 721 "bison_parser.y" /* yacc.c:1646 */ { (yyval.table) = new TableRef(kTableJoin); (yyval.table)->join = new JoinDefinition(); @@ -2584,64 +2582,64 @@ yyreduce: (yyval.table)->join->right = (yyvsp[-2].table); (yyval.table)->join->condition = (yyvsp[0].expr); } -#line 2588 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2586 "bison_parser.cpp" /* yacc.c:1646 */ break; case 134: -#line 734 "bison_parser.y" /* yacc.c:1646 */ +#line 732 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = kJoinInner; } -#line 2594 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2592 "bison_parser.cpp" /* yacc.c:1646 */ break; case 135: -#line 735 "bison_parser.y" /* yacc.c:1646 */ +#line 733 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = kJoinOuter; } -#line 2600 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2598 "bison_parser.cpp" /* yacc.c:1646 */ break; case 136: -#line 736 "bison_parser.y" /* yacc.c:1646 */ +#line 734 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = kJoinLeft; } -#line 2606 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2604 "bison_parser.cpp" /* yacc.c:1646 */ break; case 137: -#line 737 "bison_parser.y" /* yacc.c:1646 */ +#line 735 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = kJoinRight; } -#line 2612 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2610 "bison_parser.cpp" /* yacc.c:1646 */ break; case 138: -#line 738 "bison_parser.y" /* yacc.c:1646 */ +#line 736 "bison_parser.y" /* yacc.c:1646 */ { (yyval.uval) = kJoinInner; } -#line 2618 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2616 "bison_parser.cpp" /* yacc.c:1646 */ break; case 139: -#line 744 "bison_parser.y" /* yacc.c:1646 */ +#line 742 "bison_parser.y" /* yacc.c:1646 */ { auto tbl = new TableRef(kTableSelect); tbl->select = (yyvsp[-2].select_stmt); tbl->alias = (yyvsp[0].sval); (yyval.table) = tbl; } -#line 2629 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2627 "bison_parser.cpp" /* yacc.c:1646 */ break; case 144: -#line 769 "bison_parser.y" /* yacc.c:1646 */ +#line 767 "bison_parser.y" /* yacc.c:1646 */ { (yyval.str_vec) = new std::vector(); (yyval.str_vec)->push_back((yyvsp[0].sval)); } -#line 2635 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2633 "bison_parser.cpp" /* yacc.c:1646 */ break; case 145: -#line 770 "bison_parser.y" /* yacc.c:1646 */ +#line 768 "bison_parser.y" /* yacc.c:1646 */ { (yyvsp[-2].str_vec)->push_back((yyvsp[0].sval)); (yyval.str_vec) = (yyvsp[-2].str_vec); } -#line 2641 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2639 "bison_parser.cpp" /* yacc.c:1646 */ break; -#line 2645 "bison_parser.cpp" /* yacc.c:1646 */ +#line 2643 "bison_parser.cpp" /* yacc.c:1646 */ default: break; } /* User semantic actions sometimes alter yychar, and that requires @@ -2876,7 +2874,7 @@ yyreturn: #endif return yyresult; } -#line 773 "bison_parser.y" /* yacc.c:1906 */ +#line 771 "bison_parser.y" /* yacc.c:1906 */ /********************************* ** Section 4: Additional C code diff --git a/src/parser/bison_parser.h b/src/parser/bison_parser.h index 8da43cb..bc6a781 100644 --- a/src/parser/bison_parser.h +++ b/src/parser/bison_parser.h @@ -48,7 +48,7 @@ extern int hsql_debug; #endif /* "%code requires" blocks. */ -#line 42 "bison_parser.y" /* yacc.c:1909 */ +#line 40 "bison_parser.y" /* yacc.c:1909 */ // %code requires block @@ -209,7 +209,7 @@ extern int hsql_debug; typedef union HSQL_STYPE HSQL_STYPE; union HSQL_STYPE { -#line 101 "bison_parser.y" /* yacc.c:1909 */ +#line 99 "bison_parser.y" /* yacc.c:1909 */ double fval; int64_t ival; diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 3c2dc6b..d15ce12 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -21,10 +21,8 @@ using namespace hsql; int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const char *msg) { SQLParserResult* list = new SQLParserResult(); - list->isValid = false; - list->errorMsg = strdup(msg); - list->errorLine = llocp->first_line; - list->errorColumn = llocp->first_column; + list->setIsValid(false); + list->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column); *result = list; return 0; diff --git a/src/sql/Expr.cpp b/src/sql/Expr.cpp index fd3ee46..07bb1b2 100644 --- a/src/sql/Expr.cpp +++ b/src/sql/Expr.cpp @@ -22,7 +22,7 @@ namespace hsql { Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { Expr* e = new Expr(kExprOperator); - e->op_type = op; + e->opType = op; e->expr = expr; e->expr2 = NULL; return e; @@ -30,8 +30,8 @@ namespace hsql { Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { Expr* e = new Expr(kExprOperator); - e->op_type = op; - e->op_char = 0; + e->opType = op; + e->opChar = 0; e->expr = expr1; e->expr2 = expr2; return e; @@ -39,8 +39,8 @@ namespace hsql { Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) { Expr* e = new Expr(kExprOperator); - e->op_type = SIMPLE_OP; - e->op_char = op; + e->opType = SIMPLE_OP; + e->opChar = op; e->expr = expr1; e->expr2 = expr2; return e; @@ -114,11 +114,11 @@ namespace hsql { } bool Expr::isSimpleOp() { - return op_type == SIMPLE_OP; + return opType == SIMPLE_OP; } bool Expr::isSimpleOp(char op) { - return isSimpleOp() && op_char == op; + return isSimpleOp() && opChar == op; } char* substr(const char* source, int from, int to) { diff --git a/src/sql/Expr.h b/src/sql/Expr.h index 74d475e..9be9ed1 100644 --- a/src/sql/Expr.h +++ b/src/sql/Expr.h @@ -68,8 +68,8 @@ namespace hsql { int64_t ival; int64_t ival2; - OperatorType op_type; - char op_char; + OperatorType opType; + char opChar; bool distinct; // Convenience accessor methods. diff --git a/src/sql/SQLStatement.h b/src/sql/SQLStatement.h index 9aa9434..4c87cb8 100644 --- a/src/sql/SQLStatement.h +++ b/src/sql/SQLStatement.h @@ -32,7 +32,7 @@ namespace hsql { virtual StatementType type() const; private: - StatementType _type; + StatementType type_; }; } // namespace hsql diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 36467b8..4eae74c 100644 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -5,12 +5,12 @@ namespace hsql { // SQLStatement SQLStatement::SQLStatement(StatementType type) : - _type(type) {}; + type_(type) {}; SQLStatement::~SQLStatement() {} StatementType SQLStatement::type() const { - return _type; + return type_; } // ColumnDefinition diff --git a/src/sqlhelper.cpp b/src/sqlhelper.cpp index 7a67c3d..1361a42 100644 --- a/src/sqlhelper.cpp +++ b/src/sqlhelper.cpp @@ -62,9 +62,9 @@ namespace hsql { return; } - switch (expr->op_type) { + switch (expr->opType) { case Expr::SIMPLE_OP: - inprintC(expr->op_char, numIndent); + inprintC(expr->opChar, numIndent); break; case Expr::AND: inprint("AND", numIndent); @@ -76,7 +76,7 @@ namespace hsql { inprint("NOT", numIndent); break; default: - inprintU(expr->op_type, numIndent); + inprintU(expr->opType, numIndent); break; } printExpression(expr->expr, numIndent + 1);