1 | void addQuerySpaces(Serializable[] spaces) { | | 1 | private void renderSQL() throws QueryException, MappingException { |
2 | for ( int i = 0; i < spaces.length; i++ ) { | | 2 | |
3 | querySpaces.add( spaces[i] ); | | 3 | final int rtsize; |
4 | } | | 4 | if ( returnedTypes.size() == 0 && scalarTypes.size() == 0 ) { |
5 | if ( superQuery != null ) superQuery.addQuerySpaces( spaces ); | | 5 | //ie no select clause in HQL |
6 | } | | 6 | returnedTypes = fromTypes; |
| | | 7 | rtsize = returnedTypes.size(); |
| | | 8 | } |
| | | 9 | else { |
| | | 10 | rtsize = returnedTypes.size(); |
| | | 11 | Iterator iter = entitiesToFetch.iterator(); |
| | | 12 | while ( iter.hasNext() ) { |
| | | 13 | returnedTypes.add( iter.next() ); |
| | | 14 | } |
| | | 15 | } |
| | | 16 | int size = returnedTypes.size(); |
| | | 17 | persisters = new Queryable[size]; |
| | | 18 | names = new String[size]; |
| | | 19 | owners = new int[size]; |
| | | 20 | ownerAssociationTypes = new EntityType[size]; |
| | | 21 | suffixes = new String[size]; |
| | | 22 | includeInSelect = new boolean[size]; |
| | | 23 | for ( int i = 0; i < size; i++ ) { |
| | | 24 | String name = ( String ) returnedTypes.get( i ); |
| | | 25 | //if ( !isName(name) ) throw new QueryException("unknown type: " + name); |
| | | 26 | persisters[i] = getEntityPersisterForName( name ); |
| | | 27 | // TODO: cannot use generateSuffixes() - it handles the initial suffix differently. |
| | | 28 | suffixes[i] = ( size == 1 ) ? "" : Integer.toString( i ) + '_'; |
| | | 29 | names[i] = name; |
| | | 30 | includeInSelect[i] = !entitiesToFetch.contains( name ); |
| | | 31 | if ( includeInSelect[i] ) selectLength++; |
| | | 32 | if ( name.equals( collectionOwnerName ) ) collectionOwnerColumn = i; |
| | | 33 | String oneToOneOwner = ( String ) oneToOneOwnerNames.get( name ); |
| | | 34 | owners[i] = ( oneToOneOwner == null ) ? -1 : returnedTypes.indexOf( oneToOneOwner ); |
| | | 35 | ownerAssociationTypes[i] = (EntityType) uniqueKeyOwnerReferences.get( name ); |
| | | 36 | } |
| | | 37 | |
| | | 38 | if ( ArrayHelper.isAllNegative( owners ) ) owners = null; |
| | | 39 | |
| | | 40 | String scalarSelect = renderScalarSelect(); //Must be done here because of side-effect! yuck... |
| | | 41 | |
| | | 42 | int scalarSize = scalarTypes.size(); |
| | | 43 | hasScalars = scalarTypes.size() != rtsize; |
| | | 44 | |
| | | 45 | returnTypes = new Type[scalarSize]; |
| | | 46 | for ( int i = 0; i < scalarSize; i++ ) { |
| | | 47 | returnTypes[i] = ( Type ) scalarTypes.get( i ); |
| | | 48 | } |
| | | 49 | |
| | | 50 | QuerySelect sql = new QuerySelect( getFactory().getDialect() ); |
| | | 51 | sql.setDistinct( distinct ); |
| | | 52 | |
| | | 53 | if ( !shallowQuery ) { |
| | | 54 | renderIdentifierSelect( sql ); |
| | | 55 | renderPropertiesSelect( sql ); |
| | | 56 | } |
| | | 57 | |
| | | 58 | if ( collectionPersister != null ) { |
| | | 59 | sql.addSelectFragmentString( collectionPersister.selectFragment( fetchName, "__" ) ); |
| | | 60 | } |
| | | 61 | |
| | | 62 | if ( hasScalars || shallowQuery ) sql.addSelectFragmentString( scalarSelect ); |
| | | 63 | |
| | | 64 | //TODO: for some dialects it would be appropriate to add the renderOrderByPropertiesSelect() to other select strings |
| | | 65 | mergeJoins( sql.getJoinFragment() ); |
| | | 66 | |
| | | 67 | sql.setWhereTokens( whereTokens.iterator() ); |
| | | 68 | |
| | | 69 | sql.setGroupByTokens( groupByTokens.iterator() ); |
| | | 70 | sql.setHavingTokens( havingTokens.iterator() ); |
| | | 71 | sql.setOrderByTokens( orderByTokens.iterator() ); |
| | | 72 | |
| | | 73 | if ( collectionPersister != null && collectionPersister.hasOrdering() ) { |
| | | 74 | sql.addOrderBy( collectionPersister.getSQLOrderByString( fetchName ) ); |
| | | 75 | } |
| | | 76 | |
| | | 77 | scalarColumnNames = NameGenerator.generateColumnNames( returnTypes, getFactory() ); |
| | | 78 | |
| | | 79 | // initialize the Set of queried identifier spaces (ie. tables) |
| | | 80 | Iterator iter = collections.values().iterator(); |
| | | 81 | while ( iter.hasNext() ) { |
| | | 82 | CollectionPersister p = getCollectionPersister( ( String ) iter.next() ); |
| | | 83 | addQuerySpaces( p.getCollectionSpaces() ); |
| | | 84 | } |
| | | 85 | iter = typeMap.keySet().iterator(); |
| | | 86 | while ( iter.hasNext() ) { |
| | | 87 | Queryable p = getEntityPersisterForName( ( String ) iter.next() ); |
| | | 88 | addQuerySpaces( p.getQuerySpaces() ); |
| | | 89 | } |
| | | 90 | |
| | | 91 | sqlString = sql.toQueryString(); |
| | | 92 | |
| | | 93 | if ( holderClass != null ) holderConstructor = ReflectHelper.getConstructor( holderClass, returnTypes ); |
| | | 94 | |
| | | 95 | if ( hasScalars ) { |
| | | 96 | actualReturnTypes = returnTypes; |
| | | 97 | } |
| | | 98 | else { |
| | | 99 | actualReturnTypes = new Type[selectLength]; |
| | | 100 | int j = 0; |
| | | 101 | for ( int i = 0; i < persisters.length; i++ ) { |
| | | 102 | if ( includeInSelect[i] ) { |
| | | 103 | actualReturnTypes[j++] = TypeFactory.manyToOne( persisters[i].getEntityName(), shallowQuery ); |
| | | 104 | } |
| | | 105 | } |
| | | 106 | } |
| | | 107 | |
| | | 108 | } |