2
2
3
3
import edu .upvictoria .poo .exceptions .*;
4
4
5
+ import javax .xml .crypto .Data ;
5
6
import java .io .*;
6
7
import java .nio .file .*;
7
8
import java .util .ArrayList ;
@@ -20,7 +21,7 @@ public String clean(String line, String keyword) throws StringIndexOutOfBoundsEx
20
21
throw new StringIndexOutOfBoundsException ();
21
22
}
22
23
23
- return line ;
24
+ return line . trim () ;
24
25
}
25
26
26
27
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
357
358
table .appendDataToTable (insertionData ,insertionColumns );
358
359
}
359
360
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
+ }
361
403
}
362
404
363
- public void handleUpdate (String line , String keyword ){
405
+ public void handleUpdate (String line , String keyword , Database database ) throws SQLSyntaxException , StringIndexOutOfBoundsException {
406
+
364
407
}
365
408
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 {
368
410
ArrayList <String > columns = new ArrayList <>();
369
411
ArrayList <String > showingCol = new ArrayList <>();
370
412
String cleanedLine , selectedColumns , selectedTable , whereLine = null ;
371
413
ArrayList <String > whereTokens ;
372
- ArrayList <ArrayList <Object >> wheredData = new ArrayList <>() ;
414
+ ArrayList <ArrayList <Object >> wheredData ;
373
415
boolean tableExists = false ;
374
416
375
417
try {
376
418
cleanedLine = clean (line , keyword );
377
419
selectedColumns = cleanedLine .substring (0 ,cleanedLine .indexOf ("FROM" )-1 ).trim ();
378
420
379
421
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 ();
382
423
whereLine = cleanedLine .substring (cleanedLine .indexOf ("WHERE" ) + "WHERE" .length () + 1 ).trim ();
383
424
} else {
384
- selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) +
385
- "FROM" .length () + 1 ).trim ();
425
+ selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) + "FROM" .length () + 1 ).trim ();
386
426
}
387
427
388
428
if (!selectedColumns .equals ("*" )){
@@ -440,9 +480,6 @@ public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSynt
440
480
ArrayList <String > whereTokens = new ArrayList <>();
441
481
String value ;
442
482
443
- /*ArrayList<String> homunculus = new ArrayList<>(analyzer.getOperators());
444
- homunculus.addAll(table.getColumnsName());*/
445
-
446
483
String format1 = "^'.+'" ;
447
484
String format2 = "^\\ d*\\ s+" ;
448
485
String format3 = "^\\ d*$" ;
0 commit comments