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 19 import java.sql.PreparedStatement ; 20 import java.sql.SQLException ; 21 import java.sql.ResultSet ; 22 import java.io.*; 23 24 import javax.jdo.JDOFatalDataStoreException; 26 import com.versant.core.common.BindingSupportImpl; 27 28 33 public class InputStreamConverter extends JdbcConverterBase { 34 35 public static class Factory extends NoArgJdbcConverterFactory { 36 37 private InputStreamConverter converter; 38 39 43 public JdbcConverter createJdbcConverter(JdbcColumn col, Object args, 44 JdbcTypeRegistry jdbcTypeRegistry) { 45 if (converter == null) converter = new InputStreamConverter(); 46 return converter; 47 } 48 49 } 50 51 56 public Object get(ResultSet rs, int index, JdbcColumn col) 57 throws SQLException , JDOFatalDataStoreException { 58 try { 59 InputStream in = rs.getBinaryStream(index); 60 if (in == null) return null; 61 return StreamUtils.readAll(in); 62 } catch (IOException x) { 63 throw BindingSupportImpl.getInstance().fatalDatastore( 64 "Error reading " + col + ": " + 65 x.getClass().getName() + ": " + x.getMessage(), x); 66 } 67 } 68 69 74 public void set(PreparedStatement ps, int index, JdbcColumn col, Object value) 75 throws SQLException , JDOFatalDataStoreException { 76 if (value == null) { 77 ps.setNull(index, col.jdbcType); 78 return; 79 } 80 byte[] a = (byte[])value; 81 ps.setBinaryStream(index, new ByteArrayInputStream(a), a.length); 82 } 83 84 88 public Class getValueType() { 89 return byte[].class; 90 } 91 92 } 93 94 | Popular Tags |