1 package com.daffodilwoods.daffodildb.server.serversystem.chainedcolumn; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record; 7 import com.daffodilwoods.daffodildb.server.serversystem.*; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 12 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 13 import com.daffodilwoods.daffodildb.utils.field.*; 14 import com.daffodilwoods.database.general.*; 15 import com.daffodilwoods.database.resource.*; 16 import com.daffodilwoods.database.sqlinitiator.*; 17 public class TemporaryForeignKeyIterator extends BaseExceptionSingleIterator 18 implements _Iterator { 19 20 ArrayList parentsMapping; 21 HashMap iteratorMapping; 22 HashMap tablesMapping; 23 HashMap columnsMapping; 24 _Iterator startingIterator; 25 _ServerSession serverSession; 26 QualifiedIdentifier tableName; 27 ArrayList tableInfos = null; 28 int sourceTableInfoIndex = 0; 29 30 public TemporaryForeignKeyIterator(_ServerSession serverSession0, 31 HashMap tablesMapping0, 32 _Iterator temporaryIterator0, 33 QualifiedIdentifier tableName0, 34 HashMap columnsMapping0) 35 throws DException 36 { 37 tablesMapping = tablesMapping0; 38 startingIterator = temporaryIterator0; 39 serverSession = serverSession0; 40 tableName = tableName0; 41 columnsMapping = columnsMapping0; 42 43 44 setChainedTableInfos(tableName); 45 ChainedTableInfo temp = (ChainedTableInfo) tableInfos.get(0); 46 setMappings(temp); 47 } 48 49 private void setMappings(ChainedTableInfo tableInfo) throws DException { 50 parentsMapping = new ArrayList(); 51 iteratorMapping = new HashMap(); 52 parentsMapping.add(tableInfo.getChainedColumnInfo()); 53 iteratorMapping.put(tableInfo.getChainedColumnInfo(),startingIterator); 54 makeIterator(tableInfo,null); 55 while(true) { 56 ChainedTableInfo parentInfo = tableInfo.getParentChainedTableInfo(); 57 if(parentInfo == null) 58 break; 59 parentsMapping.add(parentInfo.getChainedColumnInfo()); 60 IteratorInfo ii = tableInfo.getReferencedToReferencingTableIteratorInfo(); 61 62 _SingleTableExecuter singleTableExecuter = new ConditionSingleTableExecuter( null , ii.tableDetails, serverSession , ii.bve , null ); 63 64 _Iterator iterator = serverSession.getIterator( 65 parentInfo.getTableName(), singleTableExecuter); 66 iteratorMapping.put(parentInfo.getChainedColumnInfo(), iterator); 67 68 makeIterator(parentInfo,tableInfo); 69 tableInfo = parentInfo; 70 } 71 } 72 73 private void makeIterator(ChainedTableInfo parentInfo, 74 ChainedTableInfo tempInfo) 75 throws DException { 76 ArrayList childInfos = parentInfo.getChildsChainedTableInfo(); 77 if(childInfos == null) 78 return; 79 for (int i = 0, size = childInfos.size() ; i < size; i++) { 80 ChainedTableInfo childInfo = (ChainedTableInfo) childInfos.get(i); 81 QualifiedIdentifier childTableName = childInfo.getTableName(); 82 if(tempInfo != null && childTableName.equals(tempInfo.getTableName())) 83 continue; 84 IteratorInfo ii = childInfo.getReferencingToReferencedTableIteratorInfo(); 85 86 _SingleTableExecuter conditionExecuter = new ConditionSingleTableExecuter( null , ii.tableDetails, serverSession , ii.bve , null ); 87 88 _Iterator childIterator = serverSession.getIterator(childInfo.getTableName(), conditionExecuter); 89 iteratorMapping.put(childInfo.getChainedColumnInfo(),childIterator); 90 makeIterator(childInfo,null); 91 } 92 } 93 135 143 144 private void alignParents(int parentIndex, _Iterator iterator) 145 throws DException{ 146 147 alignChilds((ChainedColumnInfo)parentsMapping.get(parentIndex),parentIndex,iterator); 148 _Record record = iterator.getRecord(); 149 int parentSize = parentsMapping.size(); 150 for(int size=parentSize-1;parentIndex<size;){ ChainedColumnInfo cci = (ChainedColumnInfo) parentsMapping.get(parentIndex++); ChainedTableInfo tableInfo = (ChainedTableInfo) tablesMapping.get(cci); 153 IteratorInfo ii = tableInfo.getReferencedToReferencingTableIteratorInfo(); 154 _Iterator iter = (_Iterator) iteratorMapping.get(parentsMapping.get(parentIndex)); 155 iter.setConditionVariableValue(ii.ref,new Object []{record.getObject(ii.columnIndex)},0); 156 boolean isFirst = iter.first(); 157 if(!isFirst) 158 break; 159 record = iter.getRecord(); 160 alignChilds((ChainedColumnInfo)parentsMapping.get(parentIndex) ,parentIndex,iter); 161 } 162 } 163 164 176 177 private void alignChilds(ChainedColumnInfo cci , int parentIndex , _Iterator iterator) 178 throws DException { 179 ChainedTableInfo cti = (ChainedTableInfo)tablesMapping.get(cci); 180 ArrayList list = cti.getChildsChainedTableInfo(); 181 if(list == null) { 182 ; return; 184 } 185 for (int i = 0, size = list.size(); i < size ; i++) { 186 ; ChainedTableInfo childTableInfo = (ChainedTableInfo) list.get(i); 188 ChainedColumnInfo childColumnInfo = childTableInfo.getChainedColumnInfo(); 189 if(parentIndex > 0 && 190 childColumnInfo.equals((ChainedColumnInfo) parentsMapping.get(parentIndex -1))) { 191 continue; 192 } 193 IteratorInfo ii = childTableInfo.getReferencingToReferencedTableIteratorInfo(); 194 _Iterator iter = (_Iterator) iteratorMapping.get(childColumnInfo); 195 iter.setConditionVariableValue(ii.ref,new Object [] { iterator.getRecord().getObject(ii.columnIndex)} ,0); 196 boolean isFirst = iter.first(); 197 if(!isFirst) 198 continue; 199 alignChilds(childColumnInfo, -1 ,iter); 200 } 201 } 202 203 public _OrderCount getOrderCounts() throws com.daffodilwoods.database.resource.DException { 204 205 throw new java.lang.UnsupportedOperationException ("Method getOrderCounts() not yet implemented."); 206 } 207 public TableDetails[] getTableDetails() throws com.daffodilwoods.database.resource.DException { 208 TableDetails tableD = new TableDetails(); 209 tableD.setTableName(new String []{ tableName.catalog,tableName.schema,tableName.name }); 210 return new TableDetails[]{tableD}; 211 } 212 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 213 return this; 214 } 215 public void setConditionVariableValue(_Reference[] parm1, Object [] parm2, int parm3) throws com.daffodilwoods.database.resource.DException { 216 } 217 public Object getColumnValues() throws com.daffodilwoods.database.resource.DException { 218 return startingIterator.getColumnValues(); 219 } 220 public _KeyColumnInformation[] getKeyColumnInformations() throws com.daffodilwoods.database.resource.DException { 221 222 throw new java.lang.UnsupportedOperationException ("Method getKeyColumnInformations() not yet implemented."); 223 } 224 public Object [] getUniqueColumnReference() throws com.daffodilwoods.database.resource.DException { 225 226 throw new java.lang.UnsupportedOperationException ("Method getUniqueColumnReference() not yet implemented."); 227 } 228 public boolean seek(Object parm1) throws com.daffodilwoods.database.resource.DException { 229 230 throw new java.lang.UnsupportedOperationException ("Method seek() not yet implemented."); 231 } 232 public _Order getDefaultOrder() throws com.daffodilwoods.database.resource.DException { 233 234 throw new java.lang.UnsupportedOperationException ("Method getDefaultOrder() not yet implemented."); 235 } 236 public Object getColumnValues(_Reference[] parm1) throws com.daffodilwoods.database.resource.DException { 237 Object [] values = new Object [parm1.length]; 238 for (int i = 0; i < parm1.length; i++) { 239 String [] aliasName = ((ColumnDetails)parm1[i]).getTableAliasArray(); 240 if ( aliasName == null ) 241 values[i] = startingIterator.getColumnValues(parm1); 242 _Iterator iterator = (_Iterator) iteratorMapping.get(columnsMapping.get(aliasName)); 243 if(iterator != null) 244 values[i] = iterator.getColumnValues(parm1); 245 } 246 return values; 247 } 248 public Object getColumnValues(_Reference parm1) throws com.daffodilwoods.database.resource.DException { 249 String [] aliasName = ((ColumnDetails)parm1).getTableAliasArray(); 250 if ( aliasName == null ) 251 return startingIterator.getColumnValues(parm1); 252 _Iterator iterator = (_Iterator) iteratorMapping.get(columnsMapping.get(aliasName)); 253 if(iterator != null) 254 return iterator.getColumnValues(parm1); 255 return null; 256 } 257 public void setIterator(_Iterator parm1) throws com.daffodilwoods.database.resource.DException { 258 259 throw new java.lang.UnsupportedOperationException ("Method setIterator() not yet implemented."); 260 } 261 public void addReferences(_Reference[] parm1) throws com.daffodilwoods.database.resource.DException { 262 263 throw new java.lang.UnsupportedOperationException ("Method addReferences() not yet implemented."); 264 } 265 public Object [][] getReferenceAndValuePair() throws com.daffodilwoods.database.resource.DException { 266 267 throw new java.lang.UnsupportedOperationException ("Method getReferenceAndValuePair() not yet implemented."); 268 } 269 public boolean seekFromTop(_IndexPredicate[] parm1) throws com.daffodilwoods.database.resource.DException { 270 271 throw new java.lang.UnsupportedOperationException ("Method seekFromTop() not yet implemented."); 272 } 273 public boolean seekFromTopRelative(Object parm1) throws com.daffodilwoods.database.resource.DException { 274 275 throw new java.lang.UnsupportedOperationException ("Method seekFromTopRelative() not yet implemented."); 276 } 277 public boolean seekFromBottom(_IndexPredicate[] parm1) throws com.daffodilwoods.database.resource.DException { 278 279 throw new java.lang.UnsupportedOperationException ("Method seekFromBottom() not yet implemented."); 280 } 281 public boolean seekFromBottomRelative(Object parm1) throws com.daffodilwoods.database.resource.DException { 282 283 throw new java.lang.UnsupportedOperationException ("Method seekFromBottomRelative() not yet implemented."); 284 } 285 286 public boolean first() throws com.daffodilwoods.database.resource.DException { 287 if(sourceTableInfoIndex != 0) 288 setMappings((ChainedTableInfo)tableInfos.get(0)); 289 boolean found = startingIterator.first(); 290 if(found) { 291 alignParents(0,startingIterator); 292 } 293 return found; 294 } 295 296 public boolean last() throws com.daffodilwoods.database.resource.DException { 297 if(sourceTableInfoIndex != 0) 298 setMappings((ChainedTableInfo)tableInfos.get(tableInfos.size()-1)); 299 boolean found = startingIterator.last(); 300 if(found) { 301 alignParents(0,startingIterator); 302 } 303 return found; 304 } 305 306 311 312 public boolean next() throws com.daffodilwoods.database.resource.DException { 313 int currentIndex = parentsMapping.size()-1; 314 ChainedColumnInfo cci = (ChainedColumnInfo)parentsMapping.get(currentIndex); 315 _Iterator it = (_Iterator) iteratorMapping.get(cci); while(!it.next()){ 317 if( --currentIndex == -1 ) { 318 return reInitialize(++sourceTableInfoIndex); 319 } 320 cci = (ChainedColumnInfo) parentsMapping.get(currentIndex); 321 it = (_Iterator) iteratorMapping.get(cci); 322 } 323 alignParents(currentIndex, it); 324 return true; 325 } 326 327 public boolean previous() throws com.daffodilwoods.database.resource.DException { 328 int currentIndex = parentsMapping.size()-1; 329 ChainedColumnInfo cci = (ChainedColumnInfo)parentsMapping.get(currentIndex); 330 _Iterator it = (_Iterator) iteratorMapping.get(cci); while(!(it.previous())){ 332 if( --currentIndex == -1 ) { 333 return reInitialize(--sourceTableInfoIndex); 334 } 335 cci = (ChainedColumnInfo) parentsMapping.get(currentIndex); 336 it = (_Iterator) iteratorMapping.get(cci); 337 } 338 alignParents(currentIndex, it); 339 return true; 340 } 341 public Object getKey() throws com.daffodilwoods.database.resource.DException { 342 343 throw new java.lang.UnsupportedOperationException ("Method getKey() not yet implemented."); 344 } 345 public void move(Object parm1) throws com.daffodilwoods.database.resource.DException { 346 347 throw new java.lang.UnsupportedOperationException ("Method move() not yet implemented."); 348 } 349 public Object getColumnValues(int[] parm1) throws com.daffodilwoods.database.resource.DException { 350 return startingIterator.getColumnValues(parm1); 351 } 352 public _Record getRecord() throws com.daffodilwoods.database.resource.DException { 353 354 throw new java.lang.UnsupportedOperationException ("Method getRecord() not yet implemented."); 355 } 356 357 private void setChainedTableInfos(QualifiedIdentifier tableName) throws DException { 358 Iterator iterator = tablesMapping.values().iterator(); 359 tableInfos = new ArrayList (); 360 boolean tableFound = false; 361 while(iterator.hasNext()) { 362 ChainedTableInfo cti = (ChainedTableInfo) iterator.next(); 363 if(cti.getTableName().equals(tableName)) { 364 ; tableInfos.add(cti); 366 tableFound = true; 367 } 368 } 369 if(!tableFound) 370 throw new DException("DSE0",new Object []{"TableInfo Not Found"}); 371 } 372 373 private boolean reInitialize(int index) throws DException { 374 int size = tableInfos.size(); 375 if( size > 1 && index < size ){ 376 ChainedTableInfo cti = (ChainedTableInfo) tableInfos.get(index); 377 setMappings(cti); 378 boolean first = startingIterator.first(); 379 if( first ) 380 alignParents(0,startingIterator); 381 return true; 382 } 383 else 384 return false; 385 } 386 387 388 public Object getColumnValues(int column) throws DException { 389 390 throw new java.lang.UnsupportedOperationException ("Method getColumnValues() not yet implemented."); 391 } 392 public _ExecutionPlan getExecutionPlan() throws DException{ 393 _ExecutionPlan cplan = startingIterator.getExecutionPlan(); 394 _ExecutionPlan cplans[] = cplan == null ? null : new _ExecutionPlan[]{cplan}; 395 ExecutionPlan plan = new ExecutionPlan("TemporaryForeignKeyIterator",cplans,null,null,null); 396 return plan; 397 } 398 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 399 return startingIterator.getExecutionPlanForBrowser(); 400 } 401 public _Iterator getBaseIteratorHasRecord(ColumnDetails hasRecordColumn) throws DException { 402 return startingIterator.getBaseIteratorHasRecord(hasRecordColumn); 403 } 404 public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException { 405 throw new java.lang.UnsupportedOperationException ("Method not yet implemented."); 406 } 407 public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException { 408 throw new java.lang.UnsupportedOperationException ("Method fields() not yet implemented."); 409 } 410 public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException { 411 throw new java.lang.UnsupportedOperationException ("Method fields() not yet implemented."); 412 } 413 public void releaseResource() throws DException { 414 415 } 416 public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{ 417 throw new java.lang.UnsupportedOperationException ( 418 "Method setSpecificUnderlyingReferences() not yet implemented."); 419 } 420 421 } 422 | Popular Tags |