change var names to camelCase

This commit is contained in:
Pedro 2016-02-27 15:22:22 +01:00
parent 7d1c56d0aa
commit 4632abf92d
20 changed files with 237 additions and 303 deletions

View File

@ -60,8 +60,8 @@ format:
############
test: $(BIN)/sql_tests $(BIN)/sql_grammar_test
LD_LIBRARY_PATH=./ $(BIN)/sql_grammar_test -f "test/lib/valid_queries.sql"
LD_LIBRARY_PATH=./ $(BIN)/sql_tests
@LD_LIBRARY_PATH=./ $(BIN)/sql_grammar_test -f "test/lib/valid_queries.sql"
@LD_LIBRARY_PATH=./ $(BIN)/sql_tests
$(BIN)/sql_tests: library
@mkdir -p $(BIN)/

View File

@ -1955,8 +1955,8 @@ yyreduce:
#line 296 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.import_stmt) = new ImportStatement((ImportStatement::ImportType) (yyvsp[-4].uval));
(yyval.import_stmt)->file_path = (yyvsp[-2].sval);
(yyval.import_stmt)->table_name = (yyvsp[0].sval);
(yyval.import_stmt)->filePath = (yyvsp[-2].sval);
(yyval.import_stmt)->tableName = (yyvsp[0].sval);
}
#line 1962 "bison_parser.cpp" /* yacc.c:1646 */
break;
@ -1977,9 +1977,9 @@ yyreduce:
#line 318 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.create_stmt) = new CreateStatement(CreateStatement::kTableFromTbl);
(yyval.create_stmt)->if_not_exists = (yyvsp[-5].bval);
(yyval.create_stmt)->table_name = (yyvsp[-4].sval);
(yyval.create_stmt)->file_path = (yyvsp[0].sval);
(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 */
break;
@ -1988,8 +1988,8 @@ yyreduce:
#line 324 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.create_stmt) = new CreateStatement(CreateStatement::kTable);
(yyval.create_stmt)->if_not_exists = (yyvsp[-4].bval);
(yyval.create_stmt)->table_name = (yyvsp[-3].sval);
(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 */
@ -2073,7 +2073,7 @@ yyreduce:
#line 379 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.delete_stmt) = new DeleteStatement();
(yyval.delete_stmt)->table_name = (yyvsp[-1].sval);
(yyval.delete_stmt)->tableName = (yyvsp[-1].sval);
(yyval.delete_stmt)->expr = (yyvsp[0].expr);
}
#line 2080 "bison_parser.cpp" /* yacc.c:1646 */
@ -2083,7 +2083,7 @@ yyreduce:
#line 387 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.delete_stmt) = new DeleteStatement();
(yyval.delete_stmt)->table_name = (yyvsp[0].sval);
(yyval.delete_stmt)->tableName = (yyvsp[0].sval);
}
#line 2089 "bison_parser.cpp" /* yacc.c:1646 */
break;
@ -2092,7 +2092,7 @@ yyreduce:
#line 399 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.insert_stmt) = new InsertStatement(InsertStatement::kInsertValues);
(yyval.insert_stmt)->table_name = (yyvsp[-5].sval);
(yyval.insert_stmt)->tableName = (yyvsp[-5].sval);
(yyval.insert_stmt)->columns = (yyvsp[-4].str_vec);
(yyval.insert_stmt)->values = (yyvsp[-1].expr_vec);
}
@ -2103,7 +2103,7 @@ yyreduce:
#line 405 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.insert_stmt) = new InsertStatement(InsertStatement::kInsertSelect);
(yyval.insert_stmt)->table_name = (yyvsp[-2].sval);
(yyval.insert_stmt)->tableName = (yyvsp[-2].sval);
(yyval.insert_stmt)->columns = (yyvsp[-1].str_vec);
(yyval.insert_stmt)->select = (yyvsp[0].select_stmt);
}
@ -2184,7 +2184,7 @@ yyreduce:
// TODO: capture type of set_operator
// TODO: might overwrite order and limit of first select here
(yyval.select_stmt) = (yyvsp[-4].select_stmt);
(yyval.select_stmt)->union_select = (yyvsp[-2].select_stmt);
(yyval.select_stmt)->unionSelect = (yyvsp[-2].select_stmt);
(yyval.select_stmt)->order = (yyvsp[-1].order);
(yyval.select_stmt)->limit = (yyvsp[0].limit);
}
@ -2195,11 +2195,11 @@ yyreduce:
#line 485 "bison_parser.y" /* yacc.c:1646 */
{
(yyval.select_stmt) = new SelectStatement();
(yyval.select_stmt)->select_distinct = (yyvsp[-4].bval);
(yyval.select_stmt)->select_list = (yyvsp[-3].expr_vec);
(yyval.select_stmt)->from_table = (yyvsp[-2].table);
(yyval.select_stmt)->where_clause = (yyvsp[-1].expr);
(yyval.select_stmt)->group_by = (yyvsp[0].group_t);
(yyval.select_stmt)->selectDistinct = (yyvsp[-4].bval);
(yyval.select_stmt)->selectList = (yyvsp[-3].expr_vec);
(yyval.select_stmt)->fromTable = (yyvsp[-2].table);
(yyval.select_stmt)->whereClause = (yyvsp[-1].expr);
(yyval.select_stmt)->groupBy = (yyvsp[0].group_t);
}
#line 2205 "bison_parser.cpp" /* yacc.c:1646 */
break;

View File

@ -295,8 +295,8 @@ execute_statement:
import_statement:
IMPORT FROM import_file_type FILE file_path INTO table_name {
$$ = new ImportStatement((ImportStatement::ImportType) $3);
$$->file_path = $5;
$$->table_name = $7;
$$->filePath = $5;
$$->tableName = $7;
}
;
@ -317,14 +317,14 @@ file_path:
create_statement:
CREATE TABLE opt_not_exists table_name FROM TBL FILE file_path {
$$ = new CreateStatement(CreateStatement::kTableFromTbl);
$$->if_not_exists = $3;
$$->table_name = $4;
$$->file_path = $8;
$$->ifNotExists = $3;
$$->tableName = $4;
$$->filePath = $8;
}
| CREATE TABLE opt_not_exists table_name '(' column_def_commalist ')' {
$$ = new CreateStatement(CreateStatement::kTable);
$$->if_not_exists = $3;
$$->table_name = $4;
$$->ifNotExists = $3;
$$->tableName = $4;
$$->columns = $6;
}
;
@ -378,7 +378,7 @@ drop_statement:
delete_statement:
DELETE FROM table_name opt_where {
$$ = new DeleteStatement();
$$->table_name = $3;
$$->tableName = $3;
$$->expr = $4;
}
;
@ -386,7 +386,7 @@ delete_statement:
truncate_statement:
TRUNCATE table_name {
$$ = new DeleteStatement();
$$->table_name = $2;
$$->tableName = $2;
}
;
@ -398,13 +398,13 @@ truncate_statement:
insert_statement:
INSERT INTO table_name opt_column_list VALUES '(' literal_list ')' {
$$ = new InsertStatement(InsertStatement::kInsertValues);
$$->table_name = $3;
$$->tableName = $3;
$$->columns = $4;
$$->values = $7;
}
| INSERT INTO table_name opt_column_list select_no_paren {
$$ = new InsertStatement(InsertStatement::kInsertSelect);
$$->table_name = $3;
$$->tableName = $3;
$$->columns = $4;
$$->select = $5;
}
@ -469,7 +469,7 @@ select_no_paren:
// TODO: capture type of set_operator
// TODO: might overwrite order and limit of first select here
$$ = $1;
$$->union_select = $3;
$$->unionSelect = $3;
$$->order = $4;
$$->limit = $5;
}
@ -484,11 +484,11 @@ set_operator:
select_clause:
SELECT opt_distinct select_list from_clause opt_where opt_group {
$$ = new SelectStatement();
$$->select_distinct = $2;
$$->select_list = $3;
$$->from_table = $4;
$$->where_clause = $5;
$$->group_by = $6;
$$->selectDistinct = $2;
$$->selectList = $3;
$$->fromTable = $4;
$$->whereClause = $5;
$$->groupBy = $6;
}
;

View File

@ -4,10 +4,8 @@
#include "SQLStatement.h"
namespace hsql {
/**
* @struct ColumnDefinition
* @brief Represents definition of a table column
* Represents definition of a table column
*/
struct ColumnDefinition {
enum DataType {
@ -28,41 +26,37 @@ namespace hsql {
DataType type;
};
/**
* @struct CreateStatement
* @brief Represents "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
kTableFromTbl // Hyrise file format
};
CreateStatement(CreateType type) :
SQLStatement(kStmtCreate),
type(type),
if_not_exists(false),
columns(NULL),
file_path(NULL),
table_name(NULL) {};
ifNotExists(false),
filePath(NULL),
tableName(NULL),
columns(NULL) {};
virtual ~CreateStatement() {
delete columns;
delete file_path;
delete table_name;
delete filePath;
delete tableName;
}
CreateType type;
bool if_not_exists;
bool ifNotExists;
const char* filePath;
const char* tableName;
std::vector<ColumnDefinition*>* columns;
const char* file_path;
const char* table_name;
};
} // namespace hsql
#endif

View File

@ -4,31 +4,26 @@
#include "SQLStatement.h"
namespace hsql {
/**
* @struct DeleteStatement
* @brief Represents "DELETE FROM students WHERE grade > 3.0"
* Represents SQL Delete statements.
* Example: "DELETE FROM students WHERE grade > 3.0"
*
* If expr == NULL => delete all rows (truncate)
* Note: if (expr == NULL) => delete all rows (truncate)
*/
struct DeleteStatement : SQLStatement {
DeleteStatement() :
SQLStatement(kStmtDelete),
table_name(NULL),
tableName(NULL),
expr(NULL) {};
virtual ~DeleteStatement() {
delete table_name;
delete tableName;
delete expr;
}
char* table_name;
char* tableName;
Expr* expr;
};
} // namespace hsql
#endif

View File

@ -4,11 +4,9 @@
#include "SQLStatement.h"
namespace hsql {
/**
* @struct DropStatement
* @brief Represents "DROP TABLE"
* Represents SQL Delete statements.
* Example "DROP TABLE students;"
*/
struct DropStatement : SQLStatement {
enum EntityType {
@ -19,7 +17,6 @@ namespace hsql {
kPreparedStatement
};
DropStatement(EntityType type) :
SQLStatement(kStmtDrop),
type(type),
@ -29,14 +26,9 @@ namespace hsql {
delete name;
}
EntityType type;
const char* name;
};
} // namespace hsql
#endif

View File

@ -4,11 +4,9 @@
#include "SQLStatement.h"
namespace hsql {
/**
* @struct ExecuteStatement
* @brief Represents "EXECUTE ins_prep(100, "test", 2.3);"
* Represents SQL Execute statements.
* Example: "EXECUTE ins_prep(100, "test", 2.3);"
*/
struct ExecuteStatement : SQLStatement {
ExecuteStatement() :
@ -25,8 +23,5 @@ namespace hsql {
std::vector<Expr*>* parameters;
};
} // namsepace hsql
#endif

View File

@ -4,13 +4,8 @@
#include "SQLStatement.h"
namespace hsql {
/**
* @struct ImportStatement
* @brief Represents "IMPORT"
* Represents SQL Import statements.
*/
struct ImportStatement : SQLStatement {
enum ImportType {
@ -18,26 +13,22 @@ namespace hsql {
kImportTbl, // Hyrise file format
};
ImportStatement(ImportType type) :
SQLStatement(kStmtImport),
type(type),
file_path(NULL),
table_name(NULL) {};
filePath(NULL),
tableName(NULL) {};
virtual ~ImportStatement() {
delete file_path;
delete table_name;
delete filePath;
delete tableName;
}
ImportType type;
const char* file_path;
const char* table_name;
const char* filePath;
const char* tableName;
};
} // namespace hsql

View File

@ -5,11 +5,9 @@
#include "SelectStatement.h"
namespace hsql {
/**
* @struct InsertStatement
* @brief Represents "INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)"
* Represents SQL Insert statements.
* Example: "INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)"
*/
struct InsertStatement : SQLStatement {
enum InsertType {
@ -20,27 +18,24 @@ namespace hsql {
InsertStatement(InsertType type) :
SQLStatement(kStmtInsert),
type(type),
table_name(NULL),
tableName(NULL),
columns(NULL),
values(NULL),
select(NULL) {}
virtual ~InsertStatement() {
delete table_name;
delete tableName;
delete columns;
delete values;
delete select;
}
InsertType type;
const char* table_name;
const char* tableName;
std::vector<char*>* columns;
std::vector<Expr*>* values;
SelectStatement* select;
};
} // namsepace hsql
#endif

View File

@ -7,11 +7,9 @@
#include <algorithm>
namespace hsql {
/**
* @struct PrepareStatement
* @brief Represents "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?"
* Represents SQL Prepare statements.
* Example: "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?"
*/
struct PrepareStatement : SQLStatement {
PrepareStatement() :
@ -25,10 +23,10 @@ namespace hsql {
}
/**
* @param vector of placeholders that the parser found
*
* 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) {
for (void* e : ph) {
@ -47,8 +45,5 @@ namespace hsql {
std::vector<Expr*> placeholders;
};
} // namsepace hsql
#endif

View File

@ -5,7 +5,6 @@
#include <vector>
namespace hsql {
typedef enum {
kStmtError, // unused
kStmtSelect,
@ -22,7 +21,6 @@ namespace hsql {
kStmtAlter
} StatementType;
/**
* Base struct for every SQL statement
*/
@ -41,5 +39,4 @@ namespace hsql {
};
} // namespace hsql
#endif // __SQLSTATEMENT_H__

View File

@ -6,20 +6,15 @@
#include "Table.h"
namespace hsql {
/**
* @struct OrderDescription
* @brief Description of the order by clause within a select statement
*
* TODO: hold multiple expressions to be sorted by
*/
typedef enum {
kOrderAsc,
kOrderDesc
} OrderType;
/**
* 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) :
type(type),
@ -33,12 +28,12 @@ namespace hsql {
Expr* expr;
};
/**
* @struct LimitDescription
* @brief Description of the limit clause within a select statement
*/
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) :
limit(limit),
@ -49,7 +44,7 @@ namespace hsql {
};
/**
* @struct GroupByDescription
* Description of the group-by clause within a select statement
*/
struct GroupByDescription {
GroupByDescription() :
@ -66,43 +61,40 @@ namespace hsql {
};
/**
* @struct SelectStatement
* @brief Representation of a full select statement.
*
* Representation of a full SQL select statement.
* TODO: add union_order and union_limit
*/
struct SelectStatement : SQLStatement {
SelectStatement() :
SQLStatement(kStmtSelect),
from_table(NULL),
select_list(NULL),
where_clause(NULL),
group_by(NULL),
union_select(NULL),
fromTable(NULL),
selectDistinct(false),
selectList(NULL),
whereClause(NULL),
groupBy(NULL),
unionSelect(NULL),
order(NULL),
limit(NULL) {};
virtual ~SelectStatement() {
delete from_table;
delete select_list;
delete where_clause;
delete group_by;
delete fromTable;
delete selectList;
delete whereClause;
delete groupBy;
delete order;
delete limit;
}
TableRef* from_table;
bool select_distinct;
std::vector<Expr*>* select_list;
Expr* where_clause;
GroupByDescription* group_by;
TableRef* fromTable;
bool selectDistinct;
std::vector<Expr*>* selectList;
Expr* whereClause;
GroupByDescription* groupBy;
SelectStatement* union_select;
SelectStatement* unionSelect;
OrderDescription* order;
LimitDescription* limit;
};
} // namespace hsql
#endif

View File

@ -4,21 +4,16 @@
#include "SQLStatement.h"
namespace hsql {
/**
* @struct UpdateClause
* @brief Represents "column = value" expressions
* Represents "column = value" expressions
*/
struct UpdateClause {
char* column;
Expr* value;
};
/**
* @struct UpdateStatement
* @brief Represents "UPDATE"
* Represents SQL Update statements.
*/
struct UpdateStatement : SQLStatement {
UpdateStatement() :
@ -39,7 +34,5 @@ namespace hsql {
Expr* where;
};
} // namsepace hsql
#endif

View File

@ -5,183 +5,183 @@
namespace hsql {
void printOperatorExpression(Expr* expr, uint num_indent);
void printOperatorExpression(Expr* expr, uint numIndent);
std::string indent(uint num_indent) {
return std::string(num_indent, '\t');
std::string indent(uint numIndent) {
return std::string(numIndent, '\t');
}
void inprint(int64_t val, uint num_indent) {
printf("%s%ld \n", indent(num_indent).c_str(), val);
void inprint(int64_t val, uint numIndent) {
printf("%s%ld \n", indent(numIndent).c_str(), val);
}
void inprint(float val, uint num_indent) {
printf("%s%f\n", indent(num_indent).c_str(), val);
void inprint(float val, uint numIndent) {
printf("%s%f\n", indent(numIndent).c_str(), val);
}
void inprint(const char* val, uint num_indent) {
printf("%s%s\n", indent(num_indent).c_str(), val);
void inprint(const char* val, uint numIndent) {
printf("%s%s\n", indent(numIndent).c_str(), val);
}
void inprint(const char* val, const char* val2, uint num_indent) {
printf("%s%s->%s\n", indent(num_indent).c_str(), val, val2);
void inprint(const char* val, const char* val2, uint numIndent) {
printf("%s%s->%s\n", indent(numIndent).c_str(), val, val2);
}
void inprintC(char val, uint num_indent) {
printf("%s%c\n", indent(num_indent).c_str(), val);
void inprintC(char val, uint numIndent) {
printf("%s%c\n", indent(numIndent).c_str(), val);
}
void inprintU(uint64_t val, uint num_indent) {
printf("%s%lu\n", indent(num_indent).c_str(), val);
void inprintU(uint64_t val, uint numIndent) {
printf("%s%lu\n", indent(numIndent).c_str(), val);
}
void printTableRefInfo(TableRef* table, uint num_indent) {
void printTableRefInfo(TableRef* table, uint numIndent) {
switch (table->type) {
case kTableName:
inprint(table->name, num_indent);
inprint(table->name, numIndent);
break;
case kTableSelect:
printSelectStatementInfo(table->select, num_indent);
printSelectStatementInfo(table->select, numIndent);
break;
case kTableJoin:
inprint("Join Table", num_indent);
inprint("Left", num_indent+1);
printTableRefInfo(table->join->left, num_indent+2);
inprint("Right", num_indent+1);
printTableRefInfo(table->join->right, num_indent+2);
inprint("Join Condition", num_indent+1);
printExpression(table->join->condition, num_indent+2);
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, num_indent);
for (TableRef* tbl : *table->list) printTableRefInfo(tbl, numIndent);
break;
}
if (table->alias != NULL) {
inprint("Alias", num_indent+1);
inprint(table->alias, num_indent+2);
inprint("Alias", numIndent+1);
inprint(table->alias, numIndent+2);
}
}
void printOperatorExpression(Expr* expr, uint num_indent) {
void printOperatorExpression(Expr* expr, uint numIndent) {
if (expr == NULL) {
inprint("null", num_indent);
inprint("null", numIndent);
return;
}
switch (expr->op_type) {
case Expr::SIMPLE_OP:
inprintC(expr->op_char, num_indent);
inprintC(expr->op_char, numIndent);
break;
case Expr::AND:
inprint("AND", num_indent);
inprint("AND", numIndent);
break;
case Expr::OR:
inprint("OR", num_indent);
inprint("OR", numIndent);
break;
case Expr::NOT:
inprint("NOT", num_indent);
inprint("NOT", numIndent);
break;
default:
inprintU(expr->op_type, num_indent);
inprintU(expr->op_type, numIndent);
break;
}
printExpression(expr->expr, num_indent+1);
if (expr->expr2 != NULL) printExpression(expr->expr2, num_indent+1);
printExpression(expr->expr, numIndent+1);
if (expr->expr2 != NULL) printExpression(expr->expr2, numIndent+1);
}
void printExpression(Expr* expr, uint num_indent) {
void printExpression(Expr* expr, uint numIndent) {
switch (expr->type) {
case kExprStar:
inprint("*", num_indent);
inprint("*", numIndent);
break;
case kExprColumnRef:
inprint(expr->name, num_indent);
inprint(expr->name, numIndent);
break;
// case kExprTableColumnRef: inprint(expr->table, expr->name, num_indent); break;
// case kExprTableColumnRef: inprint(expr->table, expr->name, numIndent); break;
case kExprLiteralFloat:
inprint(expr->fval, num_indent);
inprint(expr->fval, numIndent);
break;
case kExprLiteralInt:
inprint(expr->ival, num_indent);
inprint(expr->ival, numIndent);
break;
case kExprLiteralString:
inprint(expr->name, num_indent);
inprint(expr->name, numIndent);
break;
case kExprFunctionRef:
inprint(expr->name, num_indent);
inprint(expr->expr->name, num_indent+1);
inprint(expr->name, numIndent);
inprint(expr->expr->name, numIndent+1);
break;
case kExprOperator:
printOperatorExpression(expr, num_indent);
printOperatorExpression(expr, numIndent);
break;
default:
fprintf(stderr, "Unrecognized expression type %d\n", expr->type);
return;
}
if (expr->alias != NULL) {
inprint("Alias", num_indent+1);
inprint(expr->alias, num_indent+2);
inprint("Alias", numIndent+1);
inprint(expr->alias, numIndent+2);
}
}
void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) {
inprint("SelectStatement", num_indent);
inprint("Fields:", num_indent+1);
for (Expr* expr : *stmt->select_list) printExpression(expr, num_indent+2);
void printSelectStatementInfo(SelectStatement* stmt, uint numIndent) {
inprint("SelectStatement", numIndent);
inprint("Fields:", numIndent+1);
for (Expr* expr : *stmt->selectList) printExpression(expr, numIndent+2);
inprint("Sources:", num_indent+1);
printTableRefInfo(stmt->from_table, num_indent+2);
inprint("Sources:", numIndent+1);
printTableRefInfo(stmt->fromTable, numIndent+2);
if (stmt->where_clause != NULL) {
inprint("Search Conditions:", num_indent+1);
printExpression(stmt->where_clause, num_indent+2);
if (stmt->whereClause != NULL) {
inprint("Search Conditions:", numIndent+1);
printExpression(stmt->whereClause, numIndent+2);
}
if (stmt->union_select != NULL) {
inprint("Union:", num_indent+1);
printSelectStatementInfo(stmt->union_select, num_indent+2);
if (stmt->unionSelect != NULL) {
inprint("Union:", numIndent+1);
printSelectStatementInfo(stmt->unionSelect, numIndent+2);
}
if (stmt->order != NULL) {
inprint("OrderBy:", num_indent+1);
printExpression(stmt->order->expr, num_indent+2);
if (stmt->order->type == kOrderAsc) inprint("ascending", num_indent+2);
else inprint("descending", num_indent+2);
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:", num_indent+1);
inprint(stmt->limit->limit, num_indent+2);
inprint("Limit:", numIndent+1);
inprint(stmt->limit->limit, numIndent+2);
}
}
void printImportStatementInfo(ImportStatement* stmt, uint num_indent) {
inprint("ImportStatment", num_indent);
inprint(stmt->file_path, num_indent+1);
inprint(stmt->table_name, num_indent+1);
void printImportStatementInfo(ImportStatement* stmt, uint numIndent) {
inprint("ImportStatment", numIndent);
inprint(stmt->filePath, numIndent+1);
inprint(stmt->tableName, numIndent+1);
}
void printCreateStatementInfo(CreateStatement* stmt, uint num_indent) {
inprint("CreateStatment", num_indent);
inprint(stmt->table_name, num_indent+1);
inprint(stmt->file_path, num_indent+1);
void printCreateStatementInfo(CreateStatement* stmt, uint numIndent) {
inprint("CreateStatment", numIndent);
inprint(stmt->tableName, numIndent+1);
inprint(stmt->filePath, numIndent+1);
}
void printInsertStatementInfo(InsertStatement* stmt, uint num_indent) {
inprint("InsertStatment", num_indent);
inprint(stmt->table_name, num_indent+1);
void printInsertStatementInfo(InsertStatement* stmt, uint numIndent) {
inprint("InsertStatment", numIndent);
inprint(stmt->tableName, numIndent+1);
if (stmt->columns != NULL) {
inprint("Columns", num_indent+1);
inprint("Columns", numIndent+1);
for (char* col_name : *stmt->columns) {
inprint(col_name, num_indent+2);
inprint(col_name, numIndent+2);
}
}
switch (stmt->type) {
case InsertStatement::kInsertValues:
inprint("Values", num_indent+1);
inprint("Values", numIndent+1);
for (Expr* expr : *stmt->values) {
printExpression(expr, num_indent+2);
printExpression(expr, numIndent+2);
}
break;
case InsertStatement::kInsertSelect:
printSelectStatementInfo(stmt->select, num_indent+1);
printSelectStatementInfo(stmt->select, numIndent+1);
break;
}
}

View File

@ -2,21 +2,21 @@
#define __HELPER_H__
#define TEST_PARSE_SQL_QUERY(query, output_var, num_statements) \
SQLParserResult* output_var = SQLParser::parseSQLString(query); \
ASSERT(output_var->isValid); \
ASSERT_EQ(output_var->size(), num_statements);
#define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \
SQLParserResult* outputVar = SQLParser::parseSQLString(query); \
ASSERT(outputVar->isValid); \
ASSERT_EQ(outputVar->size(), numStatements);
#define TEST_PARSE_SINGLE_SQL(query, stmt_type, stmt_class, output_var) \
#define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \
TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \
ASSERT_EQ(stmt_list->getStatement(0)->type(), stmt_type); \
stmt_class* output_var = (stmt_class*) stmt_list->getStatement(0);
ASSERT_EQ(stmt_list->getStatement(0)->type(), stmtType); \
stmtClass* outputVar = (stmtClass*) stmt_list->getStatement(0);
#define TEST_CAST_STMT(stmt_list, stmt_index, stmt_type, stmt_class, output_var) \
ASSERT_EQ(stmt_list->getStatement(stmt_index)->type(), stmt_type); \
stmt_class* output_var = (stmt_class*) stmt_list->getStatement(stmt_index);
#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);
#endif

View File

@ -9,16 +9,16 @@ using namespace hsql;
TEST(SelectTest) {
TEST_PARSE_SINGLE_SQL("SELECT * FROM students;", kStmtSelect, SelectStatement, stmt);
ASSERT_NULL(stmt->where_clause);
ASSERT_NULL(stmt->group_by);
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->select_distinct);
ASSERT_FALSE(stmt->selectDistinct);
GroupByDescription* group = stmt->group_by;
GroupByDescription* group = stmt->groupBy;
ASSERT_NOTNULL(group);
ASSERT_EQ(group->columns->size(), 1);
ASSERT(group->having->isSimpleOp('<'));
@ -30,17 +30,17 @@ TEST(SelectHavingTest) {
TEST(SelectDistinctTest) {
TEST_PARSE_SINGLE_SQL("SELECT DISTINCT grade, city FROM students;", kStmtSelect, SelectStatement, stmt);
ASSERT(stmt->select_distinct);
ASSERT_NULL(stmt->where_clause);
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);
ASSERT_FALSE(stmt->select_distinct);
ASSERT_EQ(stmt->select_list->size(), 3);
ASSERT(!stmt->select_list->at(1)->distinct);
ASSERT(stmt->select_list->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

@ -7,9 +7,9 @@ class TestsManager {
// 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>& test_names() {
static std::vector<std::string>* test_names = new std::vector<std::string>;
return *test_names;
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() {
@ -22,26 +22,26 @@ public:
int AddTest(void (*foo)(void), std::string name) {
TestsManager::tests().push_back(foo);
TestsManager::test_names().push_back(name);
TestsManager::testNames().push_back(name);
return 0;
}
void RunTests() {
size_t num_failed = 0;
size_t numFailed = 0;
for (size_t i = 0; i < TestsManager::tests().size(); ++i) {
printf("\033[0;32m{ running}\033[0m %s\n", TestsManager::test_names()[i].c_str());
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::test_names()[i].c_str());
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::test_names()[i].c_str());
printf("\033[1;31m{ failed} %s\n", TestsManager::testNames()[i].c_str());
printf("\tAssertion failed: %s\n\033[0m", e.what());
num_failed++;
numFailed++;
}
}

View File

@ -44,11 +44,6 @@ protected:
std::string _msg;
};
int AddTest(void (*foo)(void), std::string name);
#endif

View File

@ -33,17 +33,17 @@ int main(int argc, char *argv[]) {
return -1;
}
bool expect_false = false;
bool use_file = false;
std::string file_path = "";
bool expectFalse = false;
bool useFile = false;
std::string filePath = "";
// Parse command line arguments
int i = 1;
for (; i < argc; ++i) {
if (STREQ(argv[i], "--false")) expect_false = true;
if (STREQ(argv[i], "--false")) expectFalse = true;
else if (STREQ(argv[i], "-f")) {
use_file = true;
file_path = argv[++i];
useFile = true;
filePath = argv[++i];
} else {
break;
}
@ -52,15 +52,15 @@ int main(int argc, char *argv[]) {
// Read list of queries for this rest
std::vector<std::string> queries;
if (use_file) {
queries = readlines(file_path);
if (useFile) {
queries = readlines(filePath);
} else {
for (; i < argc; ++i) queries.push_back(argv[i]);
}
// Execute queries
int num_failed = 0;
int numFailed = 0;
for (std::string sql : queries) {
// Measuring the parsing time
std::chrono::time_point<std::chrono::system_clock> start, end;
@ -73,21 +73,21 @@ int main(int argc, char *argv[]) {
std::chrono::duration<double> elapsed_seconds = end-start;
double us = elapsed_seconds.count() * 1000 * 1000;
if (expect_false == stmt_list->isValid) {
if (expectFalse == stmt_list->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%s\n", sql.c_str());
num_failed++;
numFailed++;
} else {
// TODO: indicate whether expect_false was set
// TODO: indicate whether expectFalse was set
printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str());
}
}
if (num_failed == 0) {
if (numFailed == 0) {
printf("\033[0;32m{ ok} \033[0mAll %lu grammar tests completed successfully!\n", queries.size());
} else {
fprintf(stderr, "\033[0;31m{ failed} \033[0mSome grammar tests failed! %d out of %lu tests failed!\n", num_failed, queries.size());
fprintf(stderr, "\033[0;31m{ failed} \033[0mSome grammar tests failed! %d out of %lu tests failed!\n", numFailed, queries.size());
}

View File

@ -17,7 +17,7 @@ TEST(DeleteStatementTest) {
ASSERT(result->getStatement(0)->type() == kStmtDelete);
DeleteStatement* stmt = (DeleteStatement*) result->getStatement(0);
ASSERT_STREQ(stmt->table_name, "students");
ASSERT_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator));
ASSERT_STREQ(stmt->expr->expr->name, "grade");
@ -32,7 +32,7 @@ TEST(CreateStatementTest) {
CreateStatement* stmt = (CreateStatement*) result->getStatement(0);
ASSERT_EQ(stmt->type, CreateStatement::kTable);
ASSERT_STREQ(stmt->table_name, "students");
ASSERT_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->columns);
ASSERT_EQ(stmt->columns->size(), 4);
ASSERT_STREQ(stmt->columns->at(0)->name, "name");
@ -114,18 +114,18 @@ TEST(PrepareStatementTest) {
TEST_CAST_STMT(prep1->query, 1, kStmtSelect, SelectStatement, select);
ASSERT(insert->values->at(0)->isType(kExprPlaceholder));
ASSERT(select->select_list->at(0)->isType(kExprPlaceholder));
ASSERT(select->where_clause->expr2->isType(kExprPlaceholder));
ASSERT(select->selectList->at(0)->isType(kExprPlaceholder));
ASSERT(select->whereClause->expr2->isType(kExprPlaceholder));
// Check IDs of placeholders
ASSERT_EQ(insert->values->at(0)->ival, 0);
ASSERT_EQ(insert->values->at(0), prep1->placeholders[0]);
ASSERT_EQ(select->select_list->at(0)->ival, 1);
ASSERT_EQ(select->select_list->at(0), prep1->placeholders[1]);
ASSERT_EQ(select->selectList->at(0)->ival, 1);
ASSERT_EQ(select->selectList->at(0), prep1->placeholders[1]);
ASSERT_EQ(select->where_clause->expr2->ival, 2);
ASSERT_EQ(select->where_clause->expr2, prep1->placeholders[2]);
ASSERT_EQ(select->whereClause->expr2->ival, 2);
ASSERT_EQ(select->whereClause->expr2, prep1->placeholders[2]);
// Prepare Statement #2
ASSERT_STREQ(prep2->name, "stmt");