change indent to spaces=2 and indent access modifiers. minor style changes

This commit is contained in:
Pedro 2017-02-08 02:06:15 +01:00
parent 8582c7f901
commit 28214e8043
24 changed files with 953 additions and 951 deletions

View File

@ -1,8 +1,10 @@
--style=google
# indentation
--indent=spaces=4
--indent=spaces=2
--indent-namespaces
--style=java
--style=attach
-A2
--align-reference=type
--align-pointer=type
--pad-oper

View File

@ -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

View File

@ -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

View File

@ -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<SQLStatement*>::iterator it = statements.begin(); it != statements.end(); ++it) {
delete *it;
}
delete errorMsg;
SQLParserResult::~SQLParserResult() {
for (std::vector<SQLStatement*>::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

View File

@ -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<SQLStatement*> statements;
bool isValid;
// public properties
std::vector<SQLStatement*> statements;
bool isValid;
const char* errorMsg;
int errorLine;
int errorColumn;
};
const char* errorMsg;
int errorLine;
int errorColumn;
};
} // namespace hsql

View File

@ -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<ColumnDefinition*>* 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<ColumnDefinition*>* columns; // default: NULL
};
} // namespace hsql
#endif

View File

@ -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

View File

@ -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

View File

@ -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<Expr*>* parameters;
};
const char* name;
std::vector<Expr*>* parameters;
};
} // namsepace hsql
#endif

View File

@ -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;
}
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;
}
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

View File

@ -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

View File

@ -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

View File

@ -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<char*>* columns;
std::vector<Expr*>* 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<char*>* columns;
std::vector<Expr*>* values;
SelectStatement* select;
};
} // namsepace hsql
#endif

View File

@ -7,26 +7,26 @@
#include <algorithm>
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<void*> 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<void*> ph);
const char* name;
SQLParserResult* query;
std::vector<Expr*> placeholders;
};
const char* name;
SQLParserResult* query;
std::vector<Expr*> placeholders;
};
} // namsepace hsql
#endif

View File

@ -5,35 +5,35 @@
#include <vector>
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__

View File

@ -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<Expr*>* columns;
Expr* having;
};
std::vector<Expr*>* 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<Expr*>* selectList;
Expr* whereClause;
GroupByDescription* groupBy;
TableRef* fromTable;
bool selectDistinct;
std::vector<Expr*>* selectList;
Expr* whereClause;
GroupByDescription* groupBy;
SelectStatement* unionSelect;
OrderDescription* order;
LimitDescription* limit;
};
SelectStatement* unionSelect;
OrderDescription* order;
LimitDescription* limit;
};
} // namespace hsql
#endif

View File

@ -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<TableRef*>* list;
JoinDefinition* join;
SelectStatement* select;
std::vector<TableRef*>* 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

View File

@ -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<UpdateClause*>* updates;
Expr* where;
};
// TODO: switch to char* instead of TableRef
TableRef* table;
std::vector<UpdateClause*>* updates;
Expr* where;
};
} // namsepace hsql
#endif

View File

@ -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<void*> ph) {
for (void* e : ph) {
if (e != NULL)
placeholders.push_back((Expr*) e);
}
// Sort by col-id
std::sort(placeholders.begin(), placeholders.end(), [](Expr * i, Expr * j) -> bool { return (i->ival < j->ival); });
// 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<void*> ph) {
for (void* e : ph) {
if (e != NULL)
placeholders.push_back((Expr*) e);
}
// Sort by col-id
std::sort(placeholders.begin(), placeholders.end(), [](Expr* i, Expr* j) -> bool { return (i->ival < j->ival); });
// Set the placeholder id on the Expr. This replaces the previously stored column id
for (uintmax_t i = 0; i < placeholders.size(); ++i) placeholders[i]->ival = i;
}
// SelectStatement.h
// OrderDescription
OrderDescription::OrderDescription(OrderType type, Expr* expr) :
type(type),
expr(expr) {}
OrderDescription::~OrderDescription() {
delete expr;
}
// LimitDescription
LimitDescription::LimitDescription(int64_t limit, int64_t offset) :
limit(limit),
offset(offset) {}
// GroypByDescription
GroupByDescription::GroupByDescription() :
columns(NULL),
having(NULL) {}
GroupByDescription::~GroupByDescription() {
delete columns;
delete having;
}
// SelectStatement
SelectStatement::SelectStatement() :
SQLStatement(kStmtSelect),
fromTable(NULL),
selectDistinct(false),
selectList(NULL),
whereClause(NULL),
groupBy(NULL),
unionSelect(NULL),
order(NULL),
limit(NULL) {};
SelectStatement::~SelectStatement() {
delete fromTable;
delete selectList;
delete whereClause;
delete groupBy;
delete order;
delete limit;
}
// UpdateStatement
UpdateStatement::UpdateStatement() :
SQLStatement(kStmtUpdate),
table(NULL),
updates(NULL),
where(NULL) {}
UpdateStatement::~UpdateStatement() {
delete table;
delete updates;
delete where;
}
// TableRef
TableRef::TableRef(TableRefType type) :
type(type),
schema(NULL),
name(NULL),
alias(NULL),
select(NULL),
list(NULL),
join(NULL) {}
TableRef::~TableRef() {
delete name;
delete alias;
delete select;
delete list;
}
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

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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<std::string>& testNames() {
static std::vector<std::string>* _testNames = new std::vector<std::string>;
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<std::string>& testNames() {
static std::vector<std::string>* _testNames = new std::vector<std::string>;
return *_testNames;
}
static std::vector<void (*)(void)>& tests() {
static std::vector<void (*)(void)>* tests = new std::vector<void (*)(void)>;
return *tests;
}
static std::vector<void (*)(void)>& tests() {
static std::vector<void (*)(void)>* tests = new std::vector<void (*)(void)>;
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;
}
}

View File

@ -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);