-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbCommands.java
More file actions
410 lines (340 loc) · 12.3 KB
/
dbCommands.java
File metadata and controls
410 lines (340 loc) · 12.3 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
import java.util.*;
import java.io.*;
//import Table.java;
//import Condition.java;
public class dbCommands {
public static void exit() {
System.exit(0);
}
public static void closeTable(Table t) {
t = null;
}
public static Table openTable(String tablename) {
try {
FileInputStream dbFile = new FileInputStream("./" + tablename + ".db");
ObjectInputStream objectIn = new ObjectInputStream(dbFile);
Object obj = objectIn.readObject();
objectIn.close();
return (Table)obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
//I borrowed the format for this function from the following source
//https://examples.javacodegeeks.com/core-java/io/fileoutputstream/how-to-write-an-object-to-file-in-java/
public static void saveTable(Table t) {
try {
FileOutputStream dbFile = new FileOutputStream("./" + t.name + ".db");
ObjectOutputStream objectOut = new ObjectOutputStream(dbFile);
objectOut.writeObject(t);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("table didn\'t successfully save");
}
}
/**
* Creates a Table object, and returns the newly made Table
*
* FINISHED
*/
public static Table createTable(String tableName, ArrayList<TypedAttribute> cols, ArrayList<String> pks) {
Table t = new Table(tableName, cols, pks);
return t;
}
/**
* Updates a given tuple in the given table
*
* FINISHED
*/
public static void updateTuple(Table t, ArrayList<TypedAttribute> cols, ArrayList<TypedAttribute> newCols, Condition c) {
//gets the column name of the condition
String colName = c.x.name;
int numRows = t.numRows();
ArrayList<Integer> indexesToUpdate = new ArrayList<Integer>();
//gets the column index of the column the condition is on
int colIndex = t.getColIndex(colName);
//this iterates through the table's tuples and checks to see if the
//individual tuples satisfy the condition
for (int i = 0; i < numRows; i++) {
//this uses the values of the tuple's column that the condition
//is being checked against to see if it meets the condition
if (c.solve(t.tuples.get(i)[colIndex]))
indexesToUpdate.add(i);
}
ArrayList<Integer> indexesColsToChange = new ArrayList<Integer>();
for (TypedAttribute col : cols)
indexesColsToChange.add(t.getColIndex(col.name));
for (int i = 0; i < indexesToUpdate.size(); i++) {
t.updateRow(indexesToUpdate.get(i), indexesColsToChange, newCols);
}
}
/**
* This function returns a Table from the given
* table that meet the given condition
*
* FINISHED
*/
public static Table selection(Table t, Condition c) {
String colName = c.x.name;
int numRows = t.numRows();
ArrayList<TypedAttribute[]> indexesToSelect = new ArrayList<TypedAttribute[]>();
//gets the column index of the column the condition is on
int colIndex = t.getColIndex(colName);
//this iterates through the table's tuples and checks to see if the
//individual tuples satisfy the condition
for (int i = 0; i < numRows; i++) {
//this uses the values of the tuple's column that the condition
//is being checked against to see if it meets the condition
if (c.solve(t.tuples.get(i)[colIndex]))
indexesToSelect.add(t.tuples.get(i));
}
return new Table(t.cols, indexesToSelect, "selection on " + t.name);
}
/**
* This function does a selection with a comparison instead of a condition
*
* FINISHED
*/
public static Table selection(Table t, Comparison c) {
ArrayList<String> colNames = c.getColNames();
int numRows = t.numRows();
ArrayList<TypedAttribute[]> tuplesToSelect = new ArrayList<TypedAttribute[]>();
//gets the column index of the column the condition is on
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
for (int i = 0; i < colNames.size(); i++)
colIndexes.add(t.getColIndex(colNames.get(i)));
//this iterates through the table's tuples and checks to see if the
//individual tuples satisfy the condition
for (int i = 0; i < numRows; i++) {
//arrraylist that will hold all the values being checking in comparison
ArrayList<TypedAttribute> tupleVals = new ArrayList<TypedAttribute>();
//for each column that needs to be checked, the row values are extracted
for (int j = 0; j < colIndexes.size(); j++)
tupleVals.add(t.tuples.get(i)[colIndexes.get(j)]);
//this uses the values of the tuple's column that the condition
//is being checked against to see if it meets the condition
if (c.solve(tupleVals))
tuplesToSelect.add(t.tuples.get(i));
}
return new Table(t.cols, tuplesToSelect, "selection on " + t.name);
}
/**
* This function returns only the columns whose names are in the cols variable
*
* FINISHED
*/
public static Table projectionS(Table t, ArrayList<String> cols) {
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
//adds the indexes of the specified column names to the AL
for (String s : cols)
colIndexes.add(t.getColIndex(s));
//this gets all the tuples in the table, but only with the specified column indexes
return new Table(t.getColHeaders(colIndexes), t.getCols(colIndexes), "projection on " + t.name);
}
/**
* This function returns only the columns whose names are in the cols variable
*/
public static Table projectionI(Table t, ArrayList<Integer> colIndexes) {
//this gets all the tuples in the table, but only with the specified column indexes
return new Table(t.getColHeaders(colIndexes), t.getCols(colIndexes), "projection on " + t.name);
}
/**
* Takes two ArrayLists of tuples and returns their union
*
* FINISHED
*/
public static Table union(Table t1, Table t2) {
if (!t1.equals(t2)) {
System.out.println("Sets are not compatible\nSet 1:\n");
dbCommands.showTable(t1);
System.out.println("Set 2:\n");
dbCommands.showTable(t2);
return null;
}
for (TypedAttribute[] ta : t2.tuples)
t1.tuples.add(ta);
t1.name = t1.name + " + " + t2.name;
return t1;
}
/**
* Computes the set difference between 2 tables
*
* FINISHED
*/
public static Table difference(Table t1, Table t2) {
if (!t1.equals(t2)) {
System.out.println("Sets are not compatible\nSet 1:\n");
dbCommands.showTable(t1);
System.out.println("Set 2:\n");
dbCommands.showTable(t2);
return null;
}
ArrayList<TypedAttribute[]> newTuples = new ArrayList<TypedAttribute[]>();
int cnt1 = 0;
for (TypedAttribute[] t1Tuple : t1.tuples) {
boolean found = false;
for (TypedAttribute[] t2Tuple : t2.tuples) {
if (Arrays.equals(t1Tuple, t2Tuple)) {
found = true;
break;
}
}
if (found)
continue;
else newTuples.add(t1Tuple);
}
return new Table(t1.cols, newTuples, t1.name + " - " + t2.name);
}
/**
* Renames the first n columns with the names in the given string arraylist
* It will not fail if there are more columns than strings, but will if
* there are more strings than columns
*
* FINISHED
*/
public static Table rename(Table t, ArrayList<String> newNames) {
if (newNames.size() > t.cols.size()) {
System.out.println("Too many arguments for number of columns, attributes not renamed\n");
return t;
}
for (int i = 0; i < newNames.size(); i++) {
t.cols.get(i).name = newNames.get(i);
}
t.name = "renamed " + t.name;
return t;
}
/**
* Renames the first n columns with the names in the given string arraylist
* It will not fail if there are more columns than strings, but will if
* there are more strings than columns
*
* FINISHED
*/
public static Table renameCopy(Table t1, ArrayList<String> newNames) {
Table t = t1.getClone();
if (newNames.size() > t.cols.size()) {
System.out.println("Too many arguments for number of columns, attributes not renamed\n");
return t;
}
for (int i = 0; i < newNames.size(); i++) {
t.cols.get(i).name = newNames.get(i);
}
t.name = "renamed " + t.name;
return t;
}
/**
* Returns the cartesian product of the two tables
*
* FINISHED
*/
public static Table product(Table t1, Table t2) {
ArrayList<TypedAttribute> allCols = new ArrayList<TypedAttribute>();
for (TypedAttribute col : t1.cols)
allCols.add(col);
for (TypedAttribute col : t2.cols)
allCols.add(col);
ArrayList<TypedAttribute[]> allTuples = new ArrayList<TypedAttribute[]>();
int numCols1 = t1.cols.size();
int numCols2 = t2.cols.size();
int totalCols = numCols1 + numCols2;
//this goes through all the tupes in table 1
for (TypedAttribute[] t1Tuple : t1.tuples) {
//this goes through all the tuples in table 2
for (TypedAttribute[] t2Tuple : t2.tuples) {
//these are the values of both of the tuples commbined
TypedAttribute[] comboTuple = new TypedAttribute[totalCols];
int cnt = 0;
//adds all the column vals for table 1s tuple
for (int i = 0; i < numCols1; i++)
comboTuple[cnt++] = t1Tuple[i];
//adds all the column vals for table 2s tuple
for (int i = 0; i < numCols2; i++)
comboTuple[cnt++] = t2Tuple[i];
allTuples.add(comboTuple);
}
}
return new Table(allCols, allTuples, t1.name + " * " + t2.name);
}
/**
* Inserts the given tuple into the table
*
* FINISHED
*/
public static void insertTuple_1(Table t, TypedAttribute[] newTuple) {
if (!t.checkForPKViolation(newTuple))
t.addTuple(newTuple);
else System.out.println("tuple could not be added because the PK already exists in table\n");
}
/**
* Deletes the tuples from the table that meet the condition
*
* FINISHED
*/
public static Table deleteTuple(Table t, Condition c) {
String colName = c.x.name;
int numRows = t.numRows();
//gets the column index of the column the condition is on
int colIndex = t.getColIndex(colName);
//this iterates through the table's tuples and checks to see if the
//individual tuples satisfy the condition
for (int i = numRows - 1; i >= 0; i--) {
//this uses the values of the tuple's column that the condition
//is being checked against to see if it meets the condition
if (c.solve(t.tuples.get(i)[colIndex]))
t.dropRow(i);
}
return t;
}
/**
* This function does a deletion with a comparison instead of a condition
*
* FINISHED
*/
public static Table deleteTuple(Table t, Comparison c) {
ArrayList<String> colNames = c.getColNames();
int numRows = t.numRows();
//gets the column index of the column the condition is on
ArrayList<Integer> colIndexes = new ArrayList<Integer>();
for (int i = 0; i < colNames.size(); i++)
colIndexes.add(t.getColIndex(colNames.get(i)));
//this iterates through the table's tuples and checks to see if the
//individual tuples satisfy the condition
for (int i = numRows - 1; i >= 0; i--) {
//arrraylist that will hold all the values being checking in comparison
ArrayList<TypedAttribute> tupleVals = new ArrayList<TypedAttribute>();
//for each column that needs to be checked, the row values are extracted
for (int j = 0; j < colIndexes.size(); j++)
tupleVals.add(t.tuples.get(i)[colIndexes.get(j)]);
//this uses the values of the tuple's column that the condition
//is being checked against to see if it meets the condition
if (c.solve(tupleVals))
t.dropRow(i);
}
return t;
}
public static void deleteTable(Table t) {
t = null; //whatever this does its supposed to delete this mf
}
public static void showTable(Table t) {
String header = t.getHeaderString();
String body = "";
for (int i = 0; i < t.tuples.size(); i++) {
body += t.getRowString(i) + "\n";
}
String sep = String.join("", Collections.nCopies(header.length()/2, "-"));
System.out.println(sep);
System.out.println(t.name);
System.out.println(sep);
System.out.print(header);
System.out.println(sep);
System.out.print(body);
System.out.println(sep);
}
public static void printTuples(ArrayList<TypedAttribute[]> tups, String header) {
String body = Table.tupleString(tups);
System.out.print(header);
System.out.println(body);
}
}