1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import com.daffodilwoods.daffodildb.client.*; 4 import com.daffodilwoods.daffodildb.server.serversystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 7 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 8 import com.daffodilwoods.daffodildb.utils.*; 9 import com.daffodilwoods.daffodildb.utils.comparator.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.database.utility.*; 12 13 19 20 21 public class DistinctIterator extends BaseSingleIterator implements TypeConstants { 22 23 26 private Object previousRecord; 27 30 private _Reference[] selectReferences; 31 35 private _VariableValues variableValues; 36 39 private int state; 40 44 private SuperComparator superComparator; 45 46 47 public DistinctIterator(_Iterator iterator0, _Reference[] selectReferences0, _Reference[] selectListQuestionReferences, _ServerSession serverSession) { 48 super(iterator0); 49 selectReferences = selectReferences0; 50 state = INVALIDSTATE; 51 variableValues = new VariableValues(selectListQuestionReferences, serverSession); try { 53 variableValues.setIterator(iterator0); } catch (DException ex) { 55 } 56 } 57 58 67 75 public boolean first() throws DException { 76 boolean flag = iterator.first(); 77 if (flag) { 78 state = VALIDSTATE; 79 previousRecord = getReferenceValues(); 80 } else { 81 state = AFTERLAST; 82 } 83 return flag; 84 } 85 86 97 public boolean next() throws DException { 98 switch (state) { 99 case INVALIDSTATE: 100 throw new DException("DSE4116", null); 101 case BEFOREFIRST: 102 return first(); 103 case AFTERLAST: 104 return false; 105 } 106 boolean flag = moveForward(); 107 previousRecord = flag ? getReferenceValues() : previousRecord; 108 if(!flag) 109 state=AFTERLAST; 110 111 return flag; 112 } 113 114 125 public boolean previous() throws DException { 126 switch (state) { 127 case INVALIDSTATE: 128 throw new DException("DSE4117", null); 129 case BEFOREFIRST: 130 return false; 131 case AFTERLAST: 132 return last(); 133 } 134 boolean flag = moveBackward(); 135 previousRecord = flag ? getReferenceValues() : previousRecord; 136 if(!flag) 137 state=BEFOREFIRST; 138 return flag; 139 } 140 141 149 public boolean last() throws DException { 150 boolean flag = iterator.last(); 151 if (flag) { 152 state = VALIDSTATE; 153 previousRecord = getReferenceValues(); 154 } else { 155 state = BEFOREFIRST; 156 } 157 return flag; 158 } 159 160 166 private boolean moveForward() throws DException { 167 boolean flag; 168 while ( (flag = iterator.next()) && checkForDistinctRecord(getReferenceValues())) { 169 ; 170 } 171 return flag; 172 } 173 174 180 private boolean moveBackward() throws DException { 181 boolean flag; 182 while ( (flag = iterator.previous()) && checkForDistinctRecord(getReferenceValues())) { 183 ; 184 } 185 return flag; 186 } 187 188 196 private boolean checkForDistinctRecord(Object nextRecord) throws DException { 197 try { 198 boolean b = superComparator.compare(previousRecord, nextRecord) == 0; 199 return b; 200 } catch (NullPointerException ex) { 201 superComparator = getAppropriateComparator(selectReferences); 202 boolean b = superComparator.compare(previousRecord, nextRecord) == 0; 203 return b; 204 } 205 } 206 207 215 private SuperComparator getAppropriateComparator(_Reference[] columns) throws DException { 216 int length = columns.length; 217 SuperComparator[] comparators = new SuperComparator[length]; 218 for (int i = 0; i < length; i++) { 219 if (! ( (ColumnDetails) columns[i]).canUseByteComparison()) { 220 comparators[i] = new CPckfduDpnqbsbups(); 221 continue; 222 } 223 int type = ( (ColumnDetails) columns[i]).getType(); 224 switch (type) { 225 case FUNCTIONAL: 226 case GROUPING: 227 case CASEEXPRESSION: 228 case SCALARFUNCTION: 229 case CONSTANT: 230 case USERFUNCTION : 231 comparators[i] = new CPckfduDpnqbsbups(); 232 break; 233 case REFERENCE: 234 try { 235 comparators[i] = GetByteComparator.getComparator(columns[i].getDatatype(), true, 236 ( (ColumnDetails) columns[i]).getTable().getColumnCharacteristics().getCollator()); 237 } catch (DException ex) { 238 throw ex; 239 } 240 break; 241 } 242 } 243 return new DistinctComparator(comparators); 244 } 245 246 252 private Object getReferenceValues() throws DException { 253 int i = 0, length = selectReferences.length; 254 Object [] values = new Object [length]; 255 for (; i < length; i++) { 256 values[i] = getSingleReferenceValue(selectReferences[i]); 257 } 258 return values; 259 } 260 261 271 private Object getSingleReferenceValue(_Reference reference) throws DException { 272 ColumnDetails column = (ColumnDetails) reference; 273 return column.getTableDetails() != null ? iterator.getColumnValues(reference) 274 : column.run(variableValues); 275 } 276 277 280 289 public Object getColumnValues(_Reference[] leftColumnReferences) throws DException { 290 int length = leftColumnReferences.length; 291 Object [] value = new Object [length]; 292 for (int i = 0; i < length; i++) { 293 value[i] = iterator.getColumnValues(leftColumnReferences[i]); 294 } 295 return value; 296 } 297 298 307 public Object getColumnValues(_Reference references) throws DException { 308 return iterator.getColumnValues(references); 309 } 310 311 319 public Object getColumnValues(int[] parm1) throws com.daffodilwoods.database.resource.DException { 320 return iterator.getColumnValues(); 321 } 322 323 public Object getColumnValues() throws DException { 324 Object [] value = (Object []) previousRecord; 325 int length = value.length; 326 Object [] newValues = new Object [length + 1]; 327 System.arraycopy(value, 0, newValues, 0, length); 328 newValues[length] = new Long (1); 329 return newValues; 330 } 331 332 337 public _ExecutionPlan getExecutionPlan() throws DException { 338 _ExecutionPlan plan = iterator.getExecutionPlan(); 339 _ExecutionPlan cplans[] = plan == null ? null : new _ExecutionPlan[] {plan}; 340 return new ExecutionPlan("DistinctIterator", cplans, null, null, null); 341 } 342 343 348 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 349 int length = 1; 350 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length]; 351 cplans[0] = iterator.getExecutionPlanForBrowser(); 352 return new ExecutionPlanForBrowser("Distinct", "DistinctIterator", cplans, null, null, null); 353 } 354 355 362 public void releaseResource() throws DException { 363 variableValues.releaseResource(); 364 iterator.releaseResource(); 365 superComparator = null; 366 } 367 368 373 374 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 375 return iterator.getBaseIterator(column); 376 } 377 378 public void setConditionVariableValue(_Reference[] references, Object [] values, int priority) throws DException { 379 if(underlyingRef!=null){ 380 references = GeneralPurposeStaticClass.getJointReferences(references,underlyingRef); 381 values = GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length); 382 } 383 iterator.setConditionVariableValue(references, values, priority); 384 variableValues.setConditionVariableValue(references, values, priority); 385 } 386 387 public boolean seekFromTop(_IndexPredicate[] parm1) throws com.daffodilwoods.database.resource.DException { 388 389 throw new java.lang.UnsupportedOperationException ("Method seekFromTop() not yet implemented."); 390 } 391 392 public boolean seekFromBottom(_IndexPredicate[] parm1) throws com.daffodilwoods.database.resource.DException { 393 394 throw new java.lang.UnsupportedOperationException ("Method seekFromBottom() not yet implemented."); 395 } 396 397 398 public String toString() { 399 String str = "DISTINCTITERATOR"; 400 str += "[" + iterator + "]"; 401 return str; 402 } 403 } 404 | Popular Tags |