1 package org.hibernate.loader.custom; 3 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.HashSet ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 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 33 public class SQLCustomQuery implements CustomQuery { 34 35 private final String [] entityNames; 36 private final String [] collectionRoles; 37 private final int[] collectionOwners; 38 private final int[] entityOwners; 39 private final LockMode[] lockModes; 40 private final String sql; 41 private final Set querySpaces = new HashSet (); 42 private final Map namedParameters; 43 private final Type[] scalarTypes; 44 private final String [] scalarColumnAliases; 45 private final EntityAliases[] entityDescriptors; 46 private final CollectionAliases[] collectionDescriptors; 47 48 public String getSQL() { 49 return sql; 50 } 51 52 public Map getNamedParameterBindPoints() { 53 return namedParameters; 54 } 55 56 public String [] getCollectionRoles() { 57 return collectionRoles; 58 } 59 60 public String [] 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 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 [] 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 sqlQuery, 100 final Collection additionalQuerySpaces, 101 final SessionFactoryImplementor factory) 102 throws HibernateException { 103 104 SQLQueryReturnProcessor processor = new SQLQueryReturnProcessor(queryReturns, scalarQueryReturns, factory); 105 processor.process(); 106 107 Map [] propertyResultMaps = (Map []) processor.getPropertyResults().toArray( new Map [0] ); 108 Map [] collectionResultMaps = (Map []) processor.getCollectionPropertyResults().toArray( new Map [0] ); 109 110 List collectionSuffixes = new ArrayList (); 111 List collectionOwnerAliases = processor.getCollectionOwnerAliases(); 112 List collectionPersisters = processor.getCollectionPersisters(); 113 int size = collectionPersisters.size(); 114 if (size!=0) { 115 collectionOwners = new int[size]; 116 collectionRoles = new String [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 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 [] aliases = ArrayHelper.toStringArray( processor.getAliases() ); 134 String [] collAliases = ArrayHelper.toStringArray( processor.getCollectionAliases() ); 135 String [] 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 [] 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 entityNames = new String [entityPersisters.length]; 163 entityDescriptors = new EntityAliases[entityPersisters.length]; 164 for (int i = 0; i < entityPersisters.length; i++) { 165 SQLLoadable persister = entityPersisters[i]; 166 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 Map alias2OwnerAlias = processor.getAlias2OwnerAlias(); 191 int[] ownersArray = new int[entityPersisters.length]; 192 for ( int j=0; j < aliases.length; j++ ) { 193 String ownerAlias = (String ) 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 |