8000 Adding Initial sql-parser code borrowed from ( https://github.com/hyr… · davinash/arangodb@b9b4083 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9b4083

Browse files
author
adongre
committed
Adding Initial sql-parser code borrowed from ( https://github.com/hyrise/sql-parser )
1 parent 7d34c5c commit b9b4083

40 files changed

+12532
-0
lines changed

arangod/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,39 @@ SET(ARANGOD_SOURCES
552552
VocBase/ManagedDocumentResult.cpp
553553
VocBase/ticks.cpp
554554
VocBase/vocbase.cpp
555+
SqlAql/parser/bison_parser.cpp
556+
SqlAql/parser/bison_parser.h
557+
SqlAql/parser/flex_lexer.cpp
558+
SqlAql/parser/flex_lexer.h
559+
SqlAql/parser/parser_typedef.h
560+
SqlAql/sql/CreateStatement.h
561+
SqlAql/sql/DeleteStatement.h
562+
SqlAql/sql/DropStatement.h
563+
SqlAql/sql/ExecuteStatement.h
564+
SqlAql/sql/Expr.cpp
565+
SqlAql/sql/Expr.h
566+
SqlAql/sql/ImportStatement.h
567+
SqlAql/sql/InsertStatement.h
568+
SqlAql/sql/PrepareStatement.cpp
569+
SqlAql/sql/PrepareStatement.h
570+
SqlAql/sql/SelectStatement.h
571+
SqlAql/sql/ShowStatement.h
572+
SqlAql/sql/SQLStatement.cpp
573+
SqlAql/sql/SQLStatement.h
574+
SqlAql/sql/statements.cpp
575+
SqlAql/sql/statements.h
576+
SqlAql/sql/Table.h
577+
SqlAql/sql/UpdateStatement.h
578+
SqlAql/util/sqlhelper.cpp
579+
SqlAql/util/sqlhelper.h
580+
SqlAql/SQLParser.cpp
581+
SqlAql/SQLParser.h
582+
SqlAql/SQLParserResult.cpp
583+
SqlAql/SQLParserResult.h
584+
SqlAql/aql/SQLStatementToAQL.cpp
585+
SqlAql/aql/SQLStatementToAQL.h
586+
SqlAql/aql/AQLSelectStatement.cpp
587+
SqlAql/aql/AQLSelectStatement.h
555588
${ADDITIONAL_BIN_ARANGOD_SOURCES}
556589
)
557590

arangod/SqlAql/SQLParser.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
#include "SQLParser.h"
3+
#include "parser/bison_parser.h"
4+
#include "parser/flex_lexer.h"
5+
#include <stdio.h>
6+< F438 div class="diff-text-inner">#include <string>
7+
8+
namespace hsql {
9+
10+
SQLParser::SQLParser() {
11+
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
12+
}
13+
14+
// static
15+
bool SQLParser::parse(const std::string& sql, SQLParserResult* result) {
16+
yyscan_t scanner;
17+
YY_BUFFER_STATE state;
18+
19+
if (hsql_lex_init(&scanner)) {
20+
// Couldn't initialize the lexer.
21+
fprintf(stderr, "SQLParser: Error when initializing lexer!\n");
22+
return false;
23+
}
24+
const char* text = sql.c_str();
25+
state = hsql__scan_string(text, scanner);
26+
27+
// Parse the tokens.
28+
// If parsing fails, the result will contain an error object.
29+
int ret = hsql_parse(result, scanner);
30+
bool success = (ret == 0);
31+
result->setIsValid(success);
32+
33+
hsql__delete_buffer(state, scanner);
34+
hsql_lex_destroy(scanner);
35+
36+
return true;
37+
}
38+
39+
// static
40+
bool SQLParser::parseSQLString(const char* sql, SQLParserResult* result) {
41+
return parse(sql, result);
42+
}
43+
44+
bool SQLParser::parseSQLString(const std::string& sql, SQLParserResult* result) {
45+
return parse(sql, result);
46+
}
47+
48+
// static
49+
10000 bool SQLParser::tokenize(const std::string& sql, std::vector<int16_t>* tokens) {
50+
// Initialize the scanner.
51+
yyscan_t scanner;
52+
if (hsql_lex_init(&scanner)) {
53+
fprintf(stderr, "SQLParser: Error when initializing lexer!\n");
54+
return false;
55+
}
56+
57+
YY_BUFFER_STATE state;
58+
state = hsql__scan_string(sql.c_str(), scanner);
59+
60+
YYSTYPE yylval;
61+
YYLTYPE yylloc;
62+
63+
// Step through the string until EOF is read.
64+
// Note: hsql_lex returns int, but we know that its range is within 16 bit.
65+
int16_t token = hsql_lex(&yylval, &yylloc, scanner);
66+
while (token != 0) {
67+
tokens->push_back(token);
68+
token = hsql_lex(&yylval, &yylloc, scanner);
69+
70+
if (token == SQL_IDENTIFIER || token == SQL_STRING) {
71+
free(yylval.sval);
72+
}
73+
}
74+
75+
hsql__delete_buffer(state, scanner);
76+
hsql_lex_destroy(scanner);
77+
return true;
78+
}
79+
80+
} // namespace hsql

arangod/SqlAql/SQLParser.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef SQLPARSER_SQLPARSER_H
2+
#define SQLPARSER_SQLPARSER_H
3+
4+
#include "SQLParserResult.h"
5+
#include "sql/statements.h"
6+
7+
namespace hsql {
8+
9+
// Static methods used to parse SQL strings.
10+
class SQLParser {
11+
public:
12+
13+
// Parses a given constant character SQL string into the result object.
14+
// Returns true if the lexer and parser could run without internal errors.
15+
// This does NOT mean that the SQL string was valid SQL. To check that
16+
// you need to check result->isValid();
17+
static bool parse(const std::string& sql, SQLParserResult* result);
18+
19+
// Run tokenization on the given string and store the tokens in the output vector.
20+
static bool tokenize(const std::string& sql, std::vector<int16_t>* tokens);
21+
22+
// Deprecated.
23+
// Old method to parse SQL strings. Replaced by parse().
24+
static bool parseSQLString(const char* sql, SQLParserResult* result);
25+
26+
// Deprecated.
27+
// Old method to parse SQL strings. Replaced by parse().
28+
static bool parseSQLString(const std::string& sql, SQLParserResult* result);
29+
30+
private:
31+
SQLParser();
32+
};
33+
34+
35+
} // namespace hsql
36+
37+
38+
#endif

arangod/SqlAql/SQLParserResult.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
2+
#include "SQLParserResult.h"
3+
#include <algorithm>
4+
5+
namespace hsql {
6+
7+
SQLParserResult::SQLParserResult() :
8+
isValid_(false),
9+
errorMsg_(nullptr) {};
10+
11+
SQLParserResult::SQLParserResult(SQLStatement* stmt) :
12+
isValid_(false),
13+
errorMsg_(nullptr) {
14+
addStatement(stmt);
15+
};
16+
17+
// Move constructor.
18+
SQLParserResult::SQLParserResult(SQLParserResult&& moved) {
19+
isValid_ = moved.isValid_;
20+
errorMsg_ = moved.errorMsg_;
21+
statements_ = std::move(moved.statements_);
22+
23+
moved.errorMsg_ = nullptr;
24+
moved.reset();
25+
}
26+
27+
SQLParserResult::~SQLParserResult() {
28+
reset();
29+
}
30+
31+
void SQLParserResult::addStatement(SQLStatement* stmt) {
32+
statements_.push_back(stmt);
33+
}
34+
35+
const SQLStatement* SQLParserResult::getStatement(int index) const {
36+
return statements_[index];
37+
}
38+
39+
SQLStatement* SQLParserResult::getMutableStatement(int index) {
40+
return statements_[index];
41+
}
42+
43+
size_t SQLParserResult::size() const {
44+
return statements_.size();
45+
}
46+
47+
bool SQLParserResult::isValid() const {
48+
return isValid_;
49+
}
50+
51+
const char* SQLParserResult::errorMsg() const {
52+
return errorMsg_;
53+
}
54+
55+
int SQLParserResult::errorLine() const {
56+
return errorLine_;
57+
}
58+
59+
int SQLParserResult::errorColumn() const {
60+
return errorColumn_;
61+
}
62+
63+
void SQLParserResult::setIsValid(bool isValid) {
64+
isValid_ = isValid;
65+
}
66+
67+
void SQLParserResult::setErrorDetails(char* errorMsg, int errorLine, int errorColumn) {
68+
errorMsg_ = errorMsg;
69+
errorLine_ = errorLine;
70+
errorColumn_ = errorColumn;
71+
}
72+
73+
const std::vector<SQLStatement*>& SQLParserResult::getStatements() const {
74+
return statements_;
75+
}
76+
77+
std::vector<SQLStatement*> SQLParserResult::releaseStatements() {
78+
std::vector<SQLStatement*> copy = statements_;
79+
80+
statements_.clear();
81+
82+
return copy;
83+
}
84+
85+
void SQLParserResult::reset() {
86+
for (SQLStatement* statement : statements_) {
87+
delete statement;
88+
}
89+
statements_.clear();
90+
91+
isValid_ = false;
92+
93+
free(errorMsg_);
94+
errorMsg_ = nullptr;
95+
errorLine_ = -1;
96+
errorColumn_ = -1;
97+
}
98+
99+
// Does NOT take ownership.
100+
void SQLParserResult::addParameter(Expr* parameter) {
101+
parameters_.push_back(parameter);
102+
std::sort(parameters_.begin(), parameters_.end(),
103+
[](const Expr * a, const Expr * b) {
104+
return a->ival < b->ival;
105+
});
106+
}
107+
108+
const std::vector<Expr*>& SQLParserResult::parameters() {
109+
return parameters_;
110+
}
111+
112+
} // namespace hsql

arangod/SqlAql/SQLParserResult.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#ifndef SQLPARSER_SQLPARSER_RESULT_H
2+
#define SQLPARSER_SQLPARSER_RESULT_H
3+
4+
#include "sql/SQLStatement.h"
5+
6+
namespace hsql {
7+
// Represents the result of the SQLParser.
8+
// If parsing was successful it contains a list of SQLStatement.
9+
class SQLParserResult {
10+
public:
11+
// Initialize with empty statement list.
12+
SQLParserResult();
13+
14+
// Initialize with a single statement.
15+
// Takes ownership of the statement.
16+
SQLParserResult(SQLStatement* stmt);
17+
18+
// Move constructor.
19+
SQLParserResult(SQLParserResult&& moved);
20+
21+
// Deletes all statements in the result.
22+
virtual ~SQLParserResult();
23+
24+
// Set whether parsing was successful.
25+
void setIsValid(bool isValid);
26+
27+
// Returns true if parsing was successful.
28+
bool isValid() const;
29+
30+
// Returns the number of statements in the result.
31+
size_t size() const;
32+
33+
// Set the details of the error, if available.
34+
// Takes ownership of errorMsg.
35+
void setErrorDetails(char* errorMsg, int errorLine, int errorColumn);
36+
37+
// Returns the error message, if an error occurred.
38+
const char* errorMsg() const;
39+
40+
// Returns the line number of the occurrance of the error in the query.
41+
int errorLine() const;
42+
43+
// Returns the column number of the occurrance of the error in the query.
44+
int errorColumn() const;
45+
46+
// Adds a statement to the result list of statements.
47+
// SQLParserResult takes ownership of the statement.
48+
void addStatement(SQLStatement* stmt);
49+
50+
// Gets the SQL statement with the given index.
51+
const SQLStatement* getStatement(int index) const;
52+
53+
// Gets the non const SQL statement with the given index.
54+
SQLStatement* getMutableStatement(int index);
55+
56+
// Get the list of all statements.
57+
const std::vector<SQLStatement*>& getStatements() const;
58+
59+
// Returns a copy of the list of all statements in this result.
60+
// Removes them from this result.
61+
std::vector<SQLStatement*> releaseStatements();
62+
63+
// Deletes all statements and other data within the result.
64+
void reset();
65+
66+
// Does NOT take ownership.
67+
void addParameter(Expr* parameter);
68+
69+
const std::vector<Expr*>& parameters();
70+
71+
private:
72+
// List of statements within the result.
73+
std::vector<SQLStatement*> statements_;
74+
75+
// Flag indicating the parsing was successful.
76+
bool isValid_;
77+
78+
// Error message, if an error occurred.
79 BD94 +
char* errorMsg_;
80+
81+
// Line number of the occurrance of the error in the query.
82+
int errorLine_;
83+
84+
// Column number of the occurrance of the error in the query.
85+
int errorColumn_;
86+
87+
// Does NOT have ownership.
88+
std::vector<Expr*> parameters_;
89+
};
90+
91+
} // namespace hsql
92+
93+
#endif // SQLPARSER_SQLPARSER_RESULT_H
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Created by adongre on 10/19/18.
3+
//
4+
5+
#include "AQLSelectStatement.h"
6+
7+
namespace hsql {
8+
9+
AQLSelectStatement::AQLSelectStatement() :
10+
tableName(""),
11+
pColumnProjections(new std::vector<std::string>()),
12+
whereClauseExists(false),
13+
groupByExists(false) {};
14+
15+
AQLSelectStatement::~AQLSelectStatement() {
16+
delete pColumnProjections;
17+
}
18+
19+
std::string AQLSelectStatement::createAQLQuery() {
20+
std::string aqlQuery = "FOR U IN " + tableName;
21+
if (false == whereClauseExists && pColumnProjections->size() == 0) {
22+
}
23+
return aqlQuery;
24+
}
25+
}

0 commit comments

Comments
 (0)
0