-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubselectEvaluator.java
82 lines (74 loc) · 3.06 KB
/
SubselectEvaluator.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
import net.sf.jsqlparser.expression.PrimitiveValue;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.select.PlainSelect;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Mugdha on 7/2/2017.
*/
public class SubselectEvaluator implements Operator {
ArrayList<PrimitiveValue[]> outputTupleList;
int tupleLocation;
ArrayList<Column> schemaList;
Column[] schema;
String alias;
PlainSelect plainSelect;
HashMap<String, CreateTable> createTableMap;
public SubselectEvaluator(PlainSelect plainSelect, HashMap createTableMap, String alias){
this.plainSelect = plainSelect;
this.createTableMap = createTableMap;
this.alias = alias;
outputTupleList = new ArrayList<>();
schemaList = new ArrayList<>();
tupleLocation = 0;
}
public void execute(){
SubMain subMain = new SubMain(this.plainSelect, this.createTableMap);
outputTupleList = subMain.execute();
Column[] tempSchema = subMain.schema;
Table table = new Table("fromTable");
table.setAlias(alias);
ArrayList<String> columnList = new ArrayList<>();
ArrayList<ColumnDefinition> columnDefinitions = new ArrayList<>();
for(int i = 0; i < tempSchema.length; i++){
String tableName = tempSchema[i].getTable().getWholeTableName().toLowerCase();
CreateTable createTable = createTableMap.get(tableName);
ArrayList<ColumnDefinition> tempColumnDefinition = (ArrayList<ColumnDefinition>) createTable.getColumnDefinitions();
for(int j = 0; j < tempColumnDefinition.size(); j++){
String columnName = tempColumnDefinition.get(j).getColumnName().toLowerCase();
if(columnName.equals(tempSchema[i].getColumnName().toLowerCase())){
if(!(columnList.contains(columnName))){
columnList.add(columnName);
columnDefinitions.add(tempColumnDefinition.get(j));
schemaList.add(tempSchema[i]);
}
}
}
}
CreateTable createTable = new CreateTable();
createTable.setTable(table);
createTable.setColumnDefinitions(columnDefinitions);
createTableMap.put(createTable.getTable().getWholeTableName().toLowerCase(),createTable);
for(int i = 0; i < schemaList.size(); i++){
schemaList.get(i).setTable(table);
}
schema = new Column[schemaList.size()];
schema = schemaList.toArray(schema);
}
public PrimitiveValue[] readOneTuple(){
PrimitiveValue[] tuple;
if(tupleLocation < outputTupleList.size()) {
tuple = outputTupleList.get(tupleLocation);
tupleLocation++;
} else {
tuple = null;
}
return tuple;
}
public void reset(){
tupleLocation = 0;
}
}