Advertisement
Jodyone

Untitled

Apr 5th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.57 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.  
  52.     /**
  53.      * Returns a boolean value indicating whether duplicates should be
  54.      * eliminated in the result of this statement -- i.e., whether the
  55.      * user specified SELECT DISTINCT.
  56.      */
  57.     public boolean distinctSpecified() {
  58.         return this.distinctSpecified;
  59.     }
  60.      
  61.     public void execute() throws DatabaseException, DeadlockException {
  62.         RelationIterator iter = null;
  63.         TableIterator tab = null;
  64.         boolean hasColName = false;
  65.         boolean hasSelName = false;
  66.  
  67.         try {
  68.             if (this.numTables() == 1) {
  69.                 Table table = this.getTable(0);
  70.                 if (table.open() == OperationStatus.NOTFOUND)
  71.                     throw new IllegalStateException();  // error msg in open()
  72.                 if (this.numWhereColumns() > 0){
  73.                     // loop to check if WHERE col name matches one of the cols in FROM
  74.                     for (int j = 0; j < this.numWhereColumns();j++) {
  75.                         for (int k = 0; k < table.numColumns(); k++) {
  76.                             if(this.getWhereColumn(j).nameMatches(table.getColumn(k), table))
  77.                                 hasColName = true;     
  78.                         }
  79.                     }
  80.                     // loop to check SELECT name matches one of the cols in FROM
  81.                     for (int i = 0; i < table.numColumns(); i++) {
  82.                         if(selectList.contains(table.getColumn(i)))
  83.                             hasSelName = true; 
  84.                     }
  85.                      
  86.                     if(!hasColName)
  87.                          throw new IllegalStateException("WHERE columns do not match " +
  88.                                                          "Table columns ");
  89.                     if(!hasSelName )
  90.                          throw new IllegalStateException("SELECT columns do not match " +
  91.                                                          "Table columns ");
  92.                 }
  93.  
  94.                 // if SELECT is *
  95.                 if (this.selectList.get(0) == STAR)
  96.                       iter = new TableIterator(this, table, true);
  97.                 else{
  98.                       tab = new TableIterator(this,table,true);
  99.                       iter = new ProjectionIterator(this,tab);            
  100.                 }
  101.                
  102.               // if numTables() > 1
  103.             } else{
  104.                  Table[] tables = new Table[this.numTables()]; // table array for tables in fromList
  105.                 // if there are WHERE col run this code
  106.                  if (this.numWhereColumns() > 0){
  107.                      for (int i = 0; i < this.numTables(); i++) {  // loop thru open all tables
  108.                          Table tempTable = new Table(this.getTable(i).getName());
  109.                          tables[i]= tempTable ;
  110.                          if (tables[i].open() == OperationStatus.NOTFOUND)
  111.                             throw new IllegalStateException();  // error msg in open()  
  112.                          
  113.                             // check the where columns for match in FROM
  114.                             for (int j2 = 0; j2 < this.numColumns();j2++) {
  115.                                 for (int k = 0; k < tempTable.numColumns(); k++) {
  116.                                     if(this.getWhereColumn(j2).nameMatches(tempTable.getColumn(k),tempTable))
  117.                                         hasColName = true;
  118.                                 }
  119.                             }
  120.                            
  121.                             for (int m = 0; m < tables[i].numColumns(); m++) {
  122.                                 if(selectList.contains(tables[i].getColumn(m)))
  123.                                     hasSelName = true; 
  124.                             }
  125.    
  126.                      }
  127.                      if(!hasColName )
  128.                          throw new IllegalStateException("where columns do not match " +
  129.                                                          "Table columns ");
  130.                      if(!hasSelName )
  131.                          throw new IllegalStateException("SELECT columns do not match " +
  132.                                                          "Table columns ");
  133.                  }
  134.                  if (this.numColumns() == 0 && this.selectList.get(0) == STAR)
  135.                      iter = new CrossIterator(this);  
  136.                  else {
  137.                       CrossIterator c = new CrossIterator(this);
  138.                       iter = new ProjectionIterator(this,c);
  139.                  }
  140.                                  
  141.             }
  142.  
  143.             // Iterate over all tuples in the relation and print them out.
  144.             iter.printAll(System.out);
  145.            
  146.             // Report the number of tuples that were selected.
  147.             int numRows = iter.numTuples();
  148.             System.out.println("Selected " + numRows +
  149.                                (numRows == 1 ? " tuple." : " tuples."));
  150.         } catch (Exception e) {
  151.             String errMsg = e.getMessage();
  152.             if (errMsg != null)
  153.                 System.err.println(errMsg + ".");
  154.         }
  155.        
  156.         if (iter != null)
  157.             iter.close();
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement