Advertisement
Jodyone

problem 9

Mar 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.74 KB | None | 0 0
  1.  /*
  2.  * SelectStatement.java
  3.  *
  4.  * DBMS Implementation
  5.  */
  6.  
  7. import java.util.*;
  8. import com.sleepycat.db.*;
  9.  
  10. /**
  11.  * A class that represents a SELECT statement.
  12.  */
  13. public class SelectStatement extends SQLStatement {
  14.     /* Used in the selectList for SELECT * statements. */
  15.     public static final String STAR = "*";
  16.    
  17.     private ArrayList<Object> selectList;
  18.     private Limit limit;
  19.     private boolean distinctSpecified;
  20.    
  21.     /**
  22.      * Constructs a SelectStatement object involving the specified
  23.      * columns and other objects from the SELECT clause, the specified
  24.      * tables from the FROM clause, the specified conditional
  25.      * expression from the WHERE clause (if any), the specified Limit
  26.      * object summarizing the LIMIT clause (if any), and the specified
  27.      * value indicating whether or not we should eliminate duplicates.
  28.      *
  29.      * @param  selectList  the columns and other objects from the SELECT clause
  30.      * @param  fromList  the list of tables from the FROM clause
  31.      * @param  where  the conditional expression from the WHERE clause (if any)
  32.      * @param  limit  summarizes the info in the LIMIT clause (if any)
  33.      * @param  distinctSpecified  should duplicates be eliminated?
  34.      */
  35.     public SelectStatement(ArrayList<Object> selectList,
  36.                            ArrayList<Table> fromList, ConditionalExpression where,
  37.                            Limit limit, Boolean distinctSpecified)
  38.     {
  39.         super(fromList, new ArrayList<Column>(), where);
  40.         this.selectList = selectList;
  41.         this.limit = limit;
  42.         this.distinctSpecified = distinctSpecified.booleanValue();
  43.        
  44.         /* add the columns in the select list to the list of columns */
  45.         for (int i = 0; i < selectList.size(); i++) {
  46.             Object selectItem = selectList.get(i);
  47.             if (selectItem instanceof Column)
  48.                 this.addColumn((Column)selectItem);
  49.         }
  50.     }
  51.     public Column[] makeWhereColArray() {
  52.         // populate Where columns
  53.         Column[] WColArry = new Column[this.numWhereColumns()];  // column Array for the where columns
  54.         Column whereCol;
  55.         for (int i = 0; i < this.numWhereColumns(); i++) {
  56.             whereCol = this.getWhereColumn(i);
  57.             WColArry[i] = whereCol;
  58.         }
  59.         return WColArry;
  60.     }
  61.    
  62.    
  63.     /**
  64.      * Returns a boolean value indicating whether duplicates should be
  65.      * eliminated in the result of this statement -- i.e., whether the
  66.      * user specified SELECT DISTINCT.
  67.      */
  68.     public boolean distinctSpecified() {
  69.         return this.distinctSpecified;
  70.     }
  71.      
  72.     public void execute() throws DatabaseException, DeadlockException {
  73.         RelationIterator iter = null;
  74.         TableIterator tab = null;
  75.         Column[] WColArry = new Column[this.numWhereColumns()];  // column Array for the where columns
  76.         boolean hasColName = false;
  77.        
  78.         WColArry = makeWhereColArray();                          //  function to make the WHERE column Array
  79.        
  80.         try {
  81.             if (this.numTables() == 1) {
  82.                 Table table = this.getTable(0);
  83.                 if (table.open() == OperationStatus.NOTFOUND)
  84.                     throw new IllegalStateException();  // error msg in open()
  85.                 // loop to check if WHERE col name matches one of the cols in FROM
  86.                 for (int j = 0; j < WColArry.length;j++) {
  87.                     for (int k = 0; k < table.numColumns(); k++) {
  88.                         if(WColArry[j].nameMatches(table.getColumn(k), table))
  89.                             hasColName = true;
  90.                     }
  91.                 }
  92.                 if(!hasColName)
  93.                      throw new IllegalStateException("where columns do not match " +
  94.                                                      "Table columns ");
  95.                 // if SELECT is *
  96.                 if (this.selectList.get(0) == STAR){
  97.                     //if there is a Where clause do this
  98.                     if (this.numWhereColumns() > 0){
  99.                         // ** this is were im stuck ???? ***
  100.                         // go into the WHERE col Array and check
  101.                         // if WHERE col name matches table col name
  102.                             // if the value in the table col evals true or false compared to WHERE col eval ?
  103.                     }
  104.                     else
  105.                         iter = new TableIterator(this, table, true);
  106.                 }
  107.                 else{
  108.                       tab = new TableIterator(this,table,true); // when i call this the
  109.                       iter = new ProjectionIterator(this,tab);            
  110.                 }
  111.                
  112.               // if numTables() > 1
  113.             } else{
  114.                  Table[] tables = new Table[this.numTables()]; // table array for tables in fromList
  115.                  
  116.                  for (int i = 0; i < this.numTables(); i++) {  // loop thru open all tables
  117.                      Table tempTable = new Table(this.getTable(i).getName());
  118.                      tables[i]= tempTable ;
  119.                      if (tables[i].open() == OperationStatus.NOTFOUND)
  120.                         throw new IllegalStateException();  // error msg in open()  
  121.                      // check the where columns for match in FROM
  122.                      Column whereCol;
  123.                      for (int j = 0; j < this.numWhereColumns(); j++) {
  124.                         whereCol = this.getWhereColumn(j);
  125.                         WColArry[j] = whereCol;
  126.                     }
  127.                      for (int j2 = 0; j2 < WColArry.length;j2++) {
  128.                         for (int k = 0; k < tempTable.numColumns(); k++) {
  129.                             if(WColArry[j2].nameMatches(tempTable.getColumn(k),tempTable))
  130.                                 hasColName = true;
  131.                         }
  132.                     }
  133.                  }
  134.                  if(!hasColName)
  135.                      throw new IllegalStateException("where columns do not match " +
  136.                                                      "Table columns ");
  137.                  
  138.                  if (this.numColumns() == 0 && this.selectList.get(0) == STAR)
  139.                      iter = new CrossIterator(this);  
  140.                  else {
  141.                       CrossIterator c = new CrossIterator(this);
  142.                       iter = new ProjectionIterator(this,c);
  143.                  }
  144.                                  
  145.             }
  146.  
  147.             // Iterate over all tuples in the relation and print them out.
  148.             iter.printAll(System.out);
  149.            
  150.             // Report the number of tuples that were selected.
  151.             int numRows = iter.numTuples();
  152.             System.out.println("Selected " + numRows +
  153.                                (numRows == 1 ? " tuple." : " tuples."));
  154.         } catch (Exception e) {
  155.             String errMsg = e.getMessage();
  156.             if (errMsg != null)
  157.                 System.err.println(errMsg + ".");
  158.         }
  159.        
  160.         if (iter != null)
  161.             iter.close();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement