HHH-13823 Introduce an indirection when instantiating Insert/Update/Delete

This allows hibernate-rx to intervene in the rendering of bind variables
and use $n instead of ?
This commit is contained in:
gavinking 2020-01-19 18:00:41 +01:00 committed by Sanne Grinovero
parent 96faae93b1
commit 12a8508e66
4 changed files with 58 additions and 37 deletions

View File

@ -81,6 +81,9 @@ import org.hibernate.persister.walking.spi.CompositionDefinition;
import org.hibernate.persister.walking.spi.EntityDefinition;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.sql.Alias;
import org.hibernate.sql.Insert;
import org.hibernate.sql.Update;
import org.hibernate.sql.Delete;
import org.hibernate.sql.SelectFragment;
import org.hibernate.sql.SimpleSelect;
import org.hibernate.sql.Template;
@ -2274,4 +2277,16 @@ public abstract class AbstractCollectionPersister
}
};
}
protected Insert createInsert() {
return new Insert( getFactory().getJdbcServices().getDialect() );
}
protected Update createUpdate() {
return new Update( getFactory().getJdbcServices().getDialect() );
}
protected Delete createDelete() {
return new Delete();
}
}

View File

@ -64,8 +64,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateDeleteString() {
final Delete delete = new Delete()
.setTableName( qualifiedTableName )
final Delete delete = createDelete().setTableName( qualifiedTableName )
.addPrimaryKeyColumns( keyColumnNames );
if ( hasWhere ) {
@ -84,8 +83,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateInsertRowString() {
final Insert insert = new Insert( getDialect() )
.setTableName( qualifiedTableName )
final Insert insert = createInsert().setTableName( qualifiedTableName )
.addColumns( keyColumnNames );
if ( hasIdentifier ) {
@ -112,8 +110,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateUpdateRowString() {
final Update update = new Update( getDialect() )
.setTableName( qualifiedTableName );
final Update update = createUpdate().setTableName( qualifiedTableName );
//if ( !elementIsFormula ) {
update.addColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters );
@ -147,7 +144,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateDeleteRowString() {
final Delete delete = new Delete().setTableName( qualifiedTableName );
final Delete delete = createDelete().setTableName( qualifiedTableName );
if ( hasIdentifier ) {
delete.addPrimaryKeyColumns( new String[] {identifierColumnName} );

View File

@ -78,10 +78,8 @@ public class OneToManyPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateDeleteString() {
final Update update = new Update( getDialect() )
.setTableName( qualifiedTableName )
.addColumns( keyColumnNames, "null" )
.addPrimaryKeyColumns( keyColumnNames );
final Update update = createUpdate().setTableName( qualifiedTableName )
.addColumns( keyColumnNames, "null" );
if ( hasIndex && !indexContainsFormula ) {
for ( int i = 0 ; i < indexColumnNames.length ; i++ ) {
@ -91,6 +89,8 @@ public class OneToManyPersister extends AbstractCollectionPersister {
}
}
update.addPrimaryKeyColumns( keyColumnNames );
if ( hasWhere ) {
update.setWhere( sqlWhereString );
}
@ -107,8 +107,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateInsertRowString() {
final Update update = new Update( getDialect() )
.setTableName( qualifiedTableName )
final Update update = createUpdate().setTableName( qualifiedTableName )
.addColumns( keyColumnNames );
if ( hasIndex && !indexContainsFormula ) {
@ -134,11 +133,8 @@ public class OneToManyPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateUpdateRowString() {
final Update update = new Update( getDialect() ).setTableName( qualifiedTableName );
update.addPrimaryKeyColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters );
if ( hasIdentifier ) {
update.addPrimaryKeyColumns( new String[] {identifierColumnName} );
}
final Update update = createUpdate().setTableName( qualifiedTableName );
if ( hasIndex && !indexContainsFormula ) {
for ( int i = 0 ; i < indexColumnNames.length ; i++ ) {
if ( indexColumnIsSettable[i] ) {
@ -147,6 +143,12 @@ public class OneToManyPersister extends AbstractCollectionPersister {
}
}
update.addPrimaryKeyColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters );
if ( hasIdentifier ) {
update.addPrimaryKeyColumns( new String[] {identifierColumnName} );
}
return update.toStatementString();
}
@ -156,8 +158,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
*/
@Override
protected String generateDeleteRowString() {
final Update update = new Update( getDialect() )
.setTableName( qualifiedTableName )
final Update update = createUpdate().setTableName( qualifiedTableName )
.addColumns( keyColumnNames, "null" );
if ( hasIndex && !indexContainsFormula ) {

View File

@ -1894,8 +1894,7 @@ public abstract class AbstractEntityPersister
}
private String generateVersionIncrementUpdateString() {
Update update = new Update( getFactory().getDialect() );
update.setTableName( getTableName( 0 ) );
Update update = createUpdate().setTableName( getTableName( 0 ) );
if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
update.setComment( "forced version increment" );
}
@ -2612,15 +2611,7 @@ public abstract class AbstractEntityPersister
final Object[] oldFields,
final boolean useRowId) {
Update update = new Update( getFactory().getDialect() ).setTableName( getTableName( j ) );
// select the correct row by either pk or rowid
if ( useRowId ) {
update.addPrimaryKeyColumns( new String[] {rowIdName} ); //TODO: eventually, rowIdName[j]
}
else {
update.addPrimaryKeyColumns( getKeyColumns( j ) );
}
Update update = createUpdate().setTableName( getTableName( j ) );
boolean hasColumns = false;
for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
@ -2649,6 +2640,14 @@ public abstract class AbstractEntityPersister
}
}
// select the correct row by either pk or rowid
if ( useRowId ) {
update.addPrimaryKeyColumns( new String[] {rowIdName} ); //TODO: eventually, rowIdName[j]
}
else {
update.addPrimaryKeyColumns( getKeyColumns( j ) );
}
if ( j == 0 && isVersioned() && entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.VERSION ) {
// this is the root (versioned) table, and we are using version-based
// optimistic locking; if we are not updating the version, also don't
@ -2721,8 +2720,7 @@ public abstract class AbstractEntityPersister
// todo : remove the identityInsert param and variations;
// identity-insert strings are now generated from generateIdentityInsertString()
Insert insert = new Insert( getFactory().getDialect() )
.setTableName( getTableName( j ) );
Insert insert = createInsert().setTableName( getTableName( j ) );
// add normal properties
for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
@ -2883,8 +2881,7 @@ public abstract class AbstractEntityPersister
* Generate the SQL that deletes a row by id (and version)
*/
public String generateDeleteString(int j) {
final Delete delete = new Delete()
.setTableName( getTableName( j ) )
final Delete delete = createDelete().setTableName( getTableName( j ) )
.addPrimaryKeyColumns( getKeyColumns( j ) );
if ( j == 0 ) {
delete.setVersionColumnName( getVersionColumnName() );
@ -3824,8 +3821,7 @@ public abstract class AbstractEntityPersister
int span = getTableSpan();
String[] deleteStrings = new String[span];
for ( int j = span - 1; j >= 0; j-- ) {
Delete delete = new Delete()
.setTableName( getTableName( j ) )
Delete delete = createDelete().setTableName( getTableName( j ) )
.addPrimaryKeyColumns( getKeyColumns( j ) );
if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
delete.setComment( "delete " + getEntityName() + " [" + j + "]" );
@ -6001,4 +5997,16 @@ public abstract class AbstractEntityPersister
return substituteBrackets( getOriginalQueryString() );
}
}
protected Insert createInsert() {
return new Insert( getFactory().getJdbcServices().getDialect() );
}
protected Update createUpdate() {
return new Update( getFactory().getJdbcServices().getDialect() );
}
protected Delete createDelete() {
return new Delete();
}
}