KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CustomLoader.java,v 1.6 2005/05/19 22:38:08 oneovthafew Exp $
2
package org.hibernate.loader.custom;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import org.hibernate.HibernateException;
13 import org.hibernate.LockMode;
14 import org.hibernate.QueryException;
15 import org.hibernate.ScrollableResults;
16 import org.hibernate.engine.QueryParameters;
17 import org.hibernate.engine.SessionFactoryImplementor;
18 import org.hibernate.engine.SessionImplementor;
19 import org.hibernate.engine.TypedValue;
20 import org.hibernate.loader.EntityAliases;
21 import org.hibernate.loader.Loader;
22 import org.hibernate.loader.CollectionAliases;
23 import org.hibernate.persister.collection.CollectionPersister;
24 import org.hibernate.persister.entity.Loadable;
25 import org.hibernate.type.Type;
26 import org.hibernate.type.TypeFactory;
27 import org.hibernate.util.ArrayHelper;
28
29 /**
30  * Extension point for loaders which use a SQL
31  * result set with "unexpected" column aliases.
32  *
33  * @author Gavin King
34  */

35 public class CustomLoader extends Loader {
36     
37     private final Type[] resultTypes;
38     private final Loadable[] persisters;
39     private final CollectionPersister[] collectionPersisters;
40     private final CustomQuery customQuery;
41
42     public CustomLoader(
43             CustomQuery customQuery,
44             SessionFactoryImplementor factory
45     ) {
46         super(factory);
47         this.customQuery = customQuery;
48         
49         String JavaDoc[] collectionRoles = customQuery.getCollectionRoles();
50         if ( collectionRoles==null ) {
51             collectionPersisters = null;
52         }
53         else {
54             int length = collectionRoles.length;
55             collectionPersisters = new CollectionPersister[length];
56             for ( int i=0; i<length; i++ ) {
57                 collectionPersisters[i] = factory.getCollectionPersister( collectionRoles[i] );
58             }
59         }
60         
61         String JavaDoc[] entityNames = customQuery.getEntityNames();
62         persisters = new Loadable[entityNames.length];
63         for ( int i=0; i<entityNames.length; i++ ) {
64             persisters[i] = (Loadable) factory.getEntityPersister( entityNames[i] );
65         }
66         
67         Type[] scalarTypes = customQuery.getScalarTypes();
68         
69         resultTypes = new Type[ entityNames.length + ( scalarTypes==null ? 0 : scalarTypes.length ) ];
70         for ( int i=0; i<scalarTypes.length; i++ ) {
71             resultTypes[i] = scalarTypes[i];
72         }
73         for ( int i=0; i<entityNames.length; i++ ) {
74             resultTypes[i + scalarTypes.length] = TypeFactory.manyToOne( entityNames[i] );
75         }
76     }
77
78     protected String JavaDoc getSQLString() {
79         return customQuery.getSQL();
80     }
81
82     protected Loadable[] getEntityPersisters() {
83         return persisters;
84     }
85
86     protected LockMode[] getLockModes(Map JavaDoc lockModesMap) {
87         return customQuery.getLockModes();
88     }
89
90     protected CollectionPersister[] getCollectionPersisters() {
91         return collectionPersisters;
92     }
93
94     protected int[] getCollectionOwners() {
95         return customQuery.getCollectionOwner();
96     }
97     
98     protected int[] getOwners() {
99         return customQuery.getEntityOwners();
100     }
101     
102     public Set JavaDoc getQuerySpaces() {
103         return customQuery.getQuerySpaces();
104     }
105     
106     protected String JavaDoc getQueryIdentifier() {
107         return customQuery.getSQL();
108     }
109
110     public List JavaDoc list(SessionImplementor session, QueryParameters queryParameters)
111     throws HibernateException {
112         return list(session, queryParameters, customQuery.getQuerySpaces(), resultTypes);
113     }
114
115     public ScrollableResults scroll(
116             final QueryParameters queryParameters,
117             final SessionImplementor session)
118     throws HibernateException {
119         return scroll(queryParameters, resultTypes, null, session);
120     }
121     
122     protected Object JavaDoc getResultColumnOrRow(Object JavaDoc[] row, ResultSet JavaDoc rs, SessionImplementor session)
123     throws SQLException JavaDoc, HibernateException {
124         
125         Type[] scalarTypes = customQuery.getScalarTypes();
126         String JavaDoc[] scalarColumnAliases = customQuery.getScalarColumnAliases();
127         
128         final Object JavaDoc[] resultRow;
129         if ( scalarTypes!=null && scalarTypes.length>0 ) {
130             //all scalar results appear first
131
resultRow = new Object JavaDoc[scalarTypes.length + row.length];
132             for ( int i=0; i<scalarTypes.length; i++ ) {
133                 resultRow[i] = scalarTypes[i].nullSafeGet(rs, scalarColumnAliases[i], session, null);
134             }
135             //then entity results
136
System.arraycopy(row, 0, resultRow, scalarTypes.length, row.length);
137         }
138         else {
139             resultRow = row;
140         }
141         
142         return resultRow.length == 1 ? resultRow[0] : resultRow;
143     }
144
145     /*protected Object getResultColumnOrRow(Object[] row, ResultSet rs, SessionImplementor session)
146     throws SQLException, HibernateException {
147         if (persisters.length == 1) {
148             return row[row.length - 1];
149         }
150         else {
151             return row;
152         }
153     }*/

154
155     protected EntityAliases[] getEntityAliases() {
156         return customQuery.getEntityAliases();
157     }
158
159     protected CollectionAliases[] getCollectionAliases() {
160         return customQuery.getCollectionAliases();
161     }
162
163     protected int bindNamedParameters(
164             PreparedStatement JavaDoc ps,
165             Map JavaDoc namedParams,
166             int start,
167             SessionImplementor session)
168     throws SQLException JavaDoc, HibernateException {
169         if (namedParams != null) {
170             // assumes that types are all of span 1
171
Iterator JavaDoc iter = namedParams.entrySet().iterator();
172             int result = 0;
173             while ( iter.hasNext() ) {
174                 Map.Entry JavaDoc e = (Map.Entry JavaDoc) iter.next();
175                 String JavaDoc name = (String JavaDoc) e.getKey();
176                 TypedValue typedval = (TypedValue) e.getValue();
177                 int[] locs = getNamedParameterLocs(name);
178                 for (int i = 0; i < locs.length; i++) {
179                     typedval.getType().nullSafeSet(
180                         ps, typedval.getValue(), locs[i] + start, session
181                     );
182                 }
183                 result += locs.length;
184             }
185             return result;
186         }
187         else {
188             return 0;
189         }
190     }
191     
192     protected int[] getNamedParameterLocs(String JavaDoc name) throws QueryException {
193         Object JavaDoc loc = customQuery.getNamedParameterBindPoints().get(name);
194         if (loc == null) {
195             QueryException qe = new QueryException(
196                     "Named parameter does not appear in Query: " +
197                     name,
198                     customQuery.getSQL()
199             );
200             throw qe;
201         }
202         if (loc instanceof Integer JavaDoc) {
203             return new int[] { ( (Integer JavaDoc) loc ).intValue() };
204         }
205         else {
206             return ArrayHelper.toIntArray( (List JavaDoc) loc );
207         }
208     }
209
210 }
211
Popular Tags