1 18 19 package org.apache.beehive.controls.system.jdbc; 20 21 import org.apache.beehive.controls.api.ControlException; 22 23 import java.sql.ResultSet ; 24 import java.sql.ResultSetMetaData ; 25 import java.sql.SQLException ; 26 import java.util.Calendar ; 27 import java.util.regex.Pattern ; 28 import java.util.regex.Matcher ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Modifier ; 31 32 41 public abstract class RowMapper { 42 43 private static final String SETTER_NAME_REGEX = "^(set)([A-Z_]\\w*+)"; 44 protected static final TypeMappingsFactory _tmf = TypeMappingsFactory.getInstance(); 45 protected static final Pattern _setterRegex = Pattern.compile(SETTER_NAME_REGEX); 46 47 48 protected final ResultSet _resultSet; 49 50 51 protected final Calendar _cal; 52 53 54 protected final Class <?> _returnTypeClass; 55 56 62 protected RowMapper(ResultSet resultSet, Class <?> returnTypeClass, Calendar cal) { 63 _resultSet = resultSet; 64 _returnTypeClass = returnTypeClass; 65 _cal = cal; 66 } 67 68 72 public abstract Object mapRowToReturnType(); 73 74 75 80 protected String [] getKeysFromResultSet() throws SQLException { 81 82 String [] keys; 83 final ResultSetMetaData md = _resultSet.getMetaData(); 84 final int columnCount = md.getColumnCount(); 85 86 keys = new String [columnCount + 1]; 87 for (int i = 1; i <= columnCount; i++) { 88 keys[i] = md.getColumnName(i).toUpperCase(); 89 } 90 return keys; 91 } 92 93 98 protected boolean isSetterMethod(Method method) { 99 Matcher matcher = _setterRegex.matcher(method.getName()); 100 if (matcher.matches()) { 101 102 if (Modifier.isStatic(method.getModifiers())) return false; 103 if (!Modifier.isPublic(method.getModifiers())) return false; 104 if (!Void.TYPE.equals(method.getReturnType())) return false; 105 106 Class [] params = method.getParameterTypes(); 108 if (params.length != 1) return false; 109 if (TypeMappingsFactory.TYPE_UNKNOWN == _tmf.getTypeId(params[0])) return false; 110 111 return true; 112 } 113 return false; 114 } 115 116 124 protected Object extractColumnValue(int index, int resultType) throws SQLException { 125 126 switch (resultType) { 127 case TypeMappingsFactory.TYPE_INT: 128 return new Integer (_resultSet.getInt(index)); 129 case TypeMappingsFactory.TYPE_LONG: 130 return new Long (_resultSet.getLong(index)); 131 case TypeMappingsFactory.TYPE_FLOAT: 132 return new Float (_resultSet.getFloat(index)); 133 case TypeMappingsFactory.TYPE_DOUBLE: 134 return new Double (_resultSet.getDouble(index)); 135 case TypeMappingsFactory.TYPE_BYTE: 136 return new Byte (_resultSet.getByte(index)); 137 case TypeMappingsFactory.TYPE_SHORT: 138 return new Short (_resultSet.getShort(index)); 139 case TypeMappingsFactory.TYPE_BOOLEAN: 140 return _resultSet.getBoolean(index) ? Boolean.TRUE : Boolean.FALSE; 141 case TypeMappingsFactory.TYPE_INT_OBJ: 142 { 143 int i = _resultSet.getInt(index); 144 return _resultSet.wasNull() ? null : new Integer (i); 145 } 146 case TypeMappingsFactory.TYPE_LONG_OBJ: 147 { 148 long i = _resultSet.getLong(index); 149 return _resultSet.wasNull() ? null : new Long (i); 150 } 151 case TypeMappingsFactory.TYPE_FLOAT_OBJ: 152 { 153 float i = _resultSet.getFloat(index); 154 return _resultSet.wasNull() ? null : new Float (i); 155 } 156 case TypeMappingsFactory.TYPE_DOUBLE_OBJ: 157 { 158 double i = _resultSet.getDouble(index); 159 return _resultSet.wasNull() ? null : new Double (i); 160 } 161 case TypeMappingsFactory.TYPE_BYTE_OBJ: 162 { 163 byte i = _resultSet.getByte(index); 164 return _resultSet.wasNull() ? null : new Byte (i); 165 } 166 case TypeMappingsFactory.TYPE_SHORT_OBJ: 167 { 168 short i = _resultSet.getShort(index); 169 return _resultSet.wasNull() ? null : new Short (i); 170 } 171 case TypeMappingsFactory.TYPE_BOOLEAN_OBJ: 172 { 173 boolean i = _resultSet.getBoolean(index); 174 return _resultSet.wasNull() ? null : (i ? Boolean.TRUE : Boolean.FALSE); 175 } 176 case TypeMappingsFactory.TYPE_STRING: 177 case TypeMappingsFactory.TYPE_XMLBEAN_ENUM: 178 return _resultSet.getString(index); 179 case TypeMappingsFactory.TYPE_BIG_DECIMAL: 180 return _resultSet.getBigDecimal(index); 181 case TypeMappingsFactory.TYPE_BYTES: 182 return _resultSet.getBytes(index); 183 case TypeMappingsFactory.TYPE_TIMESTAMP: 184 { 185 if (null == _cal) 186 return _resultSet.getTimestamp(index); 187 else 188 return _resultSet.getTimestamp(index, _cal); 189 } 190 case TypeMappingsFactory.TYPE_TIME: 191 { 192 if (null == _cal) 193 return _resultSet.getTime(index); 194 else 195 return _resultSet.getTime(index, _cal); 196 } 197 case TypeMappingsFactory.TYPE_SQLDATE: 198 { 199 if (null == _cal) 200 return _resultSet.getDate(index); 201 else 202 return _resultSet.getDate(index, _cal); 203 } 204 case TypeMappingsFactory.TYPE_DATE: 205 { 206 java.sql.Timestamp ts = (null == _cal) ? _resultSet.getTimestamp(index) : _resultSet.getTimestamp(index, _cal); 209 if (null == ts) 210 return null; 211 return new java.util.Date (ts.getTime()); 212 } 213 case TypeMappingsFactory.TYPE_CALENDAR: 214 { 215 java.sql.Timestamp ts = (null == _cal) ? _resultSet.getTimestamp(index) : _resultSet.getTimestamp(index, _cal); 216 if (null == ts) 217 return null; 218 Calendar c = (null == _cal) ? Calendar.getInstance() : (Calendar ) _cal.clone(); 219 c.setTimeInMillis(ts.getTime()); 220 return c; 221 } 222 case TypeMappingsFactory.TYPE_REF: 223 return _resultSet.getRef(index); 224 case TypeMappingsFactory.TYPE_BLOB: 225 return _resultSet.getBlob(index); 226 case TypeMappingsFactory.TYPE_CLOB: 227 return _resultSet.getClob(index); 228 case TypeMappingsFactory.TYPE_ARRAY: 229 return _resultSet.getArray(index); 230 case TypeMappingsFactory.TYPE_READER: 231 case TypeMappingsFactory.TYPE_STREAM: 232 throw new ControlException("streaming return types are not supported by the JdbcControl; use ResultSet instead"); 233 case TypeMappingsFactory.TYPE_STRUCT: 234 case TypeMappingsFactory.TYPE_UNKNOWN: 235 return _resultSet.getObject(index); 237 default: 238 throw new ControlException("internal error: unknown type ID: " + Integer.toString(resultType)); 239 } 240 } 241 } 242 | Popular Tags |