adjust names of members

This commit is contained in:
Pedro 2016-02-27 14:45:59 +01:00
parent 3df367e668
commit 57b8cdd31b
13 changed files with 205 additions and 184 deletions

View File

@ -37,6 +37,7 @@ namespace hsql {
return result; return result;
} }
SQLParserResult* SQLParser::parseSQLString(const std::string& text) { SQLParserResult* SQLParser::parseSQLString(const std::string& text) {
return parseSQLString(text.c_str()); return parseSQLString(text.c_str());
} }

View File

@ -1,7 +1,6 @@
#ifndef __SQLPARSER_H_ #ifndef __SQLPARSER_H_
#define __SQLPARSER_H_ #define __SQLPARSER_H_
#include "sqltypes.h"
#include "SQLParserResult.h" #include "SQLParserResult.h"
#include "sql/statements.h" #include "sql/statements.h"

View File

@ -1,2 +1,41 @@
#include "SQLParserResult.h" #include "SQLParserResult.h"
namespace hsql {
SQLParserResult::SQLParserResult() :
isValid(true),
errorMsg(NULL) {};
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;
}
void SQLParserResult::addStatement(SQLStatement* stmt) {
statements.push_back(stmt);
}
SQLStatement* SQLParserResult::getStatement(int id) {
return statements[id];
}
size_t SQLParserResult::size() {
return statements.size();
}
} // namespace hsql

View File

@ -8,36 +8,28 @@ namespace hsql {
* Represents the result of the SQLParser. * Represents the result of the SQLParser.
* If parsing was successful it contains a list of SQLStatement. * If parsing was successful it contains a list of SQLStatement.
*/ */
struct SQLParserResult { class SQLParserResult {
public: public:
SQLParserResult() :
isValid(true),
parser_msg(NULL) {};
SQLParserResult(SQLStatement* stmt) : SQLParserResult();
isValid(true), SQLParserResult(SQLStatement* stmt);
parser_msg(NULL) { virtual ~SQLParserResult();
addStatement(stmt);
};
virtual ~SQLParserResult() {
for (std::vector<SQLStatement*>::iterator it = statements.begin(); it != statements.end(); ++it) {
delete *it;
}
delete parser_msg;
}
void addStatement(SQLStatement* stmt) { statements.push_back(stmt); } void addStatement(SQLStatement* stmt);
SQLStatement* getStatement(int id) { return statements[id]; }
size_t numStatements() { return statements.size(); } SQLStatement* getStatement(int id);
size_t size();
// public properties
std::vector<SQLStatement*> statements; std::vector<SQLStatement*> statements;
bool isValid; bool isValid;
const char* parser_msg;
int error_line; const char* errorMsg;
int error_col; int errorLine;
int errorColumn;
}; };
} } // namespace hsql
#endif // __SQLPARSERRESULT__ #endif // __SQLPARSERRESULT__

View File

@ -95,9 +95,9 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
SQLParserResult* list = new SQLParserResult(); SQLParserResult* list = new SQLParserResult();
list->isValid = false; list->isValid = false;
list->parser_msg = strdup(msg); list->errorMsg = strdup(msg);
list->error_line = llocp->first_line; list->errorLine = llocp->first_line;
list->error_col = llocp->first_column; list->errorColumn = llocp->first_column;
*result = list; *result = list;
return 0; return 0;

View File

@ -22,9 +22,9 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
SQLParserResult* list = new SQLParserResult(); SQLParserResult* list = new SQLParserResult();
list->isValid = false; list->isValid = false;
list->parser_msg = strdup(msg); list->errorMsg = strdup(msg);
list->error_line = llocp->first_line; list->errorLine = llocp->first_line;
list->error_col = llocp->first_column; list->errorColumn = llocp->first_column;
*result = list; *result = list;
return 0; return 0;

View File

@ -26,8 +26,7 @@ typedef enum {
typedef struct Expr Expr; typedef struct Expr Expr;
/** /**
* @class Expr * Represents SQL expressions (i.e. literals, operators, column_refs)
* @brief Represents SQL expressions (i.e. literals, operators, column_refs)
* *
* TODO: When destructing a placeholder expression, we might need to alter the placeholder_list * TODO: When destructing a placeholder expression, we might need to alter the placeholder_list
*/ */

View File

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

View File

@ -1,19 +1,16 @@
#ifndef __SQLHELPER_H__ #ifndef __SQLHELPER_H__
#define __SQLHELPER_H__ #define __SQLHELPER_H__
#include "sqltypes.h"
#include "sql/statements.h" #include "sql/statements.h"
namespace hsql { namespace hsql {
void printStatementInfo(SQLStatement* stmt); void printStatementInfo(SQLStatement* stmt);
void printSelectStatementInfo(SelectStatement* stmt, uint num_indent); void printSelectStatementInfo(SelectStatement* stmt, uint num_indent);
void printImportStatementInfo(ImportStatement* stmt, uint num_indent); void printImportStatementInfo(ImportStatement* stmt, uint num_indent);
void printInsertStatementInfo(InsertStatement* stmt, uint num_indent); void printInsertStatementInfo(InsertStatement* stmt, uint num_indent);
void printCreateStatementInfo(CreateStatement* stmt, uint num_indent); void printCreateStatementInfo(CreateStatement* stmt, uint num_indent);
void printExpression(Expr* expr, uint num_indent); void printExpression(Expr* expr, uint num_indent);
} // namespace hsql } // namespace hsql

View File

@ -1,6 +0,0 @@
#ifndef __SQLLIB_H__
#define __SQLLIB_H__
typedef unsigned int uint;
#endif

View File

@ -5,7 +5,7 @@
#define TEST_PARSE_SQL_QUERY(query, output_var, num_statements) \ #define TEST_PARSE_SQL_QUERY(query, output_var, num_statements) \
SQLParserResult* output_var = SQLParser::parseSQLString(query); \ SQLParserResult* output_var = SQLParser::parseSQLString(query); \
ASSERT(output_var->isValid); \ ASSERT(output_var->isValid); \
ASSERT_EQ(output_var->numStatements(), num_statements); ASSERT_EQ(output_var->size(), num_statements);
#define TEST_PARSE_SINGLE_SQL(query, stmt_type, stmt_class, output_var) \ #define TEST_PARSE_SINGLE_SQL(query, stmt_type, stmt_class, output_var) \

View File

@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
if (expect_false == stmt_list->isValid) { if (expect_false == stmt_list->isValid) {
printf("\033[0;31m{ failed}\033[0m\n"); printf("\033[0;31m{ failed}\033[0m\n");
printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", stmt_list->parser_msg, stmt_list->error_line, stmt_list->error_col); 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()); printf("\t%s\n", sql.c_str());
num_failed++; num_failed++;
} else { } else {

View File

@ -11,12 +11,12 @@ using namespace hsql;
TEST(DeleteStatementTest) { TEST(DeleteStatementTest) {
SQLParserResult* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
ASSERT(stmt_list->isValid); ASSERT(result->isValid);
ASSERT_EQ(stmt_list->numStatements(), 1); ASSERT_EQ(result->size(), 1);
ASSERT(stmt_list->getStatement(0)->type() == kStmtDelete); ASSERT(result->getStatement(0)->type() == kStmtDelete);
DeleteStatement* stmt = (DeleteStatement*) stmt_list->getStatement(0); DeleteStatement* stmt = (DeleteStatement*) result->getStatement(0);
ASSERT_STREQ(stmt->table_name, "students"); ASSERT_STREQ(stmt->table_name, "students");
ASSERT_NOTNULL(stmt->expr); ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator)); ASSERT(stmt->expr->isType(kExprOperator));
@ -25,12 +25,12 @@ TEST(DeleteStatementTest) {
} }
TEST(CreateStatementTest) { TEST(CreateStatementTest) {
SQLParserResult* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)");
ASSERT(stmt_list->isValid); ASSERT(result->isValid);
ASSERT_EQ(stmt_list->numStatements(), 1); ASSERT_EQ(result->size(), 1);
ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtCreate); ASSERT_EQ(result->getStatement(0)->type(), kStmtCreate);
CreateStatement* stmt = (CreateStatement*) stmt_list->getStatement(0); CreateStatement* stmt = (CreateStatement*) result->getStatement(0);
ASSERT_EQ(stmt->type, CreateStatement::kTable); ASSERT_EQ(stmt->type, CreateStatement::kTable);
ASSERT_STREQ(stmt->table_name, "students"); ASSERT_STREQ(stmt->table_name, "students");
ASSERT_NOTNULL(stmt->columns); ASSERT_NOTNULL(stmt->columns);
@ -47,12 +47,12 @@ TEST(CreateStatementTest) {
TEST(UpdateStatementTest) { TEST(UpdateStatementTest) {
SQLParserResult* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';");
ASSERT(stmt_list->isValid); ASSERT(result->isValid);
ASSERT_EQ(stmt_list->numStatements(), 1); ASSERT_EQ(result->size(), 1);
ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtUpdate); ASSERT_EQ(result->getStatement(0)->type(), kStmtUpdate);
UpdateStatement* stmt = (UpdateStatement*) stmt_list->getStatement(0); UpdateStatement* stmt = (UpdateStatement*) result->getStatement(0);
ASSERT_NOTNULL(stmt->table); ASSERT_NOTNULL(stmt->table);
ASSERT_STREQ(stmt->table->name, "students"); ASSERT_STREQ(stmt->table->name, "students");
@ -99,16 +99,16 @@ TEST(PrepareStatementTest) {
"PREPARE stmt: SELECT * FROM data WHERE c1 = ?;" "PREPARE stmt: SELECT * FROM data WHERE c1 = ?;"
"DEALLOCATE PREPARE stmt;"; "DEALLOCATE PREPARE stmt;";
TEST_PARSE_SQL_QUERY(query, stmt_list, 3); TEST_PARSE_SQL_QUERY(query, result, 3);
TEST_CAST_STMT(stmt_list, 0, kStmtPrepare, PrepareStatement, prep1); TEST_CAST_STMT(result, 0, kStmtPrepare, PrepareStatement, prep1);
TEST_CAST_STMT(stmt_list, 1, kStmtPrepare, PrepareStatement, prep2); TEST_CAST_STMT(result, 1, kStmtPrepare, PrepareStatement, prep2);
TEST_CAST_STMT(stmt_list, 2, kStmtDrop, DropStatement, drop); TEST_CAST_STMT(result, 2, kStmtDrop, DropStatement, drop);
// Prepare Statement #1 // Prepare Statement #1
ASSERT_STREQ(prep1->name, "test"); ASSERT_STREQ(prep1->name, "test");
ASSERT_EQ(prep1->placeholders.size(), 3); ASSERT_EQ(prep1->placeholders.size(), 3);
ASSERT_EQ(prep1->query->numStatements(), 2); ASSERT_EQ(prep1->query->size(), 2);
TEST_CAST_STMT(prep1->query, 0, kStmtInsert, InsertStatement, insert); TEST_CAST_STMT(prep1->query, 0, kStmtInsert, InsertStatement, insert);
TEST_CAST_STMT(prep1->query, 1, kStmtSelect, SelectStatement, select); TEST_CAST_STMT(prep1->query, 1, kStmtSelect, SelectStatement, select);