1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 29 import org.apache.derby.iapi.services.stream.InfoStreams; 30 31 import org.apache.derby.iapi.sql.Activation; 32 import org.apache.derby.iapi.sql.ResultDescription; 33 import org.apache.derby.iapi.sql.ResultSet; 34 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 35 import org.apache.derby.iapi.types.TypeId; 36 37 import org.apache.derby.iapi.types.DataValueDescriptor; 38 import org.apache.derby.iapi.types.DataTypeDescriptor; 39 import org.apache.derby.iapi.types.RowLocation; 40 41 42 import org.apache.derby.iapi.sql.execute.CursorResultSet; 43 import org.apache.derby.iapi.sql.execute.ExecRow; 44 import org.apache.derby.iapi.sql.execute.ExecutionContext; 45 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 46 47 import org.apache.derby.iapi.services.loader.GeneratedMethod; 48 49 import org.apache.derby.iapi.error.StandardException; 50 import org.apache.derby.iapi.reference.SQLState; 51 52 56 57 class NormalizeResultSet extends NoPutResultSetImpl 58 implements CursorResultSet 59 { 60 63 64 public NoPutResultSet source; 65 private ExecRow normalizedRow; 66 private int numCols; 67 private int startCol; 68 69 73 private ResultDescription resultDescription; 74 75 76 private DataTypeDescriptor[] desiredTypes; 77 78 89 90 public NormalizeResultSet(NoPutResultSet source, 91 Activation activation, int resultSetNumber, 92 int erdNumber, 93 double optimizerEstimatedRowCount, 94 double optimizerEstimatedCost, 95 boolean forUpdate) throws StandardException 96 { 97 super(activation, resultSetNumber, optimizerEstimatedRowCount, 98 optimizerEstimatedCost); 99 this.source = source; 100 101 if (SanityManager.DEBUG) 102 { 103 if (! (activation.getPreparedStatement().getSavedObject(erdNumber) 104 instanceof ResultDescription)) 105 { 106 SanityManager.THROWASSERT( 107 "activation.getPreparedStatement().getSavedObject(erdNumber) " + 108 "expected to be instanceof ResultDescription"); 109 } 110 111 SanityManager.ASSERT(source != null, 114 "NRS(), source expected to be non-null"); 115 } 116 117 this.resultDescription = 118 (ResultDescription) activation.getPreparedStatement().getSavedObject(erdNumber); 119 120 numCols = resultDescription.getColumnCount(); 121 122 131 startCol = (forUpdate) ? ((numCols - 1)/ 2) + 1 : 1; 132 normalizedRow = activation.getExecutionFactory().getValueRow(numCols); 133 constructorTime += getElapsedMillis(beginTime); 134 } 135 136 137 141 148 public void openCore() throws StandardException 149 { 150 beginTime = getCurrentTimeMillis(); 151 if (SanityManager.DEBUG) 152 SanityManager.ASSERT( ! isOpen, "NormalizeResultSet already open"); 153 154 if (SanityManager.DEBUG) 157 { 158 SanityManager.ASSERT(source != null, 159 "NRS().openCore(), source expected to be non-null"); 160 } 161 162 source.openCore(); 163 isOpen = true; 164 numOpens++; 165 166 openTime += getElapsedMillis(beginTime); 167 } 168 169 176 public void reopenCore() throws StandardException 177 { 178 beginTime = getCurrentTimeMillis(); 179 if (SanityManager.DEBUG) 180 SanityManager.ASSERT(isOpen, "NormalizeResultSet already open"); 181 182 source.reopenCore(); 183 numOpens++; 184 185 openTime += getElapsedMillis(beginTime); 186 } 187 188 192 public ExecRow getNextRowCore() throws StandardException 193 { 194 ExecRow sourceRow = null; 195 ExecRow result = null; 196 197 beginTime = getCurrentTimeMillis(); 198 if (!isOpen) 199 throw StandardException.newException(SQLState.LANG_RESULT_SET_NOT_OPEN, "next"); 200 201 sourceRow = source.getNextRowCore(); 202 if (sourceRow != null) 203 { 204 result = normalizeRow(sourceRow); 205 rowsSeen++; 206 } 207 208 currentRow = result; 209 setCurrentRow(result); 210 211 nextTime += getElapsedMillis(beginTime); 212 return result; 213 } 214 215 221 public void close() throws StandardException 222 { 223 beginTime = getCurrentTimeMillis(); 224 if ( isOpen ) 225 { 226 currentRow = null; 227 source.close(); 228 229 super.close(); 230 } 231 else 232 if (SanityManager.DEBUG) 233 SanityManager.DEBUG("CloseRepeatInfo","Close of NormalizeResultSet repeated"); 234 235 closeTime += getElapsedMillis(beginTime); 236 } 237 238 246 public long getTimeSpent(int type) 247 { 248 long totTime = constructorTime + openTime + nextTime + closeTime; 249 250 if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY) 251 { 252 return totTime - source.getTimeSpent(ENTIRE_RESULTSET_TREE); 253 } 254 else 255 { 256 return totTime; 257 } 258 } 259 260 264 275 public RowLocation getRowLocation() throws StandardException 276 { 277 if (SanityManager.DEBUG) 278 SanityManager.ASSERT(source instanceof CursorResultSet, "source is not a cursorresultset"); 279 return ( (CursorResultSet)source ).getRowLocation(); 280 } 281 282 289 292 public ExecRow getCurrentRow() 293 { 294 return currentRow; 295 } 296 297 318 private ExecRow normalizeRow(ExecRow sourceRow) throws StandardException 319 { 320 int whichCol; 321 322 if (desiredTypes == null) 323 { 324 desiredTypes = new DataTypeDescriptor[numCols]; 325 for (whichCol = 1; whichCol <= numCols; whichCol++) 326 { 327 DataTypeDescriptor dtd = resultDescription.getColumnDescriptor(whichCol).getType(); 328 329 desiredTypes[whichCol - 1] = dtd; 330 } 331 332 } 333 334 for (whichCol = 1; whichCol <= numCols; whichCol++) 335 { 336 DataValueDescriptor sourceCol = sourceRow.getColumn(whichCol); 337 if (sourceCol != null) 338 { 339 DataValueDescriptor normalizedCol; 340 if (whichCol < startCol) 342 normalizedCol = sourceCol; 343 else 344 try { 345 normalizedCol = 346 desiredTypes[whichCol - 1].normalize(sourceCol, 347 normalizedRow.getColumn(whichCol)); 348 } catch (StandardException se) { 349 if (se.getMessageId().startsWith(SQLState.LANG_NULL_INTO_NON_NULL)) 351 { 352 ResultColumnDescriptor columnDescriptor = 353 resultDescription.getColumnDescriptor(whichCol); 354 throw 355 StandardException.newException(SQLState.LANG_NULL_INTO_NON_NULL, 356 columnDescriptor.getName()); 357 } 358 throw se; 360 } 361 362 normalizedRow.setColumn(whichCol, normalizedCol); 363 } 364 } 365 366 return normalizedRow; 367 } 368 369 372 public void updateRow (ExecRow row) throws StandardException { 373 source.updateRow(row); 374 } 375 376 379 public void markRowAsDeleted() throws StandardException { 380 source.markRowAsDeleted(); 381 } 382 383 } 384 | Popular Tags |