1 package org.hibernate.loader.custom; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Map ; 10 import java.util.Set ; 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 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 [] 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 [] 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 getSQLString() { 79 return customQuery.getSQL(); 80 } 81 82 protected Loadable[] getEntityPersisters() { 83 return persisters; 84 } 85 86 protected LockMode[] getLockModes(Map 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 getQuerySpaces() { 103 return customQuery.getQuerySpaces(); 104 } 105 106 protected String getQueryIdentifier() { 107 return customQuery.getSQL(); 108 } 109 110 public List 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 getResultColumnOrRow(Object [] row, ResultSet rs, SessionImplementor session) 123 throws SQLException , HibernateException { 124 125 Type[] scalarTypes = customQuery.getScalarTypes(); 126 String [] scalarColumnAliases = customQuery.getScalarColumnAliases(); 127 128 final Object [] resultRow; 129 if ( scalarTypes!=null && scalarTypes.length>0 ) { 130 resultRow = new Object [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 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 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 ps, 165 Map namedParams, 166 int start, 167 SessionImplementor session) 168 throws SQLException , HibernateException { 169 if (namedParams != null) { 170 Iterator iter = namedParams.entrySet().iterator(); 172 int result = 0; 173 while ( iter.hasNext() ) { 174 Map.Entry e = (Map.Entry ) iter.next(); 175 String name = (String ) 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 name) throws QueryException { 193 Object 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 ) { 203 return new int[] { ( (Integer ) loc ).intValue() }; 204 } 205 else { 206 return ArrayHelper.toIntArray( (List ) loc ); 207 } 208 } 209 210 } 211 | Popular Tags |