1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.sql.execute.CursorResultSet; 25 import org.apache.derby.iapi.error.StandardException; 26 import org.apache.derby.iapi.reference.SQLState; 27 28 import org.apache.derby.iapi.sql.execute.CursorActivation; 29 import org.apache.derby.iapi.sql.execute.ExecRow; 30 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 31 32 import org.apache.derby.iapi.sql.Activation; 33 import org.apache.derby.iapi.sql.ResultSet; 34 import org.apache.derby.iapi.sql.PreparedStatement; 35 36 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 37 38 import org.apache.derby.iapi.types.RowLocation; 39 40 import org.apache.derby.iapi.services.sanity.SanityManager; 41 import org.apache.derby.iapi.sql.depend.DependencyManager; 42 43 52 public class CurrentOfResultSet extends NoPutResultSetImpl 53 implements CursorResultSet { 54 55 private boolean next; 56 private RowLocation rowLocation; 57 58 private CursorResultSet cursor; 59 private CursorResultSet target; 60 private ExecRow sparseRow; 61 62 private final String cursorName; 65 private final String psName; 66 67 public CurrentOfResultSet(String cursorName, Activation activation, 71 int resultSetNumber, String psName) 72 { 73 super(activation, resultSetNumber, 0.0d, 0.0d); 74 if (SanityManager.DEBUG) 75 SanityManager.ASSERT( cursorName!=null, "current of scan must get cursor name"); 76 this.cursorName = cursorName; 77 this.psName = psName; 78 } 79 80 90 public void openCore() throws StandardException { 91 if (SanityManager.DEBUG) 92 SanityManager.ASSERT( ! isOpen, "CurrentOfResultSet already open"); 93 94 getCursor(); 96 97 next = false; 98 isOpen = true; 99 } 100 101 106 public ExecRow getNextRowCore() throws StandardException { 107 108 if ( isOpen ) { 109 if ( ! next ) { 110 next = true; 111 if (SanityManager.DEBUG) 112 SanityManager.ASSERT(! cursor.isClosed(), "cursor closed"); 113 114 ExecRow cursorRow = cursor.getCurrentRow(); 115 116 if (cursorRow == null) { 118 throw StandardException.newException(SQLState.NO_CURRENT_ROW); 119 } 120 rowLocation = cursor.getRowLocation(); 122 123 currentRow = target.getCurrentRow(); 126 127 if (rowLocation == null || 133 (cursorRow != null && currentRow == null)) { 134 activation.addWarning(StandardException. 135 newWarning(SQLState.CURSOR_OPERATION_CONFLICT)); 136 return null; 137 } 138 139 148 if (target instanceof TableScanResultSet) 149 { 150 TableScanResultSet scan = (TableScanResultSet) target; 151 if (scan.indexCols != null && currentRow != null) 152 currentRow = getSparseRow(currentRow, scan.indexCols); 153 } 154 157 TableScanResultSet scan = (TableScanResultSet) activation.getForUpdateIndexScan(); 158 if (scan != null) 159 { 160 if (target instanceof IndexRowToBaseRowResultSet) 161 scan.compareToLastKey = ((IndexRowToBaseRowResultSet) target).currentRowPrescanned; 162 else if (target instanceof TableScanResultSet) 163 scan.compareToLastKey = ((TableScanResultSet) target).currentRowPrescanned; 164 } 165 166 } 173 else { 174 currentRow = null; 175 rowLocation = null; 176 } 177 } 178 else { 179 currentRow = null; 180 rowLocation = null; 181 } 182 setCurrentRow(currentRow); 183 return currentRow; 184 } 185 186 194 private ExecRow getSparseRow(ExecRow row, int[] indexCols) throws StandardException 195 { 196 int colPos; 197 if (sparseRow == null) 198 { 199 int numCols = 1; 200 for (int i = 0; i < indexCols.length; i++) 201 { 202 colPos = (indexCols[i] > 0) ? indexCols[i] : -indexCols[i]; 203 if (colPos > numCols) 204 numCols = colPos; 205 } 206 sparseRow = new ValueRow(numCols); 207 } 208 for (int i = 1; i <= indexCols.length; i++) 209 { 210 colPos = (indexCols[i-1] > 0) ? indexCols[i-1] : -indexCols[i-1]; 211 sparseRow.setColumn(colPos, row.getColumn(i)); 212 } 213 214 return sparseRow; 215 } 216 217 223 public void close() throws StandardException 224 { 225 if ( isOpen ) { 226 clearCurrentRow(); 231 next = false; 232 233 super.close(); 234 } 235 else 236 if (SanityManager.DEBUG) 237 SanityManager.DEBUG("CloseRepeatInfo","Close of CurrentOfResultSet repeated"); 238 } 239 public void finish() throws StandardException 240 { 241 finishAndRTS(); 242 } 243 244 252 public long getTimeSpent(int type) 253 { 254 255 return 0; 256 } 257 258 268 public RowLocation getRowLocation() { 269 return rowLocation; 270 } 271 272 277 public ExecRow getCurrentRow() { 278 return currentRow; 279 } 280 281 295 private void getCursor() throws StandardException { 296 297 if (cursor != null) { 299 if (cursor.isClosed()) 300 { 301 cursor = null; 302 target = null; 303 } 304 } 305 306 if (cursor == null) { 307 308 LanguageConnectionContext lcc = getLanguageConnectionContext(); 309 310 CursorActivation cursorActivation = lcc.lookupCursorActivation(cursorName); 311 312 if (cursorActivation != null) 313 { 314 315 cursor = cursorActivation.getCursorResultSet(); 316 target = cursorActivation.getTargetResultSet(); 317 321 activation.setForUpdateIndexScan(cursorActivation.getForUpdateIndexScan()); 322 if (cursorActivation.getHeapConglomerateController() != null) 323 cursorActivation.getHeapConglomerateController().close(); 324 cursorActivation.setHeapConglomerateController(activation.getHeapConglomerateController()); 325 } 326 } 327 328 if (cursor == null || cursor.isClosed()) { 329 throw StandardException.newException(SQLState.LANG_CURSOR_CLOSED, cursorName); 330 } 331 } 332 333 336 public void updateRow (ExecRow row) throws StandardException { 337 ((NoPutResultSet)cursor).updateRow(row); 338 } 339 340 343 public void markRowAsDeleted() throws StandardException { 344 ((NoPutResultSet)cursor).markRowAsDeleted(); 345 } 346 347 } 348 | Popular Tags |