public class QueryPlanVisitor extends SQLASTvisitor { private Hashtable tableDictionary; private Hashtable col2Table; public QueryPlan qPlan; private Vector colVec = null; public void visit(SelectStatement selectStatement) { tableDictionary = new Hashtable(); selectStatement.tables.accept(this); makeCol2Table(); selectStatement.whereexpression.accept(this); colVec = new Vector(); selectStatement.columns.accept(this); String[] colNames = (String[]) colVec.toArray(new String[0]); qPlan = new Project(colNames, qPlan); } public void visit(ColumnList columnList) { visit(columnList.columnrecur); colVec.add(columnList.column.toString()); } public void visit(ColumnRecur columnRecur) { if (columnRecur.isNullObject) return; visit(columnRecur.columnlist); } public void visit(TableList tableList) { visit(tableList.tablerecur); String tableName = tableList.table.toString(); tableDictionary.put(tableName, sql.Table.byName(tableName)); } public void visit(TableRecur tableRecur) { if (tableRecur.isNullObject) return; visit(tableRecur.tablelist); } private void makeCol2Table() { col2Table = new Hashtable(); Enumeration e = tableDictionary.elements(); while (e.hasMoreElements()) { Table table = (Table) e.nextElement(); String[] colNames = table.getColumnNames(); for (int i = 0; i < colNames.length; i++) { col2Table.put(colNames[i], table); } } } public void visit(ColumnFilterExpression exp) { Binop binOp = exp.binop; Table table = (Table) col2Table.get(exp.column.toString()); String colName = exp.column.toString(); String valAsString = exp.t_int.toString(); Op op = Op.opFor(binOp.toString()); int value = Integer.parseInt(valAsString); qPlan = new Select(colName, op, value, table); } public void visit(IntFilterExpression exp) { Binop binOp = exp.binop; Table table = (Table) col2Table.get(exp.column.toString()); String colName = exp.column.toString(); String valAsString = exp.t_int.toString(); Op op = Op.opFor(binOp.toString()); int value = Integer.parseInt(valAsString); qPlan = new Select(colName, op.invert(), value, table); } public void visit(JoinExpression joinExpression) { String selfColName = joinExpression.column.toString(); String otherColName = joinExpression.rightcolumn.toString(); Table otherTable = (Table) col2Table.get(otherColName); Table selfTable = (Table) col2Table.get(selfColName); qPlan = new Join(selfTable, selfColName, otherTable, otherColName); } }