Search in sources :

Example 1 with ParameterBinder

use of org.hibernate.param.ParameterBinder in project hibernate-orm by hibernate.

the class NativeSQLQueryPlan method performExecuteUpdate.

/**
 * 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;
}
Also used : ParameterBinder(org.hibernate.param.ParameterBinder) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) RowSelection(org.hibernate.engine.spi.RowSelection)

Example 2 with ParameterBinder

use of org.hibernate.param.ParameterBinder in project hibernate-orm by hibernate.

the class CustomLoader method bindParameterValues.

@Override
protected int bindParameterValues(PreparedStatement statement, QueryParameters queryParameters, int startIndex, SharedSessionContractImplementor session) throws SQLException {
    final Serializable optionalId = queryParameters.getOptionalId();
    if (optionalId != null) {
        paramValueBinders.get(0).bind(statement, queryParameters, session, startIndex);
        return session.getFactory().getMetamodel().entityPersister(queryParameters.getOptionalEntityName()).getIdentifierType().getColumnSpan(session.getFactory());
    }
    int span = 0;
    for (ParameterBinder paramValueBinder : paramValueBinders) {
        span += paramValueBinder.bind(statement, queryParameters, session, startIndex + span);
    }
    return span;
}
Also used : Serializable(java.io.Serializable) ParameterBinder(org.hibernate.param.ParameterBinder)

Example 3 with ParameterBinder

use of org.hibernate.param.ParameterBinder in project hibernate-orm by hibernate.

the class OutputsImpl method buildSpecializedCustomLoader.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Hooks into Hibernate's Loader hierarchy for ResultSet -> Object mapping
private static CustomLoaderExtension buildSpecializedCustomLoader(final ResultContext context) {
    // might be better to just manually construct the Return(s).. SQLQueryReturnProcessor does a lot of
    // work that is really unnecessary here.
    final SQLQueryReturnProcessor processor = new SQLQueryReturnProcessor(context.getQueryReturns(), context.getSession().getFactory());
    processor.process();
    final List<org.hibernate.loader.custom.Return> customReturns = processor.generateCallableReturns();
    CustomQuery customQuery = new CustomQuery() {

        @Override
        public String getSQL() {
            return context.getSql();
        }

        @Override
        public Set<String> getQuerySpaces() {
            return context.getSynchronizedQuerySpaces();
        }

        @Override
        public List<ParameterBinder> getParameterValueBinders() {
            // no parameters in terms of embedded in the SQL string
            return Collections.emptyList();
        }

        @Override
        public List<org.hibernate.loader.custom.Return> getCustomQueryReturns() {
            return customReturns;
        }
    };
    return new CustomLoaderExtension(customQuery, context.getQueryParameters(), context.getSession());
}
Also used : Return(org.hibernate.loader.custom.Return) RootReturn(org.hibernate.loader.custom.RootReturn) ParameterBinder(org.hibernate.param.ParameterBinder) CustomQuery(org.hibernate.loader.custom.CustomQuery) SQLQueryReturnProcessor(org.hibernate.loader.custom.sql.SQLQueryReturnProcessor)

Aggregations

ParameterBinder (org.hibernate.param.ParameterBinder)3 Serializable (java.io.Serializable)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 RowSelection (org.hibernate.engine.spi.RowSelection)1 CustomQuery (org.hibernate.loader.custom.CustomQuery)1 Return (org.hibernate.loader.custom.Return)1 RootReturn (org.hibernate.loader.custom.RootReturn)1 SQLQueryReturnProcessor (org.hibernate.loader.custom.sql.SQLQueryReturnProcessor)1