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