· 9 years ago · Nov 28, 2016, 09:00 PM
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;
13
14import org.xml.sax.SAXException;
15
16/**
17 * This class parsers and conquers the SQL statements entered. To check if the
18 * entered statement is true, and send the creation order to the creation class
19 * or the operations to the operations class
20 *
21 * @author FNSY
22 *
23 */
24public class StatementsParser {
25 // private String statement;
26 private static StatementsParser statementsParser = null;
27 private String[] reservedWords;
28
29 /**
30 * current database name
31 */
32 private String dbName = null;
33
34 protected StatementsParser() {
35 initializeReservedWords();
36 }
37
38 public static synchronized StatementsParser createObject() {
39 if (statementsParser == null) {
40 statementsParser = new StatementsParser();
41 }
42 return statementsParser;
43 }
44
45 public final void enterStatement(final String statement) throws Exception {
46 String checkedStatement;
47 checkedStatement = removeWhiteSpaces(statement);
48
49 String upperCaseStatement = checkedStatement.toUpperCase();
50 try {
51 selectRightAction(upperCaseStatement.split(" "), checkedStatement);
52 } catch (Exception x) {
53 System.out.println("PLEASE ENTER A VALID STATEMENT");
54 }
55 }
56
57 private void selectRightAction(String[] upperCaseStatement, String checkedStatement)
58 throws ParserConfigurationException, SAXException, IOException, FileSystemNotFoundException,
59 XMLStreamException {
60 if (isValidStatement(checkedStatement))
61 if (upperCaseStatement[0].equals("USE"))
62 useStatement(checkedStatement.split(" ")); // done
63 else if (upperCaseStatement[0].equals("CREATE"))
64 creationStatement(checkedStatement); // done
65 else if (upperCaseStatement[0].equals("DELETE"))
66 deletionStatement(checkedStatement);
67 else if (upperCaseStatement[0].equals("UPDATE"))
68 updateStatement(checkedStatement);
69 else if (upperCaseStatement[0].equals("DROP"))
70 dropStatement(checkedStatement);
71 else if (upperCaseStatement[0].equals("INSERT") && upperCaseStatement[1].equals("INTO"))
72 insertionStatement(checkedStatement);
73 else if (upperCaseStatement[0].equals("SELECT"))
74 selectStatement(checkedStatement);
75 else
76 throw new RuntimeException();
77 }
78
79 /**
80 * This method checks if the SQL statement is written in right format. with
81 * no extra spaces and trailed by a semicolon
82 *
83 * @param statement
84 * @return
85 */
86
87 private void useStatement(String[] splitted) { // done check
88 splitted = filterSplittedArray(splitted);
89 if (splitted.length == 2) {
90 dbName = new String(splitted[1]);
91 }
92 }
93
94 /**
95 * create statement
96 *
97 * @param statement
98 * @throws IOException
99 * @throws SAXException
100 * @throws ParserConfigurationException
101 */
102 private final void creationStatement(final String statement)
103 throws ParserConfigurationException, SAXException, IOException {
104 String restOfTheString = statement.substring(7, statement.length() - 1);
105 try {
106 creationOfDatabase(restOfTheString);
107
108 } catch (RuntimeException x) {
109 creationOfTable(restOfTheString);
110 }
111
112 }
113
114 final void creationOfTable(String statement) throws ParserConfigurationException, SAXException, IOException { // done
115
116 statement = replaceVarchar(statement);
117 statement = removeWhiteSpaces(statement);
118 String tableString = filterString(statement.substring(0, 5));
119 if (tableString.compareToIgnoreCase("TABLE") == 0) {
120 String[] tableDetails = statement.substring(6, statement.length()).split(" ");
121 tableDetails = filterSplittedArrayForTableCreation(tableDetails);
122 int length = tableDetails.length;
123 if (isValidName(tableDetails[0]) && !isReservedWord(tableDetails[0])) {
124
125 ArrayList<String> columnName = new ArrayList<>();
126 ArrayList<String> dataType = new ArrayList<>();
127 for (int i = 1; i < length; i++) {
128 if (i % 2 == 1) {
129 columnName.add(tableDetails[i]);
130 } else {
131 if (tableDetails[i].replace(",", "").equals("String")
132 || tableDetails[i].replace(",", "").equals("int")) {
133 if (tableDetails[i].contains(",")) {
134 dataType.add(filterString(tableDetails[i]));
135
136 } else if (!tableDetails[i].contains(",")) {
137 try {
138 String checkLast = tableDetails[i + 1];
139 throw new RuntimeException();
140 } catch (ArrayIndexOutOfBoundsException x) {
141 dataType.add(tableDetails[i]);
142 }
143 }
144 } else
145 throw new RuntimeException();
146 }
147 }
148 if (validateEqualSize(columnName, dataType) && isValidNameInArrayList(columnName)) { // checking
149 // lengths
150 // and
151 // names
152 Functions x = new Functions(tableDetails[0], columnName, dataType);
153 x.createTable(dbName);
154 XmlHandler.createDtd(columnName, tableDetails[0], dbName, 0);
155 List<List<String>> rowsList = Arrays.asList();
156 TableFormatter.print(columnName, rowsList);
157 }
158 } else
159 throw new RuntimeException();
160
161 } else
162 throw new RuntimeException();
163 }
164
165 private final void creationOfDatabase(final String statement) { // done
166 // check
167 String databaseString = statement.substring(0, 8);
168 if (databaseString.compareToIgnoreCase("DATABASE") == 0) {
169 String databaseName = filterString(statement.substring(9, statement.length()));
170 if (isValidName(databaseName) && !isReservedWord(databaseName)) {
171 Functions.createDatabase(databaseName);
172 }
173 } else
174 throw new RuntimeException();
175 }
176
177 // end of creation
178 private void deletionStatement(String statement) {
179 String deleteString = statement.substring(0, 11);
180 if (deleteString.compareToIgnoreCase("DELETE FROM") == 0 && statement.toUpperCase().contains("WHERE")) {
181 String restOfString = statement.substring(12, statement.length() - 1);
182 String tableName, conditionSymbol, columnName, valueIndicatingRow;
183 String[] splitted = restOfString.split(" ");
184 tableName = splitted[0];
185 for (int i = 0; i < splitted.length; i++) {
186 if (splitted[i].compareToIgnoreCase("WHERE") == 0 && (splitted[i + 2].equals("=")
187 || splitted[i + 2].equals("<") || splitted[i + 2].equals(">"))) {
188 columnName = splitted[i + 1];
189 conditionSymbol = splitted[i + 2];
190 valueIndicatingRow = splitted[i + 3];
191 try {
192 if (isValidName(tableName) && isValidName(columnName) && Functions.exists(dbName, tableName)) {
193 Functions result;
194 try {
195 result = XmlHandler.load(tableName);
196 result.deleteWhere(columnName, conditionSymbol, schrodingarString(valueIndicatingRow));
197 TableFormatter.print(result.getColumnsNames(),
198 TableFormatter.toList(result.getTotalTable()));
199 } catch (FileNotFoundException | XMLStreamException e) {
200 // TODO Auto-generated catch block
201 e.printStackTrace();
202 }
203 } else
204 throw new RuntimeException();
205 } catch (FileSystemNotFoundException | ParserConfigurationException | SAXException
206 | IOException e) {
207 throw new RuntimeException();
208 }
209 break;
210 }
211 }
212 } else
213 try {
214 deleteAllTable(statement);
215 } catch (FileSystemNotFoundException | ParserConfigurationException | SAXException | IOException e) {
216 throw new RuntimeException();
217 }
218 }
219
220 private void deleteAllTable(String statement)
221 throws FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException {
222 String deleteAllString = statement.substring(0, 11);
223 String deleteAllAstString = statement.substring(0, 13);
224 String tableName;
225 if (deleteAllString.compareToIgnoreCase("DELETE FROM") == 0) {
226 tableName = statement.substring(12, statement.length() - 1);
227 if (isValidName(tableName) && Functions.exists(dbName, tableName)) {
228 Functions result = null;
229 try {
230 result = XmlHandler.load(tableName);
231 result.delete();
232 List<List<String>> rowsList = Arrays.asList();
233 TableFormatter.print(result.getColumnsNames(), rowsList);
234 } catch (FileNotFoundException | XMLStreamException e) {
235 throw new RuntimeException();
236 }
237
238 } else
239 throw new RuntimeException();
240
241 } else if (deleteAllAstString.compareToIgnoreCase("DELETE * FROM") == 0) {
242 tableName = statement.substring(14, statement.length() - 1);
243 if (isValidName(tableName) && Functions.exists(dbName, tableName)) {
244 Functions result = null;
245 try {
246 result = XmlHandler.load(tableName);
247 result.delete();
248 List<List<String>> rowsList = Arrays.asList();
249 TableFormatter.print(result.getColumnsNames(), rowsList);
250 } catch (FileNotFoundException | XMLStreamException e) {
251 throw new RuntimeException();
252 }
253
254 } else
255 throw new RuntimeException();
256 }
257
258 }
259 // end of delete
260
261 private void updateStatement(String statement) throws FileSystemNotFoundException, ParserConfigurationException,
262 SAXException, IOException, XMLStreamException {
263 String[] splitted = statement.substring(7, statement.length()).split(" ");
264 splitted = filterSplittedArray(splitted);
265 if (statement.toUpperCase().contains("SET") && statement.toUpperCase().contains("WHERE")) {
266 containingWhereUpdateStatement(splitted);
267 } else if (statement.toUpperCase().contains("SET") && !statement.toUpperCase().contains("WHERE")) {
268 notContainingWhereUpdateStatement(splitted);
269 }
270
271 }
272
273 private void containingWhereUpdateStatement(String[] splitted)
274 throws FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException {
275 splitted = filterSplittedArray(splitted);
276 String conditionSymbol = null;
277 String tableName = splitted[0];
278 if (splitted[1].compareToIgnoreCase("SET") == 0) {
279 ArrayList<String> columnName = new ArrayList<>();
280 ArrayList<String> values = new ArrayList<>();
281 String guideColumn = null, guideValue = null;
282 for (int i = 2; i < splitted.length; i++) {
283 if (splitted[i].compareToIgnoreCase("WHERE") != 0) {
284 if (i % 3 == 0)
285 if (splitted[i].equals("=")) {
286 columnName.add(splitted[i - 1]);
287 if (splitted[i + 1].compareToIgnoreCase("WHERE") != 0)
288 values.add(splitted[i + 1]);
289 } else
290 throw new RuntimeException();
291
292 } else if (splitted[i + 2].equals("=") || splitted[i + 2].equals("<") || splitted[i + 2].equals(">")) {
293
294 guideColumn = splitted[i + 1];
295 conditionSymbol = splitted[i + 2];
296 guideValue = splitted[i + 3];
297 break;
298 } else {
299 throw new RuntimeException();
300 }
301 }
302 values = makeValuesValid(values);
303 guideValue = schrodingarString(guideValue);
304 if (validateEqualSize(columnName, values) && isValidName(tableName) && isValidName(guideColumn)
305 && isValidName(guideValue) && isValidNameInArrayList(columnName)
306 && Functions.exists(dbName, tableName)) {
307 Functions result = null;
308 try {
309 result = XmlHandler.load(tableName);
310 result.update(conditionSymbol, guideColumn, guideValue, columnName, values, "WHERE");
311 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
312 } catch (FileNotFoundException | XMLStreamException e) {
313 throw new RuntimeException();
314
315 }
316
317 } else
318 throw new RuntimeException();
319
320 } else
321 throw new RuntimeException();
322
323 }
324
325 private void notContainingWhereUpdateStatement(String[] splitted) throws FileSystemNotFoundException,
326 FileNotFoundException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
327 splitted = filterSplittedArray(splitted);
328 String tableName = splitted[0];
329 if (splitted[1].compareToIgnoreCase("SET") == 0) {
330 ArrayList<String> columnName = new ArrayList<>();
331 ArrayList<String> values = new ArrayList<>();
332 for (int i = 2; i < splitted.length; i++) {
333 if (splitted[i].equals("=")) {
334 columnName.add(splitted[i - 1]);
335 values.add(schrodingarString(splitted[i + 1])); // fnsy101
336 }
337 }
338 values = makeValuesValid(values);
339 if (validateEqualSize(columnName, values) && isValidName(tableName) && isValidNameInArrayList(columnName)
340 && Functions.exists(dbName, tableName)) {
341 Functions result = XmlHandler.load(tableName);
342 result.update(null, null, null, columnName, values, "NOTWHERE");
343 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
344 } else
345 throw new RuntimeException();
346 }
347
348 }
349
350 // end of update
351 /**
352 * drop Statement
353 *
354 * @param statement
355 * @throws FileNotFoundException
356 */
357 private void dropStatement(final String statement) throws FileNotFoundException {
358 String[] splitted = statement.substring(5, statement.length()).split(" ");
359 splitted = filterSplittedArray(splitted);
360 try {
361 dropOfTable(splitted);
362
363 } catch (Exception x) {
364 dropOfDatabase(splitted);
365 }
366 }
367
368 private void dropOfDatabase(String[] splitted) {
369 if (splitted.length == 2) {
370 if (splitted[0].compareToIgnoreCase("DATABASE") == 0) {
371 Functions.dropDatabase(splitted[1]);
372 } else
373 throw new RuntimeException();
374 } else
375 throw new RuntimeException();
376 }
377
378 private void dropOfTable(String[] splitted) throws FileNotFoundException {
379 try {
380 if (splitted.length == 2) {
381 if (splitted[0].compareToIgnoreCase("TABLE") == 0 && Functions.exists(dbName, splitted[1])) {
382 Functions.dropTable(dbName, splitted[1]);
383 XmlHandler.deleteDtd(splitted[1], dbName);
384 } else
385 throw new RuntimeException();
386 } else
387 throw new RuntimeException();
388 } catch (FileSystemNotFoundException | ParserConfigurationException | SAXException | IOException e) {
389 throw new RuntimeException();
390 }
391
392 }
393 // end of drop
394
395 private void insertionStatement(String statement) throws FileSystemNotFoundException, XMLStreamException,
396 ParserConfigurationException, SAXException, IOException {
397 if (statement.toUpperCase().contains("VALUES")) {
398 String[] splitted = statement.substring(12, statement.length()).split(" ");
399 splitted = filterSplittedArray(splitted);
400 try { // try catch block fnsy101
401 withColumnsNamesInsertionStatement(splitted);
402
403 } catch (Exception x) {
404 withoutColumnsNamesInsertionStatement(splitted);
405
406 }
407
408 } else
409 throw new RuntimeException();
410
411 }
412
413 private void withColumnsNamesInsertionStatement(String[] splitted) throws FileSystemNotFoundException,
414 ParserConfigurationException, SAXException, IOException, XMLStreamException {
415 // splitted[0] is the tableName
416 ArrayList<String> columnName = new ArrayList<>();
417 ArrayList<String> valuesToBeInserted = new ArrayList<>();
418 boolean valuesTurn = false;
419 for (int i = 1; i < splitted.length; i++) {
420 if (splitted[i].toUpperCase().equals("VALUES")) {
421 i += 1;
422 valuesTurn = true;
423 }
424 if (!valuesTurn)
425 columnName.add(splitted[i]);
426 else
427 valuesToBeInserted.add(schrodingarString(splitted[i])); // fnsy101
428 }
429 if (validateEqualSize(columnName, valuesToBeInserted) && isValidNameInArrayList(columnName)
430 && isValidName(splitted[0]) && Functions.exists(dbName, splitted[0])) {
431 // fnsy101 removed is valid in// array list
432 Functions result = XmlHandler.load(splitted[0]);
433 result.Insert(columnName, valuesToBeInserted);
434 TableFormatter.print(result.getColumnsNames(), TableFormatter.toList(result.getTotalTable()));
435
436 }
437
438 else {
439
440 throw new RuntimeException();
441
442 }
443 }
444
445 private void withoutColumnsNamesInsertionStatement(String[] splitted) throws XMLStreamException,
446 FileSystemNotFoundException, ParserConfigurationException, SAXException, IOException {
447 // splitted[0] is the tableName // splitted[1] is the word VALUES
448 ArrayList<String> valuesToBeInserted = new ArrayList<>();
449 for (int i = 2; i < splitted.length; i++) {
450 valuesToBeInserted.add(schrodingarString(splitted[i])); // fnsy101
451 }
452 if (!Functions.exists(dbName, splitted[0])) {
453 System.out.println("ÙÙŠ ØØ§Ø¬Ø© غلط");
454 }
455 if (isValidName(splitted[0]) && Functions.exists(dbName, splitted[0])) { // fnsy101
456 System.out.println("هنااا");
457 Functions result = XmlHandler.load(splitted[0]);
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);
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);
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);
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);
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 check = check.replace("±", ";").replace("§", ",").replace("«", "\"").replace("»", "'").replace("π", "(")
614 .replace("α", ")").trim();// تعالي
615 try {
616 Integer.parseInt(check);
617 throw new RuntimeException();
618 } catch (NumberFormatException x) {
619 return check;
620
621 }
622 } else {
623 Integer.parseInt(check);
624 return check;
625 }
626
627 }
628
629 private String handleSingleQuotes(String statement) { // fnsy101
630 Pattern word = Pattern.compile("'[^\"]*'");
631 Matcher matcher = word.matcher(statement);
632 StringBuilder newString = new StringBuilder();
633 while (matcher.find()) {
634 newString.append(matcher.group(0));
635 }
636 String hashed = newString.toString();
637 if (hashed.length() > 0) {
638 String carry = hashed.substring(1, hashed.length() - 1);
639 hashed = hashed.substring(1, hashed.length() - 1);
640 hashed = hashed.replaceAll(" ", "#").replace(";", "±").replace(",", "§").replace("\"", "«")
641 .replace("'", "»").replace("(", "π").replace(")", "α");
642 statement = statement.replace(carry, hashed);
643 }
644 return statement;
645 }
646
647 private String handleDoubleQuotes(String statement) { // fnsy101
648 Pattern word = Pattern.compile("\"[^\"]*\"");
649 Matcher matcher = word.matcher(statement);
650 StringBuilder newString = new StringBuilder();
651 while (matcher.find()) {
652 newString.append(matcher.group(0));
653 }
654 String hashed = newString.toString();
655 if (hashed.length() > 0) {
656 String carry = hashed.substring(1, hashed.length() - 1);
657 hashed = hashed.substring(1, hashed.length() - 1);
658 hashed = hashed.replaceAll(" ", "#").replace(";", "±").replace(",", "§").replace("\"", "«")
659 .replace("'", "»").replace("(", "π").replace(")", "α");// تعالي
660 statement = statement.replace(carry, hashed);
661 }
662 return statement;
663 }
664
665 private void initializeReservedWords() {
666 this.reservedWords = new String[] { "ALL", "ALTER", "AND", "ANY", "ARRAY", "ARROW", "AS", "ASC", "AT", "BEGIN",
667 "BETWEEN", "BY", "CASE", "CHECK", "CLUSTERS", "CLUSTER", "COLAUTH", "COLUMNS", "COMPRESS", "CONNECT",
668 "CRASH", "CREATE", "CURRENT", "DECIMAL", "DECLARE", "DEFAULT", "DELETE", "DESC", "DISTINCT", "DROP",
669 "ELSE", "END", "EXCEPTION", "EXCLUSIVE", "EXISTS", "FETCH", "FORM", "FOR", "FROM", "GOTO", "GRANT",
670 "GROUP", "HAVING", "IDENTIFIED", "IF", "IN", "INDEXES", "INDEX", "INSERT", "INTERSECT", "INTO", "IS",
671 "LIKE", "LOCK", "MINUS", "MODE", "NOCOMPRESS", "NOT", "NOWAIT", "NULL", "OF", "ON", "OPTION", "OR",
672 "ORDER", "OVERLAPS", "PRIOR", "PROCEDURE", "RANGE", "RECORD", "RESOURCE", "REVOKE", "SELECT", "SHARE",
673 "SIZE", "SQL", "SUBTYPE", "TABAUTH", "TABLE", "THEN", "TO", "TYPE", "UNION", "UNIQUE", "UPDATE", "USE",
674 "VALUES", "VIEW", "VIEWS", "WHEN", "WHERE", "WITH" };
675
676 }
677
678 private final boolean isValidStatement(final String statement) {
679 int semiColonIndex = statement.length() - 1;
680 if (!(statement.charAt(semiColonIndex) == ';'))
681 return false;
682
683 return true;
684 }
685
686 private final boolean isValidName(String name) {
687 name = name.toUpperCase();
688 for (int i = 0; i < name.length(); i++) {
689 int singleChar = name.charAt(i);
690 if ((singleChar < 65 && singleChar != 36) || (singleChar > 90 && singleChar != 95)) {
691 if (singleChar < 48 || singleChar > 57)
692 return false;
693 }
694 }
695 return true;
696 }
697
698 private final boolean isValidNameInArrayList(ArrayList<String> list) {
699 for (int i = 0; i < list.size(); i++) {
700 if (!isValidName(list.get(i)))
701 return false;
702 }
703 return true;
704 }
705
706 private ArrayList<String> makeValuesValid(ArrayList<String> list) {
707 ArrayList<String> ret = new ArrayList<>();
708 for (int i = 0; i < list.size(); i++) {
709 ret.add(schrodingarString(list.get(i)));
710 }
711 return ret;
712
713 }
714
715 private boolean isReservedWord(String name) {
716 name = name.toUpperCase();
717 for (int i = 0; i < this.reservedWords.length; i++)
718 if (name.equals(this.reservedWords[i]))
719 return true;
720 return false;
721 }
722
723 private String replaceVarchar(String statement) {
724 Pattern pattern = Pattern.compile("\\(\\d+\\)");
725 Matcher matcher = pattern.matcher(statement);
726 while (matcher.find()) {
727 statement = statement.replace(matcher.group(), "");
728 }
729 return statement.replace("varchar", "String");
730 }
731
732 private String removeWhiteSpaces(final String statement) {
733 String checkedStatement;
734 checkedStatement = statement.replaceAll("=", " = ");
735 checkedStatement = checkedStatement.replace("\n", " ");
736 checkedStatement = checkedStatement.replaceAll("`", "'"); // fnsy101
737 checkedStatement = checkedStatement.replaceAll("\t", " "); // fnsy101
738 checkedStatement = handleSingleQuotes(checkedStatement);
739 checkedStatement = handleDoubleQuotes(checkedStatement);
740 checkedStatement = checkedStatement.replace("(", " (");
741 checkedStatement = checkedStatement.replaceAll(">", " > ");
742 checkedStatement = checkedStatement.replaceAll("<", " < ");
743 checkedStatement = checkedStatement.replaceAll(" +", " ");
744 checkedStatement = checkedStatement.replaceAll(" ,", ", ");
745 checkedStatement = checkedStatement.replaceAll(",", ", ");
746 checkedStatement = checkedStatement.replaceAll(" ;", "; ");
747 checkedStatement = checkedStatement.replaceAll("\\( ", "\\(");
748 checkedStatement = checkedStatement.replaceAll(" \\)", "\\)");
749 checkedStatement = checkedStatement.replaceAll("\\*", " \\* ");
750 checkedStatement = checkedStatement.replaceAll("\n", " ");
751 checkedStatement = checkedStatement.replaceAll(" +", " ");
752 checkedStatement = checkedStatement.trim();
753 return checkedStatement;
754 }
755
756}