HHH-9948 - SequenceStyleGenerators uses potentially incorrect name for table/sequence in DML statements

This commit is contained in:
Steve Ebersole 2015-07-18 17:22:57 -05:00
parent d7b1afeed7
commit 7bc37fd2f3
2 changed files with 67 additions and 46 deletions

View File

@ -35,12 +35,14 @@ public class SequenceStructure implements DatabaseStructure {
SequenceStructure.class.getName() SequenceStructure.class.getName()
); );
private QualifiedName qualifiedSequenceName; private final QualifiedName logicalQualifiedSequenceName;
private final String sequenceName;
private final int initialValue; private final int initialValue;
private final int incrementSize; private final int incrementSize;
private final Class numberType; private final Class numberType;
private final String sql;
private String sequenceName;
private String sql;
private boolean applyIncrementSizeToSourceValues; private boolean applyIncrementSizeToSourceValues;
private int accessCounter; private int accessCounter;
@ -50,15 +52,11 @@ public class SequenceStructure implements DatabaseStructure {
int initialValue, int initialValue,
int incrementSize, int incrementSize,
Class numberType) { Class numberType) {
this.qualifiedSequenceName = qualifiedSequenceName; this.logicalQualifiedSequenceName = qualifiedSequenceName;
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
qualifiedSequenceName,
jdbcEnvironment.getDialect()
);
this.initialValue = initialValue; this.initialValue = initialValue;
this.incrementSize = incrementSize; this.incrementSize = incrementSize;
this.numberType = numberType; this.numberType = numberType;
sql = jdbcEnvironment.getDialect().getSequenceNextValString( sequenceName );
} }
@Override @Override
@ -139,17 +137,28 @@ public class SequenceStructure implements DatabaseStructure {
@Override @Override
public void registerExportables(Database database) { public void registerExportables(Database database) {
final int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1; final int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1;
final Schema schema = database.locateSchema( final Schema schema = database.locateSchema(
qualifiedSequenceName.getCatalogName(), logicalQualifiedSequenceName.getCatalogName(),
qualifiedSequenceName.getSchemaName() logicalQualifiedSequenceName.getSchemaName()
); );
Sequence sequence = schema.locateSequence( qualifiedSequenceName.getObjectName() ); Sequence sequence = schema.locateSequence( logicalQualifiedSequenceName.getObjectName() );
if ( sequence != null ) { if ( sequence != null ) {
sequence.validate( initialValue, sourceIncrementSize ); sequence.validate( initialValue, sourceIncrementSize );
} }
else { else {
schema.createSequence( qualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize ); sequence = schema.createSequence( logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize );
} }
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
sequence.getName(),
dialect
);
this.sql = jdbcEnvironment.getDialect().getSequenceNextValString( sequenceName );
} }
@Override @Override

View File

@ -48,16 +48,17 @@ public class TableStructure implements DatabaseStructure {
TableStructure.class.getName() TableStructure.class.getName()
); );
private final QualifiedName qualifiedTableName; private final QualifiedName logicalQualifiedTableName;
private final Identifier logicalValueColumnNameIdentifier;
private final String tableNameText;
private final String valueColumnNameText;
private final int initialValue; private final int initialValue;
private final int incrementSize; private final int incrementSize;
private final Class numberType; private final Class numberType;
private final String selectQuery;
private final String updateQuery; private String tableNameText;
private String valueColumnNameText;
private String selectQuery;
private String updateQuery;
private boolean applyIncrementSizeToSourceValues; private boolean applyIncrementSizeToSourceValues;
private int accessCounter; private int accessCounter;
@ -69,27 +70,12 @@ public class TableStructure implements DatabaseStructure {
int initialValue, int initialValue,
int incrementSize, int incrementSize,
Class numberType) { Class numberType) {
final Dialect dialect = jdbcEnvironment.getDialect(); this.logicalQualifiedTableName = qualifiedTableName;
this.logicalValueColumnNameIdentifier = valueColumnNameIdentifier;
this.qualifiedTableName = qualifiedTableName;
this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
qualifiedTableName,
dialect
);
this.valueColumnNameText = valueColumnNameIdentifier.render( jdbcEnvironment.getDialect() );
this.initialValue = initialValue; this.initialValue = initialValue;
this.incrementSize = incrementSize; this.incrementSize = incrementSize;
this.numberType = numberType; this.numberType = numberType;
selectQuery = "select " + valueColumnNameText + " as id_val" +
" from " + dialect.appendLockHint( LockMode.PESSIMISTIC_WRITE, tableNameText ) +
dialect.getForUpdateString();
updateQuery = "update " + tableNameText +
" set " + valueColumnNameText + "= ?" +
" where " + valueColumnNameText + "=?";
} }
@Override @Override
@ -138,7 +124,12 @@ public class TableStructure implements DatabaseStructure {
final IntegralDataTypeHolder value = makeValue(); final IntegralDataTypeHolder value = makeValue();
int rows; int rows;
do { do {
final PreparedStatement selectStatement = prepareStatement( connection, selectQuery, statementLogger, statsCollector ); final PreparedStatement selectStatement = prepareStatement(
connection,
selectQuery,
statementLogger,
statsCollector
);
try { try {
final ResultSet selectRS = executeQuery( selectStatement, statsCollector ); final ResultSet selectRS = executeQuery( selectStatement, statsCollector );
if ( !selectRS.next() ) { if ( !selectRS.next() ) {
@ -158,7 +149,12 @@ public class TableStructure implements DatabaseStructure {
} }
final PreparedStatement updatePS = prepareStatement( connection, updateQuery, statementLogger, statsCollector ); final PreparedStatement updatePS = prepareStatement(
connection,
updateQuery,
statementLogger,
statsCollector
);
try { try {
final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1; final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
final IntegralDataTypeHolder updateValue = value.copy().add( increment ); final IntegralDataTypeHolder updateValue = value.copy().add( increment );
@ -247,18 +243,34 @@ public class TableStructure implements DatabaseStructure {
@Override @Override
public void registerExportables(Database database) { public void registerExportables(Database database) {
final Dialect dialect = database.getJdbcEnvironment().getDialect(); final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final Schema schema = database.locateSchema( final Schema schema = database.locateSchema(
qualifiedTableName.getCatalogName(), logicalQualifiedTableName.getCatalogName(),
qualifiedTableName.getSchemaName() logicalQualifiedTableName.getSchemaName()
); );
Table table = schema.locateTable( qualifiedTableName.getObjectName() ); Table table = schema.locateTable( logicalQualifiedTableName.getObjectName() );
if ( table != null ) { if ( table == null ) {
return; table = schema.createTable( logicalQualifiedTableName.getObjectName(), false );
} }
table = schema.createTable( qualifiedTableName.getObjectName(), false ); this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(),
dialect
);
this.valueColumnNameText = logicalValueColumnNameIdentifier.render( dialect );
this.selectQuery = "select " + valueColumnNameText + " as id_val" +
" from " + dialect.appendLockHint( LockMode.PESSIMISTIC_WRITE, tableNameText ) +
dialect.getForUpdateString();
this.updateQuery = "update " + tableNameText +
" set " + valueColumnNameText + "= ?" +
" where " + valueColumnNameText + "=?";
ExportableColumn valueColumn = new ExportableColumn( ExportableColumn valueColumn = new ExportableColumn(
database, database,