1 package org.hibernate.hql.ast.tree; 3 4 import org.hibernate.QueryException; 5 import org.hibernate.engine.JoinSequence; 6 import org.hibernate.hql.ast.SqlGenerator; 7 import org.hibernate.hql.ast.util.SessionFactoryHelper; 8 import org.hibernate.persister.collection.QueryableCollection; 9 import org.hibernate.type.CollectionType; 10 import org.hibernate.type.Type; 11 12 import antlr.RecognitionException; 13 import antlr.SemanticException; 14 import antlr.collections.AST; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 24 public class IndexNode extends FromReferenceNode { 25 26 private static final Log log = LogFactory.getLog( IndexNode.class ); 27 28 public void setScalarColumnText(int i) throws SemanticException { 29 throw new UnsupportedOperationException ( "An IndexNode cannot generate column text!" ); 30 } 31 32 public void prepareForDot(String propertyName) throws SemanticException { 33 FromElement fromElement = getFromElement(); 34 if ( fromElement == null ) { 35 throw new IllegalStateException ( "No FROM element for index operator!" ); 36 } 37 QueryableCollection queryableCollection = fromElement.getQueryableCollection(); 38 if ( queryableCollection != null && !queryableCollection.isOneToMany() ) { 39 40 FromReferenceNode collectionNode = ( FromReferenceNode ) getFirstChild(); 41 String path = collectionNode.getPath() + "[]." + propertyName; 42 if ( log.isDebugEnabled() ) { 43 log.debug( "Creating join for many-to-many elements for " + path ); 44 } 45 FromElementFactory factory = new FromElementFactory( fromElement.getFromClause(), fromElement, path ); 46 FromElement elementJoin = factory.createElementJoin( queryableCollection ); 48 setFromElement( elementJoin ); 49 } 50 } 51 52 public void resolveIndex(AST parent) throws SemanticException { 53 throw new UnsupportedOperationException (); 54 } 55 56 public void resolve(boolean generateJoin, boolean implicitJoin, String classAlias, AST parent) 57 throws SemanticException { 58 if ( isResolved() ) { 59 return; 60 } 61 FromReferenceNode collectionNode = ( FromReferenceNode ) getFirstChild(); 62 SessionFactoryHelper sessionFactoryHelper = getSessionFactoryHelper(); 63 collectionNode.resolveIndex( this ); 65 Type type = collectionNode.getDataType(); 66 if ( !type.isCollectionType() ) { 67 throw new SemanticException( "The [] operator cannot be applied to type " + type.toString() ); 68 } 69 String collectionRole = ( ( CollectionType ) type ).getRole(); 70 QueryableCollection queryableCollection = sessionFactoryHelper.requireQueryableCollection( collectionRole ); 71 if ( !queryableCollection.hasIndex() ) { 72 throw new QueryException( "unindexed fromElement before []: " + collectionNode.getPath() ); 73 } 74 75 FromElement fromElement = collectionNode.getFromElement(); 77 String elementTable = fromElement.getTableAlias(); 78 FromClause fromClause = fromElement.getFromClause(); 79 String path = collectionNode.getPath(); 80 81 FromElement elem = fromClause.findCollectionJoin( path ); 82 if ( elem == null ) { 83 FromElementFactory factory = new FromElementFactory( fromClause, fromElement, path ); 84 elem = factory.createCollectionElementsJoin( queryableCollection, elementTable ); 85 if ( log.isDebugEnabled() ) { 86 log.debug( "No FROM element found for the elements of collection join path " + path 87 + ", created " + elem ); 88 } 89 } 90 else { 91 if ( log.isDebugEnabled() ) { 92 log.debug( "FROM element found for collection join path " + path ); 93 } 94 } 95 96 AST index = collectionNode.getNextSibling(); if ( index == null ) { 99 throw new QueryException( "No index value!" ); 100 } 101 102 setFromElement( fromElement ); 104 String collectionTableAlias = elementTable; 106 if ( elem.getCollectionTableAlias() != null ) { 107 collectionTableAlias = elem.getCollectionTableAlias(); 108 } 109 110 JoinSequence joinSequence = fromElement.getJoinSequence(); 113 String [] indexCols = queryableCollection.getIndexColumnNames(); 114 if ( indexCols.length != 1 ) { 115 throw new QueryException( "composite-index appears in []: " + collectionNode.getPath() ); 116 } 117 SqlGenerator gen = new SqlGenerator( getSessionFactoryHelper().getFactory() ); 118 try { 119 gen.simpleExpr( index ); } 121 catch ( RecognitionException e ) { 122 throw new QueryException( e.getMessage(), e ); 123 } 124 String expression = gen.getSQL(); 125 joinSequence.addCondition( collectionTableAlias + '.' + indexCols[0] + " = " + expression ); 126 127 String [] elementColumns = queryableCollection.getElementColumnNames( elementTable ); 129 setText( elementColumns[0] ); 130 setResolved(); 131 } 132 133 134 } 135 | Popular Tags |