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.persister.walking.spi.EntityDefinition;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.sql.Alias; 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.SelectFragment;
import org.hibernate.sql.SimpleSelect; import org.hibernate.sql.SimpleSelect;
import org.hibernate.sql.Template; 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 @Override
protected String generateDeleteString() { protected String generateDeleteString() {
final Delete delete = new Delete() final Delete delete = createDelete().setTableName( qualifiedTableName )
.setTableName( qualifiedTableName )
.addPrimaryKeyColumns( keyColumnNames ); .addPrimaryKeyColumns( keyColumnNames );
if ( hasWhere ) { if ( hasWhere ) {
@ -84,8 +83,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/ */
@Override @Override
protected String generateInsertRowString() { protected String generateInsertRowString() {
final Insert insert = new Insert( getDialect() ) final Insert insert = createInsert().setTableName( qualifiedTableName )
.setTableName( qualifiedTableName )
.addColumns( keyColumnNames ); .addColumns( keyColumnNames );
if ( hasIdentifier ) { if ( hasIdentifier ) {
@ -112,8 +110,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/ */
@Override @Override
protected String generateUpdateRowString() { protected String generateUpdateRowString() {
final Update update = new Update( getDialect() ) final Update update = createUpdate().setTableName( qualifiedTableName );
.setTableName( qualifiedTableName );
//if ( !elementIsFormula ) { //if ( !elementIsFormula ) {
update.addColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters ); update.addColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters );
@ -147,7 +144,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
*/ */
@Override @Override
protected String generateDeleteRowString() { protected String generateDeleteRowString() {
final Delete delete = new Delete().setTableName( qualifiedTableName ); final Delete delete = createDelete().setTableName( qualifiedTableName );
if ( hasIdentifier ) { if ( hasIdentifier ) {
delete.addPrimaryKeyColumns( new String[] {identifierColumnName} ); delete.addPrimaryKeyColumns( new String[] {identifierColumnName} );

View File

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

View File

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