1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
package com.sap.dbtech.jdbc;
import java.sql.*;
import com.sap.dbtech.jdbc.packet.*;
import com.sap.dbtech.jdbc.exceptions.*;
import com.sap.dbtech.util.*;
import com.sap.dbtech.vsp001.*;
/**
* The outcome of a particular fetch operation. A fetch operation
* results in one (when the fetch size is 1) or more (when the fetch
* size is >1) data rows returned from the database server. Depending on
* the kind of the fetch, the positioning in the result at the database
* server and the start and end index computation does differ.
*/
class FetchChunk
{
/**
* The fetch operation type of a <tt>FETCH FIRST</tt>.
*/
static final int TYPE_FIRST = 1;
/**
* The fetch operation type of a <tt>FETCH LAST</tt>.
*/
static final int TYPE_LAST = 2;
/**
* The fetch operation type of a <tt>FETCH ABSOLUTE</tt> with an argument >1.
*/
static final int TYPE_ABSOLUTE_UP = 3;
/**
* The fetch operation type of a <tt>FETCH ABSOLUTE</tt> with an argument <1.
*/
static final int TYPE_ABSOLUTE_DOWN = 4;
/**
* The fetch operation type of a <tt>FETCH RELATIVE</tt> with an argument >1.
*/
static final int TYPE_RELATIVE_UP = 5;
/**
* The fetch operation type of a <tt>FETCH RELATIVE</tt> with an argument <1.
*/
static final int TYPE_RELATIVE_DOWN = 6;
/**
* The data packet from the fetch operation.
*/
private ReplyPacket replyPacket;
/**
* The <i>data part</i> of <code>replyPacket</code>.
*/
private StructuredMem replyData;
/**
* The current record inside the data part (<code>replyData</code>).
*/
private StructuredMem currentRecord;
/**
* The type of the fetch operation (one of the <code>TYPE_XXX</code> constants).
*/
private int type; // type of fetch chunk
/**
* The index of the first row in this chunk.
*/
private int start_index;
/**
* The index of the last row in this chunk.
*/
private int end_index;
/**
* The current index within this chunk, starting with 0.
*/
private int currentOffset;
/**
* A flag indicating that this chunk is the first chunk of the result set.
*/
private boolean first;
/**
* A flag indicating that this chunk is the last chunk of the result set.
*/
private boolean last;
/**
* The number of bytes in a row.
*/
private int recordSize;
/**
* The number of elements in this chunk.
*/
private int chunkSize;
/**
* The number of rows in the complete result set, or -1 if this is not known.
*/
private int rowsInResultSet;
/**
* Creates a new fetch chunk.
* @param type the type of the fetch operation.
* @param absoluteStartRow the start row of this chunk. If negative, this is calculated from
* the end of the result set.
* @param replyPacket the database server reply of the fetch
* @param recordSize the size of one row.
* @param maxRows the <code>maxRows</code> property of the statement that created this result.
* @param rowsInResultSet the number of rows in this result set, or -1 if not known.
* @exception SQLException if the reply does not contain a data part.
*/
FetchChunk(int type,
int absoluteStartRow,
ReplyPacket replyPacket,
int recordSize,
int maxRows,
int rowsInResultSet)
throws SQLException
{
this.replyPacket=replyPacket;
this.type=type;
this.recordSize=recordSize;
this.rowsInResultSet=rowsInResultSet;
try {
replyPacket.firstSegment();
replyPacket.findPart(PartKind.Data_C);
} catch(PartNotFound pnf) {
throw new InternalJDBCError("Fetch operation delivered no data part.");
}
this.chunkSize=replyPacket.partArguments();
int dataPos=replyPacket.getPartDataPos();
this.replyData=replyPacket.getPointer(dataPos);
currentOffset=0;
currentRecord=replyData.getPointer(currentOffset * this.recordSize);
if(absoluteStartRow > 0) {
start_index=absoluteStartRow;
end_index=absoluteStartRow + chunkSize - 1;
} else {
if(rowsInResultSet!=-1) {
if(absoluteStartRow < 0) {
start_index = rowsInResultSet + absoluteStartRow + 1; // - 1 is last
} else {
start_index = rowsInResultSet - absoluteStartRow + chunkSize ;
}
end_index = start_index + chunkSize -1;
} else {
start_index=absoluteStartRow;
end_index=absoluteStartRow + chunkSize -1;
}
}
determineFlags(maxRows);
}
/**
* Determines whether this chunk is the first and/or last of
* a result set. This is done by checking the index boundaries,
* and also the LAST PART information of the reply packet.
* A forward chunk is also the last if it contains the record at
* the <code>maxRows</code> row, as the user decided to make
* the limit here.
* @param maxRows the <code>maxRows</code> limit of the statement
*/
private void determineFlags(int maxRows)
{
boolean wasLastPart=replyPacket.wasLastPart();
if(wasLastPart) {
switch(this.type) {
case TYPE_FIRST:
case TYPE_LAST:
case TYPE_RELATIVE_DOWN:
this.first=true;
this.last=true;
break;
case TYPE_ABSOLUTE_UP:
case TYPE_ABSOLUTE_DOWN:
case TYPE_RELATIVE_UP:
this.last=true;
}
}
if(start_index==1) {
first=true;
}
if(end_index==-1) {
last=true;
}
// one special last for maxRows set
if(maxRows!=0 && isForward() && end_index >= maxRows) {
// if we have fetched too much, we have to cut here ...
end_index = maxRows;
chunkSize = maxRows + 1 - start_index;
last=true;
}
}
/**
* Gets the reply packet.
*
* @return the <code>replyPacket</code> property.
*/
ReplyPacket getReplyPacket()
{
return this.replyPacket;
}
/**
* Gets the current record.
* @return the <code>currentRecord</code> property.
*/
StructuredMem getCurrentRecord()
{
return this.currentRecord;
}
/**
* Returns whether the given row is truly inside the chunk.
* @param row the row to check. Rows <0 count from the end of the result.
* @return <code>true</code> if the row is inside, <code>false</code> if it's not
* or the condition could not be determined due to an unknown end of result set.
*/
boolean containsRow(int row)
{
if(start_index <= row &&
end_index >= row) {
return true;
}
// some tricks depending on whether we are on last/first chunk
if(isForward() && last && row < 0) {
return row >= start_index - end_index - 1;
}
if(!isForward() && first && row > 0) {
return row <= end_index - start_index + 1;
}
// if we know the number of rows, we can compute this anyway
// by inverting the row
if(rowsInResultSet != -1 &&
((start_index<0 && row>0) || (start_index>0 && row<0))) {
int inverted_row = (row > 0)
? (row - rowsInResultSet - 1)
: (row + rowsInResultSet + 1);
return start_index <= inverted_row && end_index >= inverted_row;
}
return false;
}
/**
* Moves the position inside the chunk by a relative offset.
* @param relativepos the relative moving offset.
* @return <code>true</code> if it was moved, <code>false</code> otherwise.
*/
boolean move(int relativepos)
{
if(currentOffset + relativepos < 0 ||
currentOffset + relativepos >= chunkSize ) {
return false;
} else {
unsafeMove(relativepos);
return true;
}
}
/**
* Moves the position inside the chunk by a relative offset, but unchecked.
* @param relativepos the relative moving offset.
*/
private void unsafeMove(int relativepos)
{
this.currentOffset+=relativepos;
this.currentRecord.moveBase((relativepos) * this.recordSize);
}
/**
* Sets the current record to the supplied absolute position.
* @param row the absolute row.
* @return <code>true</code> if the row was set, <code>false</code> otherwise.
*/
boolean setRow(int row)
{
if(start_index <= row && end_index >= row) {
unsafeMove(row - start_index - currentOffset);
return true;
}
// some tricks depending on whether we are on last/first chunk
if(isForward() && last && row < 0 && row >= start_index - end_index - 1 ) {
// move backward to the row from the end index, but
// honor the row number start at 1, make this
// relative to chunk by subtracting start index
// and relative for the move by subtracting the
// current offset
unsafeMove(end_index + row + 1 - start_index - currentOffset);
return true;
}
if(!isForward() && first && row > 0 && row <= end_index - start_index + 1) {
// simple. row is > 0. start_index if positive were 1 ...
unsafeMove(row - 1 - currentOffset);
}
// if we know the number of rows, we can compute this anyway
// by inverting the row
if(rowsInResultSet != -1 &&
((start_index<0 && row>0) || (start_index>0 && row<0))) {
int inverted_row = (row > 0)
? (row - rowsInResultSet - 1)
: (row + rowsInResultSet + 1);
return setRow(inverted_row);
}
return false;
}
/**
* Called because there is a result set where the last element
* is now interesting. This is the fact in a <code>FETCH LAST</code>
* operation.
*/
void moveToUpperBound()
{
int relativepos=chunkSize - currentOffset -1;
this.currentRecord.moveBase(relativepos * this.recordSize);
this.currentOffset= chunkSize - 1;
return;
}
/**
* Returns true if the internal position inside the chunk
* is the greatest possible towards the end of this result
* set.
* @return <code>true</code> if our current position is equal
* to the end index of this chunk, <code>false</code> otherwise.
*/
boolean isAtUpperBound()
{
return this.currentOffset==this.chunkSize-1;
}
/**
* Returns true if the internal position inside the chunk
* is the smallest possible
* @return <code>true</code> if our current position is equal
* to the start index of this chunk, <code>false</code> otherwise.
*/
boolean isAtLowerBound()
{
return this.currentOffset==0;
}
/**
* Get the reply data.
* @return the <code>replyData</code> property.
*/
StructuredMem getReplyData()
{
return replyData;
}
/**
* Returns whether this chunk is the first one.
* <b>Take care, that this information may not be reliable.</b>
* @return <code>true</code> if this is the first, and <code>false</code> if this
* is not first or the information is not known.
*/
public boolean isFirst()
{
return this.first;
}
/**
* Returns whether this chunk is the last one.
* <b>Take care, that this information may not be reliable.</b>
* @return <code>true</code> if this is the last, and <code>false</code> if this
* is not first or the information is not known.
*/
public boolean isLast()
{
return this.last;
}
/**
* Sets the <code>last</code> flag.
* @param last the new value.
*/
public void setLast(boolean last)
{
this.last=last;
}
/**
* Sets the <code>first</code> flag.
* @param first the new value.
*/
public void setFirst(boolean first)
{
this.first=first;
}
/**
* Gets the size of this chunk.
* @return the number of rows in this chunk.
*/
public int size()
{
return this.chunkSize;
}
/**
* Gets whether the current position is the first in the result set.
* @return <code>true</code> if the current position is the first row
* of the result set.
*/
public boolean positionedAtFirst()
{
return(this.first && currentOffset==0);
}
/**
* Gets whether the current position is the last in the result set.
* @return <code>true</code> if the current position is the last row
* of the result set.
*/
public boolean positionedAtLast()
{
return(this.last && currentOffset==chunkSize-1);
}
/**
* Get the current position within the result set.
* @return the current position in the result set.
*/
int getLogicalPos()
{
return start_index + currentOffset;
}
/**
* Gets the current offset in this chunk.
* @return the current position in this chunk (starts with 0).
*/
int pos()
{
return currentOffset;
}
/**
* Retrieves the position where the internal position is after the
* fetch if this chunk is the current chunk.
* @return the internal position - either the start or the end of this chunk.
*/
int getKernelPos()
{
switch(this.type) {
case TYPE_ABSOLUTE_DOWN:
case TYPE_RELATIVE_UP:
case TYPE_LAST:
return start_index;
case TYPE_FIRST:
case TYPE_ABSOLUTE_UP:
case TYPE_RELATIVE_DOWN:
default:
return end_index;
}
}
boolean isForward()
{
return (type == TYPE_FIRST ||
type == TYPE_ABSOLUTE_UP ||
type == TYPE_RELATIVE_UP);
}
/**
* Updates the number of rows in the result set.
* @param rows the number of rows in the result set.
*/
void setRowsInResultSet(int rows)
{
this.rowsInResultSet=rows;
}
/**
* Gets the start index of the fetch chunk.
*
* @return The start index (smallest valid index).
*/
int getStart()
{
return start_index;
}
/**
* Gets the end index of the fetch chunk.
*
* @return The end index (largest valid index).
*/
int getEnd()
{
return end_index;
}
/**
* Gets a string with trace information. A sample output is
* <pre>
* FETCH CHUNK [
* TYPE : FIRST
* START INDEX : 1
* END INDEX : 25
* FIRST : TRUE
* LAST : FALSE
* RECORD SIZE : 35
* SIZE : 25
* ROWS IN RESULT : -1
* ]</pre>
* @return The trace string.
*/
public String traceString()
{
StringBuffer result=new StringBuffer();
result.append("FETCH CHUNK [\n");
switch(type) {
case TYPE_FIRST:
result.append(" TYPE : FIRST\n");
break;
case TYPE_LAST:
result.append(" TYPE : LAST\n");
break;
case TYPE_ABSOLUTE_UP:
result.append(" TYPE : ABSOLUTE (UP)\n");
break;
case TYPE_ABSOLUTE_DOWN:
result.append(" TYPE : ABSOLUTE (DOWN)\n");
break;
case TYPE_RELATIVE_UP:
result.append(" TYPE : RELATIVE (UP)\n");
break;
case TYPE_RELATIVE_DOWN:
result.append(" TYPE : RELATIVE (DOWN)\n");
break;
default:
result.append(" TYPE : UNKNOWN\n");
break;
}
result.append(" START INDEX : " + start_index + "\n");
result.append(" END INDEX : " + end_index + "\n");
result.append(" CURRENT : " + currentOffset + "\n");
result.append(" FIRST : " + (first?"TRUE":"FALSE") + "\n");
result.append(" LAST : " + (last?"TRUE":"FALSE") + "\n");
result.append(" RECORD SIZE : " + recordSize + "\n");
result.append(" SIZE : " + chunkSize + "\n");
result.append(" ROWS IN RESULT : " + rowsInResultSet + "\n");
result.append("]");
return result.toString();
}
}
|