package org.hibernate.hql.spi.id;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.hql.internal.ast.HqlSqlWalker;
import org.hibernate.hql.internal.ast.SqlGenerator;
import org.hibernate.param.ParameterSpecification;
import org.hibernate.persister.entity.Queryable;
import org.hibernate.sql.InsertSelect;
import org.hibernate.sql.Select;
import org.hibernate.sql.SelectValues;
import antlr.RecognitionException;
import antlr.collections.AST;
public abstract class AbstractTableBasedBulkIdHandler {
private final SessionFactoryImplementor sessionFactory;
private final HqlSqlWalker walker;
public AbstractTableBasedBulkIdHandler(
SessionFactoryImplementor sessionFactory,
HqlSqlWalker walker) {
this.sessionFactory = sessionFactory;
this.walker = walker;
}
protected SessionFactoryImplementor factory() {
return sessionFactory;
}
protected HqlSqlWalker walker() {
return walker;
}
public abstract Queryable getTargetedQueryable();
protected static class ProcessedWhereClause {
public static final ProcessedWhereClause NO_WHERE_CLAUSE = new ProcessedWhereClause();
private final String userWhereClauseFragment;
private final List<ParameterSpecification> idSelectParameterSpecifications;
private ProcessedWhereClause() {
this( "", Collections.emptyList() );
}
public ProcessedWhereClause(String userWhereClauseFragment, List<ParameterSpecification> idSelectParameterSpecifications) {
this.userWhereClauseFragment = userWhereClauseFragment;
this.idSelectParameterSpecifications = idSelectParameterSpecifications;
}
public String getUserWhereClauseFragment() {
return userWhereClauseFragment;
}
public List<ParameterSpecification> getIdSelectParameterSpecifications() {
return idSelectParameterSpecifications;
}
}
@SuppressWarnings("unchecked")
protected ProcessedWhereClause processWhereClause(AST whereClause) {
if ( whereClause.getNumberOfChildren() != 0 ) {
try {
SqlGenerator sqlGenerator = new SqlGenerator( sessionFactory );
sqlGenerator.whereClause( whereClause );
String userWhereClause = sqlGenerator.getSQL().substring( 7 );
List<ParameterSpecification> idSelectParameterSpecifications = sqlGenerator.getCollectedParameters();
return new ProcessedWhereClause( userWhereClause, idSelectParameterSpecifications );
}
catch ( RecognitionException e ) {
throw new HibernateException( "Unable to generate id select for DML operation", e );
}
}
else {
return ProcessedWhereClause.NO_WHERE_CLAUSE;
}
}
protected String generateIdInsertSelect(
String tableAlias,
IdTableInfo idTableInfo,
ProcessedWhereClause whereClause) {
final Dialect dialect = sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect();
final Select select = generateIdSelect( tableAlias, whereClause );
InsertSelect insert = new InsertSelect( dialect );
if ( sessionFactory.getSessionFactoryOptions().isCommentsEnabled() ) {
insert.setComment( "insert-select for " + getTargetedQueryable().getEntityName() + " ids" );
}
insert.setTableName( idTableInfo.getQualifiedIdTableName() );
insert.setSelect( select );
return insert.toStatementString();
}
protected Select generateIdSelect(
String tableAlias,
ProcessedWhereClause whereClause) {
final Dialect dialect = sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect();
final Select select = new Select( dialect );
final SelectValues selectClause = new SelectValues( dialect ).addColumns(
tableAlias,
getTargetedQueryable().getIdentifierColumnNames(),
getTargetedQueryable().getIdentifierColumnNames()
);
addAnyExtraIdSelectValues( selectClause );
select.setSelectClause( selectClause.render() );
String rootTableName = getTargetedQueryable().getTableName();
String fromJoinFragment = getTargetedQueryable().fromJoinFragment( tableAlias, true, false );
String whereJoinFragment = getTargetedQueryable().whereJoinFragment( tableAlias, true, false );
select.setFromClause( rootTableName + ' ' + tableAlias + fromJoinFragment );
if ( whereJoinFragment == null ) {
whereJoinFragment = "";
}
else {
whereJoinFragment = whereJoinFragment.trim();
if ( whereJoinFragment.startsWith( "and" ) ) {
whereJoinFragment = whereJoinFragment.substring( 4 );
}
}
if ( whereClause.getUserWhereClauseFragment().length() > 0 ) {
if ( whereJoinFragment.length() > 0 ) {
whereJoinFragment += " and ";
}
}
select.setWhereClause( whereJoinFragment + whereClause.getUserWhereClauseFragment() );
return select;
}
protected void addAnyExtraIdSelectValues(SelectValues selectClause) {
}
protected String generateIdSubselect(Queryable persister, IdTableInfo idTableInfo) {
return "select " + String.join( ", ", persister.getIdentifierColumnNames() ) +
" from " + idTableInfo.getQualifiedIdTableName();
}
protected void prepareForUse(Queryable persister, SharedSessionContractImplementor session) {
}
protected void releaseFromUse(Queryable persister, SharedSessionContractImplementor session) {
}
}