use of org.hibernate.sql.Update in project hibernate-orm by hibernate.
the class UpdateLockingStrategy method generateLockString.
protected String generateLockString() {
final SessionFactoryImplementor factory = lockable.getFactory();
final Update update = new Update(factory.getDialect());
update.setTableName(lockable.getRootTableName());
update.addPrimaryKeyColumns(lockable.getRootTableIdentifierColumnNames());
update.setVersionColumnName(lockable.getVersionColumnName());
update.addColumn(lockable.getVersionColumnName());
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
update.setComment(lockMode + " lock " + lockable.getEntityName());
}
return update.toStatementString();
}
use of org.hibernate.sql.Update 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();
}
use of org.hibernate.sql.Update in project hibernate-orm by hibernate.
the class AbstractEntityPersister method generateVersionIncrementUpdateString.
private String generateVersionIncrementUpdateString() {
Update update = new Update(getFactory().getDialect());
update.setTableName(getTableName(0));
if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
update.setComment("forced version increment");
}
update.addColumn(getVersionColumnName());
update.addPrimaryKeyColumns(getIdentifierColumnNames());
update.setVersionColumnName(getVersionColumnName());
return update.toStatementString();
}
use of org.hibernate.sql.Update in project hibernate-orm by hibernate.
the class OneToManyPersister method generateDeleteRowString.
/**
* Generate the SQL UPDATE that updates a particular row's foreign
* key to null
*/
@Override
protected String generateDeleteRowString() {
final Update update = new Update(getDialect()).setTableName(qualifiedTableName).addColumns(keyColumnNames, "null");
if (hasIndex && !indexContainsFormula) {
for (int i = 0; i < indexColumnNames.length; i++) {
if (indexColumnIsSettable[i]) {
update.addColumn(indexColumnNames[i], "null");
}
}
}
if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
update.setComment("delete one-to-many row " + getRole());
}
// use a combination of foreign key columns and pk columns, since
// the ordering of removal and addition is not guaranteed when
// a child moves from one parent to another
String[] rowSelectColumnNames = ArrayHelper.join(keyColumnNames, elementColumnNames);
return update.addPrimaryKeyColumns(rowSelectColumnNames).toStatementString();
}
use of org.hibernate.sql.Update in project hibernate-orm by hibernate.
the class OneToManyPersister method generateUpdateRowString.
/**
* Generate the SQL UPDATE that inserts a collection index
*/
@Override
protected String generateUpdateRowString() {
final Update update = new Update(getDialect()).setTableName(qualifiedTableName);
update.addPrimaryKeyColumns(elementColumnNames, elementColumnIsSettable, elementColumnWriters);
if (hasIdentifier) {
update.addPrimaryKeyColumns(new String[] { identifierColumnName });
}
if (hasIndex && !indexContainsFormula) {
for (int i = 0; i < indexColumnNames.length; i++) {
if (indexColumnIsSettable[i]) {
update.addColumn(indexColumnNames[i]);
}
}
}
return update.toStatementString();
}
Aggregations