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.datasystem.indexsystem.*; 7 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 11 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 12 import com.daffodilwoods.daffodildb.utils.field.*; 13 import com.daffodilwoods.database.general.*; 14 import com.daffodilwoods.database.resource.*; 15 import com.daffodilwoods.database.sqlinitiator.*; 16 import com.daffodilwoods.database.utility.P; 17 18 34 public class ViewIterator extends BaseSingleIterator { 35 36 39 private Object [][] mappingOfColAndCD; 40 43 private TableDetails tableDetails; 44 47 private _KeyColumnInformation[] keyColumnInfo; 48 51 private _Order defaultOrder; 52 53 public ViewIterator(_Iterator iterator0, Object [][] mappingOfColAndCD0, TableDetails tableDetails0) { 54 super(iterator0); 55 mappingOfColAndCD = mappingOfColAndCD0; 56 tableDetails = tableDetails0; 57 } 58 59 68 public Object getColumnValues(_Reference[] reference) throws DException { 69 return getUnderLyingReferencesValues(reference); 70 } 71 72 81 private Object [] getUnderLyingReferencesValues(_Reference[] reference) throws DException { 82 int length = reference.length; 83 Object [] values = new Object [length]; 84 for (int j = 0; j < length; j++) { 85 values[j] = getColumnValues(reference[j]); 86 } 87 return values; 88 } 89 90 103 public Object getColumnValues(_Reference reference) throws DException { 104 _Reference underLyingReference = getUnderLyingReference(reference); 105 Object obj = underLyingReference == null ? ( (_SelectQueryIterator) iterator).getColumnValueForReference(reference) : iterator.getColumnValues(underLyingReference); 106 return obj; 107 } 108 109 116 private _Reference getUnderLyingReference(_Reference reference) throws DException { 117 if(mappingOfColAndCD !=null) 118 for (int i = 0, length1 = mappingOfColAndCD.length; i < length1; i++) { 119 if ( reference == mappingOfColAndCD[i][0] ) 120 return (_Reference)mappingOfColAndCD[i][1]; 121 if( reference.getReferenceType() ==SimpleConstants.COLUMNDETAIL ){ 122 if(((ColumnDetails)reference).isSame((_Reference)mappingOfColAndCD[i][0])) { 123 return (_Reference) mappingOfColAndCD[i][1]; 124 } 125 } 126 } 127 return null; 128 } 129 130 138 public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException { 139 _Reference underLyingReference = getUnderLyingReference(reference); 140 return underLyingReference == null ? ( (_SelectQueryIterator) iterator).getFieldValueForReference(reference) : iterator.field(underLyingReference); 141 } 142 143 151 public FieldBase[] fields(_Reference[] reference) throws com.daffodilwoods.database.resource.DException { 152 int length = reference.length; 153 FieldBase[] values = new FieldBase[length]; 154 for (int j = 0; j < length; j++) { 155 values[j] = field(reference[j]); 156 } 157 return values; 158 } 159 160 165 public TableDetails[] getTableDetails() throws DException { 166 return new TableDetails[] {tableDetails}; 167 } 168 169 177 public _Iterator getBaseIterator(ColumnDetails column) throws DException { 178 if (column.getTableDetails() != null) { 179 return this; 180 } 181 return iterator.getBaseIterator(column); 182 } 183 184 191 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 192 if (keyColumnInfo == null) { 193 keyColumnInfo = iterator.getKeyColumnInformations(); 194 keyColumnInfo = getClone(keyColumnInfo); 195 } 196 return keyColumnInfo; 197 } 198 199 205 private _KeyColumnInformation[] getClone(_KeyColumnInformation[] keys) throws DException { 206 int length = keys.length; 207 _KeyColumnInformation[] result = new _KeyColumnInformation[length]; 208 for (int i = 0; i < length; i++) { 209 ColumnDetails column = keys[i].getColumnDetails(); 210 ColumnDetails cd = null; 211 try { 212 cd = (ColumnDetails) column.clone(); 213 cd.setTableDetails(tableDetails); 214 addInMapping(column, cd); 215 } catch (CloneNotSupportedException ex) { 216 throw new DException("DSE0", new Object [] {"Clone Not Supported"}); 217 } 218 result[i] = new TableKeyColumnInformation(cd, keys[i].getOrderSpecification()); 219 } 220 return result; 221 } 222 223 230 private void addInMapping(ColumnDetails cd, ColumnDetails clonedCD) throws DException { 231 if (mappingOfColAndCD == null) { 232 mappingOfColAndCD = new Object [1][2]; 233 mappingOfColAndCD[0][0] = clonedCD; 234 mappingOfColAndCD[0][1] = cd; 235 return; 236 } 237 int length = mappingOfColAndCD.length; 238 Object [][] newMapping = new Object [length + 1][2]; 239 Object found = null; 240 for (int i = 0; i < length; i++) { 241 ColumnDetails virtualColumn = (ColumnDetails) mappingOfColAndCD[i][0]; 242 if (cd.getTable() == virtualColumn.getTable() && cd.getAppropriateColumn().equalsIgnoreCase(virtualColumn.getAppropriateColumn())) { 243 found = mappingOfColAndCD[i][1]; 244 } 245 newMapping[i][0] = mappingOfColAndCD[i][0]; 246 newMapping[i][1] = mappingOfColAndCD[i][1]; 247 } 248 newMapping[length][0] = clonedCD; 249 newMapping[length][1] = found == null ? cd : found; 250 mappingOfColAndCD = newMapping; 251 } 252 253 259 public Object [][] getFunctionalColumnMapping() throws DException { 260 Object [][] mapping = iterator.getFunctionalColumnMapping(); 261 Object [] funcIterator = ( (_FunctionalMapping) iterator).getFunctionalColumnIteratorMapping(); 262 if (mapping == null) { 263 return funcIterator == null ? null : getMappingOfFunctionalColumn(funcIterator); 264 } 265 int length = mapping.length; 266 if (funcIterator != null) { 267 Object [][] newMapping = new Object [length + 1][3]; 268 newMapping[0] = getViewEntry(funcIterator); 269 System.arraycopy(mapping, 0, newMapping, 1, length); 270 return newMapping; 271 } 272 return mapping; 273 } 274 275 283 private Object [][] getMappingOfFunctionalColumn(Object [] funcIterator) throws DException { 284 Object [][] newMaping = new Object [1][3]; 285 newMaping[0] = getViewEntry(funcIterator); 286 return newMaping; 287 } 288 289 297 private Object [] getViewEntry(Object [] funcIterator) throws DException { 298 Object [] newEntry = new Object [3]; 299 newEntry[0] = tableDetails; newEntry[1] = funcIterator[0]; newEntry[2] = funcIterator[1]; return newEntry; 303 } 304 305 311 public _Order getDefaultOrder() throws com.daffodilwoods.database.resource.DException { 312 if (defaultOrder == null) { 313 defaultOrder = iterator.getDefaultOrder(); 314 defaultOrder= getClonedOrder(defaultOrder); 315 } 316 return defaultOrder; 317 } 318 319 327 private void changeAndAddInMapping(ColumnDetails[] cd) throws DException { 328 329 330 ArrayList list = new ArrayList(); 331 list = mappingOfColAndCD == null ? new ArrayList() : 332 new ArrayList(Arrays.asList(mappingOfColAndCD)); 333 try { 334 for (int i = 0; i < cd.length; i++) { 335 if (cd[i].getAppropriateColumn().equalsIgnoreCase(SystemFields.systemFields[SystemFields.rowId])) { 336 ColumnDetails tempCD = (ColumnDetails) cd[i].clone(); 337 list.add(new Object [] {cd[i], tempCD}); 338 } 339 } 340 } catch (CloneNotSupportedException ex) { 341 throw new DException("", new Object [] {"Clone Not Supported"}); 342 } 343 mappingOfColAndCD = (Object [][]) list.toArray(new Object [0][]); 344 } 345 346 351 public _ExecutionPlan getExecutionPlan() throws com.daffodilwoods.database.resource.DException { 352 return new ExecutionPlan("ViewIterator",new _ExecutionPlan[]{iterator.getExecutionPlan()},null,null,null); 353 } 354 355 360 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 361 return iterator.getExecutionPlanForBrowser(); 362 } 363 364 372 373 public boolean first() throws DException { 374 return iterator.first(); 375 } 376 377 public boolean last() throws DException { 378 return iterator.last(); 379 } 380 381 public boolean next() throws DException { 382 return iterator.next(); 383 } 384 385 public boolean previous() throws DException { 386 return iterator.previous(); 387 } 388 389 public Object getKey() throws DException { 390 return iterator.getKey(); 391 } 392 public byte[] getByteKey() throws DException { 393 return iterator.getByteKey(); 394 } 395 396 public void move(Object key) throws DException { 397 iterator.move(key); 398 } 399 public void moveByteKey(byte[] key) throws DException { 400 iterator.moveByteKey(key); 401 } 402 403 public Object getColumnValues() throws DException { 404 return iterator.getColumnValues(); 405 } 406 407 public Object getColumnValues(int[] columns) throws DException { 408 return iterator.getColumnValues(columns); 409 } 410 411 public _Record getRecord() throws DException { 412 return iterator.getRecord(); 413 } 414 415 public boolean seek(Object indexKey) throws DException { 416 return iterator.seek(indexKey); 417 } 418 419 public boolean seekFromTop(_IndexPredicate[] condition) throws DException { 420 return iterator.seekFromTop(condition); 421 } 422 423 public boolean seekFromBottom(_IndexPredicate[] condition) throws DException { 424 return iterator.seekFromBottom(condition); 425 } 426 427 428 429 private _Order getClonedOrder(_Order defaultOrder) throws DException { 430 ColumnDetails[] cds = defaultOrder.getColumnDetails(); 431 ColumnDetails[] clonedCds = new ColumnDetails[cds.length]; 432 for (int i = 0; i < cds.length; i++) { 433 try { 434 clonedCds[i] = (ColumnDetails) cds[i].clone(); 435 clonedCds[i].setTableDetails(tableDetails); 436 addInMapping(cds[i], clonedCds[i]); 437 } 438 catch (CloneNotSupportedException ex) { 439 throw new DException("DSE0", new Object [] {"Clone Not Supported"}); 440 } 441 } 442 return new SelectOrder(clonedCds, defaultOrder.getOrderOfColumns(),defaultOrder.getComparator()); 443 } 444 public String toString() { 445 String str = "VIEWITERATOR"; 446 return str += "[" + iterator + "]"; 447 } 448 } 449 | Popular Tags |