Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SelectStatement.java
- *
- * DBMS Implementation
- */
- import java.util.*;
- import com.sleepycat.db.*;
- /**
- * A class that represents a SELECT statement.
- */
- public class SelectStatement extends SQLStatement {
- /* Used in the selectList for SELECT * statements. */
- public static final String STAR = "*";
- private ArrayList<Object> selectList;
- private Limit limit;
- private boolean distinctSpecified;
- /**
- * Constructs a SelectStatement object involving the specified
- * columns and other objects from the SELECT clause, the specified
- * tables from the FROM clause, the specified conditional
- * expression from the WHERE clause (if any), the specified Limit
- * object summarizing the LIMIT clause (if any), and the specified
- * value indicating whether or not we should eliminate duplicates.
- *
- * @param selectList the columns and other objects from the SELECT clause
- * @param fromList the list of tables from the FROM clause
- * @param where the conditional expression from the WHERE clause (if any)
- * @param limit summarizes the info in the LIMIT clause (if any)
- * @param distinctSpecified should duplicates be eliminated?
- */
- public SelectStatement(ArrayList<Object> selectList,
- ArrayList<Table> fromList, ConditionalExpression where,
- Limit limit, Boolean distinctSpecified)
- {
- super(fromList, new ArrayList<Column>(), where);
- this.selectList = selectList;
- this.limit = limit;
- this.distinctSpecified = distinctSpecified.booleanValue();
- /* add the columns in the select list to the list of columns */
- for (int i = 0; i < selectList.size(); i++) {
- Object selectItem = selectList.get(i);
- if (selectItem instanceof Column)
- this.addColumn((Column)selectItem);
- }
- }
- public Column[] makeWhereColArray() {
- // populate Where columns
- Column[] WColArry = new Column[this.numWhereColumns()]; // column Array for the where columns
- Column whereCol;
- for (int i = 0; i < this.numWhereColumns(); i++) {
- whereCol = this.getWhereColumn(i);
- WColArry[i] = whereCol;
- }
- return WColArry;
- }
- /**
- * Returns a boolean value indicating whether duplicates should be
- * eliminated in the result of this statement -- i.e., whether the
- * user specified SELECT DISTINCT.
- */
- public boolean distinctSpecified() {
- return this.distinctSpecified;
- }
- public void execute() throws DatabaseException, DeadlockException {
- RelationIterator iter = null;
- TableIterator tab = null;
- Column[] WColArry = new Column[this.numWhereColumns()]; // column Array for the where columns
- boolean hasColName = false;
- WColArry = makeWhereColArray(); // function to make the WHERE column Array
- try {
- if (this.numTables() == 1) {
- Table table = this.getTable(0);
- if (table.open() == OperationStatus.NOTFOUND)
- throw new IllegalStateException(); // error msg in open()
- // loop to check if WHERE col name matches one of the cols in FROM
- for (int j = 0; j < WColArry.length;j++) {
- for (int k = 0; k < table.numColumns(); k++) {
- if(WColArry[j].nameMatches(table.getColumn(k), table))
- hasColName = true;
- }
- }
- if(!hasColName)
- throw new IllegalStateException("where columns do not match " +
- "Table columns ");
- // if SELECT is *
- if (this.selectList.get(0) == STAR){
- //if there is a Where clause do this
- if (this.numWhereColumns() > 0){
- // ** this is were im stuck ???? ***
- // go into the WHERE col Array and check
- // if WHERE col name matches table col name
- // if the value in the table col evals true or false compared to WHERE col eval ?
- }
- else
- iter = new TableIterator(this, table, true);
- }
- else{
- tab = new TableIterator(this,table,true); // when i call this the
- iter = new ProjectionIterator(this,tab);
- }
- // if numTables() > 1
- } else{
- Table[] tables = new Table[this.numTables()]; // table array for tables in fromList
- for (int i = 0; i < this.numTables(); i++) { // loop thru open all tables
- Table tempTable = new Table(this.getTable(i).getName());
- tables[i]= tempTable ;
- if (tables[i].open() == OperationStatus.NOTFOUND)
- throw new IllegalStateException(); // error msg in open()
- // check the where columns for match in FROM
- Column whereCol;
- for (int j = 0; j < this.numWhereColumns(); j++) {
- whereCol = this.getWhereColumn(j);
- WColArry[j] = whereCol;
- }
- for (int j2 = 0; j2 < WColArry.length;j2++) {
- for (int k = 0; k < tempTable.numColumns(); k++) {
- if(WColArry[j2].nameMatches(tempTable.getColumn(k),tempTable))
- hasColName = true;
- }
- }
- }
- if(!hasColName)
- throw new IllegalStateException("where columns do not match " +
- "Table columns ");
- if (this.numColumns() == 0 && this.selectList.get(0) == STAR)
- iter = new CrossIterator(this);
- else {
- CrossIterator c = new CrossIterator(this);
- iter = new ProjectionIterator(this,c);
- }
- }
- // Iterate over all tuples in the relation and print them out.
- iter.printAll(System.out);
- // Report the number of tuples that were selected.
- int numRows = iter.numTuples();
- System.out.println("Selected " + numRows +
- (numRows == 1 ? " tuple." : " tuples."));
- } catch (Exception e) {
- String errMsg = e.getMessage();
- if (errMsg != null)
- System.err.println(errMsg + ".");
- }
- if (iter != null)
- iter.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement