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.conn.StatementContext; 32 33 import org.apache.derby.iapi.sql.execute.ExecRow; 34 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 35 36 import org.apache.derby.iapi.types.DataValueDescriptor; 37 import org.apache.derby.iapi.sql.Activation; 38 import org.apache.derby.iapi.sql.ResultSet; 39 40 import org.apache.derby.iapi.services.loader.GeneratedMethod; 41 42 import org.apache.derby.iapi.reference.SQLState; 43 import org.apache.derby.iapi.error.StandardException; 44 45 53 public class OnceResultSet extends NoPutResultSetImpl 54 { 55 56 public static final int DO_CARDINALITY_CHECK = 1; 57 public static final int NO_CARDINALITY_CHECK = 2; 58 public static final int UNIQUE_CARDINALITY_CHECK = 3; 59 60 63 private ExecRow rowWithNulls; 64 65 66 private StatementContext statementContext; 67 68 public NoPutResultSet source; 71 private GeneratedMethod emptyRowFun; 72 private int cardinalityCheck; 73 public int subqueryNumber; 74 public int pointOfAttachment; 75 76 public OnceResultSet(NoPutResultSet s, Activation a, GeneratedMethod emptyRowFun, 80 int cardinalityCheck, int resultSetNumber, 81 int subqueryNumber, int pointOfAttachment, 82 double optimizerEstimatedRowCount, 83 double optimizerEstimatedCost) 84 { 85 super(a, resultSetNumber, optimizerEstimatedRowCount, optimizerEstimatedCost); 86 source = s; 87 this.emptyRowFun = emptyRowFun; 88 this.cardinalityCheck = cardinalityCheck; 89 this.subqueryNumber = subqueryNumber; 90 this.pointOfAttachment = pointOfAttachment; 91 constructorTime += getElapsedMillis(beginTime); 92 } 93 94 98 105 public void openCore() throws StandardException 106 { 107 112 if (isOpen) 113 { 114 reopenCore(); 115 return; 116 } 117 118 beginTime = getCurrentTimeMillis(); 119 120 source.openCore(); 121 122 125 if (statementContext == null) 126 { 127 statementContext = getLanguageConnectionContext().getStatementContext(); 128 } 129 statementContext.setSubqueryResultSet(subqueryNumber, this, 130 activation.getNumSubqueries()); 131 132 numOpens++; 133 isOpen = true; 134 openTime += getElapsedMillis(beginTime); 135 } 136 137 144 public void reopenCore() throws StandardException 145 { 146 beginTime = getCurrentTimeMillis(); 147 if (SanityManager.DEBUG) 148 SanityManager.ASSERT(isOpen, "OnceResultSet already open"); 149 150 source.reopenCore(); 151 numOpens++; 152 153 openTime += getElapsedMillis(beginTime); 154 } 155 156 163 public ExecRow getNextRowCore() throws StandardException 164 { 165 ExecRow candidateRow = null; 166 ExecRow secondRow = null; 167 ExecRow result = null; 168 169 beginTime = getCurrentTimeMillis(); 170 if (SanityManager.DEBUG) 173 SanityManager.ASSERT( isOpen, "OpenResultSet not open"); 174 175 if ( isOpen ) 176 { 177 candidateRow = source.getNextRowCore(); 178 179 if (candidateRow != null) 180 { 181 switch (cardinalityCheck) 182 { 183 case DO_CARDINALITY_CHECK: 184 case NO_CARDINALITY_CHECK: 185 candidateRow = candidateRow.getClone(); 186 if (cardinalityCheck == DO_CARDINALITY_CHECK) 187 { 188 193 secondRow = source.getNextRowCore(); 194 if (secondRow != null) 195 { 196 close(); 197 StandardException se = StandardException.newException(SQLState.LANG_SCALAR_SUBQUERY_CARDINALITY_VIOLATION); 198 throw se; 199 } 200 } 201 result = candidateRow; 202 break; 203 204 case UNIQUE_CARDINALITY_CHECK: 205 candidateRow = candidateRow.getClone(); 206 secondRow = source.getNextRowCore(); 207 DataValueDescriptor orderable1 = candidateRow.getColumn(1); 208 while (secondRow != null) 209 { 210 DataValueDescriptor orderable2 = secondRow.getColumn(1); 211 if (! (orderable1.compare(DataValueDescriptor.ORDER_OP_EQUALS, orderable2, true, true))) 212 { 213 close(); 214 StandardException se = StandardException.newException(SQLState.LANG_SCALAR_SUBQUERY_CARDINALITY_VIOLATION); 215 throw se; 216 } 217 secondRow = source.getNextRowCore(); 218 } 219 result = candidateRow; 220 break; 221 222 default: 223 if (SanityManager.DEBUG) 224 { 225 SanityManager.THROWASSERT( 226 "cardinalityCheck not unexpected to be " + 227 cardinalityCheck); 228 } 229 break; 230 } 231 } 232 else if (rowWithNulls == null) 233 { 234 rowWithNulls = (ExecRow) emptyRowFun.invoke(activation); 235 result = rowWithNulls; 236 } 237 else 238 { 239 result = rowWithNulls; 240 } 241 } 242 243 currentRow = result; 244 setCurrentRow(result); 245 rowsSeen++; 246 247 nextTime += getElapsedMillis(beginTime); 248 return result; 249 } 250 251 257 public void close() throws StandardException 258 { 259 beginTime = getCurrentTimeMillis(); 260 if ( isOpen ) 261 { 262 clearCurrentRow(); 267 268 source.close(); 269 270 super.close(); 271 } 272 else 273 if (SanityManager.DEBUG) 274 SanityManager.DEBUG("CloseRepeatInfo","Close of OnceResultSet repeated"); 275 276 closeTime += getElapsedMillis(beginTime); 277 } 278 279 282 public int getPointOfAttachment() 283 { 284 return pointOfAttachment; 285 } 286 287 295 public long getTimeSpent(int type) 296 { 297 long totTime = constructorTime + openTime + nextTime + closeTime; 298 299 if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY) 300 { 301 return totTime - source.getTimeSpent(ENTIRE_RESULTSET_TREE); 302 } 303 else 304 { 305 return totTime; 306 } 307 } 308 } 309 | Popular Tags |