1 56 package org.objectstyle.cayenne.access.types; 57 58 import java.lang.reflect.Method ; 59 import java.sql.CallableStatement ; 60 import java.sql.ResultSet ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 65 import org.objectstyle.cayenne.CayenneRuntimeException; 66 import org.objectstyle.cayenne.dba.TypesMapping; 67 68 74 public class DefaultType extends AbstractType { 75 76 private static final Map readMethods = new HashMap (); 77 private static final Map procReadMethods = new HashMap (); 78 private static Method readObjectMethod; 79 private static Method procReadObjectMethod; 80 81 static { 82 try { 83 Class rsClass = ResultSet .class; 84 Class [] paramTypes = new Class [] { 85 Integer.TYPE 86 }; 87 readMethods.put(TypesMapping.JAVA_LONG, rsClass.getMethod("getLong", 88 paramTypes)); 89 readMethods.put(TypesMapping.JAVA_BIGDECIMAL, rsClass 90 .getMethod("getBigDecimal", paramTypes)); 91 readMethods.put(TypesMapping.JAVA_BOOLEAN, rsClass.getMethod("getBoolean", 92 paramTypes)); 93 readMethods.put(TypesMapping.JAVA_BYTE, rsClass.getMethod("getByte", 94 paramTypes)); 95 readMethods.put(TypesMapping.JAVA_BYTES, rsClass.getMethod("getBytes", 96 paramTypes)); 97 readMethods.put(TypesMapping.JAVA_SQLDATE, rsClass.getMethod("getDate", 98 paramTypes)); 99 readMethods.put(TypesMapping.JAVA_DOUBLE, rsClass.getMethod("getDouble", 100 paramTypes)); 101 readMethods.put(TypesMapping.JAVA_FLOAT, rsClass.getMethod("getFloat", 102 paramTypes)); 103 readMethods.put(TypesMapping.JAVA_INTEGER, rsClass.getMethod("getInt", 104 paramTypes)); 105 readMethods.put(TypesMapping.JAVA_SHORT, rsClass.getMethod("getShort", 106 paramTypes)); 107 readMethods.put(TypesMapping.JAVA_STRING, rsClass.getMethod("getString", 108 paramTypes)); 109 readMethods.put(TypesMapping.JAVA_TIME, rsClass.getMethod("getTime", 110 paramTypes)); 111 readMethods.put(TypesMapping.JAVA_TIMESTAMP, rsClass 112 .getMethod("getTimestamp", paramTypes)); 113 114 readObjectMethod = rsClass.getMethod("getObject", paramTypes); 115 116 Class csClass = CallableStatement .class; 118 procReadMethods.put(TypesMapping.JAVA_LONG, csClass.getMethod("getLong", 119 paramTypes)); 120 procReadMethods.put(TypesMapping.JAVA_BIGDECIMAL, csClass 121 .getMethod("getBigDecimal", paramTypes)); 122 procReadMethods.put(TypesMapping.JAVA_BOOLEAN, csClass 123 .getMethod("getBoolean", paramTypes)); 124 procReadMethods.put(TypesMapping.JAVA_BYTE, csClass.getMethod("getByte", 125 paramTypes)); 126 procReadMethods.put(TypesMapping.JAVA_BYTES, csClass.getMethod("getBytes", 127 paramTypes)); 128 procReadMethods.put(TypesMapping.JAVA_SQLDATE, csClass.getMethod("getDate", 129 paramTypes)); 130 procReadMethods.put(TypesMapping.JAVA_DOUBLE, csClass.getMethod("getDouble", 131 paramTypes)); 132 procReadMethods.put(TypesMapping.JAVA_FLOAT, csClass.getMethod("getFloat", 133 paramTypes)); 134 procReadMethods.put(TypesMapping.JAVA_INTEGER, csClass.getMethod("getInt", 135 paramTypes)); 136 procReadMethods.put(TypesMapping.JAVA_SHORT, csClass.getMethod("getShort", 137 paramTypes)); 138 procReadMethods.put(TypesMapping.JAVA_STRING, csClass.getMethod("getString", 139 paramTypes)); 140 procReadMethods.put(TypesMapping.JAVA_TIME, csClass.getMethod("getTime", 141 paramTypes)); 142 procReadMethods.put(TypesMapping.JAVA_TIMESTAMP, csClass 143 .getMethod("getTimestamp", paramTypes)); 144 145 procReadObjectMethod = csClass.getMethod("getObject", paramTypes); 146 } 147 catch (Exception ex) { 148 throw new CayenneRuntimeException("Error initializing read methods.", ex); 149 } 150 } 151 152 153 public static Iterator defaultTypes() { 154 return readMethods.keySet().iterator(); 155 } 156 157 protected String className; 158 protected Method readMethod; 159 protected Method procReadMethod; 160 161 164 public DefaultType() { 165 this.className = Object .class.getName(); 166 this.readMethod = readObjectMethod; 167 this.procReadMethod = procReadObjectMethod; 168 } 169 170 public DefaultType(String className) { 171 this.className = className; 172 this.readMethod = (Method ) readMethods.get(className); 173 174 if (readMethod == null) { 175 throw new CayenneRuntimeException("Unsupported default class: " 176 + className 177 + ". If you want a non-standard class to map to JDBC type," 178 + " you will need to implement ExtendedType interface yourself."); 179 } 180 181 this.procReadMethod = (Method ) procReadMethods.get(className); 182 if (procReadMethod == null) { 183 throw new CayenneRuntimeException("Unsupported default class: " 184 + className 185 + ". If you want a non-standard class to map to JDBC type," 186 + " you will need to implement ExtendedType interface yourself."); 187 } 188 } 189 190 public String getClassName() { 191 return className; 192 } 193 194 public Object materializeObject(ResultSet rs, int index, int type) throws Exception { 195 Object val = readMethod.invoke(rs, new Object [] { 196 new Integer (index) 197 }); 198 return (rs.wasNull()) ? null : val; 199 } 200 201 public Object materializeObject(CallableStatement st, int index, int type) 202 throws Exception { 203 Object val = procReadMethod.invoke(st, new Object [] { 204 new Integer (index) 205 }); 206 return (st.wasNull()) ? null : val; 207 } 208 } 209 | Popular Tags |