KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > custom > SQLCustomQuery


1 //$Id: SQLCustomQuery.java,v 1.16 2005/07/04 11:17:33 maxcsaucdk Exp $
2
package org.hibernate.loader.custom;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.hibernate.HibernateException;
12 import org.hibernate.LockMode;
13 import org.hibernate.engine.SessionFactoryImplementor;
14 import org.hibernate.loader.BasicLoader;
15 import org.hibernate.loader.DefaultEntityAliases;
16 import org.hibernate.loader.EntityAliases;
17 import org.hibernate.loader.ColumnEntityAliases;
18 import org.hibernate.loader.CollectionAliases;
19 import org.hibernate.loader.GeneratedCollectionAliases;
20 import org.hibernate.persister.collection.CollectionPersister;
21 import org.hibernate.persister.collection.SQLLoadableCollection;
22 import org.hibernate.persister.entity.SQLLoadable;
23 import org.hibernate.type.Type;
24 import org.hibernate.util.ArrayHelper;
25 import org.hibernate.util.StringHelper;
26
27 /**
28  * Implements Hibernate's built-in support for
29  * native SQL queries.
30  *
31  * @author Gavin King, Max Andersen
32  */

33 public class SQLCustomQuery implements CustomQuery {
34     
35     private final String JavaDoc[] entityNames;
36     private final String JavaDoc[] collectionRoles;
37     private final int[] collectionOwners;
38     private final int[] entityOwners;
39     private final LockMode[] lockModes;
40     private final String JavaDoc sql;
41     private final Set JavaDoc querySpaces = new HashSet JavaDoc();
42     private final Map JavaDoc namedParameters;
43     private final Type[] scalarTypes;
44     private final String JavaDoc[] scalarColumnAliases;
45     private final EntityAliases[] entityDescriptors;
46     private final CollectionAliases[] collectionDescriptors;
47
48     public String JavaDoc getSQL() {
49         return sql;
50     }
51     
52     public Map JavaDoc getNamedParameterBindPoints() {
53         return namedParameters;
54     }
55     
56     public String JavaDoc[] getCollectionRoles() {
57         return collectionRoles;
58     }
59
60     public String JavaDoc[] getEntityNames() {
61         return entityNames;
62     }
63
64     public LockMode[] getLockModes() {
65         return lockModes;
66     }
67
68     public EntityAliases[] getEntityAliases() {
69         return entityDescriptors;
70     }
71
72     public CollectionAliases[] getCollectionAliases() {
73         return collectionDescriptors;
74     }
75
76     public Set JavaDoc getQuerySpaces() {
77         return querySpaces;
78     }
79
80     public int[] getCollectionOwner() {
81         return collectionOwners;
82     }
83
84     public int[] getEntityOwners() {
85         return entityOwners;
86     }
87     
88     public String JavaDoc[] getScalarColumnAliases() {
89         return scalarColumnAliases;
90     }
91     
92     public Type[] getScalarTypes() {
93         return scalarTypes;
94     }
95     
96     public SQLCustomQuery(
97             final SQLQueryReturn[] queryReturns,
98             final SQLQueryScalarReturn[] scalarQueryReturns,
99             final String JavaDoc sqlQuery,
100             final Collection JavaDoc additionalQuerySpaces,
101             final SessionFactoryImplementor factory)
102     throws HibernateException {
103
104         SQLQueryReturnProcessor processor = new SQLQueryReturnProcessor(queryReturns, scalarQueryReturns, factory);
105         processor.process();
106         
107         Map JavaDoc[] propertyResultMaps = (Map JavaDoc[]) processor.getPropertyResults().toArray( new Map JavaDoc[0] );
108         Map JavaDoc[] collectionResultMaps = (Map JavaDoc[]) processor.getCollectionPropertyResults().toArray( new Map JavaDoc[0] );
109         
110         List JavaDoc collectionSuffixes = new ArrayList JavaDoc();
111         List JavaDoc collectionOwnerAliases = processor.getCollectionOwnerAliases();
112         List JavaDoc collectionPersisters = processor.getCollectionPersisters();
113         int size = collectionPersisters.size();
114         if (size!=0) {
115             collectionOwners = new int[size];
116             collectionRoles = new String JavaDoc[size];
117             collectionDescriptors = new CollectionAliases[size];
118             for ( int i=0; i<size; i++ ) {
119                 CollectionPersister collectionPersister = (CollectionPersister) collectionPersisters.get(i);
120                 collectionRoles[i] = ( collectionPersister ).getRole();
121                 collectionOwners[i] = processor.getAliases().indexOf( collectionOwnerAliases.get(i) );
122                 String JavaDoc suffix = i + "__";
123                 collectionSuffixes.add(suffix);
124                 collectionDescriptors[i] = new GeneratedCollectionAliases( collectionResultMaps[i], collectionPersister, suffix );
125             }
126         }
127         else {
128             collectionRoles = null;
129             collectionDescriptors = null;
130             collectionOwners = null;
131         }
132
133         String JavaDoc[] aliases = ArrayHelper.toStringArray( processor.getAliases() );
134         String JavaDoc[] collAliases = ArrayHelper.toStringArray( processor.getCollectionAliases() );
135         String JavaDoc[] collSuffixes = ArrayHelper.toStringArray(collectionSuffixes);
136         
137         SQLLoadable[] entityPersisters = (SQLLoadable[]) processor.getPersisters().toArray( new SQLLoadable[0] );
138         SQLLoadableCollection[] collPersisters = (SQLLoadableCollection[]) collectionPersisters.toArray( new SQLLoadableCollection[0] );
139         lockModes = (LockMode[]) processor.getLockModes().toArray( new LockMode[0] );
140
141         scalarColumnAliases = ArrayHelper.toStringArray( processor.getScalarColumnAliases() );
142         scalarTypes = ArrayHelper.toTypeArray( processor.getScalarTypes() );
143
144         String JavaDoc[] suffixes = BasicLoader.generateSuffixes(entityPersisters.length);
145
146         SQLQueryParser parser = new SQLQueryParser(
147                 sqlQuery,
148                 processor.getAlias2Persister(),
149                 processor.getAlias2Return(),
150                 aliases,
151                 collAliases,
152                 collPersisters,
153                 suffixes,
154                 collSuffixes
155         );
156
157         sql = parser.process();
158         
159         namedParameters = parser.getNamedParameters();
160
161         // Populate entityNames, entityDescrptors and querySpaces
162
entityNames = new String JavaDoc[entityPersisters.length];
163         entityDescriptors = new EntityAliases[entityPersisters.length];
164         for (int i = 0; i < entityPersisters.length; i++) {
165             SQLLoadable persister = entityPersisters[i];
166             //alias2Persister.put( aliases[i], persister );
167
//TODO: Does not consider any other tables referenced in the query
168
ArrayHelper.addAll( querySpaces, persister.getQuerySpaces() );
169             entityNames[i] = persister.getEntityName();
170             if ( parser.queryHasAliases() ) {
171                 entityDescriptors[i] = new DefaultEntityAliases(
172                         propertyResultMaps[i],
173                         entityPersisters[i],
174                         suffixes[i]
175                     );
176             }
177             else {
178                 entityDescriptors[i] = new ColumnEntityAliases(
179                         propertyResultMaps[i],
180                         entityPersisters[i],
181                         suffixes[i]
182                     );
183             }
184         }
185         if (additionalQuerySpaces!=null) {
186             querySpaces.addAll(additionalQuerySpaces);
187         }
188
189         // Resolve owners
190
Map JavaDoc alias2OwnerAlias = processor.getAlias2OwnerAlias();
191         int[] ownersArray = new int[entityPersisters.length];
192         for ( int j=0; j < aliases.length; j++ ) {
193             String JavaDoc ownerAlias = (String JavaDoc) alias2OwnerAlias.get( aliases[j] );
194             if ( StringHelper.isNotEmpty(ownerAlias) ) {
195                 ownersArray[j] = processor.getAliases().indexOf( ownerAlias );
196             }
197             else {
198                 ownersArray[j] = -1;
199             }
200         }
201         if ( ArrayHelper.isAllNegative(ownersArray) ) {
202             ownersArray = null;
203         }
204         this.entityOwners = ownersArray;
205
206     }
207
208 }
209
Popular Tags