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

View File

@ -48,16 +48,17 @@ public class TableStructure implements DatabaseStructure {
TableStructure.class.getName()
);
private final QualifiedName qualifiedTableName;
private final String tableNameText;
private final String valueColumnNameText;
private final QualifiedName logicalQualifiedTableName;
private final Identifier logicalValueColumnNameIdentifier;
private final int initialValue;
private final int incrementSize;
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 int accessCounter;
@ -69,27 +70,12 @@ public class TableStructure implements DatabaseStructure {
int initialValue,
int incrementSize,
Class numberType) {
final Dialect dialect = jdbcEnvironment.getDialect();
this.qualifiedTableName = qualifiedTableName;
this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
qualifiedTableName,
dialect
);
this.valueColumnNameText = valueColumnNameIdentifier.render( jdbcEnvironment.getDialect() );
this.logicalQualifiedTableName = qualifiedTableName;
this.logicalValueColumnNameIdentifier = valueColumnNameIdentifier;
this.initialValue = initialValue;
this.incrementSize = incrementSize;
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
@ -138,7 +124,12 @@ public class TableStructure implements DatabaseStructure {
final IntegralDataTypeHolder value = makeValue();
int rows;
do {
final PreparedStatement selectStatement = prepareStatement( connection, selectQuery, statementLogger, statsCollector );
final PreparedStatement selectStatement = prepareStatement(
connection,
selectQuery,
statementLogger,
statsCollector
);
try {
final ResultSet selectRS = executeQuery( selectStatement, statsCollector );
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 {
final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
final IntegralDataTypeHolder updateValue = value.copy().add( increment );
@ -247,18 +243,34 @@ public class TableStructure implements DatabaseStructure {
@Override
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(
qualifiedTableName.getCatalogName(),
qualifiedTableName.getSchemaName()
logicalQualifiedTableName.getCatalogName(),
logicalQualifiedTableName.getSchemaName()
);
Table table = schema.locateTable( qualifiedTableName.getObjectName() );
if ( table != null ) {
return;
Table table = schema.locateTable( logicalQualifiedTableName.getObjectName() );
if ( table == null ) {
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(
database,