-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdDelete.java
More file actions
210 lines (189 loc) · 5.29 KB
/
cmdDelete.java
File metadata and controls
210 lines (189 loc) · 5.29 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
/* cmdDelete.java
*
* 11/18/17
* This command deletes tuples from a relation
*/
import java.util.*;
public class cmdDelete extends base {
public cmdDelete(Scanner line, Database db, LinkedList<String> errorsList) {
run(line, db, errorsList);
}
public void run(Scanner line, Database db, LinkedList<String> errorsList) {
String rName = nextSymbol(line);
int[] rIndex = findRelation(rName, db, false);
if (rIndex[0] != -1) {
Relation r = db.getRelations().get(rIndex[0]);
deleteTuples(r, db, line);
}
}
public void deleteTuples(Relation r, Database db, Scanner line) {
if (line.hasNext() && line.next().equals("WHERE")) {
qualificationTest(r, db, line);
}
}
public void qualificationTest(Relation r, Database db, Scanner line) {
LinkedList<LinkedList<String>> qualifiers = createQualifiersList(line, r);
boolean pass = false;
for (int i = r.getTuples().size() - 1; i >= 0; i--) {
Tuple t = r.getTuples().get(i);
pass = runTest(r, t, qualifiers);
if (pass) {
r.getTuples().remove(i);
}
}
}
public LinkedList<LinkedList<String>> createQualifiersList(Scanner line, Relation r) {
LinkedList<LinkedList<String>> qualifiers = new LinkedList<LinkedList<String>>();
while (line.hasNext()) {
qualifiers.add(getAndedGroup(line, r));
}
return qualifiers;
}
/* pass The tuple passed the qualifying test
* currentConditionPass The tuple passed the current condition
*/
public boolean runTest(Relation r, Tuple t, LinkedList<LinkedList<String>> qualifiers) {
boolean pass = true;
boolean currentConditionPass = true;
for (int i = 0; i < qualifiers.size(); i++) {
for (int j = 0; j < qualifiers.get(i).size(); j++) {
String[] test = qualifiers.get(i).get(j).split(" ");
if (test.length > 3) {
for (int k = 3; k < test.length; k++) {
test[2] += " " + test[k];
}
}
int aPos = nextAttribute(r, test[0]);
String attr = t.attribute().get(aPos);
currentConditionPass = singleConditionTest(attr, test);
if (!currentConditionPass) { // Stop looking at "AND" group and move on to next "AND" group
pass = false;
break;
} else {
pass = true;
}
}
if (pass) { // No need to keep looking thru "OR" groups
break;
}
}
return pass;
}
// Run the string or the integer version of the condition test
public boolean singleConditionTest(String attr, String[] test) {
boolean currentAndPass = true;
try {
int num = Integer.parseInt(attr);
int value = Integer.parseInt(test[2]);
currentAndPass = conditionTestInt(num, test[1], value);
} catch (Exception NumberFormatException) {
String value = test[2];
currentAndPass = conditionTestStr(attr, test[1], value);
}
return currentAndPass;
}
// Check if value in tuple satifies current condition
public boolean conditionTestInt(int num, String condition, int value) {
boolean andConditionPass = false;
switch (condition) {
case "=":
if (num == value) {
andConditionPass = true;
}
break;
case "<":
if (num < value) {
andConditionPass = true;
}
break;
case "<=":
if (num <= value) {
andConditionPass = true;
}
break;
case ">":
if (num > value) {
andConditionPass = true;
}
break;
case ">=":
if (num >= value) {
andConditionPass = true;
}
break;
case "!=":
if (num != value) {
andConditionPass = true;
}
break;
default:
break;
}
return andConditionPass;
}
// Check if value in tuple satifies current condition
public boolean conditionTestStr(String attr, String condition, String value) {
boolean andConditionPass = false;
switch (condition) {
case "=":
if (attr.equals(value)) {
andConditionPass = true;
}
break;
case "!=":
if (!attr.equals(value)) {
andConditionPass = true;
}
break;
default:
break;
}
return andConditionPass;
}
// Return the index (position) of the attribute named query
public int nextAttribute(Relation r, String query) {
int i = 0;
boolean match = false;
while (!match && i < r.getCategories().size()) {
if (r.getCategories().get(i).equals(query)) {
match = true;
} else {
i++;
}
}
if (match) {
return i;
}
return -1;
}
public LinkedList<String> getAndedGroup(Scanner line, Relation r) {
LinkedList<String> anded = new LinkedList<String>();
String and = "AND";
while (line.hasNext() && and.equals("AND")) {
String next = getNextAndedQualifier(line);
if (typematch(next, r)) {
anded.add(next);
}
if (line.hasNext()) {
and = nextSymbol(line);
}
}
return anded;
}
// DOESN'T DO ANYTHING YET
public boolean typematch(String condition, Relation r) {
boolean match = true;
// Get the category corresponding to attribute name
String[] tokens = condition.split(" ");
String category = tokens[0];
String value = tokens[2];
int aPos = nextAttribute(r, category);
return match;
}
public String getNextAndedQualifier(Scanner line) {
String attr = nextSymbol(line);
String relop = nextSymbol(line);
String value = nextSymbol(line);
return attr + " " + relop + " " + value;
}
}