-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
1920 lines (1465 loc) · 69.7 KB
/
Parser.java
File metadata and controls
1920 lines (1465 loc) · 69.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaxt.sql;
//******************************************************************************
//** SQL Parser
//******************************************************************************
/**
* Used to parse and modify SQL Select Statements ("Select * from MyTable").
* Other SQL commands are not supported (insert, update, create, etc.). <br>
*
* Note that this implementation is incomplete and needs a lot more testing.
* For example, the parser does not yet handle unions, subselects, and
* having clauses. Other potential problems include arithmetic operators in
* the where clause ("where posting_time + 60 > sysdate").
*
******************************************************************************/
public class Parser {
private java.util.HashMap sql = new java.util.HashMap();
private static String[] sqlOperators =
new String[]{"IS NULL","IS NOT NULL","BETWEEN","CONTAINS","LIKE","<>","<=",">=","=","<",">","IN","MATCHES","SOME","NOT EXISTS","EXISTS"};
private static String[] sqlKeywords =
new String[]{"SELECT","FROM","WHERE","ORDER BY","GROUP BY","HAVING","LIMIT","OFFSET"};
private static String[] joinTypes = new String[]{
//Order is important!
"RIGHT OUTER JOIN",
"RIGHT INNER JOIN",
"FULL OUTER JOIN",
"LEFT OUTER JOIN",
"LEFT INNER JOIN",
"NATURAL JOIN",
"RIGHT JOIN",
"INNER JOIN",
"OUTER JOIN",
"CROSS JOIN",
"LEFT JOIN",
"JOIN"
};
private SelectStatement[] selectStatements = null;
private WhereStatement[] whereStatements = null;
private OrderByStatement[] orderByStatements = null;
private GroupByStatement[] groupByStatements = null;
private FromStatement fromStatement = null;
public String getSelectString() { return (String)sql.get("SELECT"); }
public String getFromString() { return (String)sql.get("FROM"); }
public String getWhereString() { return (String)sql.get("WHERE"); }
public String getOrderByString(){ return (String)sql.get("ORDER BY"); }
public String getGroupByString() { return (String)sql.get("GROUP BY"); }
public String getHavingString() { return (String)sql.get("HAVING"); }
public String getOffsetString() { return (String)sql.get("OFFSET"); }
public String getLimitString() { return (String)sql.get("LIMIT"); }
//**************************************************************************
//** clone
//**************************************************************************
public Parser clone(){
Parser parser = new Parser();
java.util.HashMap sql = new java.util.HashMap();
java.util.Iterator it = this.sql.keySet().iterator();
while (it.hasNext()){
Object key = it.next();
Object val = this.sql.get(key);
sql.put(key, val);
}
parser.sql = sql;
parser.fromStatement = fromStatement;
if (selectStatements!=null){
SelectStatement[] arr = new SelectStatement[selectStatements.length];
for (int i=0; i<arr.length; i++){
arr[i] = selectStatements[i];
}
parser.selectStatements = arr;
}
if (whereStatements!=null){
WhereStatement[] arr = new WhereStatement[whereStatements.length];
for (int i=0; i<arr.length; i++){
arr[i] = whereStatements[i];
}
parser.whereStatements = arr;
}
if (orderByStatements!=null){
OrderByStatement[] arr = new OrderByStatement[orderByStatements.length];
for (int i=0; i<arr.length; i++){
arr[i] = orderByStatements[i];
}
parser.orderByStatements = arr;
}
if (groupByStatements!=null){
GroupByStatement[] arr = new GroupByStatement[groupByStatements.length];
for (int i=0; i<arr.length; i++){
arr[i] = groupByStatements[i];
}
parser.groupByStatements = arr;
}
return parser;
}
//**************************************************************************
//** Constructor
//**************************************************************************
private Parser(){}
//**************************************************************************
//** Constructor
//**************************************************************************
public Parser(String sql){
//Validate input sql
if (sql==null) return;
//Trim the sql statement
sql = sql.replace("\n"," ");
sql = sql.replace("\r"," ");
sql = sql.replace("\t"," ");
sql = sql.trim();
if (sql.endsWith(";")) sql = sql.substring(0, sql.length()-1).trim();
if (sql.length()<=0) return;
//Set local variables;
boolean insideSingleQuotes = false;
boolean insideDoubleQuotes = false;
boolean insideParenthesis = false;
int parenthesis = 0;
String s = sql;
String c = "";
//Create a list of possible sql keywords found in the sql statement
java.util.Vector keywords = new java.util.Vector();
String t = sql.toUpperCase();
for (int i=0; i<sqlKeywords.length; i++){
if (t.contains(sqlKeywords[i])) keywords.add(sqlKeywords[i]);
}
String currentKeyword = null;
String previousKeyword = null;
StringBuffer phrase = new StringBuffer();
for (int i = 0; i < s.length(); i++){
c = s.substring(i,i+1);
if (c.equals("\"")){
if (!insideDoubleQuotes) insideDoubleQuotes = true;
else insideDoubleQuotes = false;
}
if (c.equals("'")){
if (!insideSingleQuotes) insideSingleQuotes = true;
else insideSingleQuotes = false;
}
if ((c.equals("(") && !insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)) {
insideParenthesis = true;
parenthesis = 0;
}
if ((c.equals("(") ) && (!insideDoubleQuotes && !insideSingleQuotes)) {
parenthesis += 1;
}
if ((c.equals(")") && insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)){
parenthesis = parenthesis - 1;
if (parenthesis==0) insideParenthesis = false;
}
phrase.append(c);
if (!insideDoubleQuotes && !insideSingleQuotes && !insideParenthesis){
for (int j=0; j<keywords.size(); j++){
String keyword = keywords.get(j).toString();
if (keyword.startsWith(c.toUpperCase()) && (i+keyword.length())<=s.length()){
String str = s.substring(i,i+keyword.length());
if (str.equalsIgnoreCase(keyword)){ //Found a string that contains an sql keyword!
//Check whether the string is an actual keyword or part of another word
String a = "";
String b = "";
if (i>0) a = s.substring(i-1,i);
if (i+keyword.length()+1<s.length()) b = s.substring(i+keyword.length(), i+keyword.length()+1);
if ((a.equals("") || a.equals(" ") || a.equals(")") || a.equals("]") || a.equals("\"") || (keyword.equals("FROM") && a.equals("*"))) &&
(b.equals("") || b.equals(" ") || b.equals("(") || b.equals("[") || b.equals("\"") || (keyword.equals("SELECT") && b.equals("*")))
){
currentKeyword = keyword;
String entry = phrase.substring(0, phrase.length()-1).trim();
if (entry.length()>0){
Object prevStatement = this.sql.get(previousKeyword);
if (prevStatement==null) {
this.sql.put(previousKeyword, entry);
}
this.sql.put(currentKeyword, null);
}
phrase = new StringBuffer();
i = i + (keyword.length()-1);
previousKeyword = currentKeyword;
keywords.remove(j);
break;
}
}
}
}
}
if (i==(s.length()-1)){
if (phrase.toString().trim().equals(s)){
}
else{
this.sql.put(previousKeyword, phrase.toString().trim());
}
}
}//end parsing text
}
// //**************************************************************************
// //** addWhere
// //**************************************************************************
// /** Used to update the where clause in the SQL String. Preserves existing
// * where clause, if one exists, by adding an "AND" statement.
// */
// public String addWhereStatement(String whereClause){
//
// if (whereClause!=null){
// whereClause = whereClause.trim();
// if (whereClause.length()>0){
// String orgWhere = this.getWhereString();
// if (orgWhere==null || orgWhere.trim().length()==0){
// setWhere(whereClause);
// }
// else{
// setWhere("(" + orgWhere.trim() + ") AND (" + whereClause + ")");
// }
// }
// }
//
// return this.toString();
// }
//**************************************************************************
//** setSelect
//**************************************************************************
/** Used to update the select clause. Returns an updated SQL statement.
*/
public String setSelect(String selectClause){
if (selectClause==null) selectClause = "*";
else{
selectClause = selectClause.trim();
if (selectClause.length()==0) selectClause = "*";
}
sql.put("SELECT", selectClause);
selectStatements = null;
getSelectStatements();
return this.toString();
}
//**************************************************************************
//** setFrom
//**************************************************************************
/** Used to update the from clause in the SQL String. The entire from
* clause will be replaced with the given string. Returns an updated SQL
* statement.
*/
public String setFrom(String fromClause){
if (fromClause!=null){
fromClause = fromClause.trim();
if (fromClause.length()==0) fromClause = null;
}
sql.put("FROM", fromClause);
this.fromStatement = null;
getFromStatement();
return this.toString();
}
//**************************************************************************
//** setWhere
//**************************************************************************
/** Used to update the where clause in the SQL String. The entire where
* clause will be replaced with the given string. Returns an updated SQL
* statement.
*/
public String setWhere(String whereClause){
if (whereClause!=null){
whereClause = whereClause.trim();
if (whereClause.length()==0) whereClause = null;
}
sql.put("WHERE", whereClause);
this.whereStatements = null;
getWhereStatements();
return this.toString();
}
//**************************************************************************
//** setOrderBy
//**************************************************************************
/** Used to update the order by clause in the SQL String. The entire order
* by clause will be replaced with the given string. Returns an updated SQL
* statement.
*/
public String setOrderBy(String orderByClause){
if (orderByClause!=null){
orderByClause = orderByClause.trim();
if (orderByClause.length()==0) orderByClause = null;
}
sql.put("ORDER BY", orderByClause);
this.orderByStatements = null;
getOrderByStatements();
return this.toString();
}
//**************************************************************************
//** setGroupBy
//**************************************************************************
/** Used to update the group by clause in the SQL String. The entire group
* by clause will be replaced with the given string. Returns an updated SQL
* statement.
*/
public String setGroupBy(String groupByClause){
if (groupByClause!=null){
groupByClause = groupByClause.trim();
if (groupByClause.length()==0) groupByClause = null;
}
sql.put("GROUP BY", groupByClause);
this.groupByStatements = null;
getGroupByStatements();
return this.toString();
}
//**************************************************************************
//** setOffset
//**************************************************************************
/** Used to update the offset clause in the SQL String. Returns an updated
* SQL statement.
*/
public String setOffset(Integer offset){
if (offset==null || offset<0){
sql.remove("OFFSET");
}
else{
sql.put("OFFSET", offset);
}
return this.toString();
}
//**************************************************************************
//** setLimit
//**************************************************************************
/** Used to update the limit clause in the SQL String. Returns an updated
* SQL statement.
*/
public String setLimit(Integer limit){
if (limit==null || limit<0){
sql.remove("LIMIT");
}
else{
sql.put("LIMIT", limit);
}
return this.toString();
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns an sql String, including any updates
*/
public String toString() {
String selectClause = this.getSelectString();
String fromClause = this.getFromString();
if (selectClause==null || fromClause==null){
return null;
}
else if(selectClause.length()<=0 || fromClause.length()<=0){
return null;
}
else{
StringBuffer sql = new StringBuffer();
sql.append("SELECT "); sql.append(selectClause);
sql.append(" FROM "); sql.append(fromClause);
String whereClause = this.getWhereString();
if (whereClause!=null) sql.append(" WHERE " + whereClause);
String orderByClause = this.getOrderByString();
if (orderByClause!=null) sql.append(" ORDER BY " + orderByClause);
String groupByClause = this.getGroupByString();
if (groupByClause!=null) sql.append(" GROUP BY " + groupByClause);
String havingClause = this.getHavingString();
if (havingClause!=null) sql.append(" HAVING " + havingClause);
String offsetClause = this.getOffsetString();
if (offsetClause!=null) sql.append(" OFFSET " + offsetClause);
String limitClause = this.getLimitString();
if (limitClause!=null) sql.append(" LIMIT " + limitClause);
return sql.toString();
}
}
//**************************************************************************
//** getSelectStatements
//**************************************************************************
/** Used to break down the select clause into individual elements. For
* example, "Select FirstName, LastName from Contacts" would return an
* array with 2 entries: "FirstName" and "LastName".
*/
public SelectStatement[] getSelectStatements(){
if (selectStatements!=null) return selectStatements;
else{
String[] array = this.split(getSelectString());
selectStatements = new SelectStatement[array.length];
for (int i=0; i<selectStatements.length; i++){
selectStatements[i] = new SelectStatement(array[i]);
}
return selectStatements;
}
}
//**************************************************************************
//** SelectStatement
//**************************************************************************
/** Used to represent an individual select statement found in the select
* clause.
*/
public class SelectStatement{
private String field;
private String alias;
private String statement;
private String columnName;
private boolean isFunction = false;
private java.util.List exposedElements = new java.util.LinkedList();
public SelectStatement(String statement){
this.field = statement;
this.statement = statement;
this.columnName = stripFunctions(statement);
//Find the alias, defined by the "AS" keyword
if (statement.toUpperCase().contains("AS")){
boolean insideSingleQuotes = false;
boolean insideDoubleQuotes = false;
boolean insideParenthesis = false;
int parenthesis = 0;
String s = statement;
String c = "";
for (int i = 0; i < s.length(); i++){
c = s.substring(i,i+1);
if (c.equals("\"")){
if (!insideDoubleQuotes) insideDoubleQuotes = true;
else insideDoubleQuotes = false;
}
if (c.equals("'")){
if (!insideSingleQuotes) insideSingleQuotes = true;
else insideSingleQuotes = false;
}
if ((c.equals("(") && !insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)) {
insideParenthesis = true;
parenthesis = 0;
}
if ((c.equals("(") ) && (!insideDoubleQuotes && !insideSingleQuotes)) {
parenthesis += 1;
}
if ((c.equals(")") && insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)){
parenthesis = parenthesis - 1;
if (parenthesis==0) insideParenthesis = false;
}
if (!insideDoubleQuotes && !insideSingleQuotes && !insideParenthesis){
if (c.equalsIgnoreCase("A") && (i+3)<s.length()){
String keyword = s.substring(i,i+2);
if (keyword.equalsIgnoreCase("AS")){
String as = s.substring(i+2);
String prevChar = "";
String nextChar = "";
if (i-1>=0 && i+3<=s.length()){
prevChar = s.substring(i-1,i);
nextChar = s.substring(i+2,i+3);
}
if ((prevChar.equals(" ") || prevChar.equals(")") || prevChar.equals("]") || prevChar.equals("\"")) &&
(nextChar.equals(" ") || nextChar.equals("(") || nextChar.equals("[") || nextChar.equals("\"")))
{
this.alias = removeParentheses(as);
this.field = s.substring(0,i-1).trim();
this.columnName = stripFunctions(field);
}
}
}
}
}//end parsing text
}//end if
//Check whether the field equals the column name. If not, then there
//is a function present in the expression
if (!field.equals(columnName)) isFunction = true;
//Iterate throught the list of operands and identify any exposed columns (columns that are not wrapped in quotes)
String[] elements = new String[]{columnName, alias};
for (int i=0; i<elements.length; i++){
String entry = elements[i];
if (entry!=null){
if (entry.endsWith("*")){//<-- Get rid of this case
if (entry.endsWith(".*")) {
exposedElements.add(entry.substring(0, entry.length()-2));
}
}
else if(entry.startsWith("\"") && entry.endsWith("\"")){
}
else if(entry.startsWith("'") && entry.endsWith("'")){
}
else{
exposedElements.add(entry);
}
}
}
}//end constructor
public String getField(){
return field;
}
public String getAlias(){
return alias;
}
public String getColumnName(){
return columnName;
}
public boolean isFunction(){
return isFunction;
}
public String toString(){
return statement;
}
}
//**************************************************************************
//** getFromStatement
//**************************************************************************
/** Used to parse the "FROM" statement and extracts */
private FromStatement getFromStatement(){
if (fromStatement!=null) return fromStatement;
String fromClause = getFromString();
fromClause = fromClause.replace("(", " ");
fromClause = fromClause.replace(")", " ");
fromClause = fromClause.trim();
fromStatement = new FromStatement(fromClause);
if (fromClause.toUpperCase().contains("JOIN")){
boolean insideSingleQuotes = false;
boolean insideDoubleQuotes = false;
String s = fromClause;
String c = "";
StringBuffer phrase = new StringBuffer();
for (int i = 0; i < s.length(); i++){
c = s.substring(i,i+1);
if (c.equals("\"")){
if (!insideDoubleQuotes) insideDoubleQuotes = true;
else insideDoubleQuotes = false;
}
if (c.equals("'")){
if (!insideSingleQuotes) insideSingleQuotes = true;
else insideSingleQuotes = false;
}
phrase.append(c);
if (!insideDoubleQuotes && !insideSingleQuotes){
for (int j=0; j<joinTypes.length; j++){
String joinType = joinTypes[j];
if (joinType.startsWith(c.toUpperCase()) && (i+joinType.length())<=s.length()){
String keyword = s.substring(i,i+joinType.length());
if (keyword.equalsIgnoreCase(joinType)){
String entry = phrase.substring(0, phrase.length()-1).trim();
if (entry.length()>0){
phrase = new StringBuffer();
i = i + (keyword.length()-1);
fromStatement.addEntry(entry);
//System.out.println("(+) " + entry + " (" + keyword + ")");
}
break;
}
}
}
}
if (i==(s.length()-1)){
if (phrase.toString().trim().equals(s)){
}
else{
String entry = phrase.toString().trim();
fromStatement.addEntry(entry);
}
}
}//end parsing text
}
else{
String[] tables = fromClause.split(",");
for (int i=0; i<tables.length; i++){
String tableName = tables[i].trim();
fromStatement.addTable(tableName);
}
}
return fromStatement;
}
//**************************************************************************
//** FromStatement
//**************************************************************************
/** Used to represent the "FROM" clause.
*/
public class FromStatement {
private String statement = null;
private java.util.HashSet tables = new java.util.HashSet();
private java.util.HashSet columns = new java.util.HashSet();
private java.util.HashSet exposedElements = new java.util.HashSet();
private FromStatement(String statement){
this.statement = statement;
}
public void addTable(String tableName){
tableName = tableName.trim();
tables.add(tableName);
if (isExposed(tableName)) exposedElements.add(tableName);
}
public void addColumn(String columnName){
columnName = columnName.trim();
columns.add(columnName);
if (isExposed(columnName)) exposedElements.add(columnName);
}
private void addEntry(String entry){
String tableName = null;
String joinCondition = null;
if (entry.toUpperCase().contains(" ON ")){
tableName = entry.substring(0, entry.toUpperCase().indexOf(" ON ")).trim();
this.addTable(tableName);
joinCondition = entry.substring(entry.toUpperCase().indexOf(" ON ") + 4).trim();
WhereStatement statement = new WhereStatement(joinCondition);
this.addColumn(statement.getLeftOperand());
this.addColumn(statement.getRightOperand());
}
else{
tableName = entry;
this.addTable(tableName);
}
//System.out.println("--> " + tableName + " [" + joinCondition + "]");
}
public java.util.HashSet getExposedElements(){ return exposedElements; }
public String[] getTables(){
String[] array = new String[tables.size()];
java.util.Iterator it = tables.iterator();
int i=0;
while (it.hasNext()){
String tableName = (String)it.next();
array[i] = tableName;
i++;
}
return array;
}
public String toString(){ return statement; }
}
//**************************************************************************
//** getTables
//**************************************************************************
/** Returns an array of Tables Found in the SQL String */
public String[] getTables(){
return getFromStatement().getTables();
}
//**************************************************************************
//** getWhereStatements
//**************************************************************************
/** Used to retrieve a list of where statements found in the where clause.
* Returns an empty array if no where statements are found.
*/
public WhereStatement[] getWhereStatements(){
if (this.whereStatements!=null) return this.whereStatements;
String whereClause = this.getWhereString();
if (whereClause==null || whereClause.trim().length()<=0) {
return new WhereStatement[0];
}
else{
//Create a list of sql fragments to parse
java.util.List list = new java.util.LinkedList();
//Add Where Clause to the list of sql fragments to parse
list.add(whereClause);
//Iterate through all the sql fragments and extract individual statements
for (int x=0; x<list.size(); x++){
boolean insideSingleQuotes = false;
boolean insideDoubleQuotes = false;
boolean insideParenthesis = false;
int parenthesis = 0;
String s = list.get(x).toString().trim();
String c = "";
/*
System.out.println();
System.out.println("-----------------------------------------");
System.out.println(s);
System.out.println("-----------------------------------------");
*/
StringBuffer phrase = new StringBuffer();
for (int i = 0; i < s.length(); i++){
c = s.substring(i,i+1);
if (c.equals("\"")){
if (!insideDoubleQuotes) insideDoubleQuotes = true;
else insideDoubleQuotes = false;
}
if (c.equals("'")){
if (!insideSingleQuotes) insideSingleQuotes = true;
else insideSingleQuotes = false;
}
if ((c.equals("(") && !insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)) {
insideParenthesis = true;
parenthesis = 0;
}
if ((c.equals("(") ) && (!insideDoubleQuotes && !insideSingleQuotes)) {
parenthesis += 1;
}
if ((c.equals(")") && insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)){
parenthesis = parenthesis - 1;
if (parenthesis==0) insideParenthesis = false;
}
phrase.append(c);
if (!insideDoubleQuotes && !insideSingleQuotes && !insideParenthesis){
String keyword = null;
if (c.equalsIgnoreCase("A") && (i+3)<s.length()){
keyword = s.substring(i,i+3);
if (!keyword.equalsIgnoreCase("AND")) keyword = null;
}
else if (c.equalsIgnoreCase("O") && (i+2)<s.length()){
keyword = s.substring(i,i+2);
if (!keyword.equalsIgnoreCase("OR")) keyword = null;
}
if (keyword!=null){
String entry = phrase.substring(0, phrase.length()-1).trim();
entry = removeParentheses(entry);
if (entry.length()>0){
list.add(entry);
phrase = new StringBuffer();
i = i + (keyword.length()-1);
//System.out.println("(+) " + entry);
}
}
}
if (i==(s.length()-1)){
if (phrase.toString().trim().equals(s)){ //<--might need to check the size of the list as well
}
else{
String entry = phrase.toString().trim();
//entry = phrase.substring(0, phrase.length()-1).trim();
entry = removeParentheses(entry);
list.add(entry);
list.remove(x);
//System.out.println("(+) " + entry + " *");
//System.out.println("(-) " + s);
}
}
}//end parsing text
}
//Create a list of possible SQL Operators found in the Where clause
java.util.List operators = new java.util.LinkedList();
for (int i=0; i<this.sqlOperators.length; i++){
String operator = this.sqlOperators[i];
if (whereClause.toUpperCase().contains(operator)){
operators.add(operator);
}
}
String[] sqlOperators = new String[operators.size()];
for (int i=0; i<sqlOperators.length; i++){
sqlOperators[i] = (String) operators.get(i);
}
//Iterate through all the where statements and create an array
WhereStatement[] statements = new WhereStatement[list.size()];
for (int i=0; i<list.size(); i++){
String entry = list.get(i).toString();
//System.out.println("xx " + entry);
statements[i] = new WhereStatement(entry, sqlOperators);
}
this.whereStatements = statements;
return statements;
}
}
//**************************************************************************
//** WhereStatement
//**************************************************************************
/** Used to represent an individual where condition found in the "WHERE"
* clause. Note that this class is also used to parse join logic found
* in the "FROM" clause.
*/
public class WhereStatement {
private String statement = null;
private String leftOperand = null;
private String rightOperand = null;
private String operator = null;
private java.util.List exposedColumns = new java.util.LinkedList();
public WhereStatement(String statement) {
this(statement, sqlOperators);
}
public WhereStatement(String statement, String[] sqlOperators) {
this.statement = statement;
if (statement!=null){
//Trim down the list of sql operators
java.util.Vector array = new java.util.Vector();
for (int i=0; i<sqlOperators.length; i++){
String sqlOperator = sqlOperators[i];
if (statement.toUpperCase().contains(sqlOperator)){
array.add(sqlOperator);
}
}
sqlOperators = new String[array.size()];
for (int i=0; i<sqlOperators.length; i++){
sqlOperators[i] = (String) array.get(i);
}
boolean insideSingleQuotes = false;
boolean insideDoubleQuotes = false;
boolean insideParenthesis = false;
int parenthesis = 0;
String s = statement;
String c = "";
StringBuffer phrase = new StringBuffer();
java.util.List list = new java.util.LinkedList();
for (int i = 0; i < s.length(); i++){
c = s.substring(i,i+1);
if (c.equals("\"")){
if (!insideDoubleQuotes) insideDoubleQuotes = true;
else insideDoubleQuotes = false;
}
if (c.equals("'")){
if (!insideSingleQuotes) insideSingleQuotes = true;
else insideSingleQuotes = false;
}
if ((c.equals("(") && !insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)) {
insideParenthesis = true;
parenthesis = 0;
}
if ((c.equals("(") ) && (!insideDoubleQuotes && !insideSingleQuotes)) {
parenthesis += 1;
}
if ((c.equals(")") && insideParenthesis) && (!insideDoubleQuotes && !insideSingleQuotes)){
parenthesis = parenthesis - 1;
if (parenthesis==0) insideParenthesis = false;