-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSQLParser.h
More file actions
95 lines (73 loc) · 2.85 KB
/
SQLParser.h
File metadata and controls
95 lines (73 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Created by 张程易 on 2017/3/21.
//
#ifndef DB_SQLPARSER_H
#define DB_SQLPARSER_H
#include "Common.h"
#include "SQLSession.h"
#include "QueryPlans/SelectQueryPlan.h"
#include "QueryPlans/InsertQueryPlan.h"
#include "QueryPlans/DDL/CreateQueryPlan.h"
#include "QueryPlans/DDL/CreateTableQueryPlan.h"
#include "QueryPlans/DDL/CreateDataBaseQueryPlan.h"
#include "Scanners/QueryScanner.h"
#include "Scanners/LinearQueryScanner.h"
#include "Scanners/OneIndexQueryScanner.h"
#include "Scanners/RangeIndexQueryScanner.h"
#include "QueryPlans/SQLCondition/SQLConditionFactory.h"
#include "QueryPlans/DDL/DropQueryPlan.h"
#include "QueryPlans/DeleteQueryPlan.h"
class SQLParser{
SQLSession * sqlSession;
const char * str = nullptr;// = "create table test2( id int, value char(20), primary key(id) );";
//const char * str = "insert into test (id, value) values (20, '20');";
//const char * str = "select id, value from test where value = '20' and value = 'F';";
AbstractSQLConditionFactory * IntegerSQLConditionFactory = new SQLConditionFactory<int>();
AbstractSQLConditionFactory * DoubleSQLConditionFactory = new SQLConditionFactory<double>();
AbstractSQLConditionFactory * CharSQLConditionFactory = new SQLConditionFactory<char *>();
public:
SQLParser(SQLSession *sqlSession) : sqlSession(sqlSession) {}
~SQLParser(){
delete IntegerSQLConditionFactory;
delete DoubleSQLConditionFactory;
delete CharSQLConditionFactory;
}
enum class TokenType {
integer, real, name, string, ope, null, __keyword
};
struct Token {
int begin, end;
TokenType type;
Token(int begin_,int end_, TokenType type_)
:begin(begin_),end(end_),type(type_){}
};
std::vector<Token> tokens;
int pos = 0;
inline bool isNum(char x) {
return x >= '0' && x <= '9';
}
inline bool isChar(char x) {
return (x >= 'a' && x <= 'z') || (x >= 'A' && x <='Z') || x == '_' || x == '-';
}
void tokenize();
bool tokencmp(Token & token, const char * target);
std::list<AbstractSQLCondition *> * parseWhereClause(TableModel * tableModel);
std::pair<QueryScanner *, SQLWhereClause *>
getQueryScanner(TableModel * table, std::list<AbstractSQLCondition *> * list);
SelectQueryPlan * parseSelectStatement();
InsertQueryPlan * parseInsertStatement();
void parseCreateDefinition(JSONArray * defJson, JSONObject * indexJson);
CreateQueryPlan * parseCreateStatement();
DeleteQueryPlan * parseDeleteStatement();
QueryPlan * parseSQLStatement(const char * str);
DropQueryPlan *parseDropStatement() ;
inline void test(){
for(auto & x: tokens){
for(int i = x.begin; i<= x.end; i++){
putchar(str[i]);
}
printf("%d\n", x.end - x.begin + 1);
}
}
};
#endif //DB_SQLPARSER_H