01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.conv;
12:
13: import com.versant.core.jdbc.JdbcConverter;
14: import com.versant.core.jdbc.JdbcConverterFactory;
15: import com.versant.core.jdbc.JdbcTypeRegistry;
16: import com.versant.core.jdbc.metadata.JdbcColumn;
17:
18: import java.sql.PreparedStatement;
19: import java.sql.SQLException;
20: import java.sql.ResultSet;
21: import java.io.*;
22:
23: import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws-clause
24:
25: import com.versant.core.common.BindingSupportImpl;
26:
27: /**
28: * This converter converts byte[] stored in LONGVARBINARY columns to
29: * and from SQL using rs.getBinaryStream and ps.setBinaryStream.
30: * @keep-all
31: */
32: public class InputStreamConverter extends JdbcConverterBase {
33:
34: public static class Factory extends NoArgJdbcConverterFactory {
35:
36: private InputStreamConverter converter;
37:
38: /**
39: * Create a converter for col using args as parameters. Return null if
40: * no converter is required.
41: */
42: public JdbcConverter createJdbcConverter(JdbcColumn col,
43: Object args, JdbcTypeRegistry jdbcTypeRegistry) {
44: if (converter == null)
45: converter = new InputStreamConverter();
46: return converter;
47: }
48:
49: }
50:
51: /**
52: * Get the value of col from rs at position index.
53: * @exception SQLException on SQL errors
54: * @exception JDOFatalDataStoreException if the ResultSet value is invalid
55: */
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)
61: return null;
62: return StreamUtils.readAll(in);
63: } catch (IOException x) {
64: throw BindingSupportImpl.getInstance().fatalDatastore(
65: "Error reading " + col + ": "
66: + x.getClass().getName() + ": "
67: + x.getMessage(), x);
68: }
69: }
70:
71: /**
72: * Set parameter index on ps to value (for col).
73: * @exception SQLException on SQL errors
74: * @exception JDOFatalDataStoreException if value is invalid
75: */
76: public void set(PreparedStatement ps, int index, JdbcColumn col,
77: Object value) throws SQLException,
78: JDOFatalDataStoreException {
79: if (value == null) {
80: ps.setNull(index, col.jdbcType);
81: return;
82: }
83: byte[] a = (byte[]) value;
84: ps
85: .setBinaryStream(index, new ByteArrayInputStream(a),
86: a.length);
87: }
88:
89: /**
90: * Get the type of our expected value objects (e.g. java.util.Locale
91: * for a converter for Locale's).
92: */
93: public Class getValueType() {
94: return byte[].class;
95: }
96:
97: }
|