01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package com.ibatis.sqlmap.engine.mapping.result.loader;
17:
18: import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
19: import com.ibatis.sqlmap.engine.type.DomCollectionTypeMarker;
20:
21: import java.sql.SQLException;
22: import java.util.Collection;
23: import java.util.HashSet;
24: import java.util.List;
25: import java.util.Set;
26:
27: /**
28: * Class to load results into objects
29: */
30: public class ResultLoader {
31:
32: private ResultLoader() {
33: }
34:
35: /**
36: * Loads a result lazily
37: *
38: * @param client - the client creating the object
39: * @param statementName - the name of the statement to be used
40: * @param parameterObject - the parameters for the statement
41: * @param targetType - the target type of the result
42: * @return the loaded result
43: * @throws SQLException
44: */
45: public static Object loadResult(ExtendedSqlMapClient client,
46: String statementName, Object parameterObject,
47: Class targetType) throws SQLException {
48: Object value = null;
49:
50: if (client.isLazyLoadingEnabled()) {
51: if (client.isEnhancementEnabled()) {
52: EnhancedLazyResultLoader lazy = new EnhancedLazyResultLoader(
53: client, statementName, parameterObject,
54: targetType);
55: value = lazy.loadResult();
56: } else {
57: LazyResultLoader lazy = new LazyResultLoader(client,
58: statementName, parameterObject, targetType);
59: value = lazy.loadResult();
60: }
61: } else {
62: value = getResult(client, statementName, parameterObject,
63: targetType);
64: }
65:
66: return value;
67: }
68:
69: protected static Object getResult(ExtendedSqlMapClient client,
70: String statementName, Object parameterObject,
71: Class targetType) throws SQLException {
72: Object value = null;
73: if (DomCollectionTypeMarker.class.isAssignableFrom(targetType)) {
74: value = client.queryForList(statementName, parameterObject);
75: } else if (Set.class.isAssignableFrom(targetType)) {
76: value = new HashSet(client.queryForList(statementName,
77: parameterObject));
78: } else if (Collection.class.isAssignableFrom(targetType)) {
79: value = client.queryForList(statementName, parameterObject);
80: } else if (targetType.isArray()) {
81: List list = client.queryForList(statementName,
82: parameterObject);
83: value = listToArray(list, targetType.getComponentType());
84: } else {
85: value = client.queryForObject(statementName,
86: parameterObject);
87: }
88: return value;
89: }
90:
91: private static Object[] listToArray(List list, Class type) {
92: Object array = java.lang.reflect.Array.newInstance(type, list
93: .size());
94: array = list.toArray((Object[]) array);
95: return (Object[]) array;
96: }
97:
98: }
|