1 16 package com.ibatis.sqlmap.engine.mapping.result; 17 18 import com.ibatis.common.beans.ClassInfo; 19 import com.ibatis.common.exception.NestedRuntimeException; 20 import com.ibatis.sqlmap.client.SqlMapException; 21 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate; 22 import com.ibatis.sqlmap.engine.scope.RequestScope; 23 import com.ibatis.sqlmap.engine.type.DomTypeMarker; 24 25 import java.sql.ResultSet ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 36 public class AutoResultMap extends BasicResultMap { 37 38 43 public AutoResultMap(SqlMapExecutorDelegate delegate, boolean allowRemapping) { 44 super(delegate); 45 this.allowRemapping = allowRemapping; 46 } 47 48 public synchronized Object [] getResults(RequestScope request, ResultSet rs) 49 throws SQLException { 50 if (allowRemapping || getResultMappings() == null) { 51 initialize(rs); 52 } 53 return super.getResults(request, rs); 54 } 55 56 private void initialize(ResultSet rs) { 57 if (getResultClass() == null) { 58 throw new SqlMapException("The automatic ResultMap named " + this.getId() + " had a null result class (not allowed)."); 59 } else if (Map .class.isAssignableFrom(getResultClass())) { 60 initializeMapResults(rs); 61 } else if (getDelegate().getTypeHandlerFactory().getTypeHandler(getResultClass()) != null) { 62 initializePrimitiveResults(rs); 63 } else if (DomTypeMarker.class.isAssignableFrom(getResultClass())) { 64 initializeXmlResults(rs); 65 } else { 66 initializeBeanResults(rs); 67 } 68 } 69 70 private void initializeBeanResults(ResultSet rs) { 71 try { 72 ClassInfo classInfo = ClassInfo.getInstance(getResultClass()); 73 String [] propertyNames = classInfo.getWriteablePropertyNames(); 74 75 Map propertyMap = new HashMap (); 76 for (int i = 0; i < propertyNames.length; i++) { 77 propertyMap.put(propertyNames[i].toUpperCase(), propertyNames[i]); 78 } 79 80 List resultMappingList = new ArrayList (); 81 ResultSetMetaData rsmd = rs.getMetaData(); 82 for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) { 83 String columnName = rsmd.getColumnLabel(i + 1); 84 String upperColumnName = columnName.toUpperCase(); 85 String matchedProp = (String ) propertyMap.get(upperColumnName); 86 if (matchedProp != null) { 87 BasicResultMapping resultMapping = new BasicResultMapping(); 88 resultMapping.setPropertyName(matchedProp); 89 resultMapping.setColumnName(columnName); 90 resultMapping.setColumnIndex(i + 1); 91 Class type = classInfo.getSetterType(matchedProp); 92 resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(type)); 93 resultMappingList.add(resultMapping); 94 } 95 } 96 97 setResultMappingList(resultMappingList); 98 99 } catch (SQLException e) { 100 throw new NestedRuntimeException("Error automapping columns. Cause: " + e); 101 } 102 103 } 104 105 private void initializeXmlResults(ResultSet rs) { 106 try { 107 List resultMappingList = new ArrayList (); 108 ResultSetMetaData rsmd = rs.getMetaData(); 109 for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) { 110 String columnName = rsmd.getColumnLabel(i + 1); 111 BasicResultMapping resultMapping = new BasicResultMapping(); 112 resultMapping.setPropertyName(columnName); 113 resultMapping.setColumnName(columnName); 114 resultMapping.setColumnIndex(i + 1); 115 resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(String .class)); 116 resultMappingList.add(resultMapping); 117 } 118 setResultMappingList(resultMappingList); 119 } catch (SQLException e) { 120 throw new NestedRuntimeException("Error automapping columns. Cause: " + e); 121 } 122 } 123 124 private void initializeMapResults(ResultSet rs) { 125 try { 126 List resultMappingList = new ArrayList (); 127 ResultSetMetaData rsmd = rs.getMetaData(); 128 for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) { 129 String columnName = rsmd.getColumnLabel(i + 1); 130 BasicResultMapping resultMapping = new BasicResultMapping(); 131 resultMapping.setPropertyName(columnName); 132 resultMapping.setColumnName(columnName); 133 resultMapping.setColumnIndex(i + 1); 134 resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(Object .class)); 135 resultMappingList.add(resultMapping); 136 } 137 138 setResultMappingList(resultMappingList); 139 140 } catch (SQLException e) { 141 throw new NestedRuntimeException("Error automapping columns. Cause: " + e); 142 } 143 } 144 145 private void initializePrimitiveResults(ResultSet rs) { 146 try { 147 ResultSetMetaData rsmd = rs.getMetaData(); 148 String columnName = rsmd.getColumnLabel(1); 149 BasicResultMapping resultMapping = new BasicResultMapping(); 150 resultMapping.setPropertyName(columnName); 151 resultMapping.setColumnName(columnName); 152 resultMapping.setColumnIndex(1); 153 resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(getResultClass())); 154 155 List resultMappingList = new ArrayList (); 156 resultMappingList.add(resultMapping); 157 158 setResultMappingList(resultMappingList); 159 160 } catch (SQLException e) { 161 throw new NestedRuntimeException("Error automapping columns. Cause: " + e); 162 } 163 } 164 165 } 166 167 | Popular Tags |