package org.hibernate.hql.spi.id;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.mapping.JoinedSubclass;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.Subclass;
import org.hibernate.mapping.UnionSubclass;
import org.jboss.logging.Logger;
public class IdTableHelper {
private static final Logger log = Logger.getLogger( IdTableHelper.class );
public static final IdTableHelper INSTANCE = new IdTableHelper();
private IdTableHelper() {
}
public boolean needsIdTable(PersistentClass entityBinding) {
if ( entityBinding.getJoinClosureSpan() > 0 ) {
return true;
}
final RootClass rootEntityBinding = entityBinding.getRootClass();
final Iterator itr = rootEntityBinding.getSubclassIterator();
if ( itr.hasNext() ) {
final Subclass subclassEntityBinding = (Subclass) itr.next();
if ( subclassEntityBinding instanceof JoinedSubclass || subclassEntityBinding instanceof UnionSubclass ) {
return true;
}
}
return false;
}
public void executeIdTableCreationStatements(
List<String> creationStatements,
JdbcServices jdbcServices,
JdbcConnectionAccess connectionAccess) {
try {
Connection connection;
try {
connection = connectionAccess.obtainConnection();
}
catch (UnsupportedOperationException e) {
log.debug( "Unable to obtain JDBC connection; assuming ID tables already exist or wont be needed" );
return;
}
try {
Statement statement = connection.createStatement();
for ( String creationStatement : creationStatements ) {
try {
jdbcServices.getSqlStatementLogger().logStatement( creationStatement );
statement.execute( creationStatement );
}
catch (SQLException e) {
log.debugf( "Error attempting to export id-table [%s] : %s", creationStatement, e.getMessage() );
}
}
statement.close();
}
catch (SQLException e) {
log.error( "Unable to use JDBC Connection to create Statement", e );
}
finally {
try {
connectionAccess.releaseConnection( connection );
}
catch (SQLException ignore) {
}
}
}
catch (SQLException e) {
log.error( "Unable obtain JDBC Connection", e );
}
}
public void executeIdTableDropStatements(
String[] dropStatements,
JdbcServices jdbcServices,
JdbcConnectionAccess connectionAccess) {
if ( dropStatements == null ) {
return;
}
try {
Connection connection = connectionAccess.obtainConnection();
try {
Statement statement = connection.createStatement();
for ( String dropStatement : dropStatements ) {
try {
jdbcServices.getSqlStatementLogger().logStatement( dropStatement );
statement.execute( dropStatement );
}
catch (SQLException e) {
log.debugf( "Error attempting to cleanup id-table : [%s]", e.getMessage() );
}
}
statement.close();
}
catch (SQLException e) {
log.error( "Unable to use JDBC Connection to create Statement", e );
}
finally {
try {
connectionAccess.releaseConnection( connection );
}
catch (SQLException ignore) {
}
}
}
catch (SQLException e) {
log.error( "Unable obtain JDBC Connection", e );
}
}
}