HHH-11131 - Refactored generator and database structure code.
This commit is contained in:
parent
555aa772a3
commit
de7ca7882b
|
@ -41,11 +41,10 @@ public class SequenceStructure implements DatabaseStructure {
|
|||
private final int incrementSize;
|
||||
private final Class numberType;
|
||||
|
||||
|
||||
private String sequenceName;
|
||||
private String sql;
|
||||
private boolean applyIncrementSizeToSourceValues;
|
||||
private int accessCounter;
|
||||
protected String sequenceName;
|
||||
|
||||
public SequenceStructure(
|
||||
JdbcEnvironment jdbcEnvironment,
|
||||
|
@ -141,8 +140,35 @@ public class SequenceStructure implements DatabaseStructure {
|
|||
|
||||
@Override
|
||||
public void registerExportables(Database database) {
|
||||
final int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1;
|
||||
buildSequence( database );
|
||||
this.sql = database.getJdbcEnvironment().getDialect().getSequenceNextValString( sequenceName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
|
||||
return dialect.getCreateSequenceStrings( sequenceName, initialValue, getSourceIncrementSize() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
|
||||
return dialect.getDropSequenceStrings( sequenceName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPhysicalSequence() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected final int getSourceIncrementSize() {
|
||||
return applyIncrementSizeToSourceValues ? incrementSize : 1;
|
||||
}
|
||||
|
||||
protected QualifiedName getQualifiedName() {
|
||||
return logicalQualifiedSequenceName;
|
||||
}
|
||||
|
||||
protected void buildSequence(Database database) {
|
||||
final int sourceIncrementSize = getSourceIncrementSize();
|
||||
|
||||
final Namespace namespace = database.locateNamespace(
|
||||
logicalQualifiedSequenceName.getCatalogName(),
|
||||
|
@ -156,29 +182,9 @@ public class SequenceStructure implements DatabaseStructure {
|
|||
sequence = namespace.createSequence( logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize );
|
||||
}
|
||||
|
||||
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
|
||||
final Dialect dialect = jdbcEnvironment.getDialect();
|
||||
|
||||
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
|
||||
this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
|
||||
sequence.getName(),
|
||||
dialect
|
||||
database.getJdbcEnvironment().getDialect()
|
||||
);
|
||||
this.sql = jdbcEnvironment.getDialect().getSequenceNextValString( sequenceName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
|
||||
final int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1;
|
||||
return dialect.getCreateSequenceStrings( sequenceName, initialValue, sourceIncrementSize );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
|
||||
return dialect.getDropSequenceStrings( sequenceName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPhysicalSequence() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -396,14 +396,34 @@ public class SequenceStyleGenerator
|
|||
int incrementSize) {
|
||||
final boolean useSequence = jdbcEnvironment.getDialect().supportsSequences() && !forceTableUse;
|
||||
if ( useSequence ) {
|
||||
return new SequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
|
||||
return buildSequenceStructure( type, params, jdbcEnvironment, sequenceName, initialValue, incrementSize );
|
||||
}
|
||||
else {
|
||||
final Identifier valueColumnName = determineValueColumnName( params, jdbcEnvironment );
|
||||
return new TableStructure( jdbcEnvironment, sequenceName, valueColumnName, initialValue, incrementSize, type.getReturnedClass() );
|
||||
return buildTableStructure( type, params, jdbcEnvironment, sequenceName, initialValue, incrementSize );
|
||||
}
|
||||
}
|
||||
|
||||
protected DatabaseStructure buildSequenceStructure(
|
||||
Type type,
|
||||
Properties params,
|
||||
JdbcEnvironment jdbcEnvironment,
|
||||
QualifiedName sequenceName,
|
||||
int initialValue,
|
||||
int incrementSize) {
|
||||
return new SequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
|
||||
}
|
||||
|
||||
protected DatabaseStructure buildTableStructure(
|
||||
Type type,
|
||||
Properties params,
|
||||
JdbcEnvironment jdbcEnvironment,
|
||||
QualifiedName sequenceName,
|
||||
int initialValue,
|
||||
int incrementSize) {
|
||||
final Identifier valueColumnName = determineValueColumnName( params, jdbcEnvironment );
|
||||
return new TableStructure( jdbcEnvironment, sequenceName, valueColumnName, initialValue, incrementSize, type.getReturnedClass() );
|
||||
}
|
||||
|
||||
|
||||
// IdentifierGenerator implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
|
|
@ -25,18 +25,13 @@ import org.hibernate.type.Type;
|
|||
*/
|
||||
public class OrderedSequenceGenerator extends SequenceStyleGenerator {
|
||||
@Override
|
||||
protected DatabaseStructure buildDatabaseStructure(
|
||||
protected DatabaseStructure buildSequenceStructure(
|
||||
Type type,
|
||||
Properties params,
|
||||
JdbcEnvironment jdbcEnvironment,
|
||||
boolean forceTableUse,
|
||||
QualifiedName sequenceName,
|
||||
int initialValue,
|
||||
int incrementSize) {
|
||||
final boolean useSequence = jdbcEnvironment.getDialect().supportsSequences() && !forceTableUse;
|
||||
if ( useSequence ) {
|
||||
return new OrderedSequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
|
||||
}
|
||||
return super.buildDatabaseStructure( type, params, jdbcEnvironment, forceTableUse, sequenceName, initialValue, incrementSize );
|
||||
return new OrderedSequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,53 +6,24 @@
|
|||
*/
|
||||
package org.hibernate.envers.enhanced;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.hibernate.AssertionFailure;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
|
||||
import org.hibernate.boot.model.relational.Database;
|
||||
import org.hibernate.boot.model.relational.Namespace;
|
||||
import org.hibernate.boot.model.relational.QualifiedName;
|
||||
import org.hibernate.boot.model.relational.QualifiedSequenceName;
|
||||
import org.hibernate.boot.model.relational.Sequence;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.Oracle8iDialect;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.id.IdentifierGeneratorHelper;
|
||||
import org.hibernate.id.IntegralDataTypeHolder;
|
||||
import org.hibernate.id.enhanced.AccessCallback;
|
||||
import org.hibernate.id.enhanced.DatabaseStructure;
|
||||
import org.hibernate.id.enhanced.Optimizer;
|
||||
import org.hibernate.id.enhanced.SequenceStructure;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Describes a sequence that supports ordered sequences.
|
||||
*
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public class OrderedSequenceStructure implements DatabaseStructure {
|
||||
public class OrderedSequenceStructure extends SequenceStructure {
|
||||
|
||||
// @todo: a fair bit of duplication from SequenceStructure - needs refactor
|
||||
private static final String ORDER = " ORDER";
|
||||
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
|
||||
CoreMessageLogger.class,
|
||||
SequenceStructure.class.getName()
|
||||
);
|
||||
|
||||
private final QualifiedName logicalQualifiedSequenceName;
|
||||
private final int initialValue;
|
||||
private final int incrementSize;
|
||||
private final Class numberType;
|
||||
|
||||
private String sequenceName;
|
||||
private String sql;
|
||||
private boolean applyIncrementSizeToSourceValues;
|
||||
private int accessCounter;
|
||||
private AuxiliaryDatabaseObject sequenceObject;
|
||||
|
||||
public OrderedSequenceStructure(
|
||||
|
@ -61,109 +32,10 @@ public class OrderedSequenceStructure implements DatabaseStructure {
|
|||
int initialValue,
|
||||
int incrementSize,
|
||||
Class numberType) {
|
||||
this.logicalQualifiedSequenceName = qualifiedSequenceName;
|
||||
|
||||
this.initialValue = initialValue;
|
||||
this.incrementSize = incrementSize;
|
||||
this.numberType = numberType;
|
||||
super( jdbcEnvironment, qualifiedSequenceName, initialValue, incrementSize, numberType );
|
||||
this.sequenceObject = new OrderedSequence();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return sequenceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIncrementSize() {
|
||||
return incrementSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTimesAccessed() {
|
||||
return accessCounter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInitialValue() {
|
||||
return initialValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessCallback buildCallback(final SharedSessionContractImplementor session) {
|
||||
if ( sql == null ) {
|
||||
throw new AssertionFailure( "SequenceStyleGenerator's SequenceStructure was not properly initialized" );
|
||||
}
|
||||
|
||||
return new AccessCallback() {
|
||||
@Override
|
||||
public IntegralDataTypeHolder getNextValue() {
|
||||
accessCounter++;
|
||||
try {
|
||||
final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql );
|
||||
try {
|
||||
final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( st );
|
||||
try {
|
||||
rs.next();
|
||||
final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder( numberType );
|
||||
value.initialize( rs, 1 );
|
||||
if ( LOG.isDebugEnabled() ) {
|
||||
LOG.debugf( "Sequence value obtained: %s", value.makeValue() );
|
||||
}
|
||||
return value;
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( rs, st );
|
||||
}
|
||||
catch( Throwable ignore ) {
|
||||
// intentionally empty
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st );
|
||||
session.getJdbcCoordinator().afterStatementExecution();
|
||||
}
|
||||
|
||||
}
|
||||
catch ( SQLException sqle) {
|
||||
throw session.getJdbcServices().getSqlExceptionHelper().convert(
|
||||
sqle,
|
||||
"could not get next sequence value",
|
||||
sql
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTenantIdentifier() {
|
||||
return session.getTenantIdentifier();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare(Optimizer optimizer) {
|
||||
applyIncrementSizeToSourceValues = optimizer.applyIncrementSizeToSourceValues();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerExportables(Database database) {
|
||||
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
|
||||
final Dialect dialect = jdbcEnvironment.getDialect();
|
||||
|
||||
// construct the next value sql
|
||||
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
|
||||
logicalQualifiedSequenceName,
|
||||
dialect
|
||||
);
|
||||
this.sql = jdbcEnvironment.getDialect().getSequenceNextValString( sequenceName );
|
||||
|
||||
// add auxiliary object
|
||||
database.addAuxiliaryDatabaseObject( sequenceObject );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
|
||||
// delegate to auxiliary object
|
||||
|
@ -177,18 +49,24 @@ public class OrderedSequenceStructure implements DatabaseStructure {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isPhysicalSequence() {
|
||||
return true;
|
||||
protected void buildSequence(Database database) {
|
||||
database.addAuxiliaryDatabaseObject( sequenceObject );
|
||||
this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
|
||||
getQualifiedName(),
|
||||
database.getJdbcEnvironment().getDialect()
|
||||
);
|
||||
}
|
||||
|
||||
private class OrderedSequence implements AuxiliaryDatabaseObject {
|
||||
@Override
|
||||
public String getExportIdentifier() {
|
||||
return logicalQualifiedSequenceName.getObjectName().getText();
|
||||
return getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean appliesToDialect(Dialect dialect) {
|
||||
// applies to all dialects
|
||||
// sqlCreateStrings applies dialect specific changes
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -199,17 +77,15 @@ public class OrderedSequenceStructure implements DatabaseStructure {
|
|||
|
||||
@Override
|
||||
public String[] sqlCreateStrings(Dialect dialect) {
|
||||
final int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1;
|
||||
|
||||
final String[] createStrings = dialect.getCreateSequenceStrings(
|
||||
sequenceName,
|
||||
initialValue,
|
||||
sourceIncrementSize
|
||||
getName(),
|
||||
getInitialValue(),
|
||||
getSourceIncrementSize()
|
||||
);
|
||||
|
||||
if ( dialect instanceof Oracle8iDialect ) {
|
||||
for ( int i = 0; i < createStrings.length; ++i ) {
|
||||
createStrings[ i ] = createStrings[ i ] + " ORDER";
|
||||
createStrings[ i ] = createStrings[ i ] + ORDER;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -218,7 +94,7 @@ public class OrderedSequenceStructure implements DatabaseStructure {
|
|||
|
||||
@Override
|
||||
public String[] sqlDropStrings(Dialect dialect) {
|
||||
return dialect.getDropSequenceStrings( sequenceName );
|
||||
return dialect.getDropSequenceStrings( getName() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue