renamed SQLStatementList to SQLParserResult and moved into separate file
This commit is contained in:
parent
f98843a316
commit
3df367e668
|
@ -13,8 +13,8 @@ SQLParser::SQLParser() {
|
|||
}
|
||||
|
||||
|
||||
SQLStatementList* SQLParser::parseSQLString(const char *text) {
|
||||
SQLStatementList* result;
|
||||
SQLParserResult* SQLParser::parseSQLString(const char *text) {
|
||||
SQLParserResult* result = NULL;
|
||||
yyscan_t scanner;
|
||||
YY_BUFFER_STATE state;
|
||||
|
||||
|
@ -37,7 +37,7 @@ SQLStatementList* SQLParser::parseSQLString(const char *text) {
|
|||
return result;
|
||||
}
|
||||
|
||||
SQLStatementList* SQLParser::parseSQLString(const std::string& text) {
|
||||
SQLParserResult* SQLParser::parseSQLString(const std::string& text) {
|
||||
return parseSQLString(text.c_str());
|
||||
}
|
||||
|
||||
|
|
|
@ -2,20 +2,17 @@
|
|||
#define __SQLPARSER_H_
|
||||
|
||||
#include "sqltypes.h"
|
||||
#include "SQLParserResult.h"
|
||||
#include "sql/statements.h"
|
||||
|
||||
namespace hsql {
|
||||
|
||||
/*!
|
||||
* \mainpage SQLParser (C++)
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Main class for parsing SQL strings
|
||||
/**
|
||||
* Main class for parsing SQL strings
|
||||
*/
|
||||
class SQLParser {
|
||||
public:
|
||||
static SQLStatementList* parseSQLString(const char* sql);
|
||||
static SQLStatementList* parseSQLString(const std::string& sql);
|
||||
static SQLParserResult* parseSQLString(const char* sql);
|
||||
static SQLParserResult* parseSQLString(const std::string& sql);
|
||||
|
||||
private:
|
||||
SQLParser();
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
#include "SQLParserResult.h"
|
|
@ -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
|
@ -48,9 +48,12 @@
|
|||
extern int hsql_debug;
|
||||
#endif
|
||||
/* "%code requires" blocks. */
|
||||
#line 43 "bison_parser.y" /* yacc.c:1909 */
|
||||
#line 42 "bison_parser.y" /* yacc.c:1909 */
|
||||
|
||||
// %code requires block
|
||||
|
||||
#include "../sql/statements.h"
|
||||
#include "../SQLParserResult.h"
|
||||
#include "parser_typedef.h"
|
||||
|
||||
// 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. */
|
||||
#ifndef HSQL_TOKENTYPE
|
||||
|
@ -206,7 +209,7 @@ extern int hsql_debug;
|
|||
typedef union HSQL_STYPE HSQL_STYPE;
|
||||
union HSQL_STYPE
|
||||
{
|
||||
#line 99 "bison_parser.y" /* yacc.c:1909 */
|
||||
#line 101 "bison_parser.y" /* yacc.c:1909 */
|
||||
|
||||
double fval;
|
||||
int64_t ival;
|
||||
|
@ -234,7 +237,7 @@ union HSQL_STYPE
|
|||
hsql::GroupByDescription* group_t;
|
||||
hsql::UpdateClause* update_t;
|
||||
|
||||
hsql::SQLStatementList* stmt_list;
|
||||
hsql::SQLParserResult* stmt_list;
|
||||
|
||||
std::vector<char*>* str_vec;
|
||||
std::vector<hsql::TableRef*>* table_vec;
|
||||
|
@ -242,7 +245,7 @@ union HSQL_STYPE
|
|||
std::vector<hsql::UpdateClause*>* update_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_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 */
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
** Section 1: C Declarations
|
||||
*********************************/
|
||||
|
||||
#include "../sqltypes.h"
|
||||
#include "bison_parser.h"
|
||||
#include "flex_lexer.h"
|
||||
|
||||
|
@ -19,9 +18,9 @@
|
|||
|
||||
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->parser_msg = strdup(msg);
|
||||
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
|
||||
%code requires {
|
||||
// %code requires block
|
||||
|
||||
#include "../sql/statements.h"
|
||||
#include "../SQLParserResult.h"
|
||||
#include "parser_typedef.h"
|
||||
|
||||
// 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 }
|
||||
|
||||
// Define additional parameters for yyparse
|
||||
%parse-param { hsql::SQLStatementList** result }
|
||||
%parse-param { hsql::SQLParserResult** result }
|
||||
%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::UpdateClause* update_t;
|
||||
|
||||
hsql::SQLStatementList* stmt_list;
|
||||
hsql::SQLParserResult* stmt_list;
|
||||
|
||||
std::vector<char*>* str_vec;
|
||||
std::vector<hsql::TableRef*>* table_vec;
|
||||
|
@ -231,7 +233,7 @@ input:
|
|||
|
||||
|
||||
statement_list:
|
||||
statement { $$ = new SQLStatementList($1); }
|
||||
statement { $$ = new SQLParserResult($1); }
|
||||
| statement_list ';' statement { $1->addStatement($3); $$ = $1; }
|
||||
;
|
||||
|
||||
|
@ -265,7 +267,7 @@ prepare_statement:
|
|||
PREPARE IDENTIFIER ':' preparable_statement {
|
||||
$$ = new PrepareStatement();
|
||||
$$->name = $2;
|
||||
$$->query = new SQLStatementList($4);
|
||||
$$->query = new SQLParserResult($4);
|
||||
}
|
||||
| PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' {
|
||||
$$ = new PrepareStatement();
|
||||
|
|
|
@ -1545,7 +1545,7 @@ static yyconst flex_int16_t yy_chk[3639] =
|
|||
***************************/
|
||||
#line 12 "flex_lexer.l"
|
||||
|
||||
#include "../sqltypes.h"
|
||||
#include "../sql/Expr.h"
|
||||
#include "bison_parser.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
***************************/
|
||||
%{
|
||||
|
||||
#include "../sqltypes.h"
|
||||
#include "../sql/Expr.h"
|
||||
#include "bison_parser.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef __PREPARE_STATEMENT_H__
|
||||
#define __PREPARE_STATEMENT_H__
|
||||
|
||||
#include "../SQLParserResult.h"
|
||||
#include "SQLStatement.h"
|
||||
#include "SelectStatement.h"
|
||||
#include <algorithm>
|
||||
|
@ -42,7 +43,7 @@ struct PrepareStatement : SQLStatement {
|
|||
}
|
||||
|
||||
const char* name;
|
||||
SQLStatementList* query;
|
||||
SQLParserResult* query;
|
||||
std::vector<Expr*> placeholders;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,18 +1,13 @@
|
|||
/*
|
||||
* SQLStatement.h
|
||||
* Definition of the structure used to build the syntax tree.
|
||||
*/
|
||||
#ifndef __STATEMENT_H__
|
||||
#define __STATEMENT_H__
|
||||
#ifndef __SQLSTATEMENT_H__
|
||||
#define __SQLSTATEMENT_H__
|
||||
|
||||
#include "Expr.h"
|
||||
#include <vector>
|
||||
|
||||
namespace hsql {
|
||||
|
||||
|
||||
typedef enum {
|
||||
kStmtError, // Unused
|
||||
kStmtError, // unused
|
||||
kStmtSelect,
|
||||
kStmtImport,
|
||||
kStmtInsert,
|
||||
|
@ -29,8 +24,7 @@ typedef enum {
|
|||
|
||||
|
||||
/**
|
||||
* @struct SQLStatement
|
||||
* @brief Base class for every SQLStatement
|
||||
* Base struct for every SQL statement
|
||||
*/
|
||||
struct SQLStatement {
|
||||
SQLStatement(StatementType type) :
|
||||
|
@ -44,43 +38,6 @@ private:
|
|||
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
|
||||
|
||||
#endif // __STATEMENT_H__
|
||||
#endif // __SQLSTATEMENT_H__
|
||||
|
|
|
@ -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__
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
|
||||
#include "sqltypes.h"
|
||||
#include "sql/statements.h"
|
||||
|
||||
namespace hsql {
|
||||
|
||||
|
|
|
@ -3,14 +3,4 @@
|
|||
|
||||
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
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
|
||||
#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_EQ(output_var->numStatements(), num_statements);
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
|
|||
start = std::chrono::system_clock::now();
|
||||
|
||||
// Parsing
|
||||
SQLStatementList* stmt_list = SQLParser::parseSQLString(sql.c_str());
|
||||
SQLParserResult* stmt_list = SQLParser::parseSQLString(sql.c_str());
|
||||
|
||||
end = std::chrono::system_clock::now();
|
||||
std::chrono::duration<double> elapsed_seconds = end-start;
|
||||
|
|
|
@ -11,7 +11,7 @@ using namespace hsql;
|
|||
|
||||
|
||||
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_EQ(stmt_list->numStatements(), 1);
|
||||
ASSERT(stmt_list->getStatement(0)->type() == kStmtDelete);
|
||||
|
@ -25,7 +25,7 @@ TEST(DeleteStatementTest) {
|
|||
}
|
||||
|
||||
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_EQ(stmt_list->numStatements(), 1);
|
||||
ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtCreate);
|
||||
|
@ -47,7 +47,7 @@ TEST(CreateStatementTest) {
|
|||
|
||||
|
||||
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_EQ(stmt_list->numStatements(), 1);
|
||||
ASSERT_EQ(stmt_list->getStatement(0)->type(), kStmtUpdate);
|
||||
|
|
Loading…
Reference in New Issue