1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 10 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 11 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 12 import com.daffodilwoods.daffodildb.utils.*; 13 import com.daffodilwoods.daffodildb.utils.field.*; 14 import com.daffodilwoods.database.resource.*; 15 import java.util.*; 16 import com.daffodilwoods.database.utility.P; 17 18 19 38 public class SpecialRightJoinIterator extends SemiJoinFilteredIterator implements _HasRecordIterator { 39 40 55 public SpecialRightJoinIterator(_Iterator leftIterator0, _Iterator rightIterator0, booleanvalueexpression condition, _KeyColumnInformation[] leftKeyColumnInformation0, _KeyColumnInformation[] rightKeyColumnInformation0, ColumnDetails[] hasRecordReferences0, _ServerSession session) throws DException { 56 super(rightIterator0, leftIterator0, condition, rightKeyColumnInformation0, leftKeyColumnInformation0, hasRecordReferences0, session); 57 } 58 59 75 public boolean first() throws DException { 76 if (!leftIterator.first()) { 77 state = AFTERLAST; 78 return false; 79 } 80 rightNull = false; 81 while (rightIterator.first() && ifConditionSolvedForward()) { 82 if (!leftIterator.next()) { 83 state = AFTERLAST; 84 return false; 85 } 86 } 87 state = VALIDSTATE; 88 rightNull = true; 89 return true; 90 } 91 92 93 94 95 109 public boolean next() throws DException { 110 switch (state) { 111 case INVALIDSTATE: 112 throw new DException("DSE4116", null); 113 case BEFOREFIRST: 114 return first(); 115 case AFTERLAST: 116 return false; 117 default: { 118 do { 119 rightNull = false; 120 if (!leftIterator.next()) { 121 state = AFTERLAST; 122 return false; 123 } 124 } while (rightIterator.first() && ifConditionSolvedForward()); 125 state = VALIDSTATE; 126 rightNull = true; 127 return true; 128 } 129 } 130 } 131 132 148 public boolean last() throws DException { 149 if (!leftIterator.last()) { 150 state = BEFOREFIRST; 151 return false; 152 } 153 rightNull = false; 154 while (rightIterator.last() && ifConditionSolvedBackward()) { 155 if (!leftIterator.previous()) { 156 state = BEFOREFIRST; 157 return false; 158 } 159 } 160 rightNull = true; 161 state = VALIDSTATE; 162 return true; 163 } 164 165 179 public boolean previous() throws DException { 180 rightNull = false; 181 switch (state) { 182 case INVALIDSTATE: 183 throw new DException("DSE4117", null); 184 case BEFOREFIRST: 185 return false; 186 case AFTERLAST: 187 return last(); 188 default: { 189 do { 190 if (!leftIterator.previous()) { 191 state = BEFOREFIRST; 192 return false; 193 } 194 } while (rightIterator.last() && ifConditionSolvedBackward()); 195 rightNull = true; 196 state = VALIDSTATE; 197 return true; 198 } 199 } 200 } 201 202 212 private boolean ifConditionSolvedForward() throws DException { 213 do { 214 if (conditionVariableValue.run().hashCode() == 0) { 215 state = VALIDSTATE; 216 return true; 217 } 218 } while (rightIterator.next()); 219 return false; 220 } 221 222 232 private boolean ifConditionSolvedBackward() throws DException { 233 do { 234 if (conditionVariableValue.run().hashCode() == 0) { 235 state = VALIDSTATE; 236 return true; 237 } 238 } while (rightIterator.previous()); 239 return false; 240 } 241 242 249 public Object getKey() throws com.daffodilwoods.database.resource.DException { 250 ArrayList list = new ArrayList(); 251 Object [] leftKeys = (Object []) leftIterator.getKey(); 252 list.addAll(java.util.Arrays.asList(leftKeys)); 253 for (int i = 0; i < rightCount; i++) { 254 list.add(null); 255 } 256 return list.toArray(); 257 } 258 259 private void moveAcctoGroupKey(Object [] keys) throws DException { 260 if (leftKeyCount == 0) { 261 leftIterator.first(); 262 } else { 263 Object [] leftKeys = new Object [leftKeyCount]; 264 System.arraycopy(keys, 0, leftKeys, 0, leftKeyCount); 265 leftIterator.move(leftKeys); 266 } 267 if (rightKeyCount == 0) { 268 rightIterator.first(); 269 } else { 270 int rightLength = keys.length - leftKeyCount; 271 Object [] rightKeys = new Object [rightLength]; 272 System.arraycopy(keys, leftKeyCount, rightKeys, 0, rightLength); 273 if (isKeyNull(rightKeys)) { 274 rightNull = true; 275 } else { 276 rightNull = false; 277 rightIterator.move(rightKeys); 278 } 279 } 280 state = VALIDSTATE; 281 } 282 283 292 public void move(Object keys) throws com.daffodilwoods.database.resource.DException { 293 if (leftKeyCount != 0 || rightKeyCount != 0) { 294 moveAcctoGroupKey( (Object []) keys); 295 return; 296 } 297 Object [] rightKeys = new Object [rightCount]; 298 int leftCount = ( (Object []) keys).length - rightCount; 299 Object [] leftKeys = new Object [leftCount]; 300 System.arraycopy(keys, 0, leftKeys, 0, leftCount); 301 System.arraycopy(keys, leftCount, rightKeys, 0, rightCount); 302 leftIterator.move(leftKeys); 303 if (isKeyNull(rightKeys)) { 304 rightNull = true; 305 } else { 306 rightNull = false; 307 rightIterator.move(rightKeys); 308 } 309 state = 0; 310 } 311 312 319 328 public Object getColumnValues(_Reference[] columnReferences) throws DException { 329 int len = columnReferences.length; 330 Object [] values = new Object [len]; 331 for (int i = 0; i < len; i++) { 332 values[i] = getColumnValues(columnReferences[i]); 333 } 334 return values; 335 } 336 337 344 private int searchInMapping(TableDetails tDetails) throws DException { 345 for (int i = 0; i < tableDetailsMapping.length; i++) { 346 if (tableDetailsMapping[i][0] == tDetails) { 347 return ( (Integer ) tableDetailsMapping[i][1]).intValue(); 348 } 349 } 350 return -1; 351 } 352 353 363 public Object getColumnValues() throws DException { 364 ArrayList aList = new ArrayList(); 365 Object [] obj2 = null; 366 obj2= makeNullFieldLiteralArray(rightColumnCount); aList.addAll(Arrays.asList( (Object []) obj2)); 368 Object obj1 = leftIterator.getColumnValues(); 369 aList.addAll(Arrays.asList( (Object []) obj1)); 370 371 return aList.toArray(); 372 } 373 374 385 public Object getColumnValues(_Reference references) throws DException { 386 Object values; 387 ColumnDetails refColumnDetail = (ColumnDetails) references; 388 TableDetails tDetails = refColumnDetail.getTable(); 389 int index = searchInMapping(tDetails); 390 if (index == -1) { 391 return GeneralPurposeStaticClass.getColumnValuesFromQualified(leftIterator, rightIterator, references, rightNull); 392 } 393 if (index == SimpleConstants.LEFT) { 394 values = leftIterator.getColumnValues(references); 395 } else { 396 if (rightNull) { 397 values = new FieldLiteral(FieldUtility.NULLBUFFERRANGE, ( (ColumnDetails) references).getDatatype()); 398 } else { 399 values = rightIterator.getColumnValues(references); 400 } 401 } 402 return values; 403 } 404 405 413 public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException { 414 FieldBase values; 415 ColumnDetails refColumnDetail = (ColumnDetails) references; 416 TableDetails tDetails = refColumnDetail.getTable(); 417 int index = searchInMapping(tDetails); 418 if (index == -1) { 419 return GeneralPurposeStaticClass.getFieldFromQualified(leftIterator, rightIterator, references, rightNull); 420 } 421 if (index == SimpleConstants.LEFT) { 422 values = leftIterator.field(references); 423 } else 424 if (rightNull) { 425 values = new FieldLiteral(FieldUtility.NULLBUFFERRANGE, ( (ColumnDetails) references).getDatatype()); 426 } else { 427 values = rightIterator.field(references); 428 } 429 return values; 430 } 431 432 440 public FieldBase[] fields(_Reference[] columnReferences) throws com.daffodilwoods.database.resource.DException { 441 int len = columnReferences.length; 442 FieldBase[] values = new FieldBase[len]; 443 for (int i = 0; i < len; i++) { 444 values[i] = field(columnReferences[i]); 445 } 446 return values; 447 } 448 449 454 public _ExecutionPlan getExecutionPlan() throws DException { 455 _ExecutionPlan plan = leftIterator.getExecutionPlan(); 456 _ExecutionPlan plan1 = rightIterator.getExecutionPlan(); 457 _ExecutionPlan cplans[] = new _ExecutionPlan[] {plan, plan1}; 458 return new ExecutionPlan("SpecialRightJoinIterator", cplans, null, null, null); 459 } 460 461 472 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 473 return this; 474 } 475 476 481 482 public byte[] getByteKey() throws DException{ 483 return leftIterator.getByteKey(); 484 } 485 486 public void moveByteKey(byte[] key) throws DException{ 487 leftIterator.moveByteKey(key); 488 rightNull = true; 489 } 490 491 public String toString() { 492 try { 493 return "SpecialRightJoinIterator[" + leftIterator + "][" + rightIterator + "]" + conditionVariableValue.getCondition() + "]"; 494 } catch (DException ex) { 495 return null; 496 } 497 } 498 } 499 | Popular Tags |