· 9 years ago · Nov 29, 2016, 03:20 AM
1package databaseManager;
2
3import java.io.FileNotFoundException;
4import java.io.IOException;
5import java.nio.file.FileSystemNotFoundException;
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.List;
9import java.util.regex.Matcher;
10import java.util.regex.Pattern;
11import javax.xml.parsers.ParserConfigurationException;
12import javax.xml.stream.XMLStreamException;
13import javax.xml.transform.TransformerException;
14
15import org.xml.sax.SAXException;
16
17/**
18 * This class parsers and conquers the SQL statements entered. To check if the
19 * entered statement is true, and send the creation order to the creation class
20 * or the operations to the operations class
21 *
22 * @author FNSY
23 *
24 */
25public class StatementsParser {
26 // private String statement;
27 private static StatementsParser statementsParser = null;
28 private String[] reservedWords;
29
30 /**
31 * current database name
32 */
33 private String dbName = null;
34
35 protected StatementsParser() {
36 initializeReservedWords();
37 }
38
39 public static synchronized StatementsParser createObject() {
40 if (statementsParser == null) {
41 statementsParser = new StatementsParser();
42 }
43 return statementsParser;
44 }
45
46 public final void enterStatement(final String statement) throws Exception {
47 String checkedStatement;
48 checkedStatement = removeWhiteSpaces(statement);
49
50 String upperCaseStatement = checkedStatement.toUpperCase();
51 try {
52 selectRightAction(upperCaseStatement.split(" "), checkedStatement);
53 } catch (Exception x) {
54 System.out.println("PLEASE ENTER A VALID STATEMENT");
55 }
56 }
57
58 private void selectRightAction(String[] upperCaseStatement, String checkedStatement)
59 throws ParserConfigurationException, SAXException, IOException, FileSystemNotFoundException,
60 XMLStreamException, TransformerException {
61 if (isValidStatement(checkedStatement))
62 if (upperCaseStatement[0].equals("USE"))
63 useStatement(checkedStatement.split(" ")); // done
64 else if (upperCaseStatement[0].equals("CREATE"))
65 creationStatement(checkedStatement); // done
66 else if (upperCaseStatement[0].equals("DELETE"))
67 deletionStatement(checkedStatement);
68 else if (upperCaseStatement[0].equals("UPDATE"))
69 updateStatement(checkedStatement);
70 else if (upperCaseStatement[0].equals("DROP"))
71 dropStatement(checkedStatement);
72 else if (upperCaseStatement[0].equals("INSERT") && upperCaseStatement[1].equals("INTO"))
73 insertionStatement(checkedStatement);
74 else if (upperCaseStatement[0].equals("SELECT"))
75 selectStatement(checkedStatement);
76 else
77 throw new RuntimeException();
78 }
79
80 /**
81 * This method checks if the SQL statement is written in right format. with
82 * no extra spaces and trailed by a semicolon
83 *
84 * @param statement
85 * @return
86 */
87
88 private void useStatement(String[] splitted) { // done check
89 splitted = filterSplittedArray(splitted);
90 if (splitted.length == 2) {
91 dbName = new String(splitted[1]);
92 }
93 }
94
95 /**
96 * create statement
97 *
98 * @param statement
99 * @throws IOException
100 * @throws SAXException
101 * @throws ParserConfigurationException
102 * @throws XMLStreamException
103 * @throws TransformerException
104 */
105 private final void creationStatement(final String statement)
106 throws ParserConfigurationException, SAXException, IOException, XMLStreamException, TransformerException {
107 String restOfTheString = statement.substring(7, statement.length() - 1);
108 try {
109 creationOfDatabase(restOfTheString);
110
111 } catch (RuntimeException x) {
112 creationOfTable(restOfTheString);
113 }
114
115 }
116
117 final void creationOfTable(String statement)
118 throws ParserConfigurationException, SAXException, IOException, XMLStreamException, TransformerException { // done
119
120 statement = replaceVarchar(statement);
121 statement = removeWhiteSpaces(statement);
122 String tableString = filterString(statement.substring(0, 5));
123 if (tableString.compareToIgnoreCase("TABLE") == 0) {
124 String[] tableDetails = statement.substring(6, statement.length()).split(" ");
125 tableDetails = filterSplittedArrayForTableCreation(tableDetails);
126 int length = tableDetails.length;
127 if (isValidName(tableDetails[0]) && !isReservedWord(tableDetails[0])) {
128
129 ArrayList<String> columnName = new ArrayList<>();
130 ArrayList<String> dataType = new ArrayList<>();
131 for (int i = 1; i < length; i++) {
132 if (i % 2 == 1) {
133 columnName.add(tableDetails[i]);
134 } else {
135 if (tableDetails[i].replace(",", "").equals("String")
136 || tableDetails[i].replace(",", "").equals("int")) {
137 if (tableDetails[i].contains(",")) {
138 dataType.add(filterString(tableDetails[i]));
139
140 } else if (!tableDetails[i].contains(",")) {
141 try {
142 String checkLast = tableDetails[i + 1];
143 throw new RuntimeException();
144 } catch (ArrayIndexOutOfBoundsException x) {
145 dataType.add(tableDetails[i]);
146 }
147 }
148 } else
149 throw new RuntimeException();
150 }
151 }
152 if (validateEqualSize(columnName, dataType) && isValidNameInArrayList(columnName)) { // checking
153 // lengths
154 // and
155 // names
156 Functions x = new Functions(tableDetails[0], columnName, dataType, dbName);
157 x.createTable(dbName);
158 XmlHandler.createDtd(columnName, tableDetails[0], dbName, 0);
159 XmlHandler.save(x, dbName);
160 List<List<String>> rowsList = Arrays.asList();
161 TableFormatter.print(columnName, rowsList);
162 }
163 } else
164 throw new RuntimeException();
165
166 } else
167 throw new RuntimeException();
168 }
169
170 private final void creationOfDatabase(final String statement) { // done
171 // check
172 String databaseString = statement.substring(0, 8);
173 if (databaseString.compareToIgnoreCase("DATABASE") == 0) {
174 String databaseName = filterString(statement.substring(9, statement.length()));
175 if (isValidName(databaseName) && !isReservedWord(databaseName)) {
176 Functions.createDatabase(databaseName);
177 }
178 } else
179 throw new RuntimeException();
180 }
181
182 // end of creation
183 private void deletionStatement(String statement) {
184 String deleteString = statement.substring(0, 11);
185 if (deleteString.compareToIgnoreCase("DELETE FROM") == 0 && statement.toUpperCase().contains("WHERE")) {
186 String restOfString = statement.substring(12, statement.length() - 1);
187 String tableName, conditionSymbol, columnName, valueIndicatingRow;
188 String[] splitted = restOfString.split(" ");
189 tableName = splitted[0];
190 for (int i = 0; i < splitted.length; i++) {
191 if (splitted[i].compareToIgnoreCase("WHERE") == 0 && (splitted[i + 2].equals("=")
192 || splitted[i + 2].equals("<") || splitted[i + 2].equals(">"))) {
193 columnName = splitted[i + 1];
194 conditionSymbol = splitted[i + 2];
195 valueIndicatingRow = splitted[i + 3];
196 try {
197 if (isValidName(tableName) && isValidName(columnName) && Functions.exists(dbName, tableName)) {
198 Functions result;
199 try {
200 result = XmlHandler.load(tableName, dbName);
201 result.deleteWhere(columnName, conditionSymbol, schrodingarString(valueIndicatingRow));
202 TableFormatter.print(result.getColumnsNames(),
203 TableFormatter.toList(result.getTotalTable()));
204 } catch (FileNotFoundException | XMLStreamException e) {
205 // TODO Auto-generated catch block
206 e.printStackTrace();
207 }
208 } else
209 throw new RuntimeException();
210 } catch (FileSystemNotFoundException | ParserConfigurationException | SAXException
211 | IOException e) {
212 throw new RuntimeException();
213 }
214 break;
215 }
216 }
217 } else
218 try {
219 deleteAllTable(statement);
220 } catch (FileSystemNotFoundException | ParserConfigurationException | SAXException | IOException e) {
221 throw new RuntimeException();
222 }
223 }
224
225 private void deleteAllTable(String statement)
226 throws FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException {
227 String deleteAllString = statement.substring(0, 11);
228 String deleteAllAstString = statement.substring(0, 13);
229 String tableName;
230 if (deleteAllString.compareToIgnoreCase("DELETE FROM") == 0) {
231 tableName = statement.substring(12, statement.length() - 1);
232 if (isValidName(tableName) && Functions.exists(dbName, tableName)) {
233 Functions result = null;
234 try {
235 result = XmlHandler.load(tableName, dbName);
236 result.delete();
237 List<List<String>> rowsList = Arrays.asList();
238 TableFormatter.print(result.getColumnsNames(), rowsList);
239 } catch (FileNotFoundException | XMLStreamException e) {
240 throw new RuntimeException();
241 }
242
243 } else
244 throw new RuntimeException();
245
246 } else if (deleteAllAstString.compareToIgnoreCase("DELETE * FROM") == 0) {
247 tableName = statement.substring(14, statement.length() - 1);
248 if (isValidName(tableName) && Functions.exists(dbName, tableName)) {
249 Functions result = null;
250 try {
251 result = XmlHandler.load(tableName, dbName);
252 result.delete();
253 List<List<String>> rowsList = Arrays.asList();
254 TableFormatter.print(result.getColumnsNames(), rowsList);
255 } catch (FileNotFoundException | XMLStreamException e) {
256 throw new RuntimeException();
257 }
258
259 } else
260 throw new RuntimeException();
261 }
262
263 }
264 // end of delete
265
266 private void updateStatement(String statement) throws FileSystemNotFoundException, ParserConfigurationException,
267 SAXException, IOException, XMLStreamException {
268 String[] splitted = statement.substring(7, statement.length()).split(" ");
269 splitted = filterSplittedArray(splitted);
270 if (statement.toUpperCase().contains("SET") && statement.toUpperCase().contains("WHERE")) {
271 containingWhereUpdateStatement(splitted);
272 } else if (statement.toUpperCase().contains("SET") && !statement.toUpperCase().contains("WHERE")) {
273 notContainingWhereUpdateStatement(splitted);
274 }
275
276 }
277
278 private void containingWhereUpdateStatement(String[] splitted)
279 throws FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException {
280 splitted = filterSplittedArray(splitted);
281 String conditionSymbol = null;
282 String tableName = splitted[0];
283 if (splitted[1].compareToIgnoreCase("SET") == 0) {
284 ArrayList<String> columnName = new ArrayList<>();
285 ArrayList<String> values = new ArrayList<>();
286 String guideColumn = null, guideValue = null;
287 for (int i = 2; i < splitted.length; i++) {
288 if (splitted[i].compareToIgnoreCase("WHERE") != 0) {
289 if (i % 3 == 0)
290 if (splitted[i].equals("=")) {
291 columnName.add(splitted[i - 1]);
292 if (splitted[i + 1].compareToIgnoreCase("WHERE") != 0)
293 values.add(splitted[i + 1]);
294 } else
295 throw new RuntimeException();
296
297 } else if (splitted[i + 2].equals("=") || splitted[i + 2].equals("<") || splitted[i + 2].equals(">")) {
298
299 guideColumn = splitted[i + 1];
300 conditionSymbol = splitted[i + 2];
301 guideValue = splitted[i + 3];
302 break;
303 } else {
304 throw new RuntimeException();
305 }
306 }
307 values = makeValuesValid(values);
308 guideValue = schrodingarString(guideValue);
309 if (validateEqualSize(columnName, values) && isValidName(tableName) && isValidName(guideColumn)
310 && isValidName(guideValue) && isValidNameInArrayList(columnName)
311 && Functions.exists(dbName, tableName)) {
312 Functions result = null;
313 try {
314 result = XmlHandler.load(tableName, dbName);
315 result.update(conditionSymbol, guideColumn, guideValue, columnName, values, "WHERE");
316 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
317 } catch (FileNotFoundException | XMLStreamException e) {
318 throw new RuntimeException();
319
320 }
321
322 } else
323 throw new RuntimeException();
324
325 } else
326 throw new RuntimeException();
327
328 }
329
330 private void notContainingWhereUpdateStatement(String[] splitted) throws FileSystemNotFoundException,
331 FileNotFoundException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
332 splitted = filterSplittedArray(splitted);
333 String tableName = splitted[0];
334 if (splitted[1].compareToIgnoreCase("SET") == 0) {
335 ArrayList<String> columnName = new ArrayList<>();
336 ArrayList<String> values = new ArrayList<>();
337 for (int i = 2; i < splitted.length; i++) {
338 if (splitted[i].equals("=")) {
339 columnName.add(splitted[i - 1]);
340 values.add(schrodingarString(splitted[i + 1])); // fnsy101
341 }
342 }
343 values = makeValuesValid(values);
344 if (validateEqualSize(columnName, values) && isValidName(tableName) && isValidNameInArrayList(columnName)
345 && Functions.exists(dbName, tableName)) {
346 Functions result = XmlHandler.load(tableName, dbName);
347 result.update(null, null, null, columnName, values, "NOTWHERE");
348 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
349 } else
350 throw new RuntimeException();
351 }
352
353 }
354
355 // end of update
356 /**
357 * drop Statement
358 *
359 * @param statement
360 * @throws FileNotFoundException
361 */
362 private void dropStatement(final String statement) throws FileNotFoundException {
363 String[] splitted = statement.substring(5, statement.length()).split(" ");
364 splitted = filterSplittedArray(splitted);
365 try {
366 dropOfTable(splitted);
367
368 } catch (Exception x) {
369 dropOfDatabase(splitted);
370 }
371 }
372
373 private void dropOfDatabase(String[] splitted) {
374 if (splitted.length == 2) {
375 if (splitted[0].compareToIgnoreCase("DATABASE") == 0) {
376 Functions.dropDatabase(splitted[1]);
377 } else
378 throw new RuntimeException();
379 } else
380 throw new RuntimeException();
381 }
382
383 private void dropOfTable(String[] splitted) throws FileNotFoundException {
384 try {
385 if (splitted.length == 2) {
386 if (splitted[0].compareToIgnoreCase("TABLE") == 0 && Functions.exists(dbName, splitted[1])) {
387 Functions.dropTable(dbName, splitted[1]);
388 XmlHandler.deleteDtd(splitted[1], dbName);
389 } else
390 throw new RuntimeException();
391 } else
392 throw new RuntimeException();
393 } catch (FileSystemNotFoundException | ParserConfigurationException | SAXException | IOException e) {
394 throw new RuntimeException();
395 }
396
397 }
398 // end of drop
399
400 private void insertionStatement(String statement) throws FileSystemNotFoundException, XMLStreamException,
401 ParserConfigurationException, SAXException, IOException {
402 if (statement.toUpperCase().contains("VALUES")) {
403 String[] splitted = statement.substring(12, statement.length()).split(" ");
404 splitted = filterSplittedArray(splitted);
405 try { // try catch block fnsy101
406 withColumnsNamesInsertionStatement(splitted);
407 } catch (Exception x) {
408 withoutColumnsNamesInsertionStatement(splitted);
409 }
410
411 } else
412 throw new RuntimeException();
413
414 }
415
416 private void withColumnsNamesInsertionStatement(String[] splitted) throws FileSystemNotFoundException,
417 ParserConfigurationException, SAXException, IOException, XMLStreamException {
418 // splitted[0] is the tableName
419 ArrayList<String> columnName = new ArrayList<>();
420 ArrayList<String> valuesToBeInserted = new ArrayList<>();
421 boolean valuesTurn = false;
422 for (int i = 1; i < splitted.length; i++) {
423 if (splitted[i].toUpperCase().equals("VALUES")) {
424 i += 1;
425 valuesTurn = true;
426 }
427 if (!valuesTurn)
428 columnName.add(splitted[i]);
429 else
430 valuesToBeInserted.add(schrodingarString(splitted[i])); // fnsy101
431 }
432 if (validateEqualSize(columnName, valuesToBeInserted) && isValidNameInArrayList(columnName)
433 && isValidName(splitted[0]) && Functions.exists(dbName, splitted[0])) {
434 // fnsy101 removed is valid in// array list
435 Functions result = XmlHandler.load(splitted[0], dbName);
436 result.Insert(columnName, valuesToBeInserted);
437 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
438
439 }
440
441 else {
442
443 throw new RuntimeException();
444
445 }
446 }
447
448 private void withoutColumnsNamesInsertionStatement(String[] splitted) throws XMLStreamException,
449 FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException {
450 // splitted[0] is the tableName // splitted[1] is the word VALUES
451 ArrayList<String> valuesToBeInserted = new ArrayList<>();
452 for (int i = 2; i < splitted.length; i++) {
453 valuesToBeInserted.add(schrodingarString(splitted[i])); // fnsy101
454 }
455
456 if (isValidName(splitted[0]) && Functions.exists(dbName, splitted[0])) { // fnsy101
457 Functions result = XmlHandler.load(splitted[0], dbName);
458 result.Insert(result.getColumnsNames(), valuesToBeInserted);
459 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
460 } else
461 throw new RuntimeException();
462 }
463
464 // end insert
465
466 private void selectStatement(String statement) throws FileSystemNotFoundException, XMLStreamException,
467 ParserConfigurationException, SAXException, IOException {
468 if (statement.toUpperCase().contains("*")) {
469 selectAllStatement(statement);
470 } else {
471 String[] splitted = statement.substring(7, statement.length()).split(" ");
472 splitted = filterSplittedArray(splitted);
473 dedicatedSelectStatement(splitted);
474 }
475
476 }
477
478 private void selectAllStatement(String statement) throws XMLStreamException, FileSystemNotFoundException,
479 ParserConfigurationException, SAXException, IOException {
480 String tableName = new String();
481 String selectAllFromString = statement.substring(0, 13);
482 if (selectAllFromString.compareToIgnoreCase("SELECT * FROM") == 0) {
483 String restOfString = filterString(statement.substring(14, statement.length()));// filter
484 if (statement.toUpperCase().contains("WHERE")) {
485 String[] splitted = filterSplittedArray(restOfString.split(" "));
486 tableName = splitted[0];
487 containsWhereSelectAllStatement(tableName, splitted);
488 } else if (!statement.toUpperCase().contains("WHERE")) {
489 // send tableName String: restOfString
490 if (isValidName(restOfString) && Functions.exists(dbName, restOfString)) {
491 Functions result = XmlHandler.load(tableName, dbName);
492 result.SelectAll();
493 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
494 } else
495 throw new RuntimeException();
496 }
497 } else
498 throw new RuntimeException();
499 }
500
501 private void dedicatedSelectStatement(String[] splitted) throws FileSystemNotFoundException, FileNotFoundException,
502 ParserConfigurationException, SAXException, IOException, XMLStreamException {
503 ArrayList<String> columnsName = new ArrayList<>();
504 int i = 0;
505 int holdIndex;
506 while (i < splitted.length && !splitted[i].toUpperCase().equals("FROM")) {
507 columnsName.add(splitted[i]);
508 i++;
509 }
510 String tableName = splitted[++i];
511 holdIndex = i;
512 try {
513 if (splitted[++i].compareToIgnoreCase("WHERE") == 0) {
514 ArrayList<String> afterFromString = new ArrayList<>();
515 for (int j = holdIndex; j < splitted.length; j++) {
516 afterFromString.add(splitted[j]);
517 }
518 String[] sendToWhere = new String[afterFromString.size()];
519 for (int j = 0; j < afterFromString.size(); j++)
520 sendToWhere[j] = afterFromString.get(j);
521
522 containsWhereDedicatedSelectStatement(tableName, columnsName, splitted);
523 } else
524 throw new RuntimeException();
525 } catch (ArrayIndexOutOfBoundsException x) {
526 if (isValidNameInArrayList(columnsName) && isValidName(tableName) && Functions.exists(dbName, tableName)) {
527 Functions result = XmlHandler.load(tableName, dbName);
528 result.SelectSpecified(columnsName);
529 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
530 } else {
531 throw new RuntimeException();
532 }
533
534 // send ArrayList:columnName String:tableName
535 }
536
537 }
538
539 private void containsWhereDedicatedSelectStatement(String tableName, ArrayList<String> columnsName,
540 String[] splitted) throws XMLStreamException, FileSystemNotFoundException, ParserConfigurationException,
541 SAXException, IOException {
542 if (splitted.length == 5) {
543 // splitted array starts after the word FROM
544 // splitted[0] table name
545 String columnName = splitted[2];
546 String conditionSymbol = splitted[3];
547 String value = schrodingarString(splitted[4]);
548 if (isValidName(columnName) && Functions.exists(dbName, tableName)
549 && (conditionSymbol.equals("=") || conditionSymbol.equals("<") || conditionSymbol.equals(">"))) {
550 Functions result = XmlHandler.load(tableName, dbName);
551 result.SelectSpecifiedWhere(columnsName, columnName, conditionSymbol, value);
552 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
553
554 } else
555 throw new RuntimeException();
556
557 } else
558 throw new RuntimeException();
559 }
560
561 private void containsWhereSelectAllStatement(String tableName, String[] splitted)
562 throws FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException,
563 XMLStreamException {
564 if (splitted.length == 5) {
565 // splitted array starts after the word FROM
566 // splitted[0] table name
567 String columnName = splitted[2];
568 String conditionSymbol = splitted[3];
569 String value = schrodingarString(splitted[4]);
570 if (isValidName(columnName) && Functions.exists(dbName, tableName)
571 && (conditionSymbol.equals("=") || conditionSymbol.equals("<") || conditionSymbol.equals(">"))) {
572 Functions result = XmlHandler.load(tableName, dbName);
573 result.SelectAllWhere(columnName, conditionSymbol, value);
574 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
575 } else
576 throw new RuntimeException();
577
578 } else
579 throw new RuntimeException();
580 }
581
582 private String[] filterSplittedArray(String[] splitted) {
583 String[] filtered = new String[splitted.length];
584 for (int i = 0; i < splitted.length; i++)
585 filtered[i] = filterString(splitted[i]);
586 return filtered;
587 }
588
589 private String filterString(String string) {
590 string = string.replace("(", "").replace(")", "").replace(";", "").replace(",", ""); // fnsy101
591 return string;
592 }
593
594 private String[] filterSplittedArrayForTableCreation(String[] splitted) {
595 String[] filtered = new String[splitted.length];
596 for (int i = 0; i < splitted.length; i++)
597 filtered[i] = splitted[i].replace("(", "").replace(")", "").replace(";", "");
598 return filtered;
599 }
600
601 private boolean validateEqualSize(ArrayList one, ArrayList two) {
602 if (one.size() != two.size())
603 return false;
604 return true;
605 }
606
607 private String schrodingarString(String check) { // fnsy101
608 check = check.trim();
609 if (check.charAt(0) == '\'' && check.charAt(check.length() - 1) == '\''
610 || check.charAt(0) == '\"' && check.charAt(check.length() - 1) == '\"') {
611
612 check = check.replace("#", " ").replace("\"", "").replace("'", "").trim();
613 try {
614 Integer.parseInt(check);
615 throw new RuntimeException();
616 } catch (NumberFormatException x) {
617 return check;
618
619 }
620 } else {
621 Integer.parseInt(check);
622 return check;
623 }
624
625 }
626
627 private String handleSingleQuotes(String statement) { // fnsy101
628 return handleDoubleQuotes(statement);
629 }
630
631 private String handleDoubleQuotes(String statement) { // fnsy101
632 char buf[] = statement.toCharArray(), quote = ' ', c;
633 for (int i = 0; i < buf.length; i++) {
634 if ((c = buf[i]) == '"' || c == '\'')
635 quote = (quote == ' ' ? c : quote == c ? ' ' : quote);
636 else if (c == ' ' && quote != ' ')
637 buf[i] = '#';
638 }
639 return new String(buf).trim();
640 }
641
642 private void initializeReservedWords() {
643 this.reservedWords = new String[] { "ALL", "ALTER", "AND", "ANY", "ARRAY", "ARROW", "AS", "ASC", "AT", "BEGIN",
644 "BETWEEN", "BY", "CASE", "CHECK", "CLUSTERS", "CLUSTER", "COLAUTH", "COLUMNS", "COMPRESS", "CONNECT",
645 "CRASH", "CREATE", "CURRENT", "DECIMAL", "DECLARE", "DEFAULT", "DELETE", "DESC", "DISTINCT", "DROP",
646 "ELSE", "END", "EXCEPTION", "EXCLUSIVE", "EXISTS", "FETCH", "FORM", "FOR", "FROM", "GOTO", "GRANT",
647 "GROUP", "HAVING", "IDENTIFIED", "IF", "IN", "INDEXES", "INDEX", "INSERT", "INTERSECT", "INTO", "IS",
648 "LIKE", "LOCK", "MINUS", "MODE", "NOCOMPRESS", "NOT", "NOWAIT", "NULL", "OF", "ON", "OPTION", "OR",
649 "ORDER", "OVERLAPS", "PRIOR", "PROCEDURE", "RANGE", "RECORD", "RESOURCE", "REVOKE", "SELECT", "SHARE",
650 "SIZE", "SQL", "SUBTYPE", "TABAUTH", "TABLE", "THEN", "TO", "TYPE", "UNION", "UNIQUE", "UPDATE", "USE",
651 "VALUES", "VIEW", "VIEWS", "WHEN", "WHERE", "WITH" };
652
653 }
654
655 private final boolean isValidStatement(final String statement) {
656 int semiColonIndex = statement.length() - 1;
657 if (!(statement.charAt(semiColonIndex) == ';'))
658 return false;
659
660 return true;
661 }
662
663 private final boolean isValidName(String name) {
664 name = name.toUpperCase();
665 for (int i = 0; i < name.length(); i++) {
666 int singleChar = name.charAt(i);
667 if ((singleChar < 65 && singleChar != 36) || (singleChar > 90 && singleChar != 95)) {
668 if (singleChar < 48 || singleChar > 57)
669 return false;
670 }
671 }
672 return true;
673 }
674
675 private final boolean isValidNameInArrayList(ArrayList<String> list) {
676 for (int i = 0; i < list.size(); i++) {
677 if (!isValidName(list.get(i)))
678 return false;
679 }
680 return true;
681 }
682
683 private ArrayList<String> makeValuesValid(ArrayList<String> list) {
684 ArrayList<String> ret = new ArrayList<>();
685 for (int i = 0; i < list.size(); i++) {
686 ret.add(schrodingarString(list.get(i)));
687 }
688 return ret;
689
690 }
691
692 private boolean isReservedWord(String name) {
693 name = name.toUpperCase();
694 for (int i = 0; i < this.reservedWords.length; i++)
695 if (name.equals(this.reservedWords[i]))
696 return true;
697 return false;
698 }
699
700 private String replaceVarchar(String statement) {
701 Pattern pattern = Pattern.compile("\\(\\d+\\)");
702 Matcher matcher = pattern.matcher(statement);
703 while (matcher.find()) {
704 statement = statement.replace(matcher.group(), "");
705 }
706 return statement.replace("varchar", "String");
707 }
708
709 private String removeWhiteSpaces(final String statement) {
710 String checkedStatement;
711 checkedStatement = statement.replaceAll("=", " = ");
712 checkedStatement = checkedStatement.replace("\n", " ");
713 checkedStatement = checkedStatement.replaceAll("`", "'"); // fnsy101
714 checkedStatement = checkedStatement.replaceAll("\t", " "); // fnsy101
715 checkedStatement = handleSingleQuotes(checkedStatement);
716 checkedStatement = handleDoubleQuotes(checkedStatement);
717 checkedStatement = checkedStatement.replace("(", " (");
718 checkedStatement = checkedStatement.replaceAll(">", " > ");
719 checkedStatement = checkedStatement.replaceAll("<", " < ");
720 checkedStatement = checkedStatement.replaceAll(" +", " ");
721 checkedStatement = checkedStatement.replaceAll(" ,", ", ");
722 checkedStatement = checkedStatement.replaceAll(",", ", ");
723 checkedStatement = checkedStatement.replaceAll(" ;", "; ");
724 checkedStatement = checkedStatement.replaceAll("\\( ", "\\(");
725 checkedStatement = checkedStatement.replaceAll(" \\)", "\\)");
726 checkedStatement = checkedStatement.replaceAll("\\*", " \\* ");
727 checkedStatement = checkedStatement.replaceAll("\n", " ");
728 checkedStatement = checkedStatement.replaceAll(" +", " ");
729 checkedStatement = checkedStatement.trim();
730 return checkedStatement;
731 }
732
733}