1 package org.hibernate.hql.ast.tree; 3 4 import java.util.LinkedList ; 5 import java.util.List ; 6 7 import org.hibernate.QueryException; 8 import org.hibernate.engine.JoinSequence; 9 import org.hibernate.hql.QueryTranslator; 10 import org.hibernate.hql.antlr.SqlTokenTypes; 11 import org.hibernate.hql.ast.util.ASTUtil; 12 import org.hibernate.persister.collection.QueryableCollection; 13 import org.hibernate.persister.entity.EntityPersister; 14 import org.hibernate.persister.entity.PropertyMapping; 15 import org.hibernate.persister.entity.Queryable; 16 import org.hibernate.type.EntityType; 17 import org.hibernate.type.Type; 18 import org.hibernate.util.StringHelper; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 37 public class FromElement extends HqlSqlWalkerNode implements DisplayableNode { 38 private static final Log log = LogFactory.getLog( FromElement.class ); 39 40 private String className; 41 private String classAlias; 42 private String tableAlias; 43 private String collectionTableAlias; 44 private FromClause fromClause; 45 private boolean includeSubclasses = true; 46 private boolean collectionJoin = false; 47 private FromElement origin; 48 private String [] columns; 49 private String role; 50 private boolean fetch; 51 private boolean isAllPropertyFetch; 52 private boolean filter = false; 53 private int sequence = -1; 54 private boolean useFromFragment = false; 55 private boolean initialized = false; 56 private FromElementType elementType; 57 private boolean useWhereFragment = true; 58 private List destinations = new LinkedList (); 59 private boolean manyToMany = false; 60 private String adHocOnClauseFragment = null; 61 62 public FromElement() { 63 } 64 65 public String getCollectionSuffix() { 66 return elementType.getCollectionSuffix(); 67 } 68 69 public void setCollectionSuffix(String suffix) { 70 elementType.setCollectionSuffix(suffix); 71 } 72 73 public void initializeCollection(FromClause fromClause, String classAlias, String tableAlias) { 74 doInitialize( fromClause, tableAlias, null, classAlias, null, null ); 75 initialized = true; 76 } 77 78 public void initializeEntity( 79 FromClause fromClause, 80 String className, 81 EntityPersister persister, 82 EntityType type, 83 String classAlias, 84 String tableAlias) { 85 doInitialize( fromClause, tableAlias, className, classAlias, persister, type ); 86 this.sequence = fromClause.nextFromElementCounter(); 87 initialized = true; 88 } 89 90 private void doInitialize(FromClause fromClause, String tableAlias, String className, String classAlias, 91 EntityPersister persister, EntityType type) { 92 if ( initialized ) { 93 throw new IllegalStateException ( "Already initialized!!" ); 94 } 95 this.fromClause = fromClause; 96 this.tableAlias = tableAlias; 97 this.className = className; 98 this.classAlias = classAlias; 99 this.elementType = new FromElementType( this, persister, type ); 100 fromClause.registerFromElement( this ); 102 if ( log.isDebugEnabled() ) { 103 log.debug( fromClause + " : " + className + " (" 104 + ( classAlias == null ? "no alias" : classAlias ) + ") -> " + tableAlias ); 105 } 106 } 107 108 public EntityPersister getEntityPersister() { 109 return elementType.getEntityPersister(); 110 } 111 112 public Type getDataType() { 113 return elementType.getDataType(); 114 } 115 116 public Type getSelectType() { 117 return elementType.getSelectType(); 118 } 119 120 public Queryable getQueryable() { 121 return elementType.getQueryable(); 122 } 123 124 public String getClassName() { 125 return className; 126 } 127 128 public String getClassAlias() { 129 return classAlias; 130 } 132 133 private String getTableName() { 134 Queryable queryable = getQueryable(); 135 return ( queryable != null ) ? queryable.getTableName() : "{none}"; 136 } 137 138 public String getTableAlias() { 139 return tableAlias; 140 } 141 142 148 String renderScalarIdentifierSelect(int i) { 149 return elementType.renderScalarIdentifierSelect( i ); 150 } 151 152 void checkInitialized() { 153 if ( !initialized ) { 154 throw new IllegalStateException ( "FromElement has not been initialized!" ); 155 } 156 } 157 158 165 String renderIdentifierSelect(int size, int k) { 166 return elementType.renderIdentifierSelect( size, k ); 167 } 168 169 176 String renderPropertySelect(int size, int k) { 177 return elementType.renderPropertySelect( size, k, isAllPropertyFetch ); 178 } 179 180 String renderCollectionSelectFragment(int size, int k) { 181 return elementType.renderCollectionSelectFragment( size, k ); 182 } 183 184 String renderValueCollectionSelectFragment(int size, int k) { 185 return elementType.renderValueCollectionSelectFragment( size, k ); 186 } 187 188 public FromClause getFromClause() { 189 return fromClause; 190 } 191 192 198 public boolean isImplied() { 199 return false; } 201 202 207 public String getDisplayText() { 208 StringBuffer buf = new StringBuffer (); 209 buf.append( "FromElement{" ); 210 appendDisplayText( buf ); 211 buf.append( "}" ); 212 return buf.toString(); 213 } 214 215 protected void appendDisplayText(StringBuffer buf) { 216 buf.append( isImplied() ? ( 217 isImpliedInFromClause() ? "implied in FROM clause" : "implied" ) 218 : "explicit" ); 219 buf.append( "," ).append( isCollectionJoin() ? "collection join" : "not a collection join" ); 220 buf.append( "," ).append( fetch ? "fetch join" : "not a fetch join" ); 221 buf.append( "," ).append( isAllPropertyFetch ? "fetch all properties" : "fetch non-lazy properties" ); 222 buf.append( ",classAlias=" ).append( getClassAlias() ); 223 buf.append( ",role=" ).append( role ); 224 buf.append( ",tableName=" ).append( getTableName() ); 225 buf.append( ",tableAlias=" ).append( getTableAlias() ); 226 buf.append( ",colums={" ); 227 if ( columns != null ) { 228 for ( int i = 0; i < columns.length; i++ ) { 229 buf.append( columns[i] ); 230 if ( i < columns.length ) { 231 buf.append( " " ); 232 } 233 } 234 } 235 buf.append( ",className=" ).append( className ); 236 buf.append( "}" ); 237 } 238 239 public int hashCode() { 240 return super.hashCode(); 241 } 242 243 public boolean equals(Object obj) { 244 return super.equals( obj ); 245 } 246 247 248 public void setJoinSequence(JoinSequence joinSequence) { 249 elementType.setJoinSequence( joinSequence ); 250 } 251 252 public JoinSequence getJoinSequence() { 253 return elementType.getJoinSequence(); 254 } 255 256 public void setIncludeSubclasses(boolean includeSubclasses) { 257 this.includeSubclasses = includeSubclasses; 258 } 259 260 public boolean isIncludeSubclasses() { 261 return includeSubclasses; 262 } 263 264 public String getIdentityColumn() { 265 checkInitialized(); 266 String table = getTableAlias(); 267 if ( table == null ) { 268 throw new IllegalStateException ( "No table alias for node " + this ); 269 } 270 String [] cols = getPropertyMapping( EntityPersister.ENTITY_ID ).toColumns( table, EntityPersister.ENTITY_ID ); 271 String result = StringHelper.join( ", ", cols ); 272 return cols.length == 1 ? result : "(" + result + ")"; 273 } 274 275 public void setCollectionJoin(boolean collectionJoin) { 276 this.collectionJoin = collectionJoin; 277 } 278 279 public boolean isCollectionJoin() { 280 return collectionJoin; 281 } 282 283 public void setRole(String role) { 284 this.role = role; 285 } 286 287 public void setQueryableCollection(QueryableCollection queryableCollection) { 288 elementType.setQueryableCollection( queryableCollection ); 289 } 290 291 public QueryableCollection getQueryableCollection() { 292 return elementType.getQueryableCollection(); 293 } 294 295 public void setColumns(String [] columns) { 296 this.columns = columns; 297 } 298 299 public void setOrigin(FromElement origin, boolean manyToMany) { 300 this.origin = origin; 301 this.manyToMany = manyToMany; 302 origin.addDestination( this ); 303 if ( origin.getFromClause() == this.getFromClause() ) { 304 if ( manyToMany ) { 307 ASTUtil.appendSibling( origin, this ); 308 } 309 else { 310 if ( !getWalker().isInFrom() && !getWalker().isInSelect() ) { 311 getFromClause().addChild( this ); 312 } 313 else { 314 origin.addChild( this ); 315 } 316 } 317 } 318 else if ( !getWalker().isInFrom() ) { 319 getFromClause().addChild( this ); } 323 else { 324 } 327 } 328 329 public boolean isManyToMany() { 330 return manyToMany; 331 } 332 333 private void addDestination(FromElement fromElement) { 334 destinations.add( fromElement ); 335 } 336 337 public List getDestinations() { 338 return destinations; 339 } 340 341 public FromElement getOrigin() { 342 return origin; 343 } 344 345 352 public Type getPropertyType(String propertyName, String propertyPath) { 353 return elementType.getPropertyType( propertyName, propertyPath ); 354 } 355 356 public String [] toColumns(String tableAlias, String path, boolean inSelect) { 357 return elementType.toColumns( tableAlias, path, inSelect ); 358 } 359 360 public PropertyMapping getPropertyMapping(String propertyName) { 361 return elementType.getPropertyMapping( propertyName ); 362 } 363 364 public void setFetch(boolean fetch) { 365 this.fetch = fetch; 366 if ( fetch && getWalker().isShallowQuery() ) { 368 throw new QueryException( QueryTranslator.ERROR_CANNOT_FETCH_WITH_ITERATE ); 369 } 370 } 371 372 public boolean isFetch() { 373 return fetch; 374 } 375 376 public int getSequence() { 377 return sequence; 378 } 379 380 public void setFilter(boolean b) { 381 filter = b; 382 } 383 384 public boolean isFilter() { 385 return filter; 386 } 387 388 391 public boolean useFromFragment() { 392 checkInitialized(); 393 return !isImplied() || this.useFromFragment; 395 } 396 397 public void setUseFromFragment(boolean useFromFragment) { 398 this.useFromFragment = useFromFragment; 399 } 400 401 public boolean useWhereFragment() { 402 return useWhereFragment; 403 } 404 405 public void setUseWhereFragment(boolean b) { 406 useWhereFragment = b; 407 } 408 409 410 public void setCollectionTableAlias(String collectionTableAlias) { 411 this.collectionTableAlias = collectionTableAlias; 412 } 413 414 public String getCollectionTableAlias() { 415 return collectionTableAlias; 416 } 417 418 public boolean isCollectionOfValuesOrComponents() { 419 return elementType.isCollectionOfValuesOrComponents(); 420 } 421 422 public boolean isEntity() { 423 return elementType.isEntity(); 424 } 425 426 public void setImpliedInFromClause(boolean flag) { 427 throw new UnsupportedOperationException ( "Explicit FROM elements can't be implied in the FROM clause!" ); 428 } 429 430 public boolean isImpliedInFromClause() { 431 return false; } 433 434 public void setInProjectionList(boolean inProjectionList) { 435 } 437 438 443 public boolean inProjectionList() { 444 return !isImplied() && isFromOrJoinFragment(); 445 } 446 447 public boolean isFromOrJoinFragment() { 448 return getType() == SqlTokenTypes.FROM_FRAGMENT || getType() == SqlTokenTypes.JOIN_FRAGMENT; 449 } 450 451 public boolean isAllPropertyFetch() { 452 return isAllPropertyFetch; 453 } 454 455 public void setAllPropertyFetch(boolean fetch) { 456 isAllPropertyFetch = fetch; 457 } 458 459 public String getAdHocOnClauseFragment() { 460 return adHocOnClauseFragment; 461 } 462 463 public void setAdHocOnClauseFragment(String adHocOnClauseFragment) { 464 this.adHocOnClauseFragment = adHocOnClauseFragment; 465 } 466 467 public boolean hasCacheablePersister() { 468 if ( getQueryableCollection() != null ) { 469 return getQueryableCollection().hasCache(); 470 } 471 else { 472 return getQueryable().hasCache(); 473 } 474 } 475 } 476 | Popular Tags |