1
1
package edu .upvictoria .poo ;
2
2
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 .*;
7
4
8
5
import java .io .*;
9
6
import java .nio .file .*;
10
7
import java .util .ArrayList ;
11
8
import java .util .Arrays ;
9
+ import java .util .regex .Matcher ;
10
+ import java .util .regex .Pattern ;
12
11
13
12
public class SQL {
14
13
public String clean (String line , String keyword ) throws StringIndexOutOfBoundsException {
@@ -364,10 +363,12 @@ public void handleDeleteFrom(String line, String keyword){
364
363
public void handleUpdate (String line , String keyword ){
365
364
}
366
365
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 {
368
368
ArrayList <String > columns = new ArrayList <>();
369
369
ArrayList <String > showingCol = new ArrayList <>();
370
- String cleanedLine , selectedColumns , selectedTable ;
370
+ String cleanedLine , selectedColumns , selectedTable , whereLine = null ;
371
+ ArrayList <String > whereTokens = new ArrayList <>();
371
372
boolean tableExists = false ;
372
373
373
374
try {
@@ -376,6 +377,7 @@ public void handleSelect(String line, String keyword, Database database) throws
376
377
377
378
if (cleanedLine .contains ("WHERE" )){
378
379
selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) + "FROM" .length () + 1 , cleanedLine .indexOf (" WHERE" )).trim ();
380
+ whereLine = cleanedLine .substring (cleanedLine .indexOf ("WHERE" ) + "WHERE" .length () + 1 ).trim ();
379
381
} else {
380
382
selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) + "FROM" .length () + 1 ).trim ();
381
383
}
@@ -394,6 +396,11 @@ public void handleSelect(String line, String keyword, Database database) throws
394
396
if (table .getTableName ().equals (selectedTable )) {
395
397
tableExists = true ;
396
398
399
+ if (whereLine != null ){
400
+ whereTokens = getWhereTokens (whereLine ,table );
401
+ whereTokens = Where .infixToPostfix (whereTokens );
402
+ }
403
+
397
404
if (!selectedColumns .equals ("*" )){
398
405
for (String tableColName : table .getColumnsName ()){
399
406
for (String selectColName : columns ) {
@@ -421,8 +428,81 @@ public void handleSelect(String line, String keyword, Database database) throws
421
428
}
422
429
}
423
430
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 ;
426
506
}
427
507
}
428
508
0 commit comments