Repository navigation
Expand file tree
/
Copy pathsqlhelper.cpp
More file actions
213 lines (191 loc) · 6.36 KB
/
Copy pathsqlhelper.cpp
File metadata and controls
213 lines (191 loc) · 6.36 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "sqlhelper.h"
#include <iostream>
#include <string>
namespace hsql {
void printOperatorExpression(Expr* expr, uintmax_t numIndent);
std::string indent(uintmax_t numIndent) {
return std::string(numIndent, '\t');
}
void inprint(int64_t val, uintmax_t numIndent) {
std::cout << indent(numIndent).c_str() << val << " " << std::endl;
}
void inprint(float val, uintmax_t numIndent) {
std::cout << indent(numIndent).c_str() << val << std::endl;
}
void inprint(const char* val, uintmax_t numIndent) {
std::cout << indent(numIndent).c_str() << val << std::endl;
}
void inprint(const char* val, const char* val2, uintmax_t numIndent) {
std::cout << indent(numIndent).c_str() << val << "->" << val2 << std::endl;
}
void inprintC(char val, uintmax_t numIndent) {
std::cout << indent(numIndent).c_str() << val << std::endl;
}
void inprintU(uint64_t val, uintmax_t numIndent) {
std::cout << indent(numIndent).c_str() << val << std::endl;
}
void printTableRefInfo(TableRef* table, uintmax_t numIndent) {
switch (table->type) {
case kTableName:
inprint(table->name, numIndent);
break;
case kTableSelect:
printSelectStatementInfo(table->select, numIndent);
break;
case kTableJoin:
inprint("Join Table", numIndent);
inprint("Left", numIndent + 1);
printTableRefInfo(table->join->left, numIndent + 2);
inprint("Right", numIndent + 1);
printTableRefInfo(table->join->right, numIndent + 2);
inprint("Join Condition", numIndent + 1);
printExpression(table->join->condition, numIndent + 2);
break;
case kTableCrossProduct:
for (TableRef* tbl : *table->list) printTableRefInfo(tbl, numIndent);
break;
}
if (table->alias != nullptr) {
inprint("Alias", numIndent + 1);
inprint(table->alias, numIndent + 2);
}
}
void printOperatorExpression(Expr* expr, uintmax_t numIndent) {
if (expr == nullptr) {
inprint("null", numIndent);
return;
}
switch (expr->opType) {
case kOpAnd:
inprint("AND", numIndent);
break;
case kOpOr:
inprint("OR", numIndent);
break;
case kOpNot:
inprint("NOT", numIndent);
break;
default:
inprintU(expr->opType, numIndent);
break;
}
printExpression(expr->expr, numIndent + 1);
if (expr->expr2 != nullptr) printExpression(expr->expr2, numIndent + 1);
}
void printExpression(Expr* expr, uintmax_t numIndent) {
switch (expr->type) {
case kExprStar:
inprint("*", numIndent);
break;
case kExprColumnRef:
inprint(expr->name, numIndent);
break;
// case kExprTableColumnRef: inprint(expr->table, expr->name, numIndent); break;
case kExprLiteralFloat:
inprint(expr->fval, numIndent);
break;
case kExprLiteralInt:
inprint(expr->ival, numIndent);
break;
case kExprLiteralString:
inprint(expr->name, numIndent);
break;
case kExprFunctionRef:
inprint(expr->name, numIndent);
for (Expr* e : *expr->exprList) inprint(e->name, numIndent + 1);
break;
case kExprOperator:
printOperatorExpression(expr, numIndent);
break;
default:
std::cerr << "Unrecognized expression type " << expr->type << std::endl;
return;
}
if (expr->alias != nullptr) {
inprint("Alias", numIndent + 1);
inprint(expr->alias, numIndent + 2);
}
}
void printSelectStatementInfo(const SelectStatement* stmt, uintmax_t numIndent) {
inprint("SelectStatement", numIndent);
inprint("Fields:", numIndent + 1);
for (Expr* expr : *stmt->selectList) printExpression(expr, numIndent + 2);
inprint("Sources:", numIndent + 1);
printTableRefInfo(stmt->fromTable, numIndent + 2);
if (stmt->whereClause != nullptr) {
inprint("Search Conditions:", numIndent + 1);
printExpression(stmt->whereClause, numIndent + 2);
}
if (stmt->groupBy != nullptr) {
inprint("GroupBy:", numIndent + 1);
for (Expr* expr : *stmt->groupBy->columns) printExpression(expr, numIndent + 2);
if (stmt->groupBy->having != nullptr) {
inprint("Having:", numIndent + 1);
printExpression(stmt->groupBy->having, numIndent + 2);
}
}
if (stmt->unionSelect != nullptr) {
inprint("Union:", numIndent + 1);
printSelectStatementInfo(stmt->unionSelect, numIndent + 2);
}
if (stmt->order != nullptr) {
inprint("OrderBy:", numIndent + 1);
printExpression(stmt->order->at(0)->expr, numIndent + 2);
if (stmt->order->at(0)->type == kOrderAsc) inprint("ascending", numIndent + 2);
else inprint("descending", numIndent + 2);
}
if (stmt->limit != nullptr) {
inprint("Limit:", numIndent + 1);
inprint(stmt->limit->limit, numIndent + 2);
}
}
void printImportStatementInfo(const ImportStatement* stmt, uintmax_t numIndent) {
inprint("ImportStatment", numIndent);
inprint(stmt->filePath, numIndent + 1);
inprint(stmt->tableName, numIndent + 1);
}
void printCreateStatementInfo(const CreateStatement* stmt, uintmax_t numIndent) {
inprint("CreateStatment", numIndent);
inprint(stmt->tableName, numIndent + 1);
inprint(stmt->filePath, numIndent + 1);
}
void printInsertStatementInfo(const InsertStatement* stmt, uintmax_t numIndent) {
inprint("InsertStatment", numIndent);
inprint(stmt->tableName, numIndent + 1);
if (stmt->columns != nullptr) {
inprint("Columns", numIndent + 1);
for (char* col_name : *stmt->columns) {
inprint(col_name, numIndent + 2);
}
}
switch (stmt->type) {
case kInsertValues:
inprint("Values", numIndent + 1);
for (Expr* expr : *stmt->values) {
printExpression(expr, numIndent + 2);
}
break;
case kInsertSelect:
printSelectStatementInfo(stmt->select, numIndent + 1);
break;
}
}
void printStatementInfo(const SQLStatement* stmt) {
switch (stmt->type()) {
case kStmtSelect:
printSelectStatementInfo((const SelectStatement*) stmt, 0);
break;
case kStmtInsert:
printInsertStatementInfo((const InsertStatement*) stmt, 0);
break;
case kStmtCreate:
printCreateStatementInfo((const CreateStatement*) stmt, 0);
break;
case kStmtImport:
printImportStatementInfo((const ImportStatement*) stmt, 0);
break;
default:
break;
}
}
} // namespace hsql