1 21 package oracle.toplink.essentials.queryframework; 23 import java.util.*; 24 25 import oracle.toplink.essentials.internal.localization.ExceptionLocalization; 26 import oracle.toplink.essentials.exceptions.QueryException; 27 import oracle.toplink.essentials.exceptions.DatabaseException; 28 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 29 import oracle.toplink.essentials.sessions.DatabaseRecord; 30 31 45 46 public class ResultSetMappingQuery extends ObjectBuildingQuery { 47 48 protected String resultSetMappingName; 49 50 protected SQLResultSetMapping resultSetMapping; 51 52 56 public ResultSetMappingQuery() { 57 super(); 58 } 59 60 64 public ResultSetMappingQuery(Call call) { 65 this(); 66 setCall(call); 67 } 68 69 73 public ResultSetMappingQuery(Call call, String sqlResultSetMappingName) { 74 this(); 75 setCall(call); 76 this.resultSetMappingName = sqlResultSetMappingName; 77 } 78 79 83 public Object clone() { 84 ResultSetMappingQuery cloneQuery = (ResultSetMappingQuery)super.clone(); 85 cloneQuery.resultSetMapping = this.resultSetMapping; 86 cloneQuery.resultSetMappingName = this.resultSetMappingName; 87 return cloneQuery; 88 } 89 90 97 public void convertClassNamesToClasses(ClassLoader classLoader){ 98 resultSetMapping.convertClassNamesToClasses(classLoader); 99 }; 100 101 105 112 117 public void setSQLResultSetMapping(SQLResultSetMapping resultSetMapping){ 118 this.resultSetMapping = resultSetMapping; 119 this.resultSetMappingName = resultSetMapping.getName(); 120 } 121 122 127 public void setSQLResultSetMappingName(String name){ 128 if (name == null && this.resultSetMapping == null){ 129 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("null_sqlresultsetmapping_in_query")); 130 } 131 this.resultSetMappingName = name; 132 133 } 134 135 140 protected List buildObjectsFromRecords(List databaseRecords){ 141 List results = new ArrayList(databaseRecords.size() ); 142 SQLResultSetMapping mapping = this.getSQLResultSetMapping(); 143 for (Iterator iterator = databaseRecords.iterator(); iterator.hasNext();){ 144 if (mapping.getResults().size()>1){ 145 Object [] resultElement = new Object [mapping.getResults().size()]; 146 DatabaseRecord record = (DatabaseRecord)iterator.next(); 147 for (int i = 0;i<mapping.getResults().size();i++){ 148 resultElement[i] = ((SQLResult)mapping.getResults().get(i)).getValueFromRecord(record, this); 149 } 150 results.add(resultElement); 151 }else if (mapping.getResults().size()==1) { 152 DatabaseRecord record = (DatabaseRecord)iterator.next(); 153 results.add( ((SQLResult)mapping.getResults().get(0)).getValueFromRecord(record, this)); 154 }else { 155 return results; 156 } 157 } 158 return results; 159 160 } 161 162 166 public Object executeDatabaseQuery() throws DatabaseException { 167 if (getSession().isUnitOfWork()) { 168 UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)getSession(); 169 170 if ((!unitOfWork.getCommitManager().isActive()) && (!unitOfWork.wasTransactionBegunPrematurely())) { 173 unitOfWork.beginTransaction(); 174 unitOfWork.setWasTransactionBegunPrematurely(true); 175 } 176 if (unitOfWork.isNestedUnitOfWork()) { 177 UnitOfWorkImpl nestedUnitOfWork = (UnitOfWorkImpl)getSession(); 179 setSession(nestedUnitOfWork.getParent()); 180 Object result = executeDatabaseQuery(); 181 setSession(nestedUnitOfWork); 182 Object clone = registerIndividualResult(result, unitOfWork, false, null); 183 184 if (shouldUseWrapperPolicy()) { 185 clone = getDescriptor().getObjectBuilder().wrapObject(clone, unitOfWork); 186 } 187 return clone; 188 } 189 } 190 session.validateQuery(this); 192 if (getQueryId() == 0) { 193 setQueryId(getSession().getNextQueryId()); 194 } 195 196 Vector rows = getQueryMechanism().executeSelect(); 197 setExecutionTime(System.currentTimeMillis()); 198 return buildObjectsFromRecords(rows); 200 } 201 202 206 protected void prepare() { 207 if ((!shouldMaintainCache()) && shouldRefreshIdentityMapResult()) { 208 throw QueryException.refreshNotPossibleWithoutCache(this); 209 } 210 211 getQueryMechanism().prepare(); 212 213 getQueryMechanism().prepareExecuteSelect(); 214 } 215 216 221 public SQLResultSetMapping getSQLResultSetMapping(){ 222 if (this.resultSetMapping == null && this.resultSetMappingName != null){ 223 this.resultSetMapping = this.getSession().getProject().getSQLResultSetMapping(this.resultSetMappingName); 224 } 225 return this.resultSetMapping; 226 } 227 228 232 public String getSQLResultSetMappingName() { 233 return this.resultSetMappingName; 234 } 235 } 236 | Popular Tags |