1 package org.hibernate.hql.ast.util; 3 4 import java.util.HashMap ; 5 import java.util.Map ; 6 7 import org.hibernate.MappingException; 8 import org.hibernate.QueryException; 9 import org.hibernate.dialect.function.SQLFunction; 10 import org.hibernate.engine.JoinSequence; 11 import org.hibernate.engine.SessionFactoryImplementor; 12 import org.hibernate.hql.NameGenerator; 13 import org.hibernate.hql.ast.DetailedSemanticException; 14 import org.hibernate.hql.ast.QuerySyntaxException; 15 import org.hibernate.hql.ast.tree.SqlNode; 16 import org.hibernate.persister.collection.CollectionPropertyMapping; 17 import org.hibernate.persister.collection.CollectionPropertyNames; 18 import org.hibernate.persister.collection.QueryableCollection; 19 import org.hibernate.persister.entity.EntityPersister; 20 import org.hibernate.persister.entity.PropertyMapping; 21 import org.hibernate.persister.entity.Queryable; 22 import org.hibernate.type.AssociationType; 23 import org.hibernate.type.CollectionType; 24 import org.hibernate.type.EntityType; 25 import org.hibernate.type.Type; 26 import org.hibernate.type.TypeFactory; 27 28 import antlr.SemanticException; 29 import antlr.collections.AST; 30 31 36 public class SessionFactoryHelper { 37 private SessionFactoryImplementor sfi; 38 private Map collectionPropertyMappingByRole; 39 40 45 public SessionFactoryHelper(SessionFactoryImplementor sfi) { 46 this.sfi = sfi; 47 collectionPropertyMappingByRole = new HashMap (); 48 } 49 50 55 public SessionFactoryImplementor getFactory() { 56 return sfi; 57 } 58 59 66 public boolean hasPhysicalDiscriminatorColumn(Queryable persister) { 67 if ( persister.getDiscriminatorType() != null ) { 68 String discrimColumnName = persister.getDiscriminatorColumnName(); 69 if ( discrimColumnName != null && !"clazz_".equals( discrimColumnName ) ) { 72 return true; 73 } 74 } 75 return false; 76 } 77 78 84 public String getImportedClassName(String className) { 85 return sfi.getImportedClassName( className ); 86 } 87 88 94 public Queryable findQueryableUsingImports(String className) { 95 final String importedClassName = sfi.getImportedClassName( className ); 96 if ( importedClassName == null ) { 97 return null; 98 } 99 try { 100 return ( Queryable ) sfi.getEntityPersister( importedClassName ); 101 } 102 catch ( MappingException me ) { 103 return null; 104 } 105 } 106 107 114 private EntityPersister findEntityPersisterByName(String name) throws MappingException { 115 try { 117 return sfi.getEntityPersister( name ); 118 } 119 catch ( MappingException ignore ) { 120 } 122 123 String importedClassName = sfi.getImportedClassName( name ); 125 if ( importedClassName == null ) { 126 return null; 127 } 128 return sfi.getEntityPersister( importedClassName ); 129 } 130 131 139 public EntityPersister requireClassPersister(String name) throws SemanticException { 140 EntityPersister cp; 141 try { 142 cp = findEntityPersisterByName( name ); 143 if ( cp == null ) { 144 throw new QuerySyntaxException( new SemanticException( name + " is not mapped." ) ); 146 } 147 } 148 catch ( MappingException e ) { 149 throw new DetailedSemanticException( e.getMessage(), e ); 150 } 151 return cp; 152 } 153 154 160 public QueryableCollection getCollectionPersister(String role) { 161 try { 162 return ( QueryableCollection ) sfi.getCollectionPersister( role ); 163 } 164 catch ( ClassCastException cce ) { 165 throw new QueryException( "collection is not queryable: " + role ); 166 } 167 catch ( Exception e ) { 168 throw new QueryException( "collection not found: " + role ); 169 } 170 } 171 172 180 public QueryableCollection requireQueryableCollection(String role) throws QueryException { 181 try { 182 QueryableCollection queryableCollection = ( QueryableCollection ) sfi.getCollectionPersister( role ); 183 if ( queryableCollection != null ) { 184 collectionPropertyMappingByRole.put( role, new CollectionPropertyMapping( queryableCollection ) ); 185 } 186 return queryableCollection; 187 } 188 catch ( ClassCastException cce ) { 189 throw new QueryException( "collection role is not queryable: " + role ); 190 } 191 catch ( Exception e ) { 192 throw new QueryException( "collection role not found: " + role ); 193 } 194 } 195 196 202 private PropertyMapping getCollectionPropertyMapping(String role) { 203 return ( PropertyMapping ) collectionPropertyMappingByRole.get( role ); 204 } 205 206 214 public String [] getCollectionElementColumns(String role, String roleAlias) { 215 return getCollectionPropertyMapping( role ).toColumns( roleAlias, CollectionPropertyNames.COLLECTION_ELEMENTS ); 216 } 217 218 223 public JoinSequence createJoinSequence() { 224 return new JoinSequence( sfi ); 225 } 226 227 237 public JoinSequence createJoinSequence(boolean implicit, AssociationType associationType, String tableAlias, int joinType, String [] columns) { 238 JoinSequence joinSequence = createJoinSequence(); 239 joinSequence.setUseThetaStyle( implicit ); joinSequence.addJoin( associationType, tableAlias, joinType, columns ); 241 return joinSequence; 242 } 243 244 251 public JoinSequence createCollectionJoinSequence(QueryableCollection collPersister, String collectionName) { 252 JoinSequence joinSequence = createJoinSequence(); 253 joinSequence.setRoot( collPersister, collectionName ); 254 joinSequence.setUseThetaStyle( true ); return joinSequence; 260 } 261 262 270 public String getIdentifierOrUniqueKeyPropertyName(EntityType entityType) { 271 try { 272 return entityType.getIdentifierOrUniqueKeyPropertyName( sfi ); 273 } 274 catch ( MappingException me ) { 275 throw new QueryException( me ); 276 } 277 } 278 279 285 public int getColumnSpan(Type type) { 286 return type.getColumnSpan( sfi ); 287 } 288 289 296 public String getAssociatedEntityName(CollectionType collectionType) { 297 return collectionType.getAssociatedEntityName( sfi ); 298 } 299 300 307 private Type getElementType(CollectionType collectionType) { 308 return collectionType.getElementType( sfi ); 309 } 310 311 318 public AssociationType getElementAssociationType(CollectionType collectionType) { 319 return ( AssociationType ) getElementType( collectionType ); 320 } 321 322 328 public SQLFunction findSQLFunction(String functionName) { 329 return ( SQLFunction ) sfi.getDialect().getFunctions().get( functionName.toLowerCase() ); 330 } 331 332 339 private SQLFunction requireSQLFunction(String functionName) { 340 SQLFunction f = findSQLFunction( functionName ); 341 if ( f == null ) { 342 throw new QueryException( "Unable to find SQL function: " + functionName ); 343 } 344 return f; 345 } 346 347 354 public Type findFunctionReturnType(String functionName, AST first) { 355 Type argumentType = null; 356 if ( "cast".equals(functionName) ) { 357 argumentType = TypeFactory.heuristicType( first.getNextSibling().getText() ); 358 } 359 else if ( first != null && first instanceof SqlNode ) { 360 argumentType = ( (SqlNode) first ).getDataType(); 361 } 362 return requireSQLFunction( functionName ).getReturnType( argumentType, sfi ); 364 } 365 366 public String [][] generateColumnNames(Type[] sqlResultTypes) { 367 return NameGenerator.generateColumnNames( sqlResultTypes, sfi ); 368 } 369 } 370 | Popular Tags |