1 2 12 package com.versant.core.jdbc.sql.conv; 13 14 import com.versant.core.jdbc.JdbcConverter; 15 import com.versant.core.jdbc.JdbcConverterFactory; 16 import com.versant.core.jdbc.JdbcTypeRegistry; 17 import com.versant.core.jdbc.metadata.JdbcColumn; 18 import com.versant.core.jdbc.metadata.JdbcTypes; 19 import com.versant.core.common.Utils; 20 21 import java.sql.PreparedStatement ; 22 import java.sql.SQLException ; 23 import java.sql.ResultSet ; 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Method ; 26 27 import javax.jdo.JDOFatalDataStoreException; 29 import com.versant.core.common.BindingSupportImpl; 30 import com.versant.core.common.Utils; 31 32 37 public class TypeAsBytesConverter implements JdbcConverter { 38 39 private Class type; 40 private Constructor constructor; 41 private Method toBytes; 42 private JdbcConverter bytesConverter; 43 44 47 public static class Factory extends NoArgJdbcConverterFactory { 48 49 53 public JdbcConverter createJdbcConverter(JdbcColumn col, Object args, 54 JdbcTypeRegistry jdbcTypeRegistry) { 55 Class type = col.javaType; 56 JdbcConverterFactory f = jdbcTypeRegistry.getJdbcConverterFactory(col.jdbcType); 57 JdbcConverter bytesConverter = f.createJdbcConverter(col, args, jdbcTypeRegistry); 58 if (bytesConverter.getValueType() != byte[].class) { 59 throw BindingSupportImpl.getInstance().illegalArgument("Invalid JDBC type: " + 60 JdbcTypes.toString(col.jdbcType)); 61 } 62 return new TypeAsBytesConverter(type, bytesConverter); 63 } 64 65 } 66 67 public TypeAsBytesConverter(Class type, JdbcConverter stringConverter) { 68 this.type = type; 69 this.bytesConverter = stringConverter; 70 try { 71 constructor = type.getConstructor(new Class []{byte[].class}); 72 } catch (NoSuchMethodException e) { 73 throw BindingSupportImpl.getInstance().runtime(type + " does not have a " + 74 "constructor that accepts a byte[]", e); 75 } 76 try { 77 toBytes = type.getMethod("toBytes", null); 78 } catch (NoSuchMethodException e) { 79 throw BindingSupportImpl.getInstance().runtime(type + " does not have a " + 80 "public toBytes() method", e); 81 } 82 if (toBytes.getReturnType() != byte[].class) { 83 throw BindingSupportImpl.getInstance().runtime(type + ".toBytes() does not " + 84 "return byte[]"); 85 } 86 } 87 88 91 public Object get(ResultSet rs, int index, JdbcColumn col) 92 throws SQLException , JDOFatalDataStoreException { 93 byte[] a = (byte[])bytesConverter.get(rs, index, col); 94 if (a == null) return null; 95 try { 96 return constructor.newInstance(new Object []{a}); 97 } catch (Throwable x) { 98 throw BindingSupportImpl.getInstance().fatalDatastore("Unable to create instance of " + 99 type.getName() + " from " + Utils.toString(a) + ": " + x, x); 100 } 101 } 102 103 104 105 106 107 private byte[] toBytes(Object value) { 108 byte[] a; 109 if (value == null) { 110 a = null; 111 } else { 112 try { 113 a = (byte[])toBytes.invoke(value, null); 114 } catch (Throwable x) { 115 throw BindingSupportImpl.getInstance().fatalDatastore("Unable to convert instance of " + 116 type.getName() + " '" + Utils.toString(value) + "' to byte[]: " + x, x); 117 } 118 } 119 return a; 120 } 121 122 125 public void set(PreparedStatement ps, int index, JdbcColumn col, 126 Object value) throws SQLException , JDOFatalDataStoreException { 127 bytesConverter.set(ps, index, col, toBytes(value)); 128 } 129 130 134 public Class getValueType() { 135 return type; 136 } 137 138 144 public boolean isOracleStyleLOB() { 145 return bytesConverter.isOracleStyleLOB(); 146 } 147 148 153 public String getOracleStyleLOBNotNullString() { 154 return bytesConverter.getOracleStyleLOBNotNullString(); 155 } 156 157 166 public void set(PreparedStatement ps, int index, JdbcColumn col, int value) 167 throws SQLException , JDOFatalDataStoreException { 168 throw BindingSupportImpl.getInstance().fatalDatastore("set(..int) called"); 169 } 170 171 180 public void set(ResultSet rs, int index, JdbcColumn col, Object value) 181 throws SQLException , JDOFatalDataStoreException { 182 bytesConverter.set(rs, index, col, toBytes(value)); 183 } 184 185 } 186 | Popular Tags |