⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericpreparedstatement.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*   Derby - Class org.apache.derby.impl.sql.GenericPreparedStatement   Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package org.apache.derby.impl.sql;import org.apache.derby.iapi.reference.JDBC20Translation;import	org.apache.derby.catalog.Dependable;import	org.apache.derby.catalog.DependableFinder;import org.apache.derby.iapi.services.context.Context;import org.apache.derby.iapi.services.context.ContextService;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.monitor.ModuleFactory;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.stream.HeaderPrintWriter;import org.apache.derby.iapi.services.cache.Cacheable;import org.apache.derby.catalog.UUID;import org.apache.derby.iapi.services.uuid.UUIDFactory;import org.apache.derby.iapi.util.ByteArray;import org.apache.derby.iapi.services.stream.HeaderPrintWriter;import org.apache.derby.iapi.services.stream.InfoStreams;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;import org.apache.derby.iapi.sql.dictionary.SPSDescriptor;import org.apache.derby.iapi.sql.ParameterValueSet;import org.apache.derby.iapi.sql.PreparedStatement;import org.apache.derby.iapi.sql.Statement;import org.apache.derby.iapi.types.DataTypeDescriptor;import org.apache.derby.iapi.sql.ResultColumnDescriptor;import org.apache.derby.iapi.sql.ResultDescription;import org.apache.derby.iapi.sql.ResultSet;import org.apache.derby.iapi.sql.Activation;import org.apache.derby.iapi.sql.execute.ConstantAction;import org.apache.derby.iapi.sql.execute.ExecutionContext;import org.apache.derby.iapi.sql.execute.ExecCursorTableReference;import org.apache.derby.iapi.sql.execute.ExecPreparedStatement;import org.apache.derby.iapi.sql.depend.DependencyManager;import org.apache.derby.iapi.sql.depend.Dependent;import org.apache.derby.iapi.sql.depend.Provider;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.sql.conn.StatementContext;import org.apache.derby.impl.sql.compile.QueryTreeNode;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.reference.Property;import org.apache.derby.iapi.services.loader.GeneratedClass;import org.apache.derby.iapi.services.loader.ClassFactory;import java.sql.Timestamp;import java.sql.SQLWarning;import java.util.Vector;/** * Basic implementation of prepared statement. * relies on implementation of ResultDescription and Statement that * are also in this package. * <p> * These are both dependents (of the schema objects and prepared statements * they depend on) and providers.  Prepared statements that are providers * are cursors that end up being used in positioned delete and update * statements (at present). * <p> * This is impl with the regular prepared statements; they will never * have the cursor info fields set. * <p> * Stored prepared statements extend this implementation * * @author ames */public class GenericPreparedStatement	implements ExecPreparedStatement{	///////////////////////////////////////////////	//	// WARNING: when adding members to this class, be	// sure to do the right thing in getClone(): if	// it is PreparedStatement specific like finished,	// then it shouldn't be copied, but stuff like parameters 	// must be copied.		//	////////////////////////////////////////////////	////////////////////////////////////////////////	// STATE that is copied by getClone()	////////////////////////////////////////////////	public Statement statement;	protected GeneratedClass activationClass; // satisfies Activation	protected ResultDescription resultDesc;	protected DataTypeDescriptor[] paramTypeDescriptors;	private String			spsName;	private SQLWarning		warnings;	//If the query node for this statement references SESSION schema tables, mark it so in the boolean below	//This information will be used by EXECUTE STATEMENT if it is executing a statement that was created with NOCOMPILE. Because	//of NOCOMPILE, we could not catch SESSION schema table reference by the statement at CREATE STATEMENT time. Need to catch	//such statements at EXECUTE STATEMENT time when the query is getting compiled.	//This information will also be used to decide if the statement should be cached or not. Any statement referencing SESSION	//schema tables will not be cached.	private boolean		referencesSessionSchema;	// fields used for cursors	protected ExecCursorTableReference	targetTable; 	protected ResultColumnDescriptor[]	targetColumns; 	protected String[] 					updateColumns; 	protected int 						updateMode;	protected ConstantAction	executionConstants;	protected Object[]	savedObjects;	// fields for dependency tracking	protected String UUIDString;	protected UUID   UUIDValue;	private ParameterValueSet params;	private boolean needsSavepoint;	private String execStmtName;	private String execSchemaName;	protected boolean isAtomic;	protected String sourceTxt;	private int inUseCount;	// true if the statement is being compiled.	boolean compilingStatement;	////////////////////////////////////////////////	// STATE that is not copied by getClone()	////////////////////////////////////////////////	// fields for run time stats	protected long parseTime;	protected long bindTime;	protected long optimizeTime;	protected long generateTime;	protected long compileTime;	protected Timestamp beginCompileTimestamp;	protected Timestamp endCompileTimestamp;	//private boolean finished;	protected boolean isValid;	protected boolean spsAction;	// state for caching.	/**		If non-null then this object is the cacheable		that holds us in the cache.	*/	private Cacheable cacheHolder;	//	// constructors	//	protected GenericPreparedStatement() {		/* Get the UUID for this prepared statement */		UUIDFactory uuidFactory = 			Monitor.getMonitor().getUUIDFactory();		UUIDValue = uuidFactory.createUUID();		UUIDString = UUIDValue.toString();		spsAction = false;	}	/**	 */	public GenericPreparedStatement(Statement st)	{		this();		statement = st;	}	//	// PreparedStatement interface	//	public synchronized boolean	upToDate()		throws StandardException	{		boolean	upToDate =  isValid && (activationClass != null) && !compilingStatement;		// this if for the Plugin		if ( executionConstants != null )	    {			boolean		constantsUpToDate = executionConstants.upToDate();			upToDate = upToDate && constantsUpToDate;		}		return upToDate;	}	public void rePrepare(LanguageConnectionContext lcc) 		throws StandardException {		if (!upToDate())		    makeValid(lcc);	}	/**	 * Get a new activation instance.	 *	 * @exception StandardException thrown if finished.	 */	public synchronized Activation	getActivation(LanguageConnectionContext lcc, boolean scrollable) throws StandardException 	{		GeneratedClass gc = getActivationClass();		if (gc == null) {			rePrepare(lcc);			gc = getActivationClass();		}		Activation ac = new GenericActivationHolder(lcc, gc, this, scrollable);		if (params != null)		{			ac.setParameters(params, null);		}		inUseCount++;		return ac;	}	public ResultSet execute(LanguageConnectionContext lcc, boolean rollbackParentContext)		throws StandardException	{		Activation a = getActivation(lcc, false);		a.setSingleExecution();		return execute(a, false, false, rollbackParentContext);	}	/**	  *	The guts of execution.	  *	  *	@param	activation					the activation to run.	  * @param	executeQuery				Called via executeQuery	  * @param	executeUpdate				Called via executeUpdate	  * @param rollbackParentContext True if 1) the statement context is	  *  NOT a top-level context, AND 2) in the event of a statement-level	  *	 exception, the parent context needs to be rolled back, too.	  *	@return	the result set to be pawed through	  *	  *	@exception	StandardException thrown on error	  */	public ResultSet execute	(Activation activation, boolean executeQuery, boolean executeUpdate,		boolean rollbackParentContext) throws StandardException 	{		boolean				needToClearSavePoint = false;		if (activation == null || activation.getPreparedStatement() != this)		{			throw StandardException.newException(SQLState.LANG_WRONG_ACTIVATION, "execute");		}recompileOutOfDatePlan:		while (true) {			// verify the activation is for me--somehow.  NOTE: This is			// different from the above check for whether the activation is			// associated with the right PreparedStatement - it's conceivable			// that someone could construct an activation of the wrong type			// that points to the right PreparedStatement.			//			//SanityManager.ASSERT(activation instanceof activationClass, "executing wrong activation");			/* This is where we set and clear savepoints around each individual			 * statement which needs one.  We don't set savepoints for cursors because			 * they're not needed and they wouldn't work in a read only database.			 * We can't set savepoints for commit/rollback because they'll get			 * blown away before we try to clear them.			 */			LanguageConnectionContext lccToUse = activation.getLanguageConnectionContext(); 			if (lccToUse.getLogStatementText())			{				HeaderPrintWriter istream = Monitor.getStream();				String xactId = lccToUse.getTransactionExecute().getActiveStateTxIdString();				String pvsString = "";				ParameterValueSet pvs = activation.getParameterValueSet();				if (pvs != null && pvs.getParameterCount() > 0)				{					pvsString = " with " + pvs.getParameterCount() +							" parameters " + pvs.toString();				}				istream.printlnWithHeader(LanguageConnectionContext.xidStr + 										  xactId + 										  "), " +										  LanguageConnectionContext.lccStr +										  lccToUse.getInstanceNumber() +										  "), " +										  LanguageConnectionContext.dbnameStr +										  lccToUse.getDbname() +										  "), " +										  LanguageConnectionContext.drdaStr +										  lccToUse.getDrdaID() +										  "), Executing prepared statement: " +										  getSource() +										  " :End prepared statement" +										  pvsString);			}			ParameterValueSet pvs = activation.getParameterValueSet();			/* put it in try block to unlock the PS in any case			 */			if (!spsAction) {			// only re-prepare if this isn't an SPS for a trigger-action;			// if it _is_ an SPS for a trigger action, then we can't just			// do a regular prepare because the statement might contain			// internal SQL that isn't allowed in other statements (such as a			// static method call to get the trigger context for retrieval			// of "new row" or "old row" values).  So in that case we			// skip the call to 'rePrepare' and if the statement is out			// of date, we'll get a NEEDS_COMPILE exception when we try			// to execute.  That exception will be caught by the executeSPS()			// method of the GenericTriggerExecutor class, and at that time			// the SPS action will be recompiled correctly.				rePrepare(lccToUse);			}			StatementContext statementContext = lccToUse.pushStatementContext(				isAtomic, getSource(), pvs, rollbackParentContext);			if (needsSavepoint())			{				/* Mark this position in the log so that a statement				* rollback will undo any changes.				*/				statementContext.setSavePoint();				needToClearSavePoint = true;			}			if (executionConstants != null)			{				lccToUse.validateStmtExecution(executionConstants);			}			ResultSet resultSet = null;			try {					resultSet = activation.execute();				resultSet.open();			} catch (StandardException se) {				/* Cann't handle recompiling SPS action recompile here */				if (!se.getMessageId().equals(SQLState.LANG_STATEMENT_NEEDS_RECOMPILE)						 || spsAction)					throw se;				statementContext.cleanupOnError(se);				continue recompileOutOfDatePlan;			}			if (needToClearSavePoint)			{				/* We're done with our updates */				statementContext.clearSavePoint();			}			lccToUse.popStatementContext(statementContext, null);								if (activation.isSingleExecution() && resultSet.isClosed())			{				// if the result set is 'done', i.e. not openable,				// then we can also release the activation.				// Note that a result set with output parameters 				// or rows to return is explicitly finished 				// by the user.				activation.close();			}			/* executeQuery() not allowed on statements			 * that return a row count,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -