Repository navigation
Expand file tree
/
Copy pathsqlhelper.cpp
More file actions
208 lines (186 loc) · 6.63 KB
/
Copy pathsqlhelper.cpp
File metadata and controls
208 lines (186 loc) · 6.63 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
#include "sqlhelper.h"
#include <stdio.h>
#include <string>
namespace hsql {
void printOperatorExpression(Expr* expr, uint numIndent);
std::string indent(uint numIndent) {
return std::string(numIndent, '\t');
}
void inprint(int64_t val, uint numIndent) {
printf("%s%ld \n", indent(numIndent).c_str(), val);
}
void inprint(float val, uint numIndent) {
printf("%s%f\n", indent(numIndent).c_str(), val);
}
void inprint(const char* val, uint numIndent) {
printf("%s%s\n", indent(numIndent).c_str(), val);
}
void inprint(const char* val, const char* val2, uint numIndent) {
printf("%s%s->%s\n", indent(numIndent).c_str(), val, val2);
}
void inprintC(char val, uint numIndent) {
printf("%s%c\n", indent(numIndent).c_str(), val);
}
void inprintU(uint64_t val, uint numIndent) {
printf("%s%lu\n", indent(numIndent).c_str(), val);
}
void printTableRefInfo(TableRef* table, uint 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 != NULL) {
inprint("Alias", numIndent+1);
inprint(table->alias, numIndent+2);
}
}
void printOperatorExpression(Expr* expr, uint numIndent) {
if (expr == NULL) {
inprint("null", numIndent);
return;
}
switch (expr->op_type) {
case Expr::SIMPLE_OP:
inprintC(expr->op_char, numIndent);
break;
case Expr::AND:
inprint("AND", numIndent);
break;
case Expr::OR:
inprint("OR", numIndent);
break;
case Expr::NOT:
inprint("NOT", numIndent);
break;
default:
inprintU(expr->op_type, numIndent);
break;
}
printExpression(expr->expr, numIndent+1);
if (expr->expr2 != NULL) printExpression(expr->expr2, numIndent+1);
}
void printExpression(Expr* expr, uint 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);
inprint(expr->expr->name, numIndent+1);
break;
case kExprOperator:
printOperatorExpression(expr, numIndent);
break;
default:
fprintf(stderr, "Unrecognized expression type %d\n", expr->type);
return;
}
if (expr->alias != NULL) {
inprint("Alias", numIndent+1);
inprint(expr->alias, numIndent+2);
}
}
void printSelectStatementInfo(SelectStatement* stmt, uint 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 != NULL) {
inprint("Search Conditions:", numIndent+1);
printExpression(stmt->whereClause, numIndent+2);
}
if (stmt->unionSelect != NULL) {
inprint("Union:", numIndent+1);
printSelectStatementInfo(stmt->unionSelect, numIndent+2);
}
if (stmt->order != NULL) {
inprint("OrderBy:", numIndent+1);
printExpression(stmt->order->expr, numIndent+2);
if (stmt->order->type == kOrderAsc) inprint("ascending", numIndent+2);
else inprint("descending", numIndent+2);
}
if (stmt->limit != NULL) {
inprint("Limit:", numIndent+1);
inprint(stmt->limit->limit, numIndent+2);
}
}
void printImportStatementInfo(ImportStatement* stmt, uint numIndent) {
inprint("ImportStatment", numIndent);
inprint(stmt->filePath, numIndent+1);
inprint(stmt->tableName, numIndent+1);
}
void printCreateStatementInfo(CreateStatement* stmt, uint numIndent) {
inprint("CreateStatment", numIndent);
inprint(stmt->tableName, numIndent+1);
inprint(stmt->filePath, numIndent+1);
}
void printInsertStatementInfo(InsertStatement* stmt, uint numIndent) {
inprint("InsertStatment", numIndent);
inprint(stmt->tableName, numIndent+1);
if (stmt->columns != NULL) {
inprint("Columns", numIndent+1);
for (char* col_name : *stmt->columns) {
inprint(col_name, numIndent+2);
}
}
switch (stmt->type) {
case InsertStatement::kInsertValues:
inprint("Values", numIndent+1);
for (Expr* expr : *stmt->values) {
printExpression(expr, numIndent+2);
}
break;
case InsertStatement::kInsertSelect:
printSelectStatementInfo(stmt->select, numIndent+1);
break;
}
}
void printStatementInfo(SQLStatement* stmt) {
switch (stmt->type()) {
case kStmtSelect:
printSelectStatementInfo((SelectStatement*) stmt, 0);
break;
case kStmtInsert:
printInsertStatementInfo((InsertStatement*) stmt, 0);
break;
case kStmtCreate:
printCreateStatementInfo((CreateStatement*) stmt, 0);
break;
case kStmtImport:
printImportStatementInfo((ImportStatement*) stmt, 0);
break;
default:
break;
}
}
} // namespace hsql