-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
489 lines (362 loc) · 16.5 KB
/
main.java
File metadata and controls
489 lines (362 loc) · 16.5 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import java.util.*;
public class main {
public static void main(String[] args) {
boolean testGrammar = false;
if (testGrammar) {
//reads input commands line by line, or from file if
//redirection is used
List<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lines.add(line);
}
scanner.close();
//this for loop iterates through the list of commands
//and checks if they are valid
//if invalid, the number of errors will be > 0
for (String line : lines) {
CharStream charStream = CharStreams.fromString(line);
pa2Lexer lexer = new pa2Lexer(charStream);
lexer.removeErrorListeners(); // disable lexer warnings
CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
pa2Parser parser = new pa2Parser(commonTokenStream);
parser.removeErrorListeners(); // disable parser warnings
ParseTree parseTree = parser.program(); // program() auto-created from program parser rule
String output = parseTree.toStringTree(parser);
int numErrors = parser.getNumberOfSyntaxErrors();
System.out.println(output); // the parse tree's flat structure format
System.out.println(numErrors); // the number of syntax errors generated from the parser
}
}
//To test the functionality of each command, toggle these boolean values
//They are set right now to do the majority of the functions, some just
//change the table so the others might not be visibly doing things
//when using, make sure that if testCreate is true, testOpen is false and vice versa
//if you toggle testDelete to false and have it save and open, you
//can scroll up and see that the table loads as expected.
//then, if you toggle testDelete to true and have it save and open, you will
//see the much smaller table after the delete from the previous run in the open statement
boolean testCreate = true,
testUpdate = false,
testSelection = true,
testProjection = true,
testUnionSameHeader = true,
testUnionDifferentHeader = true,
testDifferenceSameHeader = true,
testDifferenceDifferentHeader = true,
testRename = false,
testProduct = true,
testComparison = true,
testDelete = false,
testSave = true,
testOpen = false;
Table t = new Table();
if (testCreate) {
//this is creating a new table
ArrayList<TypedAttribute> cols = new ArrayList<TypedAttribute>();
//these are the columns of the table
cols.add(new TypedAttribute("kind", new VarcharT(20)));
cols.add(new TypedAttribute("name", new VarcharT(20)));
cols.add(new TypedAttribute("age", new IntegerT()));
//these are the primary keys of the new table
ArrayList<String> pks = new ArrayList<String>();
pks.add("kind");
pks.add("name");
t = dbCommands.createTable("animals", cols, pks);
dbCommands.showTable(t);
//these are the commands to make a new tuple from TypedAttributes
TypedAttribute c1 = new TypedAttribute("dog");
TypedAttribute c2 = new TypedAttribute("sam");
TypedAttribute c3 = new TypedAttribute(12);
TypedAttribute[] c4 = new TypedAttribute[]{c1, c2, c3};
//inserts the tuple into table
dbCommands.insertTuple_1(t, c4);
//more tuples
c1 = new TypedAttribute("cat");
c2 = new TypedAttribute("smokey");
c3 = new TypedAttribute(18);
c4 = new TypedAttribute[]{c1, c2, c3};
dbCommands.insertTuple_1(t, c4);
c1 = new TypedAttribute("duck");
c2 = new TypedAttribute("donald");
c3 = new TypedAttribute(21);
c4 = new TypedAttribute[]{c1, c2, c3};
dbCommands.insertTuple_1(t, c4);
c1 = new TypedAttribute("dog");
c2 = new TypedAttribute("donald");
c3 = new TypedAttribute(25);
c4 = new TypedAttribute[]{c1, c2, c3};
dbCommands.insertTuple_1(t, c4);
//this one shouldn't work as a tuple with this pk
//has already been added
dbCommands.insertTuple_1(t, c4);
}
//--------------------------------Open---------------------------//
if (testOpen) {
t = dbCommands.openTable("animals");
}
dbCommands.showTable(t);
//--------------------------------Update---------------------------//
if (testUpdate) {
System.out.println("Update test using VARCHARS...");
System.out.println("update (kind, name) to (cat, fred) where (kind == dog);\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
//these are the columns to change if the above condition is met
TypedAttribute b1 = new TypedAttribute("kind");
TypedAttribute b2 = new TypedAttribute("name");
ArrayList<TypedAttribute> b3 = new ArrayList<TypedAttribute>();
b3.add(b1);
b3.add(b2);
//these are the values the columns that meet the condition
//will be changed to
TypedAttribute d1 = new TypedAttribute("cat");
TypedAttribute d2 = new TypedAttribute("fred");
ArrayList<TypedAttribute> d3 = new ArrayList<TypedAttribute>();
d3.add(d1);
d3.add(d2);
//testing update
//this should change every tuple that is a dog to a cat named fred
dbCommands.updateTuple(t, b3, d3, c);
dbCommands.showTable(t);
System.out.println("Update Test using INTEGERS...");
System.out.println("update (name, age) to (bob, 35) where (age == 12);\n");
//testing update when numeric conditions are used
TypedAttribute e1 = new TypedAttribute("age");
TypedAttribute e2 = new TypedAttribute(12);
Condition f = new Condition(e1, e2, "<=");
//these are the columns to change if the above condition is met
TypedAttribute g1 = new TypedAttribute("name");
TypedAttribute g2 = new TypedAttribute("age");
ArrayList<TypedAttribute> g3 = new ArrayList<TypedAttribute>();
g3.add(g1);
g3.add(g2);
//these are the values the columns that meet the condition
//will be changed to
TypedAttribute h1 = new TypedAttribute("bob");
TypedAttribute h2 = new TypedAttribute(35);
ArrayList<TypedAttribute> h3 = new ArrayList<TypedAttribute>();
h3.add(h1);
h3.add(h2);
//testing update
//this should change every tuple that is a dog to a cat named fred
dbCommands.updateTuple(t, g3, h3, f);
System.out.println("Updated Table");
dbCommands.showTable(t);
}
//--------------------------------Selection------------------------//
if (testSelection) {
System.out.println("Selection test...");
System.out.println("select (kind == dog) from table\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
Table select = dbCommands.selection(t, c);
System.out.println("Selection Table");
dbCommands.showTable(select);
System.out.println("\n\n");
}
//--------------------------------Projection----------------------//
if (testProjection) {
System.out.println("Projection test...");
System.out.println("project (kind, age)\n");
ArrayList<String> colsToProject = new ArrayList<String>();
colsToProject.add("kind");
colsToProject.add("age");
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
Table t2 = dbCommands.projectionS(t, colsToProject);
System.out.println("Projection Table");
dbCommands.showTable(t2);
System.out.println("\n\n");
}
//--------------------------------Union - Same headers-------------//
if (testUnionSameHeader) {
System.out.println("Union Test with compatible tuple sets...");
System.out.println("(select (kind == dog) from table) + (select (age == 12) from table)\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
Table dogs = dbCommands.selection(t, c);
TypedAttribute b1 = new TypedAttribute("age");
TypedAttribute b2 = new TypedAttribute(18);
Condition d = new Condition(b1, b2, "==");
Table twelve = dbCommands.selection(t, d);
Table union = dbCommands.union(dogs, twelve);
System.out.println("Union Table");
dbCommands.showTable(union);
System.out.println("\n\n");
}
//--------------------------------Union - Different headers-------------//
if (testUnionDifferentHeader) {
System.out.println("Union Test with incompatible tuple sets...");
System.out.println("(project (kind, age) (select (kind == dog) from table)) + (select (age == 12) from table)\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
Table dogs = dbCommands.selection(t, c);
//finished selecting all tuples with kind == dog
//--------------------------------------------------------
//now projecting the kind and age column
ArrayList<String> colsToProject = new ArrayList<String>();
colsToProject.add("kind");
colsToProject.add("name");
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
//gets the column indexes so the projection
//table can have proper header
for (String colname : colsToProject)
colIndexes.add(t.getColIndex(colname));
Table project = dbCommands.projectionI(dogs, colIndexes);
//so this is the projection of the result of the selection
//--------------------------------------------------------
//now we are making the second selection with no projection
//this will make not only the number of columns different, but
//also give different headers
TypedAttribute b1 = new TypedAttribute("age");
TypedAttribute b2 = new TypedAttribute(12);
Condition d = new Condition(b1, b2, ">=");
Table twelve = dbCommands.selection(t, d);
Table union = dbCommands.union(project, twelve);
if (union != null) {
System.out.println("Union Table");
dbCommands.showTable(union);
System.out.println("\n\n");
}
}
//------------------------Differences - Same header-------------//
if (testDifferenceSameHeader) {
System.out.println("Difference Test with compatible tuple sets...");
System.out.println("(select (age >= 12) from table) - (select (kind == dog) from table)\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
Table dogs = dbCommands.selection(t, c);
TypedAttribute b1 = new TypedAttribute("age");
TypedAttribute b2 = new TypedAttribute(12);
Condition d = new Condition(b1, b2, ">=");
Table twelve = dbCommands.selection(t, d);
System.out.println("Twelve table");
dbCommands.showTable(twelve);
System.out.println("Dogs table");
dbCommands.showTable(dogs);
Table difference = dbCommands.difference(twelve, dogs);
System.out.println("Difference Table");
dbCommands.showTable(difference);
System.out.println("\n\n");
}
//------------------------Differences - Different header-------------//
if (testDifferenceDifferentHeader) {
System.out.println("Difference Test with incompatible tuple sets...");
System.out.println("(project (kind, age) (select (kind == dog) from table)) - (select (age == 12) from table)\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
Table dogs = dbCommands.selection(t, c);
//finished selecting all tuples with kind == dog
//--------------------------------------------------------
//now projecting the kind and age column
ArrayList<String> colsToProject = new ArrayList<String>();
colsToProject.add("kind");
colsToProject.add("name");
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
//gets the column indexes so the projection
//table can have proper header
for (String colname : colsToProject)
colIndexes.add(t.getColIndex(colname));
Table project = dbCommands.projectionI(dogs, colIndexes);
//so this is the projection of the result of the selection
//--------------------------------------------------------
//now we are making the second selection with no projection
TypedAttribute b1 = new TypedAttribute("age");
TypedAttribute b2 = new TypedAttribute(12);
Condition d = new Condition(b1, b2, ">=");
Table twelve = dbCommands.selection(t, d);
Table difference = dbCommands.difference(project, twelve);
if (difference != null) {
System.out.println("Difference Table");
dbCommands.showTable(difference);
System.out.println("\n\n");
}
}
//----------------------------------------Rename--------------//
if (testRename) {
System.out.println("Renaming Test...");
System.out.println("rename (type, callsign) table\n");
ArrayList<String> newNames = new ArrayList<String>();
newNames.add("type");
newNames.add("callsign");
Table renamed = dbCommands.rename(t, newNames);
dbCommands.showTable(t);
}
//--------------------------------Product---------------------------//
if (testProduct) {
System.out.println("Product Test...");
//System.out.println("(project (kind, age) (select (kind == dog) from table)) - (select (age == 12) from table)\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
dbCommands.showTable(t);
Table dogs = dbCommands.selection(t, c);
//finished selecting all tuples with kind == dog
//--------------------------------------------------------
//now projecting the kind and age column
ArrayList<String> colsToProject = new ArrayList<String>();
colsToProject.add("kind");
colsToProject.add("name");
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
//gets the column indexes so the projection
//table can have proper header
for (String colname : colsToProject)
colIndexes.add(t.getColIndex(colname));
Table project = dbCommands.projectionI(dogs, colIndexes);
//so this is the projection of the result of the selection
//--------------------------------------------------------
//now we are making the second selection with no projection
TypedAttribute b1 = new TypedAttribute("age");
TypedAttribute b2 = new TypedAttribute(12);
Condition d = new Condition(b1, b2, ">=");
Table twelve = dbCommands.selection(t, d);
Table product = dbCommands.product(dogs, twelve);
dbCommands.showTable(product);
}
//--------------------------------Comparison---------------------------//
if (testComparison) {
System.out.println("Comparison Test...");
System.out.println("select (kind == dog && age > 12) from table\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
//Table dogs = dbCommands.selection(t, c);
TypedAttribute b1 = new TypedAttribute("age");
TypedAttribute b2 = new TypedAttribute(12);
Condition d = new Condition(b1, b2, ">");
Comparison comp = new Comparison(c, d, "&&");
Table oldDogs = dbCommands.selection(t, comp);
System.out.println("oldDogs table");
dbCommands.showTable(oldDogs);
System.out.println("\n\n");
}
//--------------------------------Delete---------------------------//
if (testDelete) {
System.out.println("Delete Test...");
System.out.println("delete (kind == dog) from table\n");
TypedAttribute a1 = new TypedAttribute("kind");
TypedAttribute a2 = new TypedAttribute("dog");
Condition c = new Condition(a1, a2, "==");
Table dogs = dbCommands.deleteTuple(t, c);
System.out.println("table now that dogs have been deleted");
dbCommands.showTable(dogs);
System.out.println("\n\n");
}
//--------------------------------Save---------------------------//
if (testSave) {
dbCommands.saveTable(t);
System.out.println("Table saved to " + t.name + ".db");
}
}
}