-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdProject.java
More file actions
142 lines (130 loc) · 3.76 KB
/
cmdProject.java
File metadata and controls
142 lines (130 loc) · 3.76 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
/* cmdProject.java
*
* 11/17/17
* This command projects specified attributes
*/
import java.util.*;
public class cmdProject extends base {
public cmdProject(Scanner line, Database db, LinkedList<String> errorsList) {
run(line, db, errorsList);
}
public void run(Scanner line, Database db, LinkedList<String> errorsList) {
LinkedList<String> aList = new LinkedList<String>();
LinkedList<String> renames = new LinkedList<String>();
String rName = getAttributeNames(line, aList, renames);
int[] rIndex = findRelation(rName, db, true);
if (rIndex[0] != -1) {
Relation r = new Relation();
if (rIndex[1] == 1) {
r = db.getRelations().get(rIndex[0]);
} else {
r = db.getTmpRelations().get(rIndex[0]);
}
int[] attributePos = findAttributes(aList, r, errorsList);
createTmpRelation(attributePos, r, db, renames);
} else {
errorsList.add("RELATION \"" + rName + "\" DOES NOT EXIST.");
}
}
public String getAttributeNames(Scanner line, LinkedList<String> aList, LinkedList<String> renames) {
String current = nextSymbol(line);
String next = null;
while (!current.equals("FROM")) {
if (next == null) {
next = nextSymbol(line);
}
aList.add(current);
if (next.equals("AS")) {
String alias = nextSymbol(line);
renames.add(alias);
current = nextSymbol(line);
} else {
renames.add(null);
current = next;
}
next = nextSymbol(line);
}
return next;
}
public int[] findAttributes(LinkedList<String> aList, Relation r, LinkedList<String> errorsList) {
int[] position = new int[aList.size()]; // Indices of attributes we want
for (int i = 0; i < aList.size(); i++) {
int x = nextAttribute(r, aList.get(i));
if (x == -1) {
errorsList.add("ATTRIBUTE \"" + aList.get(i) + "\" FROM RELATION \"" + r.getTitle() + "\" DOES NOT EXIST.");
}
position[i] = x;
}
return position;
}
public void createTmpRelation(int[] attributepos, Relation r, Database db, LinkedList<String> renames) {
Relation tmp = new Relation();
// Set up relation
for (int i = 0; i < attributepos.length; i++) {
int position = attributepos[i];
if (position != -1) {
if (renames.get(i) == null) {
tmp.getCategories().add(r.getCategories().get(position));
} else {
tmp.getCategories().add(renames.get(i));
}
tmp.getdataTypes().add(r.getdataTypes().get(position));
tmp.getmaxSize().add(r.getmaxSize().get(position));
}
}
// Fill with tuples
for (int i = 0; i < r.getTuples().size(); i++) {
Tuple oldTuple = r.getTuples().get(i);
Tuple newTuple = new Tuple();
for (int j = 0; j < attributepos.length; j++) {
int position = attributepos[j];
if (position != -1) {
newTuple.attribute().add(oldTuple.attribute().get(position));
}
}
if (tmp.getTuples().size() > 0) {
if (!isDuplicate(tmp, newTuple)) {
tmp.getTuples().add(newTuple);
}
} else {
tmp.getTuples().add(newTuple);
}
}
db.getTmpRelations().add(tmp);
}
public boolean isDuplicate(Relation tmp, Tuple newTuple) {
boolean isEqual = false;
for (int j = 0; j < tmp.getTuples().size(); j++) {
for (int k = 0; k < tmp.getTuples().get(j).attribute().size(); k++) {
String newTupleAttr = newTuple.attribute().get(k);
String oldTupleAttr = tmp.getTuples().get(j).attribute().get(k);
if (newTupleAttr.equals(oldTupleAttr)) {
isEqual = true;
} else {
isEqual = false;
break;
}
}
if (isEqual) {
break;
}
}
return isEqual;
}
// Find 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;
}
}