Skip to content

Commit dec7fb4

Browse files
committed
tokenized where into clausules
1 parent a51e599 commit dec7fb4

File tree

6 files changed

+298
-15
lines changed

6 files changed

+298
-15
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ExceptionFramework/src/main/java/edu/upvictoria/poo/Analyzer.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class Analyzer {
1313
private final ArrayList<String> keywords = new ArrayList<>();
1414
private final ArrayList<String> dataTypes = new ArrayList<>();
15-
private final ArrayList<String> dataModifiers = new ArrayList<>();
15+
private final ArrayList<String> operators = new ArrayList<>();
1616
private final ArrayList<String> constraints = new ArrayList<>();
1717

1818
private final SQL sql = new SQL();
@@ -33,9 +33,16 @@ public Analyzer(){
3333
keywords.add("FROM");
3434
keywords.add("WHERE");
3535

36-
dataModifiers.add("AND");
37-
dataModifiers.add("OR");
38-
dataModifiers.add("AS");
36+
operators.add("AND");
37+
operators.add("OR");
38+
operators.add("=");
39+
operators.add("<");
40+
operators.add(">");
41+
operators.add("<=");
42+
operators.add(">=");
43+
operators.add("!=");
44+
operators.add("(");
45+
operators.add(")");
3946

4047
constraints.add("NOT NULL");
4148
constraints.add("PRIMARY KEY");
@@ -184,8 +191,8 @@ public ArrayList<String> getConstraints() {
184191
return constraints;
185192
}
186193

187-
public ArrayList<String> getDataModifiers() {
188-
return dataModifiers;
194+
public ArrayList<String> getOperators() {
195+
return operators;
189196
}
190197

191198
public void refreshDB(File file) throws FileSystemException{

ExceptionFramework/src/main/java/edu/upvictoria/poo/SQL.java

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package edu.upvictoria.poo;
22

3-
import edu.upvictoria.poo.exceptions.ColumnDoesNotMatch;
4-
import edu.upvictoria.poo.exceptions.DataTypeNotFoundException;
5-
import edu.upvictoria.poo.exceptions.DuplicateEntryException;
6-
import edu.upvictoria.poo.exceptions.InsuficientDataProvidedException;
3+
import edu.upvictoria.poo.exceptions.*;
74

85
import java.io.*;
96
import java.nio.file.*;
107
import java.util.ArrayList;
118
import java.util.Arrays;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
1211

1312
public class SQL {
1413
public String clean(String line, String keyword) throws StringIndexOutOfBoundsException {
@@ -364,10 +363,12 @@ public void handleDeleteFrom(String line, String keyword){
364363
public void handleUpdate(String line, String keyword){
365364
}
366365

367-
public void handleSelect(String line, String keyword, Database database) throws StringIndexOutOfBoundsException, NoSuchFileException, ColumnDoesNotMatch {
366+
public void handleSelect(String line, String keyword, Database database) throws SQLSyntaxException,
367+
StringIndexOutOfBoundsException, NoSuchFileException, ColumnDoesNotMatch {
368368
ArrayList<String> columns = new ArrayList<>();
369369
ArrayList<String> showingCol = new ArrayList<>();
370-
String cleanedLine, selectedColumns, selectedTable;
370+
String cleanedLine, selectedColumns, selectedTable, whereLine = null;
371+
ArrayList<String> whereTokens = new ArrayList<>();
371372
boolean tableExists = false;
372373

373374
try{
@@ -376,6 +377,7 @@ public void handleSelect(String line, String keyword, Database database) throws
376377

377378
if(cleanedLine.contains("WHERE")){
378379
selectedTable = cleanedLine.substring(cleanedLine.indexOf("FROM ") + "FROM".length() + 1, cleanedLine.indexOf(" WHERE")).trim();
380+
whereLine = cleanedLine.substring(cleanedLine.indexOf("WHERE") + "WHERE".length() + 1).trim();
379381
} else {
380382
selectedTable = cleanedLine.substring(cleanedLine.indexOf("FROM ") + "FROM".length() + 1).trim();
381383
}
@@ -394,6 +396,11 @@ public void handleSelect(String line, String keyword, Database database) throws
394396
if (table.getTableName().equals(selectedTable)) {
395397
tableExists = true;
396398

399+
if(whereLine != null){
400+
whereTokens = getWhereTokens(whereLine,table);
401+
whereTokens = Where.infixToPostfix(whereTokens);
402+
}
403+
397404
if(!selectedColumns.equals("*")){
398405
for(String tableColName : table.getColumnsName()){
399406
for(String selectColName : columns) {
@@ -421,8 +428,81 @@ public void handleSelect(String line, String keyword, Database database) throws
421428
}
422429
}
423430

424-
public void handleWhere(Analyzer analyzer){
425-
ArrayList<String> dataModifiers = analyzer.getDataModifiers();
431+
public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSyntaxException, IndexOutOfBoundsException {
432+
Analyzer analyzer = new Analyzer();
433+
ArrayList<String> operators = analyzer.getOperators();
434+
ArrayList<String> whereTokens = new ArrayList<>();
435+
String value;
436+
437+
ArrayList<String> homunculus = new ArrayList<>(operators);
438+
homunculus.addAll(table.getColumnsName());
439+
boolean foundKeyword;
440+
441+
String format1 = "^'.+'";
442+
String format2 = "^\\d*\\s+";
443+
String format3 = "^\\d*$";
444+
Pattern pattern1 = Pattern.compile(format1);
445+
Pattern pattern2 = Pattern.compile(format2);
446+
Pattern pattern3 = Pattern.compile(format3);
447+
Matcher matcher1, matcher2, matcher3;
448+
449+
line = line.replaceAll("<>","!=");
450+
line = line.replaceAll("^=","!=");
451+
452+
try {
453+
for(int i = 0; i < homunculus.size(); i++){
454+
String homunculee = homunculus.get(i);
455+
if(line.startsWith(homunculee)){
456+
whereTokens.add(homunculee);
457+
line = line.substring(line.indexOf(homunculee) + homunculee.length()).trim();
458+
i = -1;
459+
continue;
460+
}
461+
462+
if(line.startsWith("NULL")){
463+
whereTokens.add("\0");
464+
line = line.substring(line.indexOf("NULL") + "NULL".length()).trim();
465+
i = -1;
466+
continue;
467+
}
468+
469+
matcher1 = pattern1.matcher(line);
470+
if(matcher1.find()){
471+
line = line.substring(1);
472+
value = line.substring(0, line.indexOf("'")).trim();
473+
whereTokens.add(value);
474+
line = line.substring(line.indexOf(value) + value.length() + 1).trim();
475+
i = -1;
476+
continue;
477+
}
478+
479+
matcher2 = pattern2.matcher(line);
480+
if(matcher2.find()) {
481+
value = line.substring(0, line.indexOf(" ")).trim();
482+
whereTokens.add(value);
483+
line = line.substring(line.indexOf(value) + value.length() + 1).trim();
484+
i = -1;
485+
continue;
486+
}
487+
488+
matcher3 = pattern3.matcher(line);
489+
if(matcher3.find()) {
490+
value = line.trim();
491+
whereTokens.add(value);
492+
break;
493+
}
494+
495+
if(i == homunculus.size() - 1){
496+
throw new SQLSyntaxException("WHERE STATEMENT MALFORMED AT > " + line);
497+
}
498+
}
499+
} catch (SQLSyntaxException e) {
500+
throw new SQLSyntaxException(e.getMessage());
501+
} catch (IndexOutOfBoundsException e) {
502+
throw new IndexOutOfBoundsException("WHERE STATEMENT MALFORMED AT > " + line);
503+
}
504+
505+
return whereTokens;
426506
}
427507
}
428508

ExceptionFramework/src/main/java/edu/upvictoria/poo/Table.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Table(File tableFile, ArrayList<Column> columns){
3030
}
3131

3232
//para recuperar tabla ya existente
33-
public Table(File tableFile) throws FileSystemException {
33+
public Table(File tableFile) throws FileSystemException {
3434
this.tableFile = tableFile;
3535
int a = tableFile.getName().indexOf('.');
3636
this.tableName = tableFile.getName().substring(0,a);
@@ -183,6 +183,11 @@ public void writeDataToFile(ArrayList<String> rowData) throws IOException {
183183
}
184184

185185
public void printData(){
186+
for(Column col : columns){
187+
System.out.print("| " + col.getName() + "\t");
188+
}
189+
System.out.println("|");
190+
186191
for (ArrayList<Object> datum : data) {
187192
for (Object object : datum) {
188193
System.out.print("| " + object.toString() + "\t");

0 commit comments

Comments
 (0)