-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectionVisitor.java
78 lines (67 loc) · 2.77 KB
/
ProjectionVisitor.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
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.AllTableColumns;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Karan on 6/28/2017.
*/
public class ProjectionVisitor implements SelectItemVisitor {
public Column[] schema = null;
public ArrayList<Integer> columnIndexes;
HashMap<String, String> aliasHashMap;
public ProjectionVisitor(Column[] schema, HashMap<String, String> aliasHashMap) {
columnIndexes = new ArrayList<>();
this.schema = schema;
this.aliasHashMap = aliasHashMap;
}
public void visit(AllColumns allColumns) {
for(int i = 0; i < schema.length; i++) {
columnIndexes.add(i);
}
}
public void visit(AllTableColumns allTableColumns) {
String aliasName = allTableColumns.getTable().getWholeTableName().toLowerCase();
String tableName;
if(aliasHashMap.containsKey(aliasName)){
tableName = aliasHashMap.get(aliasName);
for(int i = 0; i < schema.length; i++){
if(schema[i].getTable().getWholeTableName().toLowerCase().equals(tableName)){
columnIndexes.add(i);
}
}
}
}
public void visit(SelectExpressionItem selectExpressionItem) {
String tableName;
selectExpressionItem.getAlias();
Expression expression = selectExpressionItem.getExpression();
if(expression instanceof Column) {
Column column = (Column)expression;
if(column.getTable().getName() != null) {
if(aliasHashMap.containsKey(column.getTable().getName().toLowerCase())) {
tableName = aliasHashMap.get(column.getTable().getName().toLowerCase());
for(int i = 0; i < schema.length; i++) {
if(schema[i].getTable().getName().toLowerCase().equals(tableName)) {
if(schema[i].getColumnName().toLowerCase().equals(column.getColumnName().toLowerCase())) {
columnIndexes.add(i);
break;
}
}
}
} else {
}
} else {
for(int i = 0; i < schema.length; i++) {
if(schema[i].getColumnName().toLowerCase().equals(column.getColumnName().toLowerCase())) {
columnIndexes.add(i);
break;
}
}
}
}
}
}