KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > result > loader > ResultLoader


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
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 JavaDoc loadResult(ExtendedSqlMapClient client, String JavaDoc statementName, Object JavaDoc parameterObject, Class JavaDoc targetType)
46       throws SQLException JavaDoc {
47     Object JavaDoc value = null;
48
49
50     if (client.isLazyLoadingEnabled()) {
51       if (client.isEnhancementEnabled()) {
52         EnhancedLazyResultLoader lazy = new EnhancedLazyResultLoader(client, statementName, parameterObject, targetType);
53         value = lazy.loadResult();
54       } else {
55         LazyResultLoader lazy = new LazyResultLoader(client, statementName, parameterObject, targetType);
56         value = lazy.loadResult();
57       }
58     } else {
59       value = getResult(client, statementName, parameterObject, targetType);
60     }
61
62     return value;
63   }
64
65   protected static Object JavaDoc getResult(ExtendedSqlMapClient client, String JavaDoc statementName, Object JavaDoc parameterObject, Class JavaDoc targetType) throws SQLException JavaDoc {
66     Object JavaDoc value = null;
67     if (DomCollectionTypeMarker.class.isAssignableFrom(targetType)) {
68       value = client.queryForList(statementName, parameterObject);
69     } else if (Set JavaDoc.class.isAssignableFrom(targetType)) {
70       value = new HashSet JavaDoc(client.queryForList(statementName, parameterObject));
71     } else if (Collection JavaDoc.class.isAssignableFrom(targetType)) {
72       value = client.queryForList(statementName, parameterObject);
73     } else if (targetType.isArray()) {
74       List JavaDoc list = client.queryForList(statementName, parameterObject);
75       value = listToArray(list, targetType.getComponentType());
76     } else {
77       value = client.queryForObject(statementName, parameterObject);
78     }
79     return value;
80   }
81
82
83   private static Object JavaDoc[] listToArray(List JavaDoc list, Class JavaDoc type) {
84     Object JavaDoc array = java.lang.reflect.Array.newInstance(type, list.size());
85     array = list.toArray((Object JavaDoc[]) array);
86     return (Object JavaDoc[]) array;
87   }
88
89 }
90
Popular Tags