public static ClientProperty buildClientProperty(ResultSetMetaData meta, ResultSet rs, int pos) throws Exception {
String name = meta.getColumnName(pos);
Object val = rs.getObject(pos);
if (val == null)
return new ClientProperty(name, null, null, (String)null);
if (val instanceof String) // TODO FIX THIS DIRTY HACK
return new ClientProperty(name, null, Constants.ENCODING_BASE64, (String)val);
if (val instanceof Boolean)
return new ClientProperty(name, null, null, "" + ((Boolean)val).booleanValue());
if (val instanceof Short)
return new ClientProperty(name, null, null, "" + ((Short)val).shortValue());
if (val instanceof Integer)
return new ClientProperty(name, null, null, "" + ((Integer)val).intValue());
if (val instanceof Long)
return new ClientProperty(name, null, null, "" + ((Long)val).longValue());
if (val instanceof Float)
return new ClientProperty(name, null, null, "" + ((Float)val).floatValue());
if (val instanceof Double)
return new ClientProperty(name, null, null, "" + ((Double)val).doubleValue());
if (val instanceof byte[]) {
ClientProperty prop = new ClientProperty(name, null, null);
prop.setValue((byte[])val);
}
if (val instanceof Clob) { // only for relatively small clobs (< 10MB)
try {
Clob clob = (Clob)val;
InputStream in = clob.getAsciiStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// StringBuffer strBuf = new StringBuffer();
byte[] buf = new byte[100000];
int read = 0;
int count=0;
while ( (read=in.read(buf)) != -1) {
baos.write(buf, 0, read);
count++;
if (count > 100)
throw new IllegalArgumentException("The clob '" + name + "' is too big, already exceeding 10 MB. Will stop processing it");
}
in.close();
ClientProperty prop = new ClientProperty(name, null, null);
prop.setValue(new String(baos.toByteArray()));
return prop;
}
catch (Exception ex) {
throw new Exception("An exception occured when processing '" + name + "'", ex);
}
}
if (val instanceof Blob) { // only for relatively small clobs (< 10MB)
try {
Blob blob = (Blob)val;
InputStream in = blob.getBinaryStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// StringBuffer strBuf = new StringBuffer();
byte[] buf = new byte[100000];
int read = 0;
int count=0;
while ( (read=in.read(buf)) != -1) {
baos.write(buf, 0, read);
count++;
if (count > 100)
throw new IllegalArgumentException("The blob '" + name + "' is too big, already exceeding 10 MB. Will stop processing it");
}
in.close();
ClientProperty prop = new ClientProperty(name, null, null);
prop.setValue(new String(baos.toByteArray()));
}
catch (Exception ex) {
throw new Exception("An exception occured when processing '" + name + "'", ex);
}
}
if (val instanceof Timestamp) {
Timestamp ts = (Timestamp)val;
return new ClientProperty(name, null, null, ts.toString());
}
if (val instanceof BigDecimal) {
BigDecimal dec = (BigDecimal)val;
try {
return new ClientProperty(name, null, null, "" + dec.longValue());
}
catch (Exception ex) {
return new ClientProperty(name, null, null, "" + dec.doubleValue());
}
}
if (val instanceof BigInteger) {
BigInteger dec = (BigInteger)val;
return new ClientProperty(name, null, null, "" + dec.longValue());
}
if (val instanceof Date) {
// Date date = (Date)val;
Timestamp ts = rs.getTimestamp(pos);
// DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String dateTxt = format.format(date);
String dateTxt = ts.toString();
return new ClientProperty(name, null, null, dateTxt);
}
if (val instanceof Time) {
Time time = (Time)val;
return new ClientProperty(name, null, null, "" + time.getTime());
}
else {
if (val.getClass().getName().equals("oracle.sql.TIMESTAMP")) {
Method meth = val.getClass().getMethod("timestampValue", null);
Object obj = meth.invoke(val, null);
if (obj instanceof Timestamp) {
Timestamp ts = (Timestamp)obj;
return new ClientProperty(name, null, null, ts.toString());
}
else {
return new ClientProperty(name, null, null, rs.getString(pos)); // e.g. oracle.sql.TIMESTAMP containing a byte[]
}
}
else
return new ClientProperty(name, null, null, rs.getString(pos)); // e.g. oracle.sql.TIMESTAMP containing a byte[]
//throw new Exception("The object '" + name + "' of type '" + val.getClass().getName() + "' can not be processed since this type is not implemented");
}
}