-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyzeSelect.java
616 lines (579 loc) · 20.8 KB
/
AnalyzeSelect.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import gudusoft.gsqlparser.*;
import gudusoft.gsqlparser.nodes.*;
import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
public class AnalyzeSelect {
protected static void analyzeSelectStmt(TSelectSqlStatement pStmt){
// System.out.println("\nSelect:");
// if (pStmt.isCombinedQuery()){
// String setstr="";
// switch (pStmt.getSetOperator()){
// case 1: setstr = "union";break;
// case 2: setstr = "union all";break;
// case 3: setstr = "intersect";break;
// case 4: setstr = "intersect all";break;
// case 5: setstr = "minus";break;
// case 6: setstr = "minus all";break;
// case 7: setstr = "except";break;
// case 8: setstr = "except all";break;
// }
// System.out.printf("set type: %s\n",setstr);
// System.out.println("left select:");
// analyzeSelectStmt(pStmt.getLeftStmt());
// System.out.println("right select:");
// analyzeSelectStmt(pStmt.getRightStmt());
// if (pStmt.getOrderbyClause() != null){
// System.out.printf("order by clause %s\n",pStmt.getOrderbyClause().toString());
// }
// }else
{
//select list
//boolean single_col=false;
boolean aggr=false;boolean dis_flag=false;
boolean []aggrflags={false,false,false,false};
boolean whereflag=false;
ArrayList<String> tables=new ArrayList<String>();
ArrayList<String> cols=new ArrayList<String>();
for(int i=0; i < pStmt.getResultColumnList().size();i++)
{
TResultColumn resultColumn = pStmt.getResultColumnList().getResultColumn(i);
// System.out.println("Column: "+resultColumn.getExpr().toString());
cols.add(resultColumn.getExpr().toString());
}
//from clause, check this document for detailed information
//http://www.sqlparser.com/sql-parser-query-join-table.php
for(int i=0;i<pStmt.joins.size();i++)
{
TJoin join = pStmt.joins.getJoin(i);
switch (join.getKind()){
case TBaseType.join_source_fake:
// System.out.println("TYpe 1 join");
tables.add(join.getTable().toString());
// System.out.println("Table:"+join.getTable().toString() );
//System.out.printf("table: %s, alias: %s\n",join.getTable().toString(),(join.getTable().getAliasClause() !=null)?join.getTable().getAliasClause().toString():"");
break;
case TBaseType.join_source_table:
// System.out.println("TYpe 2 join");
// System.out.printf("table: %s, alias: %s\n",join.getTable().toString(),(join.getTable().getAliasClause() !=null)?join.getTable().getAliasClause().toString():"");
for(int j=0;j<join.getJoinItems().size();j++){
TJoinItem joinItem = join.getJoinItems().getJoinItem(j);
// System.out.printf("Join type: %s\n",joinItem.getJoinType().toString());
// System.out.printf("table: %s, alias: %s\n",joinItem.getTable().toString(),(joinItem.getTable().getAliasClause() !=null)?joinItem.getTable().getAliasClause().toString():"");
if (joinItem.getOnCondition() != null){
// System.out.printf("On: %s\n",joinItem.getOnCondition().toString());
}else if (joinItem.getUsingColumns() != null){
//System.out.printf("using: %s\n",joinItem.getUsingColumns().toString());
}
}
break;
case TBaseType.join_source_join:
TJoin source_join = join.getJoin();
// System.out.println("TYpe 3 join");
// System.out.printf("table: %s, alias: %s\n",source_join.getTable().toString(),(source_join.getTable().getAliasClause() !=null)?source_join.getTable().getAliasClause().toString():"");
for(int j=0;j<source_join.getJoinItems().size();j++){
TJoinItem joinItem = source_join.getJoinItems().getJoinItem(j);
// System.out.printf("source_join type: %s\n",joinItem.getJoinType().toString());
// System.out.printf("table: %s, alias: %s\n",joinItem.getTable().toString(),(joinItem.getTable().getAliasClause() !=null)?joinItem.getTable().getAliasClause().toString():"");
if (joinItem.getOnCondition() != null){
// System.out.printf("On: %s\n",joinItem.getOnCondition().toString());
}else if (joinItem.getUsingColumns() != null){
// System.out.printf("using: %s\n",joinItem.getUsingColumns().toString());
}
}
for(int j=0;j<join.getJoinItems().size();j++){
TJoinItem joinItem = join.getJoinItems().getJoinItem(j);
// System.out.printf("Join type: %s\n",joinItem.getJoinType().toString());
// System.out.printf("table: %s, alias: %s\n",joinItem.getTable().toString(),(joinItem.getTable().getAliasClause() !=null)?joinItem.getTable().getAliasClause().toString():"");
if (joinItem.getOnCondition() != null){
// System.out.printf("On: %s\n",joinItem.getOnCondition().toString());
}else if (joinItem.getUsingColumns() != null){
// System.out.printf("using: %s\n",joinItem.getUsingColumns().toString());
}
}
break;
default:
System.out.println("unknown type in join!");
break;
}
}
//where clause
if (pStmt.getWhereClause() != null){
whereflag=true;
boolean and=false,or=false;
System.out.printf("where clause: \n%s\n", pStmt.getWhereClause().toString());
String wh=pStmt.getWhereClause().toString();
String [] a=wh.split(" ");
System.out.println("Lenght 0f: "+a.length);
if(a.length>2)
{
List<String> val=new ArrayList<String>();
List<String> cname=new ArrayList<String>();
if(a[2].equalsIgnoreCase("or"))
or=true;
else if(a[2].equals("and"))
and=true;
if(wh.indexOf('=')!=-1)
{
cname.add(a[1].substring(0,a[1].indexOf('=')));
cname.add(a[2].substring(0,a[2].indexOf('=')));
val.add(a[1].substring(a[1].indexOf('=')));
val.add(a[2].substring(a[1].indexOf('=')));
}
else
{
System.out.println("Error!!!");
return;
}
if(checkforcollist(cname, tables))
{
}
else
{
System.out.println("Error!!!");
return;
}
}
else
{
}
}
if(tables.size()==1 && cols.size()==1)
{
aggr=checkAggr(cols.get(0),aggrflags);
if(aggr)
{
StringBuilder cl=new StringBuilder(cols.get(0));
StringBuilder c=new StringBuilder();
for(int i=cols.get(0).indexOf('(')+1;i<cols.get(0).indexOf(')');i++)
{
c.append(cl.charAt(i));
}
cols.remove(0);
cols.add(0, c.toString());
//System.out.println("Col name "+cols.get(0));
String table_name=tables.get(0);
if(checkfortable(tables) && checkforcols(table_name, cols))
{
if(aggrflags[0])
printtable(table_name, cols,'a');
else if(aggrflags[1])
printtable(table_name, cols,'M');
else if(aggrflags[2])
printtable(table_name, cols,'m');
else if(aggrflags[3])
printtable(table_name, cols,'s');
}
else
System.out.println("ERROR!!!");
}
}
if(!aggr && tables.size()==1 && cols.size()==1 )
{
if(cols.get(0).contains("("))
{
// System.out.println("Index of (= "+cols.get(0).indexOf('('));
// System.out.println("Index of )= "+cols.get(0).indexOf(')'));
StringBuilder cl=new StringBuilder(cols.get(0));
StringBuilder c=new StringBuilder();
for(int i=cols.get(0).indexOf('(')+1;i<cols.get(0).indexOf(')');i++)
{
c.append(cl.charAt(i));
}
cols.remove(0);
cols.add(0, c.toString());
//System.out.println("Col name "+cols.get(0));
String table_name=tables.get(0);
if(checkfortable(tables) && checkforcols(table_name, cols))
{
printtable(table_name, cols,'d');
}
else
System.out.println("ERROR!!!");
dis_flag=true;
}
}
if(!dis_flag&&!aggr &&tables.size()==1)
{
if(checkfortable(tables))
{
String table_name=tables.get(0);
if(pStmt.getResultColumnList().size()==1)
{
String single_col_name=cols.get(0);
if(single_col_name.equalsIgnoreCase("*"))
{
printtable(table_name);
}
else
{
if(checkforcols(table_name,cols))
printtable(table_name,cols);
else
System.out.println("Error!!! :Column not exist");
}
}
else
{
if(checkforcols(table_name,cols))
printtable(table_name,cols);
else
System.out.println("Error!!! :Column not exist");
}
}
else
System.out.println("Error!!! :Table not exist");
}
//if table size >1
if(tables.size()>1 && !whereflag)
{
List<List<Integer>> all_data = new ArrayList<List<Integer>>();
int tab_count=0;
if(checkfortable(tables))
{
for(String s:tables)
{
List<String> c=Main_file.table.get(s);
if(tab_count==0)
{
for(int i=0;i<Main_file.data.get(s).get(c.get(0)).size();i++)//for no of entries size of arryaList
{
List<Integer> one=new ArrayList<Integer>();
for(int j=0;j<c.size();j++) //for no of cols
{
one.add(Main_file.data.get(s).get(c.get(j)).get(i));
}
all_data.add(one);
}
}
if(tab_count>0)
{
int len=all_data.size();
for(int i=0;i<Main_file.data.get(s).get(c.get(0)).size();i++)//for no of entries
{
List<Integer> test=new ArrayList<Integer>();
for(int j=0;j<c.size();j++) //for no of cols
{
test.add(Main_file.data.get(s).get(c.get(j)).get(i));
}
for(int k=0;k<len;k++)
{
List<Integer> tt=new ArrayList<Integer>(all_data.get(k));
for(Integer p:test)
{
tt.add(p);
}
all_data.add(tt);
}
}
for(int k=0;k<len;k++)
all_data.remove(0);
}
tab_count++;
}
if(checkforcollist(cols,tables))
{
int[] arr= new int[cols.size()];
int [] tab=new int [tables.size()];
String []append=new String[cols.size()]; //2-d array le lo
for(int i=0;i<cols.size();i++)
{
int ind=indexof(cols.get(i),tables,tab);
arr[i]=0;
for(int j=0;j<tab[ind];j++)
{
arr[i]=arr[i]+Main_file.table.get(tables.get(j)).size();
}
arr[i]=arr[i]+ind;
append[i]="";
if(tab[ind]==0)
{
for(int j=0;j<all_data.size();j++)
{
List<Integer> l=all_data.get(j);
//System.out.println(l.get(arr[i]));
append[i]=append[i]+l.get(arr[i])+'\n';
}
}
else
{
// System.out.println("ind="+arr[i]);
for(int j=0;j<all_data.size();j++)
{
List<Integer> l=all_data.get(j);
//System.out.println("l="+l);
//System.out.println("data="+l.get(arr[i]));
//append[i]="";
append[i]=append[i]+l.get(arr[i])+'\n';
}
}
}
//System.out.println("Appen array");
for(int i=0;i<cols.size();i++)
{
System.out.println(cols.get(i));
System.out.println(append[i]);
}
//System.out.println("Shi h");
}
else
System.out.println("ERROR!!!");
//System.out.println(all_data.size());
System.out.println(all_data);
}
else
System.out.print("ERROR!!!Table not Exist");
}
// group by
if (pStmt.getGroupByClause() != null){
System.out.printf("group by: \n%s\n",pStmt.getGroupByClause().toString());
}
// order by
if (pStmt.getOrderbyClause() != null){
System.out.printf("order by: \n%s\n",pStmt.getOrderbyClause().toString());
}
// for update
if (pStmt.getForUpdateClause() != null){
System.out.printf("for update: \n%s\n",pStmt.getForUpdateClause().toString());
}
// top clause
if (pStmt.getTopClause() != null){
System.out.printf("top clause: \n%s\n",pStmt.getTopClause().toString());
}
// limit clause
if (pStmt.getLimitClause() != null){
System.out.printf("top clause: \n%s\n",pStmt.getLimitClause().toString());
}
}
}
static int indexof(String col,List<String>tables,int tab_count[])
{
//System.out.println("---------"+col);
int pos=0;int count=0;
String table_name="";
for(String s:tables)
{
table_name=s;
if(Main_file.table.containsKey(s))
{
if(col.contains("."))
{
//String [] arr=col.split(".");
table_name=col.substring(0, col.indexOf('.'));
System.out.println("-Table---"+table_name);
col=col.substring(col.indexOf('.')+1);
pos=Main_file.table.get(table_name).indexOf(col);//pos+ccount*size of table
if(pos!=-1)
{
tab_count[pos]=count;
System.out.println("---index no---"+pos);
return pos;
}
//System.out.println("---------"+col);
//col=arr[1];
}
pos=Main_file.table.get(table_name).indexOf(col);//pos+ccount*size of table
if(pos!=-1)
{
tab_count[pos]=count;
//System.out.println("---index no---"+pos);
return pos;
}
}
count++;
}
return pos;
}
static boolean checkforcollist(List<String>cols,List<String>tables)
{
String table_name="";String col_name="";boolean flag=false;
int count=0;
for(String col:cols)
{
count=0;
col_name=col;
//System.out.println("---------"+col_name);
if(col_name.contains("."))
{
// System.out.println("---------"+col_name);
// System.out.println("contaons"+".");
table_name=col_name.substring(0, col_name.indexOf('.'));
col_name=col.substring(col.indexOf('.')+1);
System.out.println("-Table---"+table_name);
// System.out.println("---------"+col_name);
//// table_name=arr[0];
// col_name=arr[1];
if(Main_file.table.containsKey(table_name))
{
int ind=Main_file.table.get(table_name).indexOf(col_name);
if(ind==-1)
return false;
flag=true;
}
else
return false;
}
else
{
for(String t: tables)
{
table_name=t;
if(Main_file.table.containsKey(t))
{
int ind=Main_file.table.get(table_name).indexOf(col_name);
if(ind!=-1)
count++;
}
else
return false;
}
if(count==1)
flag=true;
else
return false;
}
}
return flag;
}
static boolean checkfortable(List<String> tables)
{
for(String table_name:tables)
{
if(!Main_file.table.containsKey(table_name))
return false;
}
return true;
}
static boolean checkforcols(String table_name,List<String> cols)
{
for(String col:cols)
{
if(!Main_file.data.get(table_name).containsKey(col))
return false;
}
return true;
}
static boolean checkAggr(String col,boolean []aggrflags)
{
if(col.contains("AVG(")||col.contains("avg(")||col.contains("Avg("))
{
aggrflags[0]=true;
return true;
}
else if(col.contains("MAX(")||col.contains("max(")||col.contains("Max("))
{
aggrflags[1]=true;
return true;
}
else if(col.contains("MIN(")||col.contains("min(")||col.contains("Min("))
{
aggrflags[2]=true;
return true;
}
else if(col.contains("SUM(")||col.contains("sum(")||col.contains("Sum("))
{
aggrflags[3]=true;
return true;
}
return false;
}
static void printtable(String table_name)
{
List<String> s=Main_file.table.get(table_name);
int len=Main_file.data.get(table_name).get(s.get(0)).size();
for (String d:s)
System.out.print(d+"\t");
System.out.println("");
//int len=s.size();
String append="";String token="\t";
for(int i=0;i<len;i++)
{
for(int j=0;j<s.size();j++)
{
if(j!=s.size()-1)
append=append+Main_file.data.get(table_name).get(s.get(j)).get(i)+token;
else
append=append+Main_file.data.get(table_name).get(s.get(j)).get(i);
}
append=append+"\n";
}
System.out.println(append);
}
static void printtable(String table_name,List<String>s)
{
// List<String> s=Main_file.table.get(table_name);
int len=Main_file.data.get(table_name).get(s.get(0)).size();
for (String d:s)
System.out.print(d+"\t");
System.out.println("");
//int len=s.size();
String append="";String token="\t";
for(int i=0;i<len;i++)
{
for(int j=0;j<s.size();j++)
{
if(j!=s.size()-1)
append=append+Main_file.data.get(table_name).get(s.get(j)).get(i)+token;
else
append=append+Main_file.data.get(table_name).get(s.get(j)).get(i);
}
append=append+"\n";
}
System.out.println(append);
}
static void printtable(String table_name,List<String>s,char chk)
{
ArrayList<Integer> l=Main_file.data.get(table_name).get(s.get(0));
TreeSet<Integer> ss1=new TreeSet<Integer>();
Set<Integer> ss=new LinkedHashSet<Integer>();
for(Integer i:l)
{
ss.add(i);
ss1.add(i);
// m.put(i, i);
}
if(chk=='d')
{
System.out.println(s.get(0));
for(Integer i:ss)
{
System.out.println(i);
}
//System.out.println(count);
}
else if(chk=='a')
{
double sum=0.0;int count=0;
for(Integer i :l)
{
sum=sum+i;
count++;
}
System.out.println("Avg:"+sum/count);
}
else if(chk=='M')
{
System.out.println("Max: "+ss1.last());
}
else if(chk=='m')
{
System.out.println("Min: "+ss1.first());
}
else if(chk=='s')
{
int sum=0,count=0;
for(Integer i :l)
{
sum=sum+i;
count++;
}
System.out.println("Sum: "+sum);
}
}
}