FileDocCategorySizeDatePackage
NativeSQLQueryPlan.javaAPI DocHibernate 3.2.55443Thu Jun 15 00:21:06 BST 2006org.hibernate.engine.query

NativeSQLQueryPlan

public class NativeSQLQueryPlan extends Object implements Serializable
Defines a query execution plan for a native-SQL query.
author
Steve Ebersole

Fields Summary
private final String
sourceQuery
private final org.hibernate.loader.custom.sql.SQLCustomQuery
customQuery
private static final Log
log
Constructors Summary
public NativeSQLQueryPlan(org.hibernate.engine.query.sql.NativeSQLQuerySpecification specification, org.hibernate.engine.SessionFactoryImplementor factory)


	 
			 
			  
		this.sourceQuery = specification.getQueryString();

		customQuery = new SQLCustomQuery(
				specification.getQueryString(),
				specification.getQueryReturns(),
				specification.getQuerySpaces(),
				factory );
	
Methods Summary
private intbindNamedParameters(java.sql.PreparedStatement ps, java.util.Map namedParams, int start, org.hibernate.engine.SessionImplementor session)
Bind named parameters to the PreparedStatement. This has an empty implementation on this superclass and should be implemented by subclasses (queries) which allow named parameters.


		if ( namedParams != null ) {
			// assumes that types are all of span 1
			Iterator iter = namedParams.entrySet().iterator();
			int result = 0;
			while ( iter.hasNext() ) {
				Map.Entry e = (Map.Entry) iter.next();
				String name = (String) e.getKey();
				TypedValue typedval = (TypedValue) e.getValue();
				int[] locs = getNamedParameterLocs( name );
				for (int i = 0; i < locs.length; i++) {
					if ( log.isDebugEnabled() ) {
						log.debug( "bindNamedParameters() "
								+ typedval.getValue() + " -> " + name + " ["
								+ (locs[i] + start ) + "]" );
					}
					typedval.getType().nullSafeSet( ps, typedval.getValue(),
							locs[i] + start, session );
				}
				result += locs.length;
			}
			return result;
		}
		else {
			return 0;
		}
	
private intbindPositionalParameters(java.sql.PreparedStatement st, org.hibernate.engine.QueryParameters queryParameters, int start, org.hibernate.engine.SessionImplementor session)
Bind positional parameter values to the PreparedStatement (these are parameters specified by a JDBC-style ?).


		final Object[] values = queryParameters
				.getFilteredPositionalParameterValues();
		final Type[] types = queryParameters
				.getFilteredPositionalParameterTypes();
		int span = 0;
		for (int i = 0; i < values.length; i++) {
			types[i].nullSafeSet( st, values[i], start + span, session );
			span += types[i].getColumnSpan( session.getFactory() );
		}
		return span;
	
protected voidcoordinateSharedCacheCleanup(org.hibernate.engine.SessionImplementor session)

		BulkOperationCleanupAction action = new BulkOperationCleanupAction( session, getCustomQuery().getQuerySpaces() );

		action.init();

		if ( session.isEventSource() ) {
			( ( EventSource ) session ).getActionQueue().addAction( action );
		}
	
public org.hibernate.loader.custom.sql.SQLCustomQuerygetCustomQuery()

		return customQuery;
	
private int[]getNamedParameterLocs(java.lang.String name)

		Object loc = customQuery.getNamedParameterBindPoints().get( name );
		if ( loc == null ) {
			throw new QueryException(
					"Named parameter does not appear in Query: " + name,
					customQuery.getSQL() );
		}
		if ( loc instanceof Integer ) {
			return new int[] { ((Integer) loc ).intValue() };
		}
		else {
			return ArrayHelper.toIntArray( (List) loc );
		}
	
public java.lang.StringgetSourceQuery()

		return sourceQuery;
	
public intperformExecuteUpdate(org.hibernate.engine.QueryParameters queryParameters, org.hibernate.engine.SessionImplementor session)

		
		coordinateSharedCacheCleanup( session );
		
		if(queryParameters.isCallable()) {
			throw new IllegalArgumentException("callable not yet supported for native queries");
		}
		
		int result = 0;
		PreparedStatement ps;
		try {
			queryParameters.processFilters( this.customQuery.getSQL(),
					session );
			String sql = queryParameters.getFilteredSQL();

			ps = session.getBatcher().prepareStatement( sql );

			try {
				int col = 1;
				col += bindPositionalParameters( ps, queryParameters, col,
						session );
				col += bindNamedParameters( ps, queryParameters
						.getNamedParameters(), col, session );
				result = ps.executeUpdate();
			}
			finally {
				if ( ps != null ) {
					session.getBatcher().closeStatement( ps );
				}				
			}			
		}
		catch (SQLException sqle) {
			throw JDBCExceptionHelper.convert( session.getFactory()
					.getSQLExceptionConverter(), sqle,
					"could not execute native bulk manipulation query", this.sourceQuery );
		}

		return result;