Search in sources :

Example 1 with AssignmentSpecification

use of org.hibernate.hql.internal.ast.tree.AssignmentSpecification in project hibernate-orm by hibernate.

the class HqlSqlWalker method evaluateAssignment.

private void evaluateAssignment(AST eq, Queryable persister, int targetIndex) {
    if (persister.isMultiTable()) {
        // no need to even collect this information if the persister is considered multi-table
        AssignmentSpecification specification = new AssignmentSpecification(eq, persister);
        if (targetIndex >= 0) {
            assignmentSpecifications.add(targetIndex, specification);
        } else {
            assignmentSpecifications.add(specification);
        }
        numberOfParametersInSetClause += specification.getParameters().length;
    }
}
Also used : AssignmentSpecification(org.hibernate.hql.internal.ast.tree.AssignmentSpecification)

Example 2 with AssignmentSpecification

use of org.hibernate.hql.internal.ast.tree.AssignmentSpecification in project hibernate-orm by hibernate.

the class AbstractInlineIdsUpdateHandlerImpl method execute.

@Override
public int execute(SharedSessionContractImplementor session, QueryParameters queryParameters) {
    IdsClauseBuilder values = prepareInlineStatement(session, queryParameters);
    if (!values.getIds().isEmpty()) {
        String[] tableNames = getTargetedQueryable().getConstraintOrderedTableNameClosure();
        String[][] columnNames = getTargetedQueryable().getContraintOrderedTableKeyColumnClosure();
        String idSubselect = values.toStatement();
        assignmentParameterSpecifications = new ParameterSpecification[tableNames.length][];
        for (int tableIndex = 0; tableIndex < tableNames.length; tableIndex++) {
            boolean affected = false;
            final List<ParameterSpecification> parameterList = new ArrayList<>();
            Update update = generateUpdate(tableNames[tableIndex], columnNames[tableIndex], idSubselect, "bulk update");
            final List<AssignmentSpecification> assignmentSpecifications = walker().getAssignmentSpecifications();
            for (AssignmentSpecification assignmentSpecification : assignmentSpecifications) {
                if (assignmentSpecification.affectsTable(tableNames[tableIndex])) {
                    affected = true;
                    update.appendAssignmentFragment(assignmentSpecification.getSqlAssignmentFragment());
                    if (assignmentSpecification.getParameters() != null) {
                        Collections.addAll(parameterList, assignmentSpecification.getParameters());
                    }
                }
            }
            if (affected) {
                updates.put(tableIndex, update.toStatementString());
                assignmentParameterSpecifications[tableIndex] = parameterList.toArray(new ParameterSpecification[parameterList.size()]);
            }
        }
        // Start performing the updates
        for (Map.Entry<Integer, String> updateEntry : updates.entrySet()) {
            int i = updateEntry.getKey();
            String update = updateEntry.getValue();
            if (update == null) {
                continue;
            }
            try {
                try (PreparedStatement ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(update, false)) {
                    // jdbc params are 1-based
                    int position = 1;
                    if (assignmentParameterSpecifications[i] != null) {
                        for (int x = 0; x < assignmentParameterSpecifications[i].length; x++) {
                            position += assignmentParameterSpecifications[i][x].bind(ps, queryParameters, session, position);
                        }
                    }
                    session.getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
                }
            } catch (SQLException e) {
                throw convert(e, "error performing bulk update", update);
            }
        }
    }
    return values.getIds().size();
}
Also used : ParameterSpecification(org.hibernate.param.ParameterSpecification) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Update(org.hibernate.sql.Update) AssignmentSpecification(org.hibernate.hql.internal.ast.tree.AssignmentSpecification) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

AssignmentSpecification (org.hibernate.hql.internal.ast.tree.AssignmentSpecification)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ParameterSpecification (org.hibernate.param.ParameterSpecification)1 Update (org.hibernate.sql.Update)1