HHH-11131 - Refactored generator and database structure code.

This commit is contained in:
Chris Cranford 2016-09-28 12:51:01 -04:00 committed by Andrea Boriero
parent 555aa772a3
commit de7ca7882b
4 changed files with 75 additions and 178 deletions

View File

@ -41,11 +41,10 @@ public class SequenceStructure implements DatabaseStructure {
private final int incrementSize; private final int incrementSize;
private final Class numberType; private final Class numberType;
private String sequenceName;
private String sql; private String sql;
private boolean applyIncrementSizeToSourceValues; private boolean applyIncrementSizeToSourceValues;
private int accessCounter; private int accessCounter;
protected String sequenceName;
public SequenceStructure( public SequenceStructure(
JdbcEnvironment jdbcEnvironment, JdbcEnvironment jdbcEnvironment,
@ -141,8 +140,35 @@ public class SequenceStructure implements DatabaseStructure {
@Override @Override
public void registerExportables(Database database) { 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( final Namespace namespace = database.locateNamespace(
logicalQualifiedSequenceName.getCatalogName(), logicalQualifiedSequenceName.getCatalogName(),
@ -156,29 +182,9 @@ public class SequenceStructure implements DatabaseStructure {
sequence = namespace.createSequence( logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize ); sequence = namespace.createSequence( logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize );
} }
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment(); this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
final Dialect dialect = jdbcEnvironment.getDialect();
this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
sequence.getName(), 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;
} }
} }

View File

@ -396,14 +396,34 @@ public class SequenceStyleGenerator
int incrementSize) { int incrementSize) {
final boolean useSequence = jdbcEnvironment.getDialect().supportsSequences() && !forceTableUse; final boolean useSequence = jdbcEnvironment.getDialect().supportsSequences() && !forceTableUse;
if ( useSequence ) { if ( useSequence ) {
return new SequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() ); return buildSequenceStructure( type, params, jdbcEnvironment, sequenceName, initialValue, incrementSize );
} }
else { else {
final Identifier valueColumnName = determineValueColumnName( params, jdbcEnvironment ); return buildTableStructure( type, params, jdbcEnvironment, sequenceName, initialValue, incrementSize );
return new TableStructure( jdbcEnvironment, sequenceName, valueColumnName, initialValue, incrementSize, type.getReturnedClass() );
} }
} }
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // IdentifierGenerator implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -25,18 +25,13 @@ import org.hibernate.type.Type;
*/ */
public class OrderedSequenceGenerator extends SequenceStyleGenerator { public class OrderedSequenceGenerator extends SequenceStyleGenerator {
@Override @Override
protected DatabaseStructure buildDatabaseStructure( protected DatabaseStructure buildSequenceStructure(
Type type, Type type,
Properties params, Properties params,
JdbcEnvironment jdbcEnvironment, JdbcEnvironment jdbcEnvironment,
boolean forceTableUse,
QualifiedName sequenceName, QualifiedName sequenceName,
int initialValue, int initialValue,
int incrementSize) { int incrementSize) {
final boolean useSequence = jdbcEnvironment.getDialect().supportsSequences() && !forceTableUse; return new OrderedSequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
if ( useSequence ) {
return new OrderedSequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
}
return super.buildDatabaseStructure( type, params, jdbcEnvironment, forceTableUse, sequenceName, initialValue, incrementSize );
} }
} }

View File

@ -6,53 +6,24 @@
*/ */
package org.hibernate.envers.enhanced; 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.HibernateException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject; import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.model.relational.Database; 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.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.Oracle8iDialect; import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; 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.id.enhanced.SequenceStructure;
import org.hibernate.internal.CoreMessageLogger;
import org.jboss.logging.Logger;
/** /**
* Describes a sequence that supports ordered sequences.
*
* @author Chris Cranford * @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; private AuxiliaryDatabaseObject sequenceObject;
public OrderedSequenceStructure( public OrderedSequenceStructure(
@ -61,109 +32,10 @@ public class OrderedSequenceStructure implements DatabaseStructure {
int initialValue, int initialValue,
int incrementSize, int incrementSize,
Class numberType) { Class numberType) {
this.logicalQualifiedSequenceName = qualifiedSequenceName; super( jdbcEnvironment, qualifiedSequenceName, initialValue, incrementSize, numberType );
this.initialValue = initialValue;
this.incrementSize = incrementSize;
this.numberType = numberType;
this.sequenceObject = new OrderedSequence(); 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 @Override
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException { public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
// delegate to auxiliary object // delegate to auxiliary object
@ -177,18 +49,24 @@ public class OrderedSequenceStructure implements DatabaseStructure {
} }
@Override @Override
public boolean isPhysicalSequence() { protected void buildSequence(Database database) {
return true; database.addAuxiliaryDatabaseObject( sequenceObject );
this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
getQualifiedName(),
database.getJdbcEnvironment().getDialect()
);
} }
private class OrderedSequence implements AuxiliaryDatabaseObject { private class OrderedSequence implements AuxiliaryDatabaseObject {
@Override @Override
public String getExportIdentifier() { public String getExportIdentifier() {
return logicalQualifiedSequenceName.getObjectName().getText(); return getName();
} }
@Override @Override
public boolean appliesToDialect(Dialect dialect) { public boolean appliesToDialect(Dialect dialect) {
// applies to all dialects
// sqlCreateStrings applies dialect specific changes
return true; return true;
} }
@ -199,17 +77,15 @@ public class OrderedSequenceStructure implements DatabaseStructure {
@Override @Override
public String[] sqlCreateStrings(Dialect dialect) { public String[] sqlCreateStrings(Dialect dialect) {
final int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1;
final String[] createStrings = dialect.getCreateSequenceStrings( final String[] createStrings = dialect.getCreateSequenceStrings(
sequenceName, getName(),
initialValue, getInitialValue(),
sourceIncrementSize getSourceIncrementSize()
); );
if ( dialect instanceof Oracle8iDialect ) { if ( dialect instanceof Oracle8iDialect ) {
for ( int i = 0; i < createStrings.length; ++i ) { 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 @Override
public String[] sqlDropStrings(Dialect dialect) { public String[] sqlDropStrings(Dialect dialect) {
return dialect.getDropSequenceStrings( sequenceName ); return dialect.getDropSequenceStrings( getName() );
} }
} }
} }