1 2 3 package com.opensourceconnections.msjdbcproxy; 4 5 import java.io.ByteArrayInputStream; 6 import java.io.InputStream; 7 import java.io.Reader; 8 import java.math.BigDecimal; 9 import java.net.URL; 10 import java.sql.Array; 11 import java.sql.Blob; 12 import java.sql.Clob; 13 import java.sql.Date; 14 import java.sql.Ref; 15 import java.sql.ResultSet; 16 import java.sql.ResultSetMetaData; 17 import java.sql.SQLException; 18 import java.sql.Time; 19 import java.sql.Timestamp; 20 import java.sql.Types; 21 import java.util.Calendar; 22 import java.util.Map; 23 24 import org.hibernate.Hibernate; 25 26 27 public class ColumnSet 28 { 29 private final ResultSet _resultSet; 30 private final Column[] _columns; 31 private boolean _null; 32 33 public ColumnSet(ResultSet resultSet) throws SQLException 34 { 35 _resultSet = resultSet; 36 37 ResultSetMetaData meta = _resultSet.getMetaData(); 38 39 int count = meta.getColumnCount(); 40 41 _columns = new Column[count]; 42 43 for (int index = 0; index < count; index++) _columns[index] = new Column(meta, index + 1); 44 } 45 46 public final int findColumn(String name) throws SQLException 47 { 48 for (int index = 0; index < _columns.length; index++) 49 if (_columns[index]._name.equalsIgnoreCase(name)) return index + 1; 50 51 throw new SQLException("Column " + name + " not found"); 52 } 53 54 public final Column getColumn(int index) 55 { 56 return _columns[index - 1]; 57 } 58 59 public final Column getColumn(String name) throws SQLException 60 { 61 return getColumn(findColumn(name)); 62 } 63 64 public final boolean wasNull() 65 { 66 return _null; 67 } 68 69 public final boolean load(boolean valid) throws SQLException 70 { 71 if (valid) for (int index = 0; index < _columns.length; index++) _columns[index].load(); 72 73 return valid; 74 } 75 76 class Column 77 { 78 private final int _index; 79 private final String _name; 80 private final int _type; 81 private Object _value; 82 83 public Column(ResultSetMetaData meta, int index) throws SQLException 84 { 85 _index = index; 86 _name = meta.getColumnName(index); 87 _type = meta.getColumnType(index); 88 } 89 90 public void load() throws SQLException 91 { 92 switch (_type) { 93 case Types.ARRAY: 94 _value = _resultSet.getArray(_index); 95 break; 96 case Types.BINARY: 97 case Types.VARBINARY: 98 case Types.BLOB: 99 if (true){ 100 _value = _resultSet.getBlob(_index); 101 } 102 else{ 103 _value = _resultSet.getBytes(_index); 104 } 105 break; 106 case Types.LONGVARBINARY: 107 _value = _resultSet.getBytes(_index); 108 break; 109 case Types.CLOB: 110 _value = _resultSet.getClob(_index); 111 break; 112 case Types.REF: 113 _value = _resultSet.getRef(_index); 114 break; 115 default: 116 _value = _resultSet.getObject(_index); 117 } 118 119 if (_resultSet.wasNull()) _value = null; 120 } 121 122 public String getString() throws SQLException 123 { 124 if (_null = (_value == null)) return null; 125 126 if (_value instanceof String) return (String)_value; 127 128 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 129 } 130 131 public boolean getBoolean() throws SQLException 132 { 133 if (_null = (_value == null)) return false; 134 135 if (_value instanceof Boolean) return ((Boolean)_value).booleanValue(); 136 if (_value instanceof Number) return ((Number)_value).intValue() != 0; 137 138 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 139 } 140 141 public byte getByte() throws SQLException 142 { 143 if (_null = (_value == null)) return 0; 144 145 if (_value instanceof Number) return ((Number)_value).byteValue(); 146 147 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 148 } 149 150 public short getShort() throws SQLException 151 { 152 if (_null = (_value == null)) return 0; 153 154 if (_value instanceof Number) return ((Number)_value).shortValue(); 155 156 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 157 } 158 159 public int getInt() throws SQLException 160 { 161 if (_null = (_value == null)) return 0; 162 163 if (_value instanceof Number) return ((Number)_value).intValue(); 164 165 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 166 } 167 168 public long getLong() throws SQLException 169 { 170 if (_null = (_value == null)) return 0; 171 172 if (_value instanceof Number) return ((Number)_value).longValue(); 173 174 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 175 } 176 177 public float getFloat() throws SQLException 178 { 179 if (_null = (_value == null)) return 0; 180 181 if (_value instanceof Number) return ((Number)_value).floatValue(); 182 183 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 184 } 185 186 public double getDouble() throws SQLException 187 { 188 if (_null = (_value == null)) return 0; 189 190 if (_value instanceof Number) return ((Number)_value).doubleValue(); 191 192 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 193 } 194 195 public BigDecimal getBigDecimal(int scale) throws SQLException 196 { 197 throw new SQLException("ResultSet.getBigDecimal(int) not supported"); 198 } 199 200 public BigDecimal getBigDecimal() throws SQLException 201 { 202 if (_null = (_value == null)) return null; 203 204 if (_value instanceof BigDecimal) return (BigDecimal)_value; 205 206 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 207 } 208 209 public byte[] getBytes() throws SQLException 210 { 211 if (_null = (_value == null)) return null; 212 213 if (_value instanceof byte[]) return (byte[])_value; 214 215 if (_value instanceof Blob) { 216 Blob blob = (Blob)_value; 217 218 return blob.getBytes(1, (int)blob.length()); 219 } 220 221 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 222 } 223 224 public Date getDate() throws SQLException 225 { 226 if (_null = (_value == null)) return null; 227 228 if (_value instanceof Date) { 229 return (Date)_value; 230 } 231 if (_value instanceof Timestamp) { 232 return new Date(((Timestamp)_value).getTime()); 233 } 234 235 236 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 237 } 238 239 public Date getDate(Calendar cal) throws SQLException 240 { 241 throw new SQLException("ResultSet.getDate(Calendar) not supported"); 242 } 243 244 public Time getTime() throws SQLException 245 { 246 if (_null = (_value == null)) return null; 247 248 if (_value instanceof Time) return (Time)_value; 249 250 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 251 } 252 253 public Time getTime(Calendar cal) throws SQLException 254 { 255 throw new SQLException("ResultSet.getTime(Calendar) not supported"); 256 } 257 258 public Timestamp getTimestamp() throws SQLException 259 { 260 if (_null = (_value == null)) return null; 261 262 if (_value instanceof Timestamp) return (Timestamp)_value; 263 264 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 265 } 266 267 public Timestamp getTimestamp(Calendar cal) throws SQLException 268 { 269 throw new SQLException("ResultSet.getTimestamp(Calendar) not supported"); 270 } 271 272 public URL getURL() throws SQLException 273 { 274 if (_null = (_value == null)) return null; 275 276 if (_value instanceof URL) return (URL)_value; 277 278 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 279 } 280 281 public Object getObject() 282 { 283 if (_null = (_value == null)) return null; 284 285 return _value; 286 } 287 288 public Object getObject(Map map) throws SQLException 289 { 290 throw new SQLException("ResultSet.getObject(Map) not supported"); 291 } 292 293 public InputStream getAsciiStream() throws SQLException 294 { 295 throw new SQLException("ResultSet.getAsciiStream() not supported"); 296 } 297 298 public InputStream getUnicodeStream() throws SQLException 299 { 300 throw new SQLException("ResultSet.getUnicodeStream() not supported"); 301 } 302 303 public InputStream getBinaryStream() throws SQLException 304 { 305 if (_null = (_value == null)) return null; 306 307 if (_value instanceof byte[]) return new ByteArrayInputStream((byte[])_value); 308 309 if (_value instanceof Blob) return ((Blob)_value).getBinaryStream(); 310 311 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 312 } 313 314 public Reader getCharacterStream() throws SQLException 315 { 316 throw new SQLException("ResultSet.getCharacterStream() not supported"); 317 } 318 319 public Ref getRef() throws SQLException 320 { 321 throw new SQLException("ResultSet.getRef() not supported"); 322 } 323 324 public Blob getBlob() throws SQLException 325 { 326 if (_null = (_value == null)) return null; 327 328 if (_value instanceof Blob) return (Blob)_value; 329 330 if (_value instanceof byte[]) return Hibernate.createBlob((byte[])_value); 331 332 throw new SQLException("Invalid type for column " + _name + " (" + _value.getClass() + ")"); 333 } 334 335 public Clob getClob() throws SQLException 336 { 337 throw new SQLException("ResultSet.getClob() not supported"); 338 } 339 340 public Array getArray() throws SQLException 341 { 342 throw new SQLException("ResultSet.getArray() not supported"); 343 } 344 } 345 }