-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidators.java
More file actions
237 lines (203 loc) · 7.85 KB
/
Copy pathValidators.java
File metadata and controls
237 lines (203 loc) · 7.85 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Set;
import java.util.Arrays;
import java.util.List;
public class Validators {
public static boolean checkTableExistsMF(String tableName) throws DBAppException {
// Check if a table with the table name exists in the database by checking for it in the metafile.
try {
FileReader fr = new FileReader("resources/metaFile.csv");
BufferedReader br = new BufferedReader(fr);
String z = br.readLine();
while (z != null) {
String[] mFile = z.split(",");
String a = mFile[0].substring(1, mFile[0].length() - 1).trim();
if (a.equals(tableName)) {
br.close();
fr.close();
return true;
}
z = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
public static boolean checkColumnExistsMF(String columnName, String tableName) throws DBAppException {
// Check if a column with the column name exists in the database by checking for it in the metafile.
// The column has to belong to the table.
try {
FileReader fr = new FileReader("resources/metaFile.csv");
BufferedReader br = new BufferedReader(fr);
String z = br.readLine();
while (z != null) {
String[] mFile = z.split(",");
String a = mFile[0].substring(1, mFile[0].length() - 1).trim();
String b = mFile[1].substring(1, mFile[1].length() - 1).trim();
if (a.equals(tableName) && b.equals(columnName)) {
br.close();
fr.close();
return true;
}
z = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
public static String dataType(String columnName, String tableName) {
try {
FileReader fr = new FileReader("resources/metaFile.csv");
BufferedReader br = new BufferedReader(fr);
String z = br.readLine();
while (z != null) {
String[] mfile = z.split(",");
String a = mfile[0].substring(1, mfile[0].length() - 1).trim();
String b = mfile[1].substring(1, mfile[1].length() - 1).trim();
String c = mfile[2].substring(1, mfile[2].length() - 1).trim();
if (a.equals(tableName) && b.equals(columnName)) {
if (c.equalsIgnoreCase("java.lang.Integer")) {
br.close();
fr.close();
return "int";
} else if (c.equalsIgnoreCase("java.lang.Double")) {
br.close();
fr.close();
return "double";
} else if (c.equalsIgnoreCase("java.lang.String")) {
br.close();
fr.close();
return "string";
}
}
z = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return "";
}
private void generalExcCheck(String tablename, String key, String keyType) throws DBAppException {
Boolean keyattr = false;
Boolean table = false;
int i = 0;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("resources/metaFile.csv"));
String ln = br.readLine();
while (ln != null) {
String[] a = ln.split(",");
if (a[0].equals(tablename)) {
table = true;
i++;
if (a[1].equals(key)) {
keyattr = true;
if (!(keyType.equals(a[2]))) {
DBAppException exc = new DBAppException(key + " should be of type: "
+ a[2]);
throw exc;
}
}
}
ln = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (!(table)) {
DBAppException exc = new DBAppException(
"Table can't be found");
throw exc;
}
if (!(keyattr)) {
DBAppException exc = new DBAppException(
"There's no attribute in table" + tablename + " with the name" + key);
throw exc;
}
}
private void allowedKeyType(Hashtable<String, String> htblColNameType) throws DBAppException {
Set<String> keys = htblColNameType.keySet();
List<String> datatypes = Arrays.asList("java.lang.Integer", "java.lang.Double", "java.lang.String");
int i = 0;
for (String key : keys) {
if (!(datatypes.contains((htblColNameType.get(key)))))
throw new DBAppException("The type of the key is invalid. It has be either Integer or Double or String");
}
}
private void allowedOps(String op) throws DBAppException {
List<String> operators = Arrays.asList(">",">=","<","<=","!=","=");
if (!(operators.contains(op)))
throw new DBAppException(
"The operator has to be one of the following: >, >=, <, <=, != , =");
}
private void allowedSTROps(String strOp) throws DBAppException {
List<String> operators = Arrays.asList("AND", "OR", "XOR");
if (!(operators.contains(strOp.toUpperCase())))
throw new DBAppException(
"The operator has to be one of the following: AND, OR, XOR");
}
private int numOfCols(String tablename) throws DBAppException {
int i = 0;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("resources/metaFile.csv"));
String ln = br.readLine();
while (ln != null) {
String[] a = ln.split(",");
if (a[0].equals(tablename))
i++;
ln = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return i;
}
private void checkTableName(String tablename) throws DBAppException {
Boolean flag = false;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("resources/metaFile.csv"));
String ln = br.readLine();
while (ln != null) {
String[] a = ln.split(",");
if (a[0].equals(tablename))
flag = true;
ln = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (flag) {
DBAppException exc = new DBAppException("Table already exists");
throw exc;
}
}
}