-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilestone1.cpp
More file actions
378 lines (309 loc) · 8.49 KB
/
Copy pathMilestone1.cpp
File metadata and controls
378 lines (309 loc) · 8.49 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include "SQLParser.h"
#include "sql/SelectStatement.h"
#include "sql/CreateStatement.h"
#include "sql/Table.h"
#include "sql/Expr.h"
#include "db_cxx.h"
using namespace hsql;
using namespace std;
/*
* ======================================================================
* Function Declarations
* ======================================================================
*/
std::string printExpression(Expr* expr);
std::string printSelectStatement(const SelectStatement* stmt);
std::string printTableInfo(TableRef* table);
/*
* ======================================================================
* Print table information
* ======================================================================
*/
std::string printTableInfo(TableRef* table) {
std::string tableStr = "";
std::string leftTableName;
std::string rightTableName;
std::string joinCondition;
std::string joinType;
switch (table->type) {
case kTableName:
tableStr += table->name;
break;
case kTableSelect:
tableStr += printSelectStatement(table->select);
break;
case kTableJoin: {
leftTableName = printTableInfo(table->join->left);
rightTableName = printTableInfo(table->join->right);
joinCondition = printExpression(table->join->condition);
switch (table->join->type) {
case kJoinInner: joinType = "INNER JOIN"; break;
case kJoinOuter: joinType = "OUTER JOIN"; break;
case kJoinLeft: joinType = "LEFT JOIN"; break;
case kJoinRight: joinType = "RIGHT JOIN"; break;
case kJoinLeftOuter: joinType = "LEFT OUTER JOIN"; break;
case kJoinRightOuter: joinType = "RIGHT OUTER JOIN"; break;
case kJoinCross: joinType = "CROSS JOIN"; break;
case kJoinNatural: joinType = "NATURAL JOIN"; break;
}
tableStr += leftTableName
+ " " + joinType
+ " " + rightTableName
+ " ON " + joinCondition;
joinType.clear();
leftTableName.clear();
rightTableName.clear();
joinCondition.clear();
break;
}
case kTableCrossProduct:
for (TableRef* temp : *table->list)
tableStr += printTableInfo(temp);
break;
}
if (table->alias != NULL)
tableStr += " AS " + std::string(table->alias) + " ";
return tableStr;
}
/*
* ======================================================================
* Print Operator information
* ======================================================================
*/
std::string printOperatorExpression(Expr* expr) {
std::string opStr = "";
std::ostringstream oss;
if (expr == NULL) {
opStr == "";
}
//print LHS
opStr.append(printExpression(expr->expr));
//print operator
switch (expr->opType) {
case Expr::SIMPLE_OP:
opStr += expr->opChar;
break;
case Expr::AND:
opStr.append("AND ");
break;
case Expr::OR:
opStr.append("OR ");
break;
case Expr::NOT:
opStr.append("NOT ");
break;
default:
oss << expr->opType;
opStr.append(oss.str());
cout <<"Default "<< oss.str() << endl;
oss.clear();
break;
}
//Print RHS
if (expr->expr2 != NULL)
opStr.append(printExpression(expr->expr2));
return opStr;
}
/*
* ======================================================================
* Print Operator information
* ======================================================================
*/
std::string printExpression(Expr* expr) {
std:string exprString = "";
std::ostringstream oss;
switch (expr->type) {
case kExprStar:
exprString.append("* ");
break;
case kExprColumnRef: {
if (expr->table != NULL) { //check if we need to insert table name
exprString.append(expr->table);
exprString.append(".");
}
exprString.append(expr->name);
break;
}
case kExprLiteralFloat:
oss << expr->fval;
exprString.append(oss.str());
oss.clear();
break;
case kExprLiteralInt:
oss << expr->ival;
exprString.append(oss.str());
oss.clear();
break;
case kExprLiteralString:
exprString.append(expr->name);
break;
case kExprFunctionRef:
exprString.append(expr->name);
exprString.append(expr->expr->name);
break;
case kExprOperator:
exprString.append(printOperatorExpression(expr));
break;
default:
fprintf(stderr, "Unrecognized expression type %d\n", expr->type);
return "ERROR";
}
if (expr->alias != NULL) {
exprString.append("as ");
exprString.append(expr->alias);
}
return exprString;
}
/*
* ======================================================================
* Print Select statement
* ======================================================================
*/
string printSelectStatement(const SelectStatement* stmt) {
std::string output = "";
output.append("SELECT ");
//print fields
for (Expr* expr : *stmt->selectList) {
output.append(printExpression(expr));
output.append(", ");
}
//cleanup last occurence of ','
string::size_type pos = output.find_last_of(",");
if (pos != string::npos)
{
output.erase(pos);
}
//print "FROM" clause
output.append(" FROM ");
output.append(printTableInfo(stmt->fromTable));
output.append(" ");
//check "WHERE" clause
if (stmt->whereClause != NULL) {
output.append("WHERE ");
output.append(printExpression(stmt->whereClause));
output.append(" ");
}
//Check union
if (stmt->unionSelect != NULL) {
output.append("\n\tUNION\n");
output.append("\t " + printSelectStatement(stmt->unionSelect));
output.append(" ");
}
//Check Order BY
if (stmt->order != NULL) {
output.append("ORDER BY ");
output.append(
printExpression(stmt->order->at(0)->expr)
);
if (stmt->order->at(0)->type == kOrderAsc)
output.append(" ascending ");
else
output.append(" descending ");
}
return output;
}
/*
* ======================================================================
* Print Create statement
* ======================================================================
*/
std::string printCreateStatement(const CreateStatement* stmt2) {
std::string output;
output = "CREATE TABLE " + std::string(stmt2->tableName);
output.append(" (");
//iterate over columns
std::vector<ColumnDefinition*>* cols = stmt2->columns;
std::vector<ColumnDefinition*>::iterator iter;
for (iter = cols->begin(); iter != cols->end(); iter++) {
output += std::string((*iter)->name) + " ";
switch ((*iter)->type) {
case 0: output += "unknown, "; break;
case 1: output += "text, "; break;
case 2: output += "int, "; break;
case 3: output += "double, "; break;
}
}
//cleanup last occurence of ','
string::size_type pos = output.find_last_of(",");
if (pos != string::npos)
{
output.erase(pos);
}
output.append(" )");
return output;
}
/*
* ======================================================================
* Generic Print statement
* ======================================================================
*/
void printStatement(const SQLStatement* stmt, StatementType stmtType) {
switch (stmtType) {
case kStmtSelect: {
cout<<printSelectStatement((const SelectStatement*) stmt)<<endl;
break;
}
case kStmtCreate: {
cout << printCreateStatement((const CreateStatement*)stmt) << endl;
break;
}
default: {
cout << "Not Supported" << endl;
break;
}
}
}
/*
* ======================================================================
* Execute Method
* ======================================================================
*/
void execute(const SQLStatement* stmt) {
printStatement(stmt, stmt->type());
}
/*
* ======================================================================
* Main
* ======================================================================
*/
int main()
{
const char *HOME = "cpsc4300/data";
const char *EXAMPLE = "temp.db";
const unsigned int BLOCK_SZ = 4096;
const char *home = std::getenv("HOME");
std::string envdir = std::string(home) + "/" + HOME;
DbEnv env(0U);
env.set_message_stream(&std::cout);
env.set_error_stream(&std::cerr);
env.open(envdir.c_str(), DB_CREATE | DB_INIT_MPOOL, 0);
cout << "Running with database environment at " << envdir.c_str() << endl;
std::string query;
cout << "Type 'quit' to quit."<<endl;
while (true) {
query.clear();
cout << "\nSQL> ";
getline(cin, query);
if (query == "quit") {
return 0;
}
hsql::SQLParserResult* result = hsql::SQLParser::parseSQLString(query);
if (result->isValid()) {
for (int i = 0; i < result->size(); i++) {
execute(result->getStatement(i));
}
}
else {
cout << "Invalid query : " << query;
}
query.clear();
result = NULL;
cin.clear();
fflush(stdin);
}
return 0;
}