1 21 22 package org.apache.derby.impl.sql; 23 24 import org.apache.derby.catalog.Dependable; 25 import org.apache.derby.catalog.DependableFinder; 26 27 import org.apache.derby.iapi.services.context.ContextService; 28 import org.apache.derby.iapi.services.context.ContextManager; 29 30 import org.apache.derby.iapi.services.monitor.Monitor; 31 32 import org.apache.derby.iapi.services.sanity.SanityManager; 33 34 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 35 import org.apache.derby.iapi.services.cache.Cacheable; 36 37 import org.apache.derby.catalog.UUID; 38 import org.apache.derby.iapi.services.uuid.UUIDFactory; 39 import org.apache.derby.iapi.util.ByteArray; 40 41 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 42 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 43 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor; 44 45 import org.apache.derby.iapi.sql.ParameterValueSet; 46 import org.apache.derby.iapi.sql.PreparedStatement; 47 import org.apache.derby.iapi.sql.Statement; 48 import org.apache.derby.iapi.types.DataTypeDescriptor; 49 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 50 import org.apache.derby.iapi.sql.ResultDescription; 51 import org.apache.derby.iapi.sql.ResultSet; 52 import org.apache.derby.iapi.sql.Activation; 53 54 import org.apache.derby.iapi.sql.execute.ConstantAction; 55 import org.apache.derby.iapi.sql.execute.ExecCursorTableReference; 56 import org.apache.derby.iapi.sql.execute.ExecPreparedStatement; 57 58 import org.apache.derby.iapi.sql.depend.DependencyManager; 59 import org.apache.derby.iapi.sql.depend.Provider; 60 61 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 62 import org.apache.derby.iapi.sql.conn.StatementContext; 63 64 import org.apache.derby.impl.sql.compile.QueryTreeNode; 65 import org.apache.derby.impl.sql.compile.CursorNode; 66 67 import org.apache.derby.iapi.error.StandardException; 68 69 import org.apache.derby.iapi.reference.SQLState; 70 71 import org.apache.derby.iapi.services.loader.GeneratedClass; 72 73 import java.sql.Timestamp ; 74 import java.sql.SQLWarning ; 75 import java.util.List ; 76 77 94 public class GenericPreparedStatement 95 implements ExecPreparedStatement 96 { 97 107 public Statement statement; 111 protected GeneratedClass activationClass; protected ResultDescription resultDesc; 113 protected DataTypeDescriptor[] paramTypeDescriptors; 114 private String spsName; 115 private SQLWarning warnings; 116 117 private boolean referencesSessionSchema; 124 125 protected ExecCursorTableReference targetTable; 127 protected ResultColumnDescriptor[] targetColumns; 128 protected String [] updateColumns; 129 protected int updateMode; 130 131 protected ConstantAction executionConstants; 132 protected Object [] savedObjects; 133 protected List requiredPermissionsList; 134 135 protected String UUIDString; 137 protected UUID UUIDValue; 138 139 private boolean needsSavepoint; 140 141 private String execStmtName; 142 private String execSchemaName; 143 protected boolean isAtomic; 144 protected String sourceTxt; 145 146 private int inUseCount; 147 148 boolean compilingStatement; 150 151 152 protected long parseTime; 157 protected long bindTime; 158 protected long optimizeTime; 159 protected long generateTime; 160 protected long compileTime; 161 protected Timestamp beginCompileTimestamp; 162 protected Timestamp endCompileTimestamp; 163 164 protected boolean isValid; 166 protected boolean spsAction; 167 168 173 private Cacheable cacheHolder; 174 175 179 GenericPreparedStatement() { 180 181 UUIDFactory uuidFactory = 182 Monitor.getMonitor().getUUIDFactory(); 183 184 UUIDValue = uuidFactory.createUUID(); 185 UUIDString = UUIDValue.toString(); 186 spsAction = false; 187 } 188 189 191 public GenericPreparedStatement(Statement st) 192 { 193 this(); 194 195 statement = st; 196 } 197 198 public synchronized boolean upToDate() 202 throws StandardException 203 { 204 return isValid && (activationClass != null) && !compilingStatement; 205 } 206 207 public void rePrepare(LanguageConnectionContext lcc) 208 throws StandardException { 209 if (!upToDate()) 210 makeValid(lcc); 211 } 212 213 218 public synchronized Activation getActivation(LanguageConnectionContext lcc, boolean scrollable) throws StandardException 219 { 220 GeneratedClass gc = getActivationClass(); 221 222 if (gc == null) { 223 rePrepare(lcc); 224 gc = getActivationClass(); 225 } 226 227 Activation ac = new GenericActivationHolder(lcc, gc, this, scrollable); 228 229 inUseCount++; 230 231 return ac; 232 } 233 234 public ResultSet execute(LanguageConnectionContext lcc, 235 boolean rollbackParentContext, 236 long timeoutMillis) 237 throws StandardException 238 { 239 Activation a = getActivation(lcc, false); 240 a.setSingleExecution(); 241 return execute(a, rollbackParentContext, timeoutMillis); 242 } 243 244 256 257 public ResultSet execute(Activation activation, 258 boolean rollbackParentContext, 259 long timeoutMillis) 260 throws 261 StandardException 262 { 263 boolean needToClearSavePoint = false; 264 265 if (activation == null || activation.getPreparedStatement() != this) 266 { 267 throw StandardException.newException(SQLState.LANG_WRONG_ACTIVATION, "execute"); 268 } 269 270 recompileOutOfDatePlan: 271 while (true) { 272 280 286 287 LanguageConnectionContext lccToUse = activation.getLanguageConnectionContext(); 288 289 if (lccToUse.getLogStatementText()) 290 { 291 HeaderPrintWriter istream = Monitor.getStream(); 292 String xactId = lccToUse.getTransactionExecute().getActiveStateTxIdString(); 293 String pvsString = ""; 294 ParameterValueSet pvs = activation.getParameterValueSet(); 295 if (pvs != null && pvs.getParameterCount() > 0) 296 { 297 pvsString = " with " + pvs.getParameterCount() + 298 " parameters " + pvs.toString(); 299 } 300 istream.printlnWithHeader(LanguageConnectionContext.xidStr + 301 xactId + 302 "), " + 303 LanguageConnectionContext.lccStr + 304 lccToUse.getInstanceNumber() + 305 "), " + 306 LanguageConnectionContext.dbnameStr + 307 lccToUse.getDbname() + 308 "), " + 309 LanguageConnectionContext.drdaStr + 310 lccToUse.getDrdaID() + 311 "), Executing prepared statement: " + 312 getSource() + 313 " :End prepared statement" + 314 pvsString); 315 } 316 317 ParameterValueSet pvs = activation.getParameterValueSet(); 318 319 321 if (!spsAction) { 322 rePrepare(lccToUse); 334 } 335 336 StatementContext statementContext = lccToUse.pushStatementContext( 337 isAtomic, updateMode==CursorNode.READ_ONLY, getSource(), pvs, rollbackParentContext, timeoutMillis); 338 339 if (needsSavepoint()) 340 { 341 344 statementContext.setSavePoint(); 345 needToClearSavePoint = true; 346 } 347 348 if (executionConstants != null) 349 { 350 lccToUse.validateStmtExecution(executionConstants); 351 } 352 353 ResultSet resultSet = null; 354 try { 355 356 resultSet = activation.execute(); 357 358 resultSet.open(); 359 } catch (StandardException se) { 360 361 if (!se.getMessageId().equals(SQLState.LANG_STATEMENT_NEEDS_RECOMPILE) 362 || spsAction) 363 throw se; 364 statementContext.cleanupOnError(se); 365 continue recompileOutOfDatePlan; 366 367 } 368 369 370 if (needToClearSavePoint) 371 { 372 373 statementContext.clearSavePoint(); 374 } 375 376 lccToUse.popStatementContext(statementContext, null); 377 378 if (activation.isSingleExecution() && resultSet.isClosed()) 379 { 380 activation.close(); 386 } 387 388 return resultSet; 389 390 } 391 } 392 393 public ResultDescription getResultDescription() { 394 return resultDesc; 395 } 396 397 public DataTypeDescriptor[] getParameterTypes() { 398 return paramTypeDescriptors; 399 } 400 401 public String getSource() { 402 return (sourceTxt != null) ? 403 sourceTxt : 404 (statement == null) ? 405 "null" : 406 statement.getSource(); 407 } 408 409 public void setSource(String text) 410 { 411 sourceTxt = text; 412 } 413 414 public final void setSPSName(String name) { 415 spsName = name; 416 } 417 418 public String getSPSName() { 419 return spsName; 420 } 421 422 423 429 public long getCompileTimeInMillis() 430 { 431 return compileTime; 432 } 433 434 439 public long getParseTimeInMillis() 440 { 441 return parseTime; 442 } 443 444 449 public long getBindTimeInMillis() 450 { 451 return bindTime; 452 } 453 454 459 public long getOptimizeTimeInMillis() 460 { 461 return optimizeTime; 462 } 463 464 469 public long getGenerateTimeInMillis() 470 { 471 return generateTime; 472 } 473 474 479 public Timestamp getBeginCompileTimestamp() 480 { 481 return beginCompileTimestamp; 482 } 483 484 489 public Timestamp getEndCompileTimestamp() 490 { 491 return endCompileTimestamp; 492 } 493 494 void setCompileTimeWarnings(SQLWarning warnings) { 495 this.warnings = warnings; 496 } 497 498 public final SQLWarning getCompileTimeWarnings() { 499 return warnings; 500 } 501 502 507 protected void setCompileTimeMillis(long parseTime, long bindTime, 508 long optimizeTime, 509 long generateTime, 510 long compileTime, 511 Timestamp beginCompileTimestamp, 512 Timestamp endCompileTimestamp) 513 { 514 this.parseTime = parseTime; 515 this.bindTime = bindTime; 516 this.optimizeTime = optimizeTime; 517 this.generateTime = generateTime; 518 this.compileTime = compileTime; 519 this.beginCompileTimestamp = beginCompileTimestamp; 520 this.endCompileTimestamp = endCompileTimestamp; 521 } 522 523 524 527 public void finish(LanguageConnectionContext lcc) { 528 529 synchronized (this) { 530 inUseCount--; 531 532 if (cacheHolder != null) 533 return; 534 535 if (inUseCount != 0) { 536 return; 541 } 542 } 543 544 try 548 { 549 552 makeInvalid(DependencyManager.PREPARED_STATEMENT_RELEASE, lcc); 553 } 554 catch (StandardException se) 555 { 556 if (SanityManager.DEBUG) 557 { 558 se.printStackTrace(System.out); 559 SanityManager.THROWASSERT( 560 "Unexpected exception - " + se); 561 } 562 } 563 } 564 565 571 final void setConstantAction( ConstantAction constantAction ) 572 { 573 executionConstants = constantAction; 574 } 575 576 577 582 public final ConstantAction getConstantAction() 583 { 584 return executionConstants; 585 } 586 587 592 final void setSavedObjects( Object [] objects ) 593 { 594 savedObjects = objects; 595 } 596 597 603 public final Object getSavedObject(int objectNum) 604 { 605 if (SanityManager.DEBUG) { 606 if (!(objectNum>=0 && objectNum<savedObjects.length)) 607 SanityManager.THROWASSERT( 608 "request for savedObject entry "+objectNum+" invalid; "+ 609 "savedObjects has "+savedObjects.length+" entries"); 610 } 611 return savedObjects[objectNum]; 612 } 613 614 619 public final Object [] getSavedObjects() 620 { 621 return savedObjects; 622 } 623 624 632 public boolean isValid() { 633 return isValid; 634 } 635 636 640 public void setValid() 641 { 642 isValid = true; 643 } 644 645 649 public void setSPSAction() 650 { 651 spsAction = true; 652 } 653 654 663 public void prepareToInvalidate(Provider p, int action, 664 LanguageConnectionContext lcc) 665 throws StandardException { 666 667 675 switch (action) { 676 case DependencyManager.CHANGED_CURSOR: 677 case DependencyManager.CREATE_INDEX: 678 return; 679 } 680 681 684 lcc.verifyNoOpenResultSets(this, p, action); 685 } 686 687 688 696 public void makeInvalid(int action, LanguageConnectionContext lcc) 697 throws StandardException 698 { 699 700 boolean alreadyInvalid; 701 702 synchronized (this) { 703 704 if (compilingStatement) 705 return; 706 707 alreadyInvalid = !isValid; 708 709 isValid = false; 711 712 compilingStatement = true; 714 } 715 716 try { 717 718 DependencyManager dm = lcc.getDataDictionary().getDependencyManager(); 719 720 if (!alreadyInvalid) 721 { 722 dm.invalidateFor(this, action, lcc); 723 } 724 725 728 dm.clearDependencies(lcc, this); 729 730 734 if (execStmtName != null) { 735 switch (action) { 736 case DependencyManager.INTERNAL_RECOMPILE_REQUEST: 737 case DependencyManager.CHANGED_CURSOR: 738 { 739 743 DataDictionary dd = lcc.getDataDictionary(); 744 745 SchemaDescriptor sd = dd.getSchemaDescriptor(execSchemaName, lcc.getTransactionCompile(), true); 746 SPSDescriptor spsd = dd.getSPSDescriptor(execStmtName, sd); 747 spsd.makeInvalid(action, lcc); 748 break; 749 } 750 } 751 } 752 } finally { 753 synchronized (this) { 754 compilingStatement = false; 755 notifyAll(); 756 } 757 } 758 } 759 760 769 public void makeValid(LanguageConnectionContext lcc) 770 throws StandardException 771 { 772 PreparedStatement ps; 773 774 779 ps = statement.prepare(lcc); 781 if (SanityManager.DEBUG) 782 SanityManager.ASSERT(ps == this, "ps != this"); 783 } 784 785 791 public boolean isPersistent() 792 { 793 794 return false; 795 } 796 797 801 806 public DependableFinder getDependableFinder() 807 { 808 return null; 809 } 810 811 816 public String getObjectName() 817 { 818 return UUIDString; 819 } 820 821 826 public UUID getObjectID() 827 { 828 return UUIDValue; 829 } 830 831 836 public String getClassType() 837 { 838 return Dependable.PREPARED_STATEMENT; 839 } 840 841 854 public boolean referencesSessionSchema() 855 { 856 return referencesSessionSchema; 857 } 858 859 875 public boolean referencesSessionSchema(QueryTreeNode qt) 876 throws StandardException { 877 referencesSessionSchema = qt.referencesSessionSchema(); 880 return(referencesSessionSchema); 881 } 882 883 887 896 void completeCompile(QueryTreeNode qt) 897 throws StandardException { 898 901 paramTypeDescriptors = qt.getParameterTypes(); 902 903 if (targetTable!=null) { 905 targetTable = null; 906 updateMode = 0; 907 updateColumns = null; 908 targetColumns = null; 909 } 910 911 resultDesc = qt.makeResultDescription(); 915 916 920 if (resultDesc != null) 921 { 922 925 CursorInfo cursorInfo = (CursorInfo)qt.getCursorInfo(); 926 if (cursorInfo != null) 927 { 928 targetTable = cursorInfo.targetTable; 929 targetColumns = cursorInfo.targetColumns; 930 updateColumns = cursorInfo.updateColumns; 931 updateMode = cursorInfo.updateMode; 932 } 933 } 934 isValid = true; 935 936 return; 937 } 938 939 public GeneratedClass getActivationClass() 940 throws StandardException 941 { 942 return activationClass; 943 } 944 945 void setActivationClass(GeneratedClass ac) 946 { 947 activationClass = ac; 948 } 949 950 954 959 public int getUpdateMode() { 960 return updateMode; 961 } 962 963 968 public ExecCursorTableReference getTargetTable() 969 { 970 if (SanityManager.DEBUG) 971 { 972 SanityManager.ASSERT(targetTable!=null, "Not a cursor, no target table"); 973 } 974 return targetTable; 975 } 976 977 982 public ResultColumnDescriptor[] getTargetColumns() { 983 return targetColumns; 984 } 985 986 991 public String [] getUpdateColumns() 992 { 993 return updateColumns; 994 } 995 996 1000 public Object getCursorInfo() 1001 { 1002 return new CursorInfo( 1003 updateMode, 1004 targetTable, 1005 targetColumns, 1006 updateColumns); 1007 } 1008 1009 void setCursorInfo(CursorInfo cursorInfo) 1010 { 1011 if (cursorInfo != null) 1012 { 1013 updateMode = cursorInfo.updateMode; 1014 targetTable = cursorInfo.targetTable; 1015 targetColumns = cursorInfo.targetColumns; 1016 updateColumns = cursorInfo.updateColumns; 1017 } 1018 } 1019 1020 1021 1025 1032 ByteArray getByteCodeSaver() 1033 { 1034 return null; 1035 } 1036 1037 1042 public boolean needsSavepoint() 1043 { 1044 return needsSavepoint; 1045 } 1046 1047 1054 void setNeedsSavepoint(boolean needsSavepoint) 1055 { 1056 this.needsSavepoint = needsSavepoint; 1057 } 1058 1059 1065 void setIsAtomic(boolean isAtomic) 1066 { 1067 this.isAtomic = isAtomic; 1068 } 1069 1070 1078 public boolean isAtomic() 1079 { 1080 return isAtomic; 1081 } 1082 1083 1087 void setExecuteStatementNameAndSchema(String execStmtName, 1088 String execSchemaName) 1089 { 1090 this.execStmtName = execStmtName; 1091 this.execSchemaName = execSchemaName; 1092 } 1093 1094 1102 public ExecPreparedStatement getClone() throws StandardException 1103 { 1104 1105 GenericPreparedStatement clone = new GenericPreparedStatement(statement); 1106 1107 clone.activationClass = getActivationClass(); 1108 clone.resultDesc = resultDesc; 1109 clone.paramTypeDescriptors = paramTypeDescriptors; 1110 clone.executionConstants = executionConstants; 1111 clone.UUIDString = UUIDString; 1112 clone.UUIDValue = UUIDValue; 1113 clone.savedObjects = savedObjects; 1114 clone.execStmtName = execStmtName; 1115 clone.execSchemaName = execSchemaName; 1116 clone.isAtomic = isAtomic; 1117 clone.sourceTxt = sourceTxt; 1118 clone.targetTable = targetTable; 1119 clone.targetColumns = targetColumns; 1120 clone.updateColumns = updateColumns; 1121 clone.updateMode = updateMode; 1122 clone.needsSavepoint = needsSavepoint; 1123 1124 return clone; 1125 } 1126 1127 public void setCacheHolder(Cacheable cacheHolder) { 1129 1130 this.cacheHolder = cacheHolder; 1131 1132 if (cacheHolder == null) { 1133 1134 if (!isValid || (inUseCount != 0)) 1136 return; 1137 1138 ContextManager cm = ContextService.getFactory().getCurrentContextManager(); 1139 LanguageConnectionContext lcc = 1140 (LanguageConnectionContext) 1141 (cm.getContext(LanguageConnectionContext.CONTEXT_ID)); 1142 1143 try 1147 { 1148 1151 makeInvalid(DependencyManager.PREPARED_STATEMENT_RELEASE, lcc); 1152 } 1153 catch (StandardException se) 1154 { 1155 if (SanityManager.DEBUG) 1156 { 1157 se.printStackTrace(System.out); 1158 SanityManager.THROWASSERT( 1159 "Unexpected exception - " + se); 1160 } 1161 } 1162 } 1163 } 1164 1165 public String toString() { 1166 return getObjectName(); 1167 } 1168 1169 public boolean isStorable() { 1170 return false; 1171 } 1172 1173 public void setRequiredPermissionsList( List requiredPermissionsList) 1174 { 1175 this.requiredPermissionsList = requiredPermissionsList; 1176 } 1177 1178 public List getRequiredPermissionsList() 1179 { 1180 return requiredPermissionsList; 1181 } 1182} 1183 | Popular Tags |