renamed SQLStatementList to SQLParserResult and moved into separate file

This commit is contained in:
Pedro 2016-02-27 14:24:23 +01:00
parent f98843a316
commit 3df367e668
17 changed files with 419 additions and 407 deletions

View File

@ -13,8 +13,8 @@ SQLParser::SQLParser() {
} }
SQLStatementList* SQLParser::parseSQLString(const char *text) { SQLParserResult* SQLParser::parseSQLString(const char *text) {
SQLStatementList* result; SQLParserResult* result = NULL;
yyscan_t scanner; yyscan_t scanner;
YY_BUFFER_STATE state; YY_BUFFER_STATE state;
@ -37,7 +37,7 @@ SQLStatementList* SQLParser::parseSQLString(const char *text) {
return result; return result;
} }
SQLStatementList* 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

@ -2,20 +2,17 @@
#define __SQLPARSER_H_ #define __SQLPARSER_H_
#include "sqltypes.h" #include "sqltypes.h"
#include "SQLParserResult.h"
#include "sql/statements.h"
namespace hsql { namespace hsql {
/**
/*! * Main class for parsing SQL strings
* \mainpage SQLParser (C++)
*/
/*!
* @brief Main class for parsing SQL strings
*/ */
class SQLParser { class SQLParser {
public: public:
static SQLStatementList* parseSQLString(const char* sql); static SQLParserResult* parseSQLString(const char* sql);
static SQLStatementList* parseSQLString(const std::string& sql); static SQLParserResult* parseSQLString(const std::string& sql);
private: private:
SQLParser(); SQLParser();

2
src/SQLParserResult.cpp Normal file
View File

@ -0,0 +1,2 @@
#include "SQLParserResult.h"

43
src/SQLParserResult.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef __SQLPARSERRESULT__
#define __SQLPARSERRESULT__
#include "sql/SQLStatement.h"
namespace hsql {
/**
* Represents the result of the SQLParser.
* If parsing was successful it contains a list of SQLStatement.
*/
struct SQLParserResult {
public:
SQLParserResult() :
isValid(true),
parser_msg(NULL) {};
SQLParserResult(SQLStatement* stmt) :
isValid(true),
parser_msg(NULL) {
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); }
SQLStatement* getStatement(int id) { return statements[id]; }
size_t numStatements() { return statements.size(); }
std::vector<SQLStatement*> statements;
bool isValid;
const char* parser_msg;
int error_line;
int error_col;
};
}
#endif // __SQLPARSERRESULT__

File diff suppressed because it is too large Load Diff

View File

@ -48,9 +48,12 @@
extern int hsql_debug; extern int hsql_debug;
#endif #endif
/* "%code requires" blocks. */ /* "%code requires" blocks. */
#line 43 "bison_parser.y" /* yacc.c:1909 */ #line 42 "bison_parser.y" /* yacc.c:1909 */
// %code requires block // %code requires block
#include "../sql/statements.h"
#include "../SQLParserResult.h"
#include "parser_typedef.h" #include "parser_typedef.h"
// Auto update column and line number // Auto update column and line number
@ -68,7 +71,7 @@ extern int hsql_debug;
} \ } \
} }
#line 72 "bison_parser.h" /* yacc.c:1909 */ #line 75 "bison_parser.h" /* yacc.c:1909 */
/* Token type. */ /* Token type. */
#ifndef HSQL_TOKENTYPE #ifndef HSQL_TOKENTYPE
@ -206,7 +209,7 @@ extern int hsql_debug;
typedef union HSQL_STYPE HSQL_STYPE; typedef union HSQL_STYPE HSQL_STYPE;
union HSQL_STYPE union HSQL_STYPE
{ {
#line 99 "bison_parser.y" /* yacc.c:1909 */ #line 101 "bison_parser.y" /* yacc.c:1909 */
double fval; double fval;
int64_t ival; int64_t ival;
@ -234,7 +237,7 @@ union HSQL_STYPE
hsql::GroupByDescription* group_t; hsql::GroupByDescription* group_t;
hsql::UpdateClause* update_t; hsql::UpdateClause* update_t;
hsql::SQLStatementList* stmt_list; hsql::SQLParserResult* stmt_list;
std::vector<char*>* str_vec; std::vector<char*>* str_vec;
std::vector<hsql::TableRef*>* table_vec; std::vector<hsql::TableRef*>* table_vec;
@ -242,7 +245,7 @@ union HSQL_STYPE
std::vector<hsql::UpdateClause*>* update_vec; std::vector<hsql::UpdateClause*>* update_vec;
std::vector<hsql::Expr*>* expr_vec; std::vector<hsql::Expr*>* expr_vec;
#line 246 "bison_parser.h" /* yacc.c:1909 */ #line 249 "bison_parser.h" /* yacc.c:1909 */
}; };
# define HSQL_STYPE_IS_TRIVIAL 1 # define HSQL_STYPE_IS_TRIVIAL 1
# define HSQL_STYPE_IS_DECLARED 1 # define HSQL_STYPE_IS_DECLARED 1
@ -264,6 +267,6 @@ struct HSQL_LTYPE
int hsql_parse (hsql::SQLStatementList** result, yyscan_t scanner); int hsql_parse (hsql::SQLParserResult** result, yyscan_t scanner);
#endif /* !YY_HSQL_BISON_PARSER_H_INCLUDED */ #endif /* !YY_HSQL_BISON_PARSER_H_INCLUDED */

View File

@ -11,7 +11,6 @@
** Section 1: C Declarations ** Section 1: C Declarations
*********************************/ *********************************/
#include "../sqltypes.h"
#include "bison_parser.h" #include "bison_parser.h"
#include "flex_lexer.h" #include "flex_lexer.h"
@ -19,9 +18,9 @@
using namespace hsql; using namespace hsql;
int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const char *msg) { int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const char *msg) {
SQLStatementList* list = new SQLStatementList(); SQLParserResult* list = new SQLParserResult();
list->isValid = false; list->isValid = false;
list->parser_msg = strdup(msg); list->parser_msg = strdup(msg);
list->error_line = llocp->first_line; list->error_line = llocp->first_line;
@ -42,6 +41,9 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c
// Specify code that is included in the generated .h and .c files // Specify code that is included in the generated .h and .c files
%code requires { %code requires {
// %code requires block // %code requires block
#include "../sql/statements.h"
#include "../SQLParserResult.h"
#include "parser_typedef.h" #include "parser_typedef.h"
// Auto update column and line number // Auto update column and line number
@ -89,7 +91,7 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c
%lex-param { yyscan_t scanner } %lex-param { yyscan_t scanner }
// Define additional parameters for yyparse // Define additional parameters for yyparse
%parse-param { hsql::SQLStatementList** result } %parse-param { hsql::SQLParserResult** result }
%parse-param { yyscan_t scanner } %parse-param { yyscan_t scanner }
@ -123,7 +125,7 @@ int yyerror(YYLTYPE* llocp, SQLStatementList** result, yyscan_t scanner, const c
hsql::GroupByDescription* group_t; hsql::GroupByDescription* group_t;
hsql::UpdateClause* update_t; hsql::UpdateClause* update_t;
hsql::SQLStatementList* stmt_list; hsql::SQLParserResult* stmt_list;
std::vector<char*>* str_vec; std::vector<char*>* str_vec;
std::vector<hsql::TableRef*>* table_vec; std::vector<hsql::TableRef*>* table_vec;
@ -231,7 +233,7 @@ input:
statement_list: statement_list:
statement { $$ = new SQLStatementList($1); } statement { $$ = new SQLParserResult($1); }
| statement_list ';' statement { $1->addStatement($3); $$ = $1; } | statement_list ';' statement { $1->addStatement($3); $$ = $1; }
; ;
@ -265,7 +267,7 @@ prepare_statement:
PREPARE IDENTIFIER ':' preparable_statement { PREPARE IDENTIFIER ':' preparable_statement {
$$ = new PrepareStatement(); $$ = new PrepareStatement();
$$->name = $2; $$->name = $2;
$$->query = new SQLStatementList($4); $$->query = new SQLParserResult($4);
} }
| PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' { | PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' {
$$ = new PrepareStatement(); $$ = new PrepareStatement();

View File

@ -1545,7 +1545,7 @@ static yyconst flex_int16_t yy_chk[3639] =
***************************/ ***************************/
#line 12 "flex_lexer.l" #line 12 "flex_lexer.l"
#include "../sqltypes.h" #include "../sql/Expr.h"
#include "bison_parser.h" #include "bison_parser.h"
#include <stdio.h> #include <stdio.h>

View File

@ -10,7 +10,7 @@
***************************/ ***************************/
%{ %{
#include "../sqltypes.h" #include "../sql/Expr.h"
#include "bison_parser.h" #include "bison_parser.h"
#include <stdio.h> #include <stdio.h>

View File

@ -1,6 +1,7 @@
#ifndef __PREPARE_STATEMENT_H__ #ifndef __PREPARE_STATEMENT_H__
#define __PREPARE_STATEMENT_H__ #define __PREPARE_STATEMENT_H__
#include "../SQLParserResult.h"
#include "SQLStatement.h" #include "SQLStatement.h"
#include "SelectStatement.h" #include "SelectStatement.h"
#include <algorithm> #include <algorithm>
@ -42,7 +43,7 @@ struct PrepareStatement : SQLStatement {
} }
const char* name; const char* name;
SQLStatementList* query; SQLParserResult* query;
std::vector<Expr*> placeholders; std::vector<Expr*> placeholders;
}; };

View File

@ -1,18 +1,13 @@
/* #ifndef __SQLSTATEMENT_H__
* SQLStatement.h #define __SQLSTATEMENT_H__
* Definition of the structure used to build the syntax tree.
*/
#ifndef __STATEMENT_H__
#define __STATEMENT_H__
#include "Expr.h" #include "Expr.h"
#include <vector> #include <vector>
namespace hsql { namespace hsql {
typedef enum { typedef enum {
kStmtError, // Unused kStmtError, // unused
kStmtSelect, kStmtSelect,
kStmtImport, kStmtImport,
kStmtInsert, kStmtInsert,
@ -29,8 +24,7 @@ typedef enum {
/** /**
* @struct SQLStatement * Base struct for every SQL statement
* @brief Base class for every SQLStatement
*/ */
struct SQLStatement { struct SQLStatement {
SQLStatement(StatementType type) : SQLStatement(StatementType type) :
@ -44,43 +38,6 @@ private:
StatementType _type; StatementType _type;
}; };
/**
* @struct SQLStatementList
* @brief Represents the result of the SQLParser. If parsing was successful it is a list of SQLStatement.
*/
struct SQLStatementList {
public:
SQLStatementList() :
isValid(true),
parser_msg(NULL) {};
SQLStatementList(SQLStatement* stmt) :
isValid(true),
parser_msg(NULL) {
addStatement(stmt);
};
virtual ~SQLStatementList() {
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); }
SQLStatement* getStatement(int id) { return statements[id]; }
size_t numStatements() { return statements.size(); }
std::vector<SQLStatement*> statements;
bool isValid;
const char* parser_msg;
int error_line;
int error_col;
};
} // namespace hsql } // namespace hsql
#endif // __STATEMENT_H__ #endif // __SQLSTATEMENT_H__

14
src/sql/statements.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __STATEMENTS_H__
#define __STATEMENTS_H__
#include "SelectStatement.h"
#include "ImportStatement.h"
#include "CreateStatement.h"
#include "InsertStatement.h"
#include "UpdateStatement.h"
#include "DeleteStatement.h"
#include "DropStatement.h"
#include "PrepareStatement.h"
#include "ExecuteStatement.h"
#endif // __STATEMENTS_H__

View File

@ -4,6 +4,7 @@
#include "sqltypes.h" #include "sqltypes.h"
#include "sql/statements.h"
namespace hsql { namespace hsql {

View File

@ -3,14 +3,4 @@
typedef unsigned int uint; typedef unsigned int uint;
#include "sql/SelectStatement.h"
#include "sql/ImportStatement.h"
#include "sql/CreateStatement.h"
#include "sql/InsertStatement.h"
#include "sql/UpdateStatement.h"
#include "sql/DeleteStatement.h"
#include "sql/DropStatement.h"
#include "sql/PrepareStatement.h"
#include "sql/ExecuteStatement.h"
#endif #endif

View File

@ -3,7 +3,7 @@
#define TEST_PARSE_SQL_QUERY(query, output_var, num_statements) \ #define TEST_PARSE_SQL_QUERY(query, output_var, num_statements) \
SQLStatementList* 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->numStatements(), num_statements);

View File

@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
start = std::chrono::system_clock::now(); start = std::chrono::system_clock::now();
// Parsing // Parsing
SQLStatementList* stmt_list = SQLParser::parseSQLString(sql.c_str()); SQLParserResult* stmt_list = SQLParser::parseSQLString(sql.c_str());
end = std::chrono::system_clock::now(); end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start; std::chrono::duration<double> elapsed_seconds = end-start;

View File

@ -11,7 +11,7 @@ using namespace hsql;
TEST(DeleteStatementTest) { TEST(DeleteStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); SQLParserResult* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->numStatements(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT(stmt_list->getStatement(0)->type() == kStmtDelete); ASSERT(stmt_list->getStatement(0)->type() == kStmtDelete);
@ -25,7 +25,7 @@ TEST(DeleteStatementTest) {
} }
TEST(CreateStatementTest) { TEST(CreateStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); SQLParserResult* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->numStatements(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtCreate); ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtCreate);
@ -47,7 +47,7 @@ TEST(CreateStatementTest) {
TEST(UpdateStatementTest) { TEST(UpdateStatementTest) {
SQLStatementList* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); SQLParserResult* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';");
ASSERT(stmt_list->isValid); ASSERT(stmt_list->isValid);
ASSERT_EQ(stmt_list->numStatements(), 1); ASSERT_EQ(stmt_list->numStatements(), 1);
ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtUpdate); ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtUpdate);