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 javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
19: import java.sql.PreparedStatement;
20: import java.sql.SQLException;
21: import java.sql.ResultSet;
22:
23: /**
24: * This converter converts byte[] to and from SQL. It assumes the data is
25: * accessable using ResultSet.getBytes and PreparedStatement.setBytes.
26: */
27: public class BytesConverter extends JdbcConverterBase {
28:
29: public static class Factory extends NoArgJdbcConverterFactory {
30:
31: private BytesConverter converter;
32:
33: /**
34: * Create a converter for col using props as parameters. Return null if
35: * no converter is required.
36: */
37: public JdbcConverter createJdbcConverter(JdbcColumn col,
38: Object args, JdbcTypeRegistry jdbcTypeRegistry) {
39: if (converter == null)
40: converter = new BytesConverter();
41: return converter;
42: }
43:
44: }
45:
46: /**
47: * Get the value of col from rs at position index.
48: * @exception SQLException on SQL errors
49: * @exception JDOFatalDataStoreException if the ResultSet value is invalid
50: */
51: public Object get(ResultSet rs, int index, JdbcColumn col)
52: throws SQLException, JDOFatalDataStoreException {
53: return rs.getBytes(index);
54: }
55:
56: /**
57: * Set parameter index on ps to value (for col).
58: * @exception SQLException on SQL errors
59: * @exception JDOFatalDataStoreException if value is invalid
60: */
61: public void set(PreparedStatement ps, int index, JdbcColumn col,
62: Object value) throws SQLException,
63: JDOFatalDataStoreException {
64: if (value == null) {
65: ps.setNull(index, col.jdbcType);
66: } else {
67: ps.setBytes(index, (byte[]) value);
68: }
69: }
70:
71: /**
72: * Get the type of our expected value objects (e.g. java.util.Locale
73: * for a converter for Locale's).
74: */
75: public Class getValueType() {
76: return byte[].class;
77: }
78:
79: }
|