Skip to content

Commit 59e99b1

Browse files
committed
DELETE finished
1 parent 767c273 commit 59e99b1

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ public void analyzeSyntax(String line) throws Exception {
126126
}
127127

128128
refreshDB(this.database.getDbFile());
129-
sql.handleDeleteFrom(line, keyword);
129+
sql.handleDeleteFrom(line, keyword, database);
130130
refreshDB(this.database.getDbFile());
131131
return;
132132

133133
case "UPDATE":
134134
refreshDB(this.database.getDbFile());
135-
sql.handleUpdate(line, keyword);
135+
sql.handleUpdate(line, keyword, this.database);
136136
refreshDB(this.database.getDbFile());
137137
return;
138138

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

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.upvictoria.poo.exceptions.*;
44

5+
import javax.xml.crypto.Data;
56
import java.io.*;
67
import java.nio.file.*;
78
import java.util.ArrayList;
@@ -20,7 +21,7 @@ public String clean(String line, String keyword) throws StringIndexOutOfBoundsEx
2021
throw new StringIndexOutOfBoundsException();
2122
}
2223

23-
return line;
24+
return line.trim();
2425
}
2526

2627
public File handleUse(String line, String keyword) throws FileSystemException, StringIndexOutOfBoundsException, FileNotFoundException {
@@ -357,32 +358,71 @@ public void handleInsertInto(String line, String keyword, Database database) thr
357358
table.appendDataToTable(insertionData,insertionColumns);
358359
}
359360

360-
public void handleDeleteFrom(String line, String keyword){
361+
public void handleDeleteFrom(String line, String keyword, Database database) throws IOException, StringIndexOutOfBoundsException, NoSuchFileException{
362+
String cleanedLine, selectedTable, whereLine = null;
363+
boolean tableExists = false;
364+
ArrayList<String> whereTokens;
365+
ArrayList<ArrayList<Object>> wheredData;
366+
367+
try{
368+
cleanedLine = clean(line, keyword);
369+
if(cleanedLine.contains("WHERE")){
370+
selectedTable = cleanedLine.substring(0,cleanedLine.indexOf(" ")).trim();
371+
whereLine = cleanedLine.substring(cleanedLine.indexOf("WHERE") + "WHERE".length() + 1).trim();
372+
} else {
373+
selectedTable = cleanedLine;
374+
}
375+
} catch (StringIndexOutOfBoundsException e){
376+
throw new StringIndexOutOfBoundsException();
377+
}
378+
379+
ArrayList<Table> tables = database.getTables();
380+
for(Table table : tables){
381+
if(table.getTableName().equals(selectedTable)){
382+
tableExists = true;
383+
384+
if(whereLine != null){
385+
whereTokens = getWhereTokens(whereLine,table);
386+
whereTokens = Where.infixToPostfix(whereTokens);
387+
Tree.Node root = Where.createTree(whereTokens);
388+
wheredData = Where.evaluateTree(root, table.getData(), table);
389+
} else {
390+
wheredData = table.getData();
391+
}
392+
393+
ArrayList<ArrayList<Object>> pureData = new ArrayList<>(table.getData());
394+
pureData.removeAll(wheredData);
395+
table.setData(pureData);
396+
table.writeDataToFile();
397+
}
398+
}
399+
400+
if(!tableExists){
401+
throw new NoSuchFileException("TABLE DOES NOT EXISTS");
402+
}
361403
}
362404

363-
public void handleUpdate(String line, String keyword){
405+
public void handleUpdate(String line, String keyword, Database database) throws SQLSyntaxException, StringIndexOutOfBoundsException {
406+
364407
}
365408

366-
public void handleSelect(String line, String keyword, Database database) throws SQLSyntaxException,
367-
StringIndexOutOfBoundsException, NoSuchFileException, ColumnDoesNotMatch {
409+
public void handleSelect(String line, String keyword, Database database) throws SQLSyntaxException, StringIndexOutOfBoundsException, NoSuchFileException, ColumnDoesNotMatch {
368410
ArrayList<String> columns = new ArrayList<>();
369411
ArrayList<String> showingCol = new ArrayList<>();
370412
String cleanedLine, selectedColumns, selectedTable, whereLine = null;
371413
ArrayList<String> whereTokens;
372-
ArrayList<ArrayList<Object>> wheredData = new ArrayList<>();
414+
ArrayList<ArrayList<Object>> wheredData;
373415
boolean tableExists = false;
374416

375417
try{
376418
cleanedLine = clean(line, keyword);
377419
selectedColumns = cleanedLine.substring(0,cleanedLine.indexOf("FROM")-1).trim();
378420

379421
if(cleanedLine.contains("WHERE")){
380-
selectedTable = cleanedLine.substring(cleanedLine.indexOf("FROM ") + "FROM".length() + 1,
381-
cleanedLine.indexOf(" WHERE")).trim();
422+
selectedTable = cleanedLine.substring(cleanedLine.indexOf("FROM ") + "FROM".length() + 1, cleanedLine.indexOf(" WHERE")).trim();
382423
whereLine = cleanedLine.substring(cleanedLine.indexOf("WHERE") + "WHERE".length() + 1).trim();
383424
} else {
384-
selectedTable = cleanedLine.substring(cleanedLine.indexOf("FROM ") +
385-
"FROM".length() + 1).trim();
425+
selectedTable = cleanedLine.substring(cleanedLine.indexOf("FROM ") + "FROM".length() + 1).trim();
386426
}
387427

388428
if(!selectedColumns.equals("*")){
@@ -440,9 +480,6 @@ public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSynt
440480
ArrayList<String> whereTokens = new ArrayList<>();
441481
String value;
442482

443-
/*ArrayList<String> homunculus = new ArrayList<>(analyzer.getOperators());
444-
homunculus.addAll(table.getColumnsName());*/
445-
446483
String format1 = "^'.+'";
447484
String format2 = "^\\d*\\s+";
448485
String format3 = "^\\d*$";

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Table {
1919
private final String tableName;
2020
private final File tableFile;
2121
private final ArrayList<Column> columns;
22-
private final ArrayList<ArrayList<Object>> data = new ArrayList<>();
22+
private ArrayList<ArrayList<Object>> data = new ArrayList<>();
2323

2424
//para crear nueva tabla
2525
public Table(File tableFile, ArrayList<Column> columns){
@@ -87,6 +87,10 @@ public ArrayList<ArrayList<Object>> getData() {
8787
return data;
8888
}
8989

90+
public void setData(ArrayList<ArrayList<Object>> data) {
91+
this.data = data;
92+
}
93+
9094
public void appendDataToTable(ArrayList<String> data, ArrayList<String> columnNames) throws IOException {
9195
ArrayList<Object> newData = new ArrayList<>();
9296

@@ -202,6 +206,7 @@ public void printData(ArrayList<ArrayList<Object>> data){
202206
}
203207
System.out.println("+");
204208
}
209+
System.out.println();
205210
}
206211

207212
public void printData(ArrayList<String> columns, ArrayList<ArrayList<Object>> data) {
@@ -234,6 +239,7 @@ public void printData(ArrayList<String> columns, ArrayList<ArrayList<Object>> da
234239
}
235240
System.out.println("+");
236241
}
242+
System.out.println();
237243
}
238244

239245
public ArrayList<String> getColumnsName(){

PRUEBITA_DB/ALUMNOS.csv

-15 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)