-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.cpp
More file actions
224 lines (202 loc) · 5.96 KB
/
query.cpp
File metadata and controls
224 lines (202 loc) · 5.96 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
#include <iostream>
#include <iomanip>
#include "query.h"
extern Token lookahead;
extern std::vector<Token> tokens;
extern int tokenIndex;
extern bool hasError;
extern Table *currentTable;
std::vector<std::string> selected;
std::vector<Column> result;
bool all = false;
void queryParse(Database& db, std::vector<Token> _tokens) {
selected.clear();
tokenIndex = 0;
hasError = false;
lookahead = _tokens.front();
tokens = _tokens;
// predictive
matchName("select");
select_list();
matchName("from");
// check the type and judge whether the table is in the db
if (checkType("id")) {
std::string tableName = lookahead.getName();
if (db.isTableExist(tableName)) {
currentTable = &db.getTableRef(tableName);
nextToken();
} else {
hasError = true;
std::cerr << "Table doesn't exist." << std::endl;
return;
}
} else {
std::cerr << "ID error." << std::endl;
hasError = true;
}
//get selected row index after where condition
std::vector<int> rowIndex = where_clause();
if (hasError) {
std::cerr << "Expression error." << std::endl;
return;
}
matchName(";");
if (!hasError) {
queryOperation(db, selected, rowIndex);
} else {
std:: cout << "Error!" << std::endl;
hasError = true;
}
}
//return all column if select_list equals "*",else jump to Qcolumn_list funtion
void select_list() {
if (!hasError && lookahead.getName() == "*") {
all = true;
matchName("*");
}
else if (!hasError && lookahead.getType() == "id") {
Qcolumn_list();
all = false;
}
}
//get selected column from given id name
void Qcolumn_list() {
if (!hasError && lookahead.getType() == "id") {
matchSelected("id");
Qcolumn_list2();
}
}
void Qcolumn_list2() {
if (!hasError && lookahead.getName() == ",") {
matchName(",");
matchSelected("id");
Qcolumn_list2();
}
}
//save the selected id into vector<string> selected
void matchSelected(std::string target) {
std::string str = lookahead.getType();
if (str.compare(target) == 0) {
if (tokenIndex + 1 < tokens.size()) {
lookahead = tokens[tokenIndex + 1];
// put Qid into vector
if (target.compare("id") == 0)
selected.push_back(tokens[tokenIndex].getName());
tokenIndex++;
}
}
else
hasError = true;
}
std::string QTransLow(std::string str) {
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
return str;
}
//query and print
void queryOperation(Database& db, std::vector<std::string> selected,
std::vector<int> rowIndex) {
std::vector<int> &queryIndex = rowIndex;
// check whether table exist
if (currentTable != NULL) {
//get all columns from table
std::vector<Column> columns = currentTable->getColumns();
//store the selected columns in result
result = queryRows(*currentTable, queryIndex);
if(!hasError) {
//output format
for (int iter = 0; iter < result.size(); iter++) {
std::cout << "|------------";
}
std::cout << "|" << std::endl;
for (std::vector<Column>::iterator iter = result.begin(); iter != result.end(); iter++) {
std::cout << "|";
for(int i = 0; i < (12 - (*iter).getName().size()) / 2; i++) {
std::cout << " ";
}
std::cout << (*iter).getName();
for(int i = 0; i < (12 - (*iter).getName().size() + 1) / 2; i++) {
std::cout << " ";
}
}
std::cout << "|" << std::endl;
for (std::vector<Column>::iterator iter = result.begin(); iter != result.end(); iter++) {
std::cout << "|------------";
}
std::cout << "|" << std::endl;
int num = 0;
//tranverse all columns and compare it to the selected columns, once matched, print the element according the selected rows
for (std::vector<int>::iterator iter1 = queryIndex.begin(); iter1 != queryIndex.end(); iter1++, num++) {
for (std::vector<Column>::iterator iter = result.begin(); iter != result.end(); iter++) {
for (std::vector<Column>::iterator iter2 = columns.begin(); iter2 != columns.end(); iter2++) {
//judge if the selected colum's name equal the table' column's name
if ((*iter).getName() == (*iter2).getName()) {
std::cout << "|";
int l = 0;
int length = (*iter2).getValue(*iter1);
if (length <= 0) {
l = 1;
length = -length;
}
while(length > 0) {
length = length /10;
l++;
}
for(int i = 0; i < (12 - l) / 2; i++) {
std::cout << " ";
}
std::cout << (*iter2).getValue(*iter1);
for(int i = 0; i < (12 - l + 1) / 2; i++) {
std::cout << " ";
}
}
}
}
std::cout << "|" << std::endl;
for (int i = 0; i < result.size(); i++) {
std::cout << "|------------";
}
std::cout << "|" << std::endl;
}
std::cout << num << " row affected." << std::endl;
std::cout << std::endl;
}
}
}
//get rows from selected index and selected column
std::vector<Column> queryRows(Table table, std::vector<int> index) {
std::vector<Column> tmpCol;
std::vector<Column> allCol = table.getColumns();
std::vector<std::string> tmp_select;
//if the select_list equals "*", then select all columns
if (all) {
for (std::vector<Column>::iterator iter = allCol.begin(); iter != allCol.end(); iter++) {
tmp_select.push_back((*iter).getName());
}
}
else {
tmp_select = selected;
}
//push back the selected rows into vector<Column>
for (std::vector<std::string>::iterator coliter = tmp_select.begin(); coliter != tmp_select.end(); coliter++) {
std::vector<int> newEle;
for (std::vector<int>::iterator iter = index.begin(); iter != index.end(); iter++) {
if (!hasError && table.isColumnExist(*coliter)) {
//according to selected rows and selected column,get the value and save the value in newEle
newEle.push_back(table.getColumn(*coliter).getValue(*iter));
}
else {
std::cout << "Column name error: " << *coliter << " !\n";
hasError = true;
break;
}
}
//construct a new column to save selected vlaue
Column newcol(*coliter, newEle, false, -1);
//save all selected column
tmpCol.push_back(newcol);
}
return tmpCol;
}