View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
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    *    https://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.SqlMapClientImpl;
19  import com.ibatis.sqlmap.engine.type.DomCollectionTypeMarker;
20  
21  import java.lang.reflect.Array;
22  import java.sql.SQLException;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Set;
28  
29  /**
30   * Class to load results into objects.
31   */
32  public class ResultLoader {
33  
34    /**
35     * Instantiates a new result loader.
36     */
37    private ResultLoader() {
38    }
39  
40    /**
41     * Loads a result lazily.
42     *
43     * @param client
44     *          - the client creating the object
45     * @param statementName
46     *          - the name of the statement to be used
47     * @param parameterObject
48     *          - the parameters for the statement
49     * @param targetType
50     *          - the target type of the result
51     *
52     * @return the loaded result
53     *
54     * @throws SQLException
55     *           the SQL exception
56     */
57    public static Object loadResult(SqlMapClientImpl client, String statementName, Object parameterObject,
58        Class targetType) throws SQLException {
59      Object value = null;
60  
61      if (client.isLazyLoadingEnabled()) {
62        if (client.isEnhancementEnabled()) {
63          EnhancedLazyResultLoader lazy = new EnhancedLazyResultLoader(client, statementName, parameterObject,
64              targetType);
65          value = lazy.loadResult();
66        } else {
67          LazyResultLoader lazy = new LazyResultLoader(client, statementName, parameterObject, targetType);
68          value = lazy.loadResult();
69        }
70      } else {
71        value = getResult(client, statementName, parameterObject, targetType);
72      }
73  
74      return value;
75    }
76  
77    /**
78     * Gets the result.
79     *
80     * @param client
81     *          the client
82     * @param statementName
83     *          the statement name
84     * @param parameterObject
85     *          the parameter object
86     * @param targetType
87     *          the target type
88     *
89     * @return the result
90     *
91     * @throws SQLException
92     *           the SQL exception
93     */
94    protected static Object getResult(SqlMapClientImpl client, String statementName, Object parameterObject,
95        Class targetType) throws SQLException {
96      Object value = null;
97      if (DomCollectionTypeMarker.class.isAssignableFrom(targetType)) {
98        value = client.queryForList(statementName, parameterObject);
99      } else if (Set.class.isAssignableFrom(targetType)) {
100       value = new HashSet<>(client.queryForList(statementName, parameterObject));
101     } else if (Collection.class.isAssignableFrom(targetType)) {
102       value = client.queryForList(statementName, parameterObject);
103     } else if (targetType.isArray()) {
104       List list = client.queryForList(statementName, parameterObject);
105       value = listToArray(list, targetType.getComponentType());
106     } else {
107       value = client.queryForObject(statementName, parameterObject);
108     }
109     return value;
110   }
111 
112   /**
113    * List to array.
114    *
115    * @param list
116    *          the list
117    * @param type
118    *          the type
119    *
120    * @return the object
121    */
122   private static Object listToArray(List list, Class type) {
123     Object array = java.lang.reflect.Array.newInstance(type, list.size());
124     if (type.isPrimitive()) {
125       Iterator iter = list.iterator();
126       int index = 0;
127       while (iter.hasNext()) {
128         Array.set(array, index, iter.next());
129         index++;
130       }
131     } else {
132       array = list.toArray((Object[]) array);
133     }
134     return array;
135 
136   }
137 
138 }