Advertisement
Jodyone

problem 6 next()

Mar 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1.     public CrossIterator(SQLStatement stmt) throws DatabaseException {
  2.         /* not yet implemented */
  3.         int numTables = stmt.numTables();
  4.         int colCTR = 0;
  5.         int refMark = 0;                // refMark is used to set the for loop iterator at the
  6.                                         // correct index to store the next column object
  7.         int ctr = 0;
  8.         int sizeOfcolArry = 0;
  9.         int colAdvance = 0;
  10.         /**
  11.          * loop to get sum of columns from each table
  12.          * in order to dynamically size columns array
  13.          */
  14.         for (int i = 0; i < numTables; i++) {  
  15.             sizeOfcolArry += stmt.getTable(i).numColumns(); // count number of columns in each table    
  16.         }
  17.         this.columns = new Column[sizeOfcolArry];
  18.         this.tableIter = new TableIterator[stmt.numTables()];
  19.         /* *
  20.          *   loop over num tables in the stmt
  21.          */
  22.         for (int i = 0; i < numTables; i++) {
  23.             Table table = new Table(stmt.getTable(i).getName()); // create table object to store table info
  24.             table.open();
  25.             TableIterator iter = new TableIterator(stmt,table,false);
  26.             colCTR = ctr + table.numColumns();                         // count number of columns in each table
  27.             tableIter[i] = iter;                                 // assign iter to tableIter array
  28.             /**
  29.              * loop to store column objects into column array
  30.              */
  31.             ctr = 0;
  32.             for (int j = refMark; j < colCTR; j++) {
  33.                     this.columns[j] = table.getColumn(colAdvance);
  34.                     colAdvance++;
  35.                     ctr++;
  36.             }
  37.             colAdvance = 0;
  38.             refMark = ctr ;           // update refMark
  39.  
  40.         }
  41.         this.where = stmt.getWhere();
  42.         if (this.where == null)
  43.             this.where = new TrueExpression();   // set were clause
  44.        
  45.         this.numTuples = 0;
  46.     }
  47.  
  48.     public boolean next() throws DeadlockException, DatabaseException {
  49.         /* not yet implemented */
  50. //      for (int i = 0; i < tableIter.length-1; i++) {
  51. //            tableIter[i].first();                      // set all iterators to first row
  52. //        }
  53.  
  54.         if (tableIter.length > 1){
  55.             for (int i = tableIter.length; i > 0 ; i--) { // start at the right most cell
  56.                 // If this is the first time the method has been called,
  57.                 // preload the first row of R.
  58.                 if (numTuples == 0) {
  59.                    if (!tableIter[0].first())
  60.                        return false;   // R is empty
  61.                 }
  62.  
  63.                 // Advance the table iterators as much as needed to get
  64.                 // to a combination of rows from R and S that satisfies the
  65.                 // WHERE clause.
  66.                 do {
  67.                     if (!tableIter[i].next()) {
  68.                         // We've paired all rows of S with the current row of R,
  69.                         // so reset S to its first row...
  70.                         if (!tableIter[i].first())
  71.                             return false;   // S is empty
  72.                        
  73.                         // ...and advance to R's next row (if any).
  74.                         if (!tableIter[i-1].next())
  75.                             return false;   // no more rows in R, so we're done
  76.                     }
  77.                    
  78.                 } while(!this.where.isTrue());
  79.  
  80.             }
  81.            
  82.         }
  83.  
  84.    
  85.         this.numTuples++;
  86.         return true;
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement