@@ -368,18 +368,21 @@ public void handleSelect(String line, String keyword, Database database) throws
368
368
ArrayList <String > columns = new ArrayList <>();
369
369
ArrayList <String > showingCol = new ArrayList <>();
370
370
String cleanedLine , selectedColumns , selectedTable , whereLine = null ;
371
- ArrayList <String > whereTokens = new ArrayList <>();
371
+ ArrayList <String > whereTokens ;
372
+ ArrayList <ArrayList <Object >> wheredData = new ArrayList <>();
372
373
boolean tableExists = false ;
373
374
374
375
try {
375
376
cleanedLine = clean (line , keyword );
376
377
selectedColumns = cleanedLine .substring (0 ,cleanedLine .indexOf ("FROM" )-1 ).trim ();
377
378
378
379
if (cleanedLine .contains ("WHERE" )){
379
- selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) + "FROM" .length () + 1 , cleanedLine .indexOf (" WHERE" )).trim ();
380
+ selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) + "FROM" .length () + 1 ,
381
+ cleanedLine .indexOf (" WHERE" )).trim ();
380
382
whereLine = cleanedLine .substring (cleanedLine .indexOf ("WHERE" ) + "WHERE" .length () + 1 ).trim ();
381
383
} else {
382
- selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) + "FROM" .length () + 1 ).trim ();
384
+ selectedTable = cleanedLine .substring (cleanedLine .indexOf ("FROM " ) +
385
+ "FROM" .length () + 1 ).trim ();
383
386
}
384
387
385
388
if (!selectedColumns .equals ("*" )){
@@ -399,6 +402,10 @@ public void handleSelect(String line, String keyword, Database database) throws
399
402
if (whereLine != null ){
400
403
whereTokens = getWhereTokens (whereLine ,table );
401
404
whereTokens = Where .infixToPostfix (whereTokens );
405
+ Tree .Node root = Where .createTree (whereTokens );
406
+ wheredData = Where .evaluateTree (root , table .getData (), table );
407
+ } else {
408
+ wheredData = table .getData ();
402
409
}
403
410
404
411
if (!selectedColumns .equals ("*" )){
@@ -414,10 +421,10 @@ public void handleSelect(String line, String keyword, Database database) throws
414
421
throw new ColumnDoesNotMatch ("COLUMN DOES NOT MATCH" );
415
422
}
416
423
417
- table .printData (showingCol );
424
+ table .printData (showingCol , wheredData );
418
425
419
426
} else {
420
- table .printData ();
427
+ table .printData (wheredData );
421
428
}
422
429
break ;
423
430
}
@@ -430,17 +437,16 @@ public void handleSelect(String line, String keyword, Database database) throws
430
437
431
438
public ArrayList <String > getWhereTokens (String line , Table table ) throws SQLSyntaxException , IndexOutOfBoundsException {
432
439
Analyzer analyzer = new Analyzer ();
433
- ArrayList <String > operators = analyzer .getOperators ();
434
440
ArrayList <String > whereTokens = new ArrayList <>();
435
441
String value ;
436
442
437
- ArrayList <String > homunculus = new ArrayList <>(operators );
438
- homunculus .addAll (table .getColumnsName ());
439
- boolean foundKeyword ;
443
+ /*ArrayList<String> homunculus = new ArrayList<>(analyzer.getOperators());
444
+ homunculus.addAll(table.getColumnsName());*/
440
445
441
446
String format1 = "^'.+'" ;
442
447
String format2 = "^\\ d*\\ s+" ;
443
448
String format3 = "^\\ d*$" ;
449
+
444
450
Pattern pattern1 = Pattern .compile (format1 );
445
451
Pattern pattern2 = Pattern .compile (format2 );
446
452
Pattern pattern3 = Pattern .compile (format3 );
@@ -450,50 +456,101 @@ public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSynt
450
456
line = line .replaceAll ("^=" ,"!=" );
451
457
452
458
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 ;
459
+ while (!line .isBlank ()){
460
+ String initStateLine = line ;
461
+ for (int i = 0 ; i < analyzer .getOperators ().size (); i ++){
462
+ String operator = analyzer .getOperators ().get (i );
463
+
464
+ if (line .startsWith (operator )){
465
+ if (operator .equals ("(" ) || operator .equals (")" ) || operator .equals ("AND" ) || operator .equals ("OR" )){
466
+ whereTokens .add (operator );
467
+ } else {
468
+ String lastToken = whereTokens .get (whereTokens .size ()-1 );
469
+
470
+ if (!table .getColumnsName ().contains (lastToken .split (" " )[0 ])){
471
+ throw new SQLSyntaxException ("UNEXPECTED OPERATOR: " + operator );
472
+ }
473
+
474
+ lastToken += (" " + operator );
475
+ whereTokens .set (whereTokens .size ()-1 , lastToken );
476
+ }
477
+
478
+ line = line .substring (line .indexOf (operator ) + operator .length ()).trim ();
479
+ break ;
480
+ }
481
+ }
482
+
483
+ for (int i = 0 ; i < table .getColumnsName ().size (); i ++){
484
+ String columnName = table .getColumnsName ().get (i );
485
+
486
+ if (line .startsWith (columnName )){
487
+ whereTokens .add (columnName );
488
+ line = line .substring (line .indexOf (columnName ) + columnName .length ()).trim ();
489
+ break ;
490
+ }
460
491
}
461
492
462
493
if (line .startsWith ("NULL" )){
463
- whereTokens .add ("\0 " );
494
+ String lastToken = whereTokens .get (whereTokens .size ()-1 );
495
+
496
+ if (!table .getColumnsName ().contains (lastToken .split (" " )[0 ])){
497
+ throw new SQLSyntaxException ("UNEXPECTED OPERATOR: NULL" );
498
+ }
499
+
500
+ lastToken += " \0 " ;
501
+ whereTokens .set (whereTokens .size ()-1 , lastToken );
464
502
line = line .substring (line .indexOf ("NULL" ) + "NULL" .length ()).trim ();
465
- i = -1 ;
466
- continue ;
467
503
}
468
504
469
505
matcher1 = pattern1 .matcher (line );
470
506
if (matcher1 .find ()){
507
+ String lastToken = whereTokens .get (whereTokens .size ()-1 );
508
+
509
+ if (!table .getColumnsName ().contains (lastToken .split (" " )[0 ])){
510
+ throw new SQLSyntaxException ("UNEXPECTED OPERATOR: NULL" );
511
+ }
512
+
471
513
line = line .substring (1 );
472
514
value = line .substring (0 , line .indexOf ("'" )).trim ();
473
- whereTokens .add (value );
515
+ lastToken += (" " + value );
516
+ whereTokens .set (whereTokens .size ()-1 , lastToken );
474
517
line = line .substring (line .indexOf (value ) + value .length () + 1 ).trim ();
475
- i = - 1 ;
518
+
476
519
continue ;
477
520
}
478
521
479
522
matcher2 = pattern2 .matcher (line );
480
523
if (matcher2 .find ()) {
524
+ String lastToken = whereTokens .get (whereTokens .size ()-1 );
525
+
526
+ if (!table .getColumnsName ().contains (lastToken .split (" " )[0 ])){
527
+ throw new SQLSyntaxException ("UNEXPECTED OPERATOR: NULL" );
528
+ }
529
+
481
530
value = line .substring (0 , line .indexOf (" " )).trim ();
482
- whereTokens .add (value );
531
+ lastToken += (" " + value );
532
+ whereTokens .set (whereTokens .size ()-1 , lastToken );
483
533
line = line .substring (line .indexOf (value ) + value .length () + 1 ).trim ();
484
- i = - 1 ;
534
+
485
535
continue ;
486
536
}
487
537
488
538
matcher3 = pattern3 .matcher (line );
489
539
if (matcher3 .find ()) {
540
+ String lastToken = whereTokens .get (whereTokens .size ()-1 );
541
+
542
+ if (!table .getColumnsName ().contains (lastToken .split (" " )[0 ])){
543
+ throw new SQLSyntaxException ("UNEXPECTED OPERATOR: NULL" );
544
+ }
545
+
490
546
value = line .trim ();
491
- whereTokens .add (value );
492
- break ;
547
+ lastToken += (" " + value );
548
+ whereTokens .set (whereTokens .size ()-1 , lastToken );
549
+ line = line .substring (line .indexOf (value ) + value .length ()).trim ();
493
550
}
494
551
495
- if (i == homunculus . size () - 1 ){
496
- throw new SQLSyntaxException ("WHERE STATEMENT MALFORMED AT > " + line );
552
+ if (initStateLine . equals ( line ) ){
553
+ throw new SQLSyntaxException ("MALFORMED STATEMENT" );
497
554
}
498
555
}
499
556
} catch (SQLSyntaxException e ) {
@@ -506,6 +563,6 @@ public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSynt
506
563
}
507
564
}
508
565
509
- // TODO: COMPROBRAR QUE SE INGRESA UN TIPO DE DATO CORRECTO AL CREAR UN REGISTRO
510
- // TODO: COMPROBRAR QUE NO SE ELIGEN DOS COLUMNAS IGUALES A LAS QUE INGRESAR UN REGISTRO
566
+ // TODO: COMPROBAR QUE SE INGRESA UN TIPO DE DATO CORRECTO AL CREAR UN REGISTRO
567
+ // TODO: COMPROBAR QUE NO SE ELIGEN DOS COLUMNAS IGUALES A LAS QUE INGRESAR UN REGISTRO
511
568
// TODO: CREAR EL WHERE
0 commit comments