package org.hibernate.hql.internal.ast.tree;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.MappingException;
import org.hibernate.QueryException;
import org.hibernate.engine.internal.JoinSequence;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.internal.CollectionProperties;
import org.hibernate.hql.internal.CollectionSubqueryFactory;
import org.hibernate.hql.internal.NameGenerator;
import org.hibernate.hql.internal.antlr.HqlSqlTokenTypes;
import org.hibernate.hql.internal.ast.HqlSqlWalker;
import org.hibernate.hql.internal.ast.util.SessionFactoryHelper;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.param.ParameterSpecification;
import org.hibernate.persister.collection.CollectionPropertyMapping;
import org.hibernate.persister.collection.CollectionPropertyNames;
import org.hibernate.persister.collection.QueryableCollection;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.Joinable;
import org.hibernate.persister.entity.PropertyMapping;
import org.hibernate.persister.entity.Queryable;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import antlr.SemanticException;
class FromElementType {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( FromElementType.class );
private FromElement fromElement;
private EntityType entityType;
private EntityPersister persister;
private QueryableCollection queryableCollection;
private CollectionPropertyMapping collectionPropertyMapping;
private JoinSequence joinSequence;
private String collectionSuffix;
private ParameterSpecification indexCollectionSelectorParamSpec;
public FromElementType(FromElement fromElement, EntityPersister persister, EntityType entityType) {
this.fromElement = fromElement;
this.persister = persister;
this.entityType = entityType;
if ( persister != null ) {
fromElement.setText( ( (Queryable) persister ).getTableName() + " " + getTableAlias() );
}
}
protected FromElementType(FromElement fromElement) {
this.fromElement = fromElement;
}
private String getTableAlias() {
return fromElement.getTableAlias();
}
private String getCollectionTableAlias() {
return fromElement.getCollectionTableAlias();
}
public String getCollectionSuffix() {
return collectionSuffix;
}
public void setCollectionSuffix(String suffix) {
collectionSuffix = suffix;
}
public EntityPersister getEntityPersister() {
return persister;
}
public Type getDataType() {
if ( persister == null ) {
if ( queryableCollection == null ) {
return null;
}
return queryableCollection.getType();
}
else {
return entityType;
}
}
public Type getSelectType() {
if ( entityType == null ) {
return null;
}
boolean shallow = fromElement.getFromClause().getWalker().isShallowQuery();
return fromElement.getSessionFactoryHelper()
.getFactory()
.getTypeResolver()
.getTypeFactory().manyToOne( entityType.getAssociatedEntityName(), shallow );
}
public Queryable getQueryable() {
return ( persister instanceof Queryable ) ? (Queryable) persister : null;
}
String renderScalarIdentifierSelect(int i) {
checkInitialized();
final String idPropertyName = getIdentifierPropertyName();
String[] cols = getPropertyMapping( idPropertyName ).toColumns( getTableAlias(), idPropertyName );
StringBuilder buf = new StringBuilder();
for ( int j = 0; j < cols.length; j++ ) {
String column = cols[j];
if ( j > 0 ) {
buf.append( ", " );
}
buf.append( column ).append( " as " ).append( NameGenerator.scalarName( i, j ) );
}
return buf.toString();
}
String renderIdentifierSelect(int size, int k) {
checkInitialized();
if ( fromElement.getFromClause().isSubQuery() ) {
String[] idColumnNames = ( persister != null ) ?
( (Queryable) persister ).getIdentifierColumnNames() : new String[0];
StringBuilder buf = new StringBuilder();
for ( int i = 0; i < idColumnNames.length; i++ ) {
buf.append( fromElement.getTableAlias() ).append( '.' ).append( idColumnNames[i] );
if ( i != idColumnNames.length - 1 ) {
buf.append( ", " );
}
}
return buf.toString();
}
else {
if ( persister == null ) {
throw new QueryException( "not an entity" );
}
String fragment = ( (Queryable) persister ).identifierSelectFragment(
getTableAlias(), getSuffix(
size,
k
)
);
return trimLeadingCommaAndSpaces( fragment );
}
}
private String getSuffix(int size, int sequence) {
return generateSuffix( size, sequence );
}
private static String generateSuffix(int size, int k) {
return size == 1 ? "" : Integer.toString( k ) + '_';
}
private void checkInitialized() {
fromElement.checkInitialized();
}
String renderPropertySelect(int size, int k, boolean allProperties) {
checkInitialized();
if ( persister == null ) {
return "";
}
else {
String fragment = ( (Queryable) persister ).propertySelectFragment(
getTableAlias(),
getSuffix( size, k ),
allProperties
);
return trimLeadingCommaAndSpaces( fragment );
}
}
public String renderMapKeyPropertySelectFragment(int size, int k) {
if ( persister == null ) {
throw new IllegalStateException( "Unexpected state in call to renderMapKeyPropertySelectFragment" );
}
final String fragment = ( (Queryable) persister ).propertySelectFragment(
getTableAlias(),
getSuffix( size, k ),
false
);
return trimLeadingCommaAndSpaces( fragment );
}
public String renderMapEntryPropertySelectFragment(int size, int k) {
return null;
}
String renderCollectionSelectFragment(int size, int k) {
if ( queryableCollection == null ) {
return "";
}
else {
if ( collectionSuffix == null ) {
collectionSuffix = generateSuffix( size, k );
}
String fragment = queryableCollection.selectFragment( getCollectionTableAlias(), collectionSuffix );
return trimLeadingCommaAndSpaces( fragment );
}
}
public String renderValueCollectionSelectFragment(int size, int k) {
if ( queryableCollection == null ) {
return "";
}
else {
if ( collectionSuffix == null ) {
collectionSuffix = generateSuffix( size, k );
}
String fragment = queryableCollection.selectFragment( getTableAlias(), collectionSuffix );
return trimLeadingCommaAndSpaces( fragment );
}
}
private static String trimLeadingCommaAndSpaces(String fragment) {
if ( fragment.length() > 0 && fragment.charAt( 0 ) == ',' ) {
fragment = fragment.substring( 1 );
}
fragment = fragment.trim();
return fragment.trim();
}
public void setJoinSequence(JoinSequence joinSequence) {
this.joinSequence = joinSequence;
joinSequence.applyTreatAsDeclarations( treatAsDeclarations );
}
public JoinSequence getJoinSequence() {
if ( joinSequence != null ) {
return joinSequence;
}
if ( persister instanceof Joinable ) {
Joinable joinable = (Joinable) persister;
final JoinSequence joinSequence = fromElement.getSessionFactoryHelper().createJoinSequence().setRoot(
joinable,
getTableAlias()
);
joinSequence.applyTreatAsDeclarations( treatAsDeclarations );
return joinSequence;
}
else {
return null;
}
}
private Set<String> treatAsDeclarations;
public void applyTreatAsDeclarations(Set<String> treatAsDeclarations) {
if ( treatAsDeclarations != null && !treatAsDeclarations.isEmpty() ) {
if ( this.treatAsDeclarations == null ) {
this.treatAsDeclarations = new HashSet<String>();
}
for ( String treatAsSubclassName : treatAsDeclarations ) {
try {
EntityPersister subclassPersister = fromElement.getSessionFactoryHelper().requireClassPersister(
treatAsSubclassName
);
this.treatAsDeclarations.add( subclassPersister.getEntityName() );
}
catch (SemanticException e) {
throw new QueryException( "Unable to locate persister for subclass named in TREAT-AS : " + treatAsSubclassName );
}
}
if ( joinSequence != null ) {
joinSequence.applyTreatAsDeclarations( this.treatAsDeclarations );
}
}
}
public void setQueryableCollection(QueryableCollection queryableCollection) {
if ( this.queryableCollection != null ) {
throw new IllegalStateException( "QueryableCollection is already defined for " + this + "!" );
}
this.queryableCollection = queryableCollection;
if ( !queryableCollection.isOneToMany() ) {
fromElement.setText( queryableCollection.getTableName() + " " + getTableAlias() );
}
}
public QueryableCollection getQueryableCollection() {
return queryableCollection;
}
public Type getPropertyType(String propertyName, String propertyPath) {
checkInitialized();
Type type = null;
if ( persister != null && propertyName.equals( propertyPath ) && propertyName.equals( persister.getIdentifierPropertyName() ) ) {
type = persister.getIdentifierType();
}
else {
PropertyMapping mapping = getPropertyMapping( propertyName );
type = mapping.toType( propertyPath );
}
if ( type == null ) {
throw new MappingException(
"Property " + propertyName + " does not exist in " +
( ( queryableCollection == null ) ? "class" : "collection" ) + " "
+ ( ( queryableCollection == null ) ?
fromElement.getClassName() :
queryableCollection.getRole() )
);
}
return type;
}
String[] toColumns(String tableAlias, String path, boolean inSelect) {
return toColumns( tableAlias, path, inSelect, false );
}
String[] toColumns(String tableAlias, String path, boolean inSelect, boolean forceAlias) {
checkInitialized();
PropertyMapping propertyMapping = getPropertyMapping( path );
if ( !inSelect && queryableCollection != null && CollectionProperties.isCollectionProperty( path ) ) {
if ( persister != propertyMapping ) {
return getCollectionPropertyReference( path ).toColumns( tableAlias );
}
}
if ( forceAlias ) {
return propertyMapping.toColumns( tableAlias, path );
}
if ( fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) {
return propertyMapping.toColumns( tableAlias, path );
}
if ( fromElement.getWalker().isSubQuery() ) {
if ( isCorrelation() ) {
if ( isMultiTable() || isInsertQuery() ) {
return propertyMapping.toColumns( tableAlias, path );
}
return propertyMapping.toColumns( extractTableName(), path );
}
return propertyMapping.toColumns( tableAlias, path );
}
if ( fromElement.getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.SELECT ) {
return propertyMapping.toColumns( tableAlias, path );
}
if ( isManipulationQuery() && isMultiTable() && inWhereClause() ) {
return propertyMapping.toColumns( tableAlias, path );
}
String[] columns = propertyMapping.toColumns( path );
LOG.tracev( "Using non-qualified column reference [{0} -> ({1})]", path, ArrayHelper.toString( columns ) );
return columns;
}
private boolean isCorrelation() {
FromClause top = fromElement.getWalker().getFinalFromClause();
return fromElement.getFromClause() != fromElement.getWalker().getCurrentFromClause() &&
fromElement.getFromClause() == top;
}
private boolean isMultiTable() {
return fromElement.getQueryable() != null &&
fromElement.getQueryable().isMultiTable();
}
private String () {
return fromElement.getQueryable().getTableName();
}
private boolean isInsertQuery() {
return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.INSERT;
}
private boolean isManipulationQuery() {
return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.UPDATE
|| fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.DELETE;
}
private boolean inWhereClause() {
return fromElement.getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.WHERE;
}
private static final List SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES = java.util.Arrays.asList(
CollectionPropertyNames.COLLECTION_INDEX,
CollectionPropertyNames.COLLECTION_MIN_INDEX,
CollectionPropertyNames.COLLECTION_MAX_INDEX
);
PropertyMapping getPropertyMapping(String propertyName) {
checkInitialized();
if ( queryableCollection == null ) {
return (PropertyMapping) persister;
}
if ( queryableCollection.isManyToMany()
&& queryableCollection.hasIndex()
&& SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES.contains( propertyName ) ) {
return new SpecialManyToManyCollectionPropertyMapping();
}
if ( CollectionProperties.isCollectionProperty( propertyName ) ) {
if ( collectionPropertyMapping == null ) {
if ( persister != null ) {
try {
if ( persister.getPropertyType( propertyName ) != null ) {
return (PropertyMapping) persister;
}
}
catch (QueryException ignore) {
}
}
collectionPropertyMapping = new CollectionPropertyMapping( queryableCollection );
}
return collectionPropertyMapping;
}
if ( queryableCollection.getElementType().isAnyType() ) {
return queryableCollection;
}
if ( queryableCollection.getElementType().isComponentType() ) {
if ( propertyName.equals( EntityPersister.ENTITY_ID ) ) {
return (PropertyMapping) queryableCollection.getOwnerEntityPersister();
}
}
return queryableCollection;
}
public boolean isCollectionOfValuesOrComponents() {
return persister == null
&& queryableCollection != null
&& !queryableCollection.getElementType().isEntityType();
}
public boolean isEntity() {
return persister != null;
}
public ParameterSpecification getIndexCollectionSelectorParamSpec() {
return indexCollectionSelectorParamSpec;
}
public void setIndexCollectionSelectorParamSpec(ParameterSpecification indexCollectionSelectorParamSpec) {
this.indexCollectionSelectorParamSpec = indexCollectionSelectorParamSpec;
}
public CollectionPropertyReference getCollectionPropertyReference(final String propertyName) {
if ( queryableCollection == null ) {
throw new QueryException( "Not a collection reference" );
}
final PropertyMapping collectionPropertyMapping;
if ( queryableCollection.isManyToMany()
&& queryableCollection.hasIndex()
&& SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES.contains( propertyName ) ) {
collectionPropertyMapping = new SpecialManyToManyCollectionPropertyMapping();
}
else if ( CollectionProperties.isCollectionProperty( propertyName ) ) {
if ( this.collectionPropertyMapping == null ) {
this.collectionPropertyMapping = new CollectionPropertyMapping( queryableCollection );
}
collectionPropertyMapping = this.collectionPropertyMapping;
}
else {
collectionPropertyMapping = queryableCollection;
}
return new CollectionPropertyReference() {
@Override
public Type getType() {
return collectionPropertyMapping.toType( propertyName );
}
@Override
public String[] toColumns(final String tableAlias) {
if ( propertyName.equalsIgnoreCase( "index" ) ) {
return collectionPropertyMapping.toColumns( tableAlias, propertyName );
}
Map enabledFilters = fromElement.getWalker().getEnabledFilters();
String subquery = CollectionSubqueryFactory.createCollectionSubquery(
joinSequence.copyForCollectionProperty().setUseThetaStyle( true ),
enabledFilters,
collectionPropertyMapping.toColumns( tableAlias, propertyName )
);
LOG.debugf( "toColumns(%s,%s) : subquery = %s", tableAlias, propertyName, subquery );
return new String[] {"(" + subquery + ")"};
}
};
}
private class SpecialManyToManyCollectionPropertyMapping implements PropertyMapping {
@Override
public Type getType() {
return queryableCollection.getCollectionType();
}
private void validate(String propertyName) {
if ( !( CollectionPropertyNames.COLLECTION_INDEX.equals( propertyName )
|| CollectionPropertyNames.COLLECTION_MAX_INDEX.equals( propertyName )
|| CollectionPropertyNames.COLLECTION_MIN_INDEX.equals( propertyName ) ) ) {
throw new IllegalArgumentException( "Expecting index-related function call" );
}
}
@Override
public Type toType(String propertyName) throws QueryException {
validate( propertyName );
return queryableCollection.getIndexType();
}
@Override
public String[] toColumns(String alias, String propertyName) throws QueryException {
validate( propertyName );
final String joinTableAlias = joinSequence.getFirstJoin().getAlias();
if ( CollectionPropertyNames.COLLECTION_INDEX.equals( propertyName ) ) {
return queryableCollection.toColumns( joinTableAlias, propertyName );
}
final String[] cols = queryableCollection.getIndexColumnNames( joinTableAlias );
if ( CollectionPropertyNames.COLLECTION_MIN_INDEX.equals( propertyName ) ) {
if ( cols.length != 1 ) {
throw new QueryException( "composite collection index in minIndex()" );
}
return new String[] {"min(" + cols[0] + ')'};
}
else {
if ( cols.length != 1 ) {
throw new QueryException( "composite collection index in maxIndex()" );
}
return new String[] {"max(" + cols[0] + ')'};
}
}
@Override
public String[] toColumns(String propertyName) throws QueryException, UnsupportedOperationException {
validate( propertyName );
return queryableCollection.toColumns( propertyName );
}
}
public String getIdentifierPropertyName() {
if ( getEntityPersister() != null && getEntityPersister().getEntityMetamodel() != null
&& getEntityPersister().getEntityMetamodel().hasNonIdentifierPropertyNamedId() ) {
return getEntityPersister().getIdentifierPropertyName();
}
else {
return EntityPersister.ENTITY_ID;
}
}
}