Java Code Examples for org.hibernate.engine.spi.QueryParameters#isCallable()

The following examples show how to use org.hibernate.engine.spi.QueryParameters#isCallable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactiveNativeSQLQueryPlan.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
public CompletionStage<Integer> performExecuteReactiveUpdate(QueryParameters queryParameters, ReactiveSession session) {
		SharedSessionContractImplementor sessionContract = session.getSharedContract();

		coordinateSharedCacheCleanup(sessionContract);

		if ( queryParameters.isCallable() ) {
			throw new IllegalArgumentException("callable not yet supported for native queries");
		}

		queryParameters.processFilters( customQuery.getSQL(), sessionContract );

		Object[] params = PreparedStatementAdaptor.bind( statement -> {
			int col = 1;
			for ( ParameterBinder binder : customQuery.getParameterValueBinders() ) {
				col += binder.bind( statement, queryParameters, sessionContract, col );
			}
		} );

//		RowSelection selection = queryParameters.getRowSelection();
//		if ( selection != null && selection.getTimeout() != null ) {
//			statement.setQueryTimeout( selection.getTimeout() );
//		}

		boolean commentsEnabled = session.getFactory().getSessionFactoryOptions().isCommentsEnabled();
		String sql = session.getDialect()
				.addSqlHintOrComment( queryParameters.getFilteredSQL(), queryParameters, commentsEnabled );

		return session.getReactiveConnection().update( sql, params );
	}
 
Example 2
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected SqlStatementWrapper executeQueryStatement(
		String sqlStatement,
		QueryParameters queryParameters,
		boolean scroll,
		List<AfterLoadAction> afterLoadActions,
		SharedSessionContractImplementor session) throws SQLException {

	// Processing query filters.
	queryParameters.processFilters( sqlStatement, session );

	// Applying LIMIT clause.
	final LimitHandler limitHandler = getLimitHandler(
			queryParameters.getRowSelection()
	);
	String sql = limitHandler.processSql( queryParameters.getFilteredSQL(), queryParameters.getRowSelection() );

	// Adding locks and comments.
	sql = preprocessSQL( sql, queryParameters, getFactory(), afterLoadActions );

	final PreparedStatement st = prepareQueryStatement( sql, queryParameters, limitHandler, scroll, session );

	final ResultSet rs;

	if( queryParameters.isCallable() && isTypeOf( st, CallableStatement.class ) ) {
		final CallableStatement cs = st.unwrap( CallableStatement.class );

		rs = getResultSet(
				cs,
				queryParameters.getRowSelection(),
				limitHandler,
				queryParameters.hasAutoDiscoverScalarTypes(),
				session
		);
	}
	else {
		rs = getResultSet(
			st,
			queryParameters.getRowSelection(),
			limitHandler,
			queryParameters.hasAutoDiscoverScalarTypes(),
			session
		);
	}

	return new SqlStatementWrapper(
		st,
		rs
	);

}
 
Example 3
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
 * Bind JDBC-style <tt>?</tt> parameters, named parameters, and
 * limit parameters.
 */
protected final PreparedStatement prepareQueryStatement(
		String sql,
		final QueryParameters queryParameters,
		final LimitHandler limitHandler,
		final boolean scroll,
		final SharedSessionContractImplementor session) throws SQLException, HibernateException {
	final Dialect dialect = getFactory().getDialect();
	final RowSelection selection = queryParameters.getRowSelection();
	final boolean useLimit = LimitHelper.useLimit( limitHandler, selection );
	final boolean hasFirstRow = LimitHelper.hasFirstRow( selection );
	final boolean useLimitOffset = hasFirstRow && useLimit && limitHandler.supportsLimitOffset();
	final boolean callable = queryParameters.isCallable();
	final ScrollMode scrollMode = getScrollMode( scroll, hasFirstRow, useLimitOffset, queryParameters );

	PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareQueryStatement(
			sql,
			callable,
			scrollMode
	);

	try {

		int col = 1;
		//TODO: can we limit stored procedures ?!
		col += limitHandler.bindLimitParametersAtStartOfQuery( selection, st, col );

		if ( callable ) {
			col = dialect.registerResultSetOutParameter( (CallableStatement) st, col );
		}

		col += bindParameterValues( st, queryParameters, col, session );

		col += limitHandler.bindLimitParametersAtEndOfQuery( selection, st, col );

		limitHandler.setMaxRows( selection, st );

		if ( selection != null ) {
			if ( selection.getTimeout() != null ) {
				st.setQueryTimeout( selection.getTimeout() );
			}
			if ( selection.getFetchSize() != null ) {
				st.setFetchSize( selection.getFetchSize() );
			}
		}

		// handle lock timeout...
		LockOptions lockOptions = queryParameters.getLockOptions();
		if ( lockOptions != null ) {
			if ( lockOptions.getTimeOut() != LockOptions.WAIT_FOREVER ) {
				if ( !dialect.supportsLockTimeouts() ) {
					if ( LOG.isDebugEnabled() ) {
						LOG.debugf(
								"Lock timeout [%s] requested but dialect reported to not support lock timeouts",
								lockOptions.getTimeOut()
						);
					}
				}
				else if ( dialect.isLockTimeoutParameterized() ) {
					st.setInt( col++, lockOptions.getTimeOut() );
				}
			}
		}

		if ( LOG.isTraceEnabled() ) {
			LOG.tracev( "Bound [{0}] parameters total", col );
		}
	}
	catch (SQLException | HibernateException e) {
		session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st );
		session.getJdbcCoordinator().afterStatementExecution();
		throw e;
	}

	return st;
}
 
Example 4
Source File: QueryLoader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Iterator iterate(
			QueryParameters queryParameters,
			EventSource session) throws HibernateException {
		checkQuery( queryParameters );
		final boolean stats = session.getFactory().getStatistics().isStatisticsEnabled();
		long startTime = 0;
		if ( stats ) {
			startTime = System.nanoTime();
		}

		try {
			if ( queryParameters.isCallable() ) {
				throw new QueryException( "iterate() not supported for callable statements" );
			}
			final SqlStatementWrapper wrapper = executeQueryStatement(
					queryParameters,
					false,
					Collections.emptyList(),
					session
			);
			final ResultSet rs = wrapper.getResultSet();
			final PreparedStatement st = (PreparedStatement) wrapper.getStatement();
			final Iterator result = new IteratorImpl(
					rs,
					st,
					session,
					queryParameters.isReadOnly( session ),
					queryReturnTypes,
					queryTranslator.getColumnNames(),
					buildHolderInstantiator( queryParameters.getResultTransformer() )
			);

			if ( stats ) {
				final long endTime = System.nanoTime();
				final long milliseconds = TimeUnit.MILLISECONDS.convert( endTime - startTime, TimeUnit.NANOSECONDS );
				session.getFactory().getStatistics().queryExecuted(
//						"HQL: " + queryTranslator.getQueryString(),
						getQueryIdentifier(),
						0,
						milliseconds
				);
			}

			return result;

		}
		catch (SQLException sqle) {
			throw session.getJdbcServices().getSqlExceptionHelper().convert(
					sqle,
					"could not execute query using iterate",
					getSQLString()
			);
		}

	}
 
Example 5
Source File: AbstractLoadPlanBasedLoader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
 * Bind JDBC-style <tt>?</tt> parameters, named parameters, and
 * limit parameters.
 */
protected final PreparedStatement prepareQueryStatement(
		final String sql,
		final QueryParameters queryParameters,
		final LimitHandler limitHandler,
		final boolean scroll,
		final SharedSessionContractImplementor session) throws SQLException, HibernateException {
	final Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
	final RowSelection selection = queryParameters.getRowSelection();
	final boolean useLimit = LimitHelper.useLimit( limitHandler, selection );
	final boolean hasFirstRow = LimitHelper.hasFirstRow( selection );
	final boolean useLimitOffset = hasFirstRow && useLimit && limitHandler.supportsLimitOffset();
	final boolean callable = queryParameters.isCallable();
	final ScrollMode scrollMode = getScrollMode( scroll, hasFirstRow, useLimitOffset, queryParameters );

	final PreparedStatement st = session.getJdbcCoordinator()
			.getStatementPreparer().prepareQueryStatement( sql, callable, scrollMode );

	try {

		int col = 1;
		//TODO: can we limit stored procedures ?!
		col += limitHandler.bindLimitParametersAtStartOfQuery( selection, st, col );

		if (callable) {
			col = dialect.registerResultSetOutParameter( (CallableStatement)st, col );
		}

		col += bindParameterValues( st, queryParameters, col, session );

		col += limitHandler.bindLimitParametersAtEndOfQuery( selection, st, col );

		limitHandler.setMaxRows( selection, st );

		if ( selection != null ) {
			if ( selection.getTimeout() != null ) {
				st.setQueryTimeout( selection.getTimeout() );
			}
			if ( selection.getFetchSize() != null ) {
				st.setFetchSize( selection.getFetchSize() );
			}
		}

		// handle lock timeout...
		final LockOptions lockOptions = queryParameters.getLockOptions();
		if ( lockOptions != null ) {
			if ( lockOptions.getTimeOut() != LockOptions.WAIT_FOREVER ) {
				if ( !dialect.supportsLockTimeouts() ) {
					if ( log.isDebugEnabled() ) {
						log.debugf(
								"Lock timeout [%s] requested but dialect reported to not support lock timeouts",
								lockOptions.getTimeOut()
						);
					}
				}
				else if ( dialect.isLockTimeoutParameterized() ) {
					st.setInt( col++, lockOptions.getTimeOut() );
				}
			}
		}

		if ( log.isTraceEnabled() ) {
			log.tracev( "Bound [{0}] parameters total", col );
		}
	}
	catch ( SQLException sqle ) {
		session.getJdbcCoordinator().getResourceRegistry().release( st );
		session.getJdbcCoordinator().afterStatementExecution();
		throw sqle;
	}
	catch ( HibernateException he ) {
		session.getJdbcCoordinator().getResourceRegistry().release( st );
		session.getJdbcCoordinator().afterStatementExecution();
		throw he;
	}

	return st;
}
 
Example 6
Source File: NativeSQLQueryPlan.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Performs the execute query
 *
 * @param queryParameters The query parameters
 * @param session The session
 *
 * @return The number of affected rows as returned by the JDBC driver
 *
 * @throws HibernateException Indicates a problem performing the query execution
 */
public int performExecuteUpdate(
		QueryParameters queryParameters,
		SharedSessionContractImplementor session) throws HibernateException {

	coordinateSharedCacheCleanup( session );

	if ( queryParameters.isCallable() ) {
		throw new IllegalArgumentException("callable not yet supported for native queries");
	}

	int result = 0;
	PreparedStatement ps;
	RowSelection selection = queryParameters.getRowSelection();
	try {
		queryParameters.processFilters( this.customQuery.getSQL(), session );
		final String sql = session.getJdbcServices().getDialect()
				.addSqlHintOrComment(
					queryParameters.getFilteredSQL(),
					queryParameters,
					session.getFactory().getSessionFactoryOptions().isCommentsEnabled()
				);

		ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql, false );

		try {
			int col = 1;
			for ( ParameterBinder binder : this.customQuery.getParameterValueBinders() ) {
				col += binder.bind( ps, queryParameters, session, col );
			}
			if ( selection != null && selection.getTimeout() != null ) {
				ps.setQueryTimeout( selection.getTimeout() );
			}
			result = session.getJdbcCoordinator().getResultSetReturn().executeUpdate( ps );
		}
		finally {
			if ( ps != null ) {
				session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( ps );
				session.getJdbcCoordinator().afterStatementExecution();
			}
		}
	}
	catch (SQLException sqle) {
		throw session.getFactory().getSQLExceptionHelper().convert(
				sqle,
				"could not execute native bulk manipulation query",
				this.sourceQuery
		);
	}

	return result;
}