1 24 25 package org.objectweb.cjdbc.common.sql; 26 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.sql.SQLException ; 30 31 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema; 32 import org.objectweb.cjdbc.common.stream.CJDBCInputStream; 33 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream; 34 35 46 public abstract class AbstractRequest implements Serializable 47 { 48 64 68 73 74 75 int requestType = RequestType.UNDEFINED; 76 77 78 protected transient long id; 79 80 81 protected String sqlQuery; 82 83 84 protected String sqlSkeleton = null; 85 86 90 protected String login; 91 92 93 protected int cacheable = RequestType.UNCACHEABLE; 94 95 96 protected boolean isParsed = false; 97 98 101 int maxRows; 102 int fetchSize; 103 String cursorName; 104 105 109 110 protected boolean isReadOnly = false; 111 112 116 protected boolean isAutoCommit; 117 118 122 protected long transactionId; 123 124 128 protected int transactionIsolation; 129 130 136 protected int timeoutInSeconds; 137 138 145 protected boolean escapeProcessing = true; 146 147 151 private String lineSeparator = null; 152 153 158 private boolean driverProcessed = true; 159 160 171 public AbstractRequest(String sqlQuery, boolean escapeProcessing, 172 int timeout, String lineSeparator, int requestType) 173 { 174 this.sqlQuery = sqlQuery; 175 this.escapeProcessing = escapeProcessing; 176 this.timeoutInSeconds = timeout; 177 this.lineSeparator = lineSeparator; 178 this.requestType = requestType; 179 } 180 181 190 191 public AbstractRequest(CJDBCInputStream in, int requestType) 192 throws IOException 193 { 194 this.sqlQuery = in.readUTF(); 195 this.escapeProcessing = in.readBoolean(); 196 this.lineSeparator = in.readUTF(); 197 this.timeoutInSeconds = in.readInt(); 198 199 this.isAutoCommit = in.readBoolean(); 200 this.driverProcessed = in.readBoolean(); 201 202 if (in.readBoolean()) 206 this.sqlSkeleton = in.readUTF(); 207 208 210 this.requestType = requestType; 211 } 212 213 222 223 public void sendToStream(CJDBCOutputStream out, 224 boolean controllerWantsSqlSkeleton) throws IOException 225 { 226 out.writeUTF(sqlQuery); 227 out.writeBoolean(escapeProcessing); 228 out.writeUTF(lineSeparator); 229 out.writeInt(timeoutInSeconds); 230 231 out.writeBoolean(isAutoCommit); 232 out.writeBoolean(driverProcessed); 233 234 if ((controllerWantsSqlSkeleton || !isDriverProcessed()) 237 && sqlSkeleton != null) 238 { 239 out.writeBoolean(true); 240 out.writeUTF(sqlSkeleton); 241 } 242 else 243 out.writeBoolean(false); 244 245 } 246 247 254 void receiveResultSetParams(CJDBCInputStream in) throws IOException 255 { 256 this.maxRows = in.readInt(); 257 this.fetchSize = in.readInt(); 258 259 if (in.readBoolean()) this.cursorName = in.readUTF(); 261 } 262 263 270 void sendResultSetParams(CJDBCOutputStream out) throws IOException 271 { 272 out.writeInt(maxRows); 273 out.writeInt(fetchSize); 274 275 if (this.cursorName != null) { 277 out.writeBoolean(true); 278 out.writeUTF(cursorName); 279 } 280 else 281 out.writeBoolean(false); 282 } 283 284 291 public abstract boolean needsMacroProcessing(); 292 293 298 public abstract boolean returnsResultSet(); 299 300 306 public final boolean isAlter() 307 { 308 return RequestType.isAlter(this.requestType); 309 } 310 311 317 public final boolean isCreate() 318 { 319 return RequestType.isCreate(this.requestType); 320 } 321 322 328 public final boolean isDelete() 329 { 330 return RequestType.isDelete(this.requestType); 331 } 332 333 344 public final boolean isDDL() 345 { 346 return RequestType.isDDL(this.requestType); 347 } 348 349 359 public final boolean isDML() 360 { 361 return RequestType.isDML(this.requestType); 362 } 363 364 370 public final boolean isDrop() 371 { 372 return RequestType.isDrop(this.requestType); 373 } 374 375 381 public final boolean isInsert() 382 { 383 return RequestType.isInsert(this.requestType); 384 } 385 386 392 public boolean isParsed() 393 { 394 return isParsed; 395 } 396 397 402 public boolean isReadOnly() 403 { 404 return isReadOnly; 405 } 406 407 413 public final boolean isSelect() 414 { 415 return RequestType.isSelect(this.requestType); 416 } 417 418 424 public final boolean isUpdate() 425 { 426 return RequestType.isUpdate(this.requestType); 427 } 428 429 434 public void setIsReadOnly(boolean isReadOnly) 435 { 436 this.isReadOnly = isReadOnly; 437 } 438 439 447 public int getCacheAbility() 448 { 449 return cacheable; 450 } 451 452 460 public void setCacheAbility(int cacheAbility) 461 { 462 this.cacheable = cacheAbility; 463 } 464 465 471 public boolean getEscapeProcessing() 472 { 473 return escapeProcessing; 474 } 475 476 481 public long getId() 482 { 483 return id; 484 } 485 486 491 public void setId(long id) 492 { 493 this.id = id; 494 } 495 496 502 public boolean isAutoCommit() 503 { 504 return isAutoCommit; 505 } 506 507 513 public void setIsAutoCommit(boolean isAutoCommit) 514 { 515 this.isAutoCommit = isAutoCommit; 516 } 517 518 523 public String getLogin() 524 { 525 return login; 526 } 527 528 533 public String getLineSeparator() 534 { 535 return lineSeparator; 536 } 537 538 543 public void setLogin(String login) 544 { 545 this.login = login; 546 } 547 548 553 public String getSQL() 554 { 555 return sqlQuery; 556 } 557 558 565 public String getSQLShortForm(int nbOfCharacters) 566 { 567 if ((nbOfCharacters == 0) || (sqlQuery.length() < nbOfCharacters)) 568 return sqlQuery; 569 else 570 return sqlQuery.substring(0, nbOfCharacters) + "..."; 571 } 572 573 579 public int getMaxRows() 580 { 581 return maxRows; 582 } 583 584 590 public void setMaxRows(int rows) 591 { 592 maxRows = rows; 593 } 594 595 603 public void setSQL(String sql) 604 { 605 this.sqlQuery = sql; 606 } 607 608 613 public int getTimeout() 614 { 615 return timeoutInSeconds; 616 } 617 618 624 public void setTimeout(int timeout) 625 { 626 this.timeoutInSeconds = timeout; 627 } 628 629 635 public long getTransactionId() 636 { 637 return transactionId; 638 } 639 640 645 public void setTransactionId(long id) 646 { 647 transactionId = id; 648 } 649 650 657 public boolean equals(Object other) 658 { 659 if ((other == null) || !(other instanceof AbstractRequest)) 660 return false; 661 662 AbstractRequest r = (AbstractRequest) other; 663 return id == r.getId(); 664 } 665 666 681 public abstract void parse(DatabaseSchema schema, int granularity, 682 boolean isCaseSensitive) throws SQLException ; 683 684 689 public abstract void cloneParsing(AbstractRequest request); 690 691 701 public String trimCarriageReturnAndTabs() 702 { 703 if (sqlSkeleton != null) 704 return replaceStringWithSpace(replaceStringWithSpace(sqlSkeleton, 705 lineSeparator), "\t"); 706 else 707 return replaceStringWithSpace(replaceStringWithSpace(sqlQuery, 708 lineSeparator), "\t"); 709 } 710 711 719 private String replaceStringWithSpace(String s, String toReplace) 720 { 721 int toReplaceLength = toReplace.length(); 722 int idx = s.indexOf(toReplace); 723 if (idx == -1) 724 return s; 725 else 726 { 727 if (idx == 0) 728 return replaceStringWithSpace(s.substring(toReplaceLength), toReplace); 730 else if (idx == (s.length() - toReplaceLength)) 731 return s.substring(0, s.length() - toReplaceLength); 733 else 734 return s.substring(0, idx) 736 + " " 737 + replaceStringWithSpace(s.substring(idx + toReplaceLength), 738 toReplace); 739 } 740 } 741 742 745 public String getSqlSkeleton() 746 { 747 return sqlSkeleton; 748 } 749 750 754 public void setSqlSkeleton(String skel) 755 { 756 sqlSkeleton = skel; 757 } 758 759 764 public boolean isDriverProcessed() 765 { 766 return driverProcessed; 767 } 768 769 774 public void setDriverProcessed(boolean driverProcessed) 775 { 776 this.driverProcessed = driverProcessed; 777 } 778 779 785 public void setFetchSize(int fetchSize) 786 { 787 this.fetchSize = fetchSize; 788 } 789 790 795 public int getFetchSize() 796 { 797 return fetchSize; 798 } 799 800 805 public int getTransactionIsolation() 806 { 807 return transactionIsolation; 808 } 809 810 816 public void setTransactionIsolation(int isolationLevel) 817 { 818 this.transactionIsolation = isolationLevel; 819 } 820 821 826 public String getCursorName() 827 { 828 return cursorName; 829 } 830 831 836 public void setCursorName(String cursorName) 837 { 838 this.cursorName = cursorName; 839 } 840 841 844 public void debug() 845 { 846 System.out.println("Request: " + sqlQuery); 847 System.out.print("Cacheable status: "); 848 System.out.println(RequestType.getInformation(cacheable)); 849 } 850 851 } 852 | Popular Tags |