1 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 30 public class ResultLoader { 31 32 private ResultLoader() { 33 } 34 35 45 public static Object loadResult(ExtendedSqlMapClient client, String statementName, Object parameterObject, Class targetType) 46 throws SQLException { 47 Object 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 getResult(ExtendedSqlMapClient client, String statementName, Object parameterObject, Class targetType) throws SQLException { 66 Object value = null; 67 if (DomCollectionTypeMarker.class.isAssignableFrom(targetType)) { 68 value = client.queryForList(statementName, parameterObject); 69 } else if (Set .class.isAssignableFrom(targetType)) { 70 value = new HashSet (client.queryForList(statementName, parameterObject)); 71 } else if (Collection .class.isAssignableFrom(targetType)) { 72 value = client.queryForList(statementName, parameterObject); 73 } else if (targetType.isArray()) { 74 List 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 [] listToArray(List list, Class type) { 84 Object array = java.lang.reflect.Array.newInstance(type, list.size()); 85 array = list.toArray((Object []) array); 86 return (Object []) array; 87 } 88 89 } 90 | Popular Tags |