1 24 25 package org.continuent.sequoia.controller.requests; 26 27 import java.io.Serializable ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.SortedSet ; 33 34 import org.continuent.sequoia.common.i18n.Translate; 35 import org.continuent.sequoia.common.util.TransactionIsolationLevelConverter; 36 import org.continuent.sequoia.controller.semantic.SemanticBehavior; 37 import org.continuent.sequoia.controller.semantic.SemanticManager; 38 import org.continuent.sequoia.controller.sql.schema.DatabaseSchema; 39 import org.continuent.sequoia.controller.sql.schema.DatabaseTable; 40 41 52 public abstract class AbstractRequest implements Serializable ![JavaDoc](../../../../../../cmn/javadoc.gif) 53 { 54 57 61 int requestType = RequestType.UNDEFINED; 62 63 64 protected long id; 65 66 67 protected transient long logId; 68 69 73 protected String sqlQueryOrTemplate; 74 75 78 protected String preparedStatementParameters = null; 79 80 85 protected transient String uniqueKey = null; 86 87 90 private transient SemanticBehavior semantic = null; 91 92 93 private transient SemanticManager semanticManager = null; 94 95 99 protected String login; 100 101 102 protected transient int cacheable = RequestType.UNCACHEABLE; 103 104 105 protected transient boolean isParsed = false; 106 107 108 private boolean macrosAreProcessed = false; 109 110 113 protected int maxRows; 114 protected int fetchSize; 115 protected String cursorName; 116 117 121 122 protected boolean isReadOnly = false; 123 124 128 protected boolean isAutoCommit; 129 130 134 protected long transactionId; 135 136 140 protected int transactionIsolation; 141 142 148 protected int timeoutInSeconds; 149 150 157 protected boolean escapeProcessing = true; 158 159 160 private String lineSeparator = null; 161 162 165 private boolean persistentConnection; 166 167 170 private long persistentConnectionId; 171 172 175 private boolean retrieveSQLWarnings = false; 176 177 181 private transient boolean isLazyTransactionStart; 182 183 186 private String clientIpAddress; 187 188 191 protected boolean requiresConnectionPoolFlush = false; 192 193 196 protected transient long startTime; 197 198 201 protected transient long endTime; 202 203 207 protected SortedSet writeLockedTables = null; 208 209 220 public AbstractRequest(String sqlQuery, boolean escapeProcessing, 221 int timeout, String lineSeparator, int requestType) 222 { 223 this.sqlQueryOrTemplate = sqlQuery; 224 this.escapeProcessing = escapeProcessing; 225 this.timeoutInSeconds = timeout; 226 this.lineSeparator = lineSeparator; 227 this.requestType = requestType; 228 } 229 230 234 240 public abstract boolean altersAggregateList(); 241 242 248 public abstract boolean altersDatabaseCatalog(); 249 250 256 public abstract boolean altersDatabaseSchema(); 257 258 264 public abstract boolean altersMetadataCache(); 265 266 272 public abstract boolean altersQueryResultCache(); 273 274 280 public abstract boolean altersStoredProcedureList(); 281 282 288 public abstract boolean altersUserDefinedTypes(); 289 290 297 public abstract boolean altersUsers(); 298 299 307 public abstract boolean altersSomething(); 308 309 314 public abstract void cloneParsing(AbstractRequest request); 315 316 321 protected void cloneParsingCommons(AbstractRequest request) 322 { 323 cacheable = request.getCacheAbility(); 324 semantic = request.getSemantic(); 325 } 326 327 335 public SortedSet getWriteLockedDatabaseTables() 336 { 337 return writeLockedTables; 338 } 339 340 347 public abstract boolean needsMacroProcessing(); 348 349 364 public abstract void parse(DatabaseSchema schema, int granularity, 365 boolean isCaseSensitive) throws SQLException ; 366 367 371 377 public final boolean isAlter() 378 { 379 return RequestType.isAlter(this.requestType); 380 } 381 382 388 public final boolean isCreate() 389 { 390 return RequestType.isCreate(this.requestType); 391 } 392 393 399 public final boolean isDelete() 400 { 401 return RequestType.isDelete(this.requestType); 402 } 403 404 410 public final boolean isDrop() 411 { 412 return RequestType.isDrop(this.requestType); 413 } 414 415 421 public final boolean isInsert() 422 { 423 return RequestType.isInsert(this.requestType); 424 } 425 426 432 public final boolean isSelect() 433 { 434 return RequestType.isSelect(this.requestType); 435 } 436 437 443 public final boolean isUpdate() 444 { 445 return RequestType.isUpdate(this.requestType); 446 } 447 448 452 460 public int getCacheAbility() 461 { 462 return cacheable; 463 } 464 465 473 public void setCacheAbility(int cacheAbility) 474 { 475 this.cacheable = cacheAbility; 476 } 477 478 483 public String getClientIpAddress() 484 { 485 return clientIpAddress; 486 } 487 488 493 public void setClientIpAddress(String clientIpAddress) 494 { 495 this.clientIpAddress = clientIpAddress; 496 } 497 498 503 public String getCursorName() 504 { 505 return cursorName; 506 } 507 508 513 public void setCursorName(String cursorName) 514 { 515 this.cursorName = cursorName; 516 } 517 518 523 public long getEndTime() 524 { 525 return endTime; 526 } 527 528 533 public void setEndTime(long endTime) 534 { 535 this.endTime = endTime; 536 } 537 538 546 public long getExecTimeInMs() 547 { 548 long execTime = 0; 549 if (startTime != 0) 550 { 551 if (endTime != 0) 552 execTime = endTime - startTime; 553 else 554 execTime = System.currentTimeMillis() - startTime; 555 if (execTime < 0) 556 execTime = 0; 557 } 558 return execTime; 559 } 560 561 567 public boolean getEscapeProcessing() 568 { 569 return escapeProcessing; 570 } 571 572 577 public int getFetchSize() 578 { 579 return fetchSize; 580 } 581 582 588 public void setFetchSize(int fetchSize) 589 { 590 this.fetchSize = fetchSize; 591 } 592 593 598 public long getId() 599 { 600 return id; 601 } 602 603 608 public void setId(long id) 609 { 610 this.id = id; 611 } 612 613 619 public final boolean isLazyTransactionStart() 620 { 621 return isLazyTransactionStart; 622 } 623 624 630 public void setIsLazyTransactionStart(boolean isLazyStart) 631 { 632 isLazyTransactionStart = isLazyStart; 633 } 634 635 640 public final long getLogId() 641 { 642 return logId; 643 } 644 645 650 public final void setLogId(long logId) 651 { 652 this.logId = logId; 653 } 654 655 660 public String getLogin() 661 { 662 return login; 663 } 664 665 674 public String getLineSeparator() 675 { 676 return lineSeparator; 677 } 678 679 686 public final void setLineSeparator(String lineSeparator) 687 { 688 this.lineSeparator = lineSeparator; 689 } 690 691 696 public void setLogin(String login) 697 { 698 this.login = login; 699 } 700 701 706 public boolean getMacrosAreProcessed() 707 { 708 return macrosAreProcessed; 709 } 710 711 716 public void setMacrosAreProcessed(boolean macrosAreProcessed) 717 { 718 this.macrosAreProcessed = macrosAreProcessed; 719 } 720 721 727 public int getMaxRows() 728 { 729 return maxRows; 730 } 731 732 738 public void setMaxRows(int rows) 739 { 740 maxRows = rows; 741 } 742 743 748 public final SemanticBehavior getSemantic() 749 { 750 return semantic; 751 } 752 753 759 public final void setSemantic(SemanticBehavior semantic) 760 { 761 this.semantic = semantic; 762 if (semanticManager == null) 763 throw new RuntimeException ("Semantic manager not set on request " 764 + toString()); 765 writeLockedTables = semanticManager.getWriteLockedTables(semantic); 766 isParsed = semantic != null; 767 } 768 769 774 public final SemanticManager getSemanticManager() 775 { 776 return semanticManager; 777 } 778 779 784 public final void setSemanticManager(SemanticManager semanticManager) 785 { 786 this.semanticManager = semanticManager; 787 } 788 789 795 public String getSqlOrTemplate() 796 { 797 return sqlQueryOrTemplate; 798 } 799 800 809 public void setSqlOrTemplate(String sql) 810 { 811 this.sqlQueryOrTemplate = sql; 812 uniqueKey = null; 814 } 815 816 823 public String getSqlShortForm(int nbOfCharacters) 824 { 825 String key = getUniqueKey(); 826 if ((nbOfCharacters == 0) || (key.length() < nbOfCharacters)) 827 return key; 828 else 829 return key.substring(0, nbOfCharacters) + "..."; 830 } 831 832 838 public String getPreparedStatementParameters() 839 { 840 return preparedStatementParameters; 841 } 842 843 848 public void setPreparedStatementParameters(String params) 849 { 850 preparedStatementParameters = params; 851 } 852 853 858 public long getStartTime() 859 { 860 return startTime; 861 } 862 863 868 public void setStartTime(long startTime) 869 { 870 this.startTime = startTime; 871 } 872 873 878 public int getTimeout() 879 { 880 return timeoutInSeconds; 881 } 882 883 889 public void setTimeout(int timeout) 890 { 891 this.timeoutInSeconds = timeout; 892 } 893 894 900 public long getTransactionId() 901 { 902 return transactionId; 903 } 904 905 910 public void setTransactionId(long id) 911 { 912 transactionId = id; 913 } 914 915 920 public int getTransactionIsolation() 921 { 922 return transactionIsolation; 923 } 924 925 931 public void setTransactionIsolation(int isolationLevel) 932 { 933 this.transactionIsolation = isolationLevel; 934 } 935 936 943 public String getType() 944 { 945 return RequestType.getTypeAsString(this); 946 } 947 948 953 public String getUniqueKey() 954 { 955 if (uniqueKey == null) 956 { 957 if (preparedStatementParameters == null) 958 uniqueKey = sqlQueryOrTemplate; 959 else 960 uniqueKey = sqlQueryOrTemplate + "/" + preparedStatementParameters; 961 } 962 return uniqueKey; 963 } 964 965 971 public boolean isAutoCommit() 972 { 973 return isAutoCommit; 974 } 975 976 982 public void setIsAutoCommit(boolean isAutoCommit) 983 { 984 this.isAutoCommit = isAutoCommit; 985 } 986 987 993 public boolean isParsed() 994 { 995 return isParsed; 996 } 997 998 1003 public final boolean isPersistentConnection() 1004 { 1005 return persistentConnection; 1006 } 1007 1008 1013 public final void setPersistentConnection(boolean persistentConnection) 1014 { 1015 this.persistentConnection = persistentConnection; 1016 } 1017 1018 1023 public final long getPersistentConnectionId() 1024 { 1025 return persistentConnectionId; 1026 } 1027 1028 1033 public final void setPersistentConnectionId(long persistentConnectionId) 1034 { 1035 this.persistentConnectionId = persistentConnectionId; 1036 } 1037 1038 1043 public boolean isReadOnly() 1044 { 1045 return isReadOnly; 1046 } 1047 1048 1053 public void setIsReadOnly(boolean isReadOnly) 1054 { 1055 this.isReadOnly = isReadOnly; 1056 } 1057 1058 1063 public boolean requiresConnectionPoolFlush() 1064 { 1065 return requiresConnectionPoolFlush; 1066 } 1067 1068 1073 public boolean getRetrieveSQLWarnings() 1074 { 1075 return retrieveSQLWarnings; 1076 } 1077 1078 1083 public void setRetrieveSQLWarnings(boolean isRetrieveSQLWarnings) 1084 { 1085 retrieveSQLWarnings = isRetrieveSQLWarnings; 1086 } 1087 1088 1092 1100 protected void addDependingTables(DatabaseSchema schema, 1101 SortedSet lockedTables) 1102 { 1103 if ((schema == null) || (lockedTables == null)) 1104 return; 1105 1106 List tablesToAdd = null; 1107 for (Iterator iter = lockedTables.iterator(); iter.hasNext();) 1108 { 1109 String tableName = (String ) iter.next(); 1110 1111 DatabaseTable table = schema.getTable(tableName, false); 1113 if (table == null) 1114 continue; 1115 ArrayList dependingTables = table.getDependingTables(); 1116 if (dependingTables == null) 1117 continue; 1118 1119 if (tablesToAdd == null) 1122 tablesToAdd = new ArrayList (); 1123 tablesToAdd.addAll(dependingTables); 1124 } 1125 if (tablesToAdd != null) 1127 lockedTables.addAll(tablesToAdd); 1128 } 1129 1130 1136 public boolean equals(Object other) 1137 { 1138 if ((other == null) || !(other instanceof AbstractRequest)) 1139 return false; 1140 1141 AbstractRequest r = (AbstractRequest) other; 1142 return id == r.getId(); 1143 } 1144 1145 1148 public int hashCode() 1149 { 1150 return (int) id; 1151 } 1152 1153 1163 public String trimCarriageReturnAndTabs() 1164 { 1165 return replaceStringWithSpace(replaceStringWithSpace(sqlQueryOrTemplate, 1166 this.getLineSeparator()), "\t"); 1167 } 1168 1169 1177 private String replaceStringWithSpace(String s, String toReplace) 1178 { 1179 return s.replaceAll(toReplace, " "); 1180 } 1181 1182 1185 public String toString() 1186 { 1187 return getUniqueKey(); 1188 } 1189 1190 1199 public String toStringShortForm(int nbOfCharacters) 1200 { 1201 return getSqlShortForm(nbOfCharacters); 1202 } 1203 1204 1209 public String getParsingResultsAsString() 1210 { 1211 StringBuffer sb = new StringBuffer (Translate.get("request.parsing.results")); 1212 sb.append(Translate.get("request.type", RequestType 1213 .getTypeAsString(requestType))); 1214 sb.append(Translate.get("request.unique.key", getUniqueKey())); 1215 if (writeLockedTables != null && writeLockedTables.size() > 0) 1216 { 1217 sb.append(Translate.get("request.locked.tables")); 1218 for (int i = 0; i < writeLockedTables.size(); i++) 1219 { 1220 sb.append(Translate.get("request.locked.table", writeLockedTables 1221 .toArray()[i])); 1222 } 1223 } 1224 return sb.toString(); 1225 } 1226 1227 1238 public String toDebugString() 1239 { 1240 StringBuffer ret = new StringBuffer (); 1241 ret.append("Request id: " + id); 1242 ret.append("\n query: " + sqlQueryOrTemplate); 1243 if (preparedStatementParameters != null) 1244 ret.append("\n parameters: " + preparedStatementParameters); 1245 ret.append("\n login: " + login); 1246 ret.append("\n autocommit: " + isAutoCommit); 1247 ret.append("\n transaction id: " + transactionId); 1248 ret.append("\n cacheable status: " 1249 + RequestType.getInformation(cacheable)); 1250 ret.append("\n isolation level: " 1251 + TransactionIsolationLevelConverter.toString(transactionIsolation)); 1252 ret.append("\n start time: " + startTime); 1253 ret.append("\n end time: " + endTime); 1254 ret.append("\n timeout in seconds: " + timeoutInSeconds); 1255 if (writeLockedTables != null && writeLockedTables.size() > 0) 1256 { 1257 ret.append("\n locked tables:"); 1258 for (int i = 0; i < writeLockedTables.size(); i++) 1259 { 1260 ret.append("\n " + writeLockedTables.toArray()[i]); 1261 } 1262 } 1263 ret.append("\n persistent connection id: " + persistentConnectionId); 1264 ret.append("\n client ip address: " + clientIpAddress); 1265 1266 return ret.toString(); 1267 } 1268 1269 1272 public void debug() 1273 { 1274 System.out.println(toDebugString()); 1275 } 1276 1277 1286 public String toShortDebugString() 1287 { 1288 StringBuffer ret = new StringBuffer (); 1289 ret.append("Id=" + id); 1290 ret.append(", query=" + sqlQueryOrTemplate); 1291 if (preparedStatementParameters != null) 1292 ret.append(", params=" + preparedStatementParameters); 1293 ret.append(", transaction id=" + transactionId); 1294 ret.append(", persistent connection id=" + persistentConnectionId); 1295 return ret.toString(); 1296 } 1297 1298} 1299 | Popular Tags |