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.JdbcTypeRegistry;
15: import com.versant.core.jdbc.metadata.JdbcColumn;
16:
17: import javax.jdo.JDOFatalDataStoreException;
18: import java.sql.PreparedStatement;
19: import java.sql.SQLException;
20: import java.sql.ResultSet;
21: import java.sql.Types;
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: * This is special Converter for mssql that handles null blobs as Binary.
27: */
28: public class NullBytesAsBinaryConverter extends JdbcConverterBase {
29:
30: public static class Factory extends NoArgJdbcConverterFactory {
31:
32: private NullBytesAsBinaryConverter asBinaryConverter;
33:
34: /**
35: * Create a converter for col using props as parameters. Return null if
36: * no converter is required.
37: */
38: public JdbcConverter createJdbcConverter(JdbcColumn col,
39: Object args, JdbcTypeRegistry jdbcTypeRegistry) {
40: if (asBinaryConverter == null)
41: asBinaryConverter = new NullBytesAsBinaryConverter();
42: return asBinaryConverter;
43: }
44:
45: }
46:
47: /**
48: * Get the value of col from rs at position index.
49: * @exception java.sql.SQLException on SQL errors
50: * @exception javax.jdo.JDOFatalDataStoreException if the ResultSet value is invalid
51: */
52: public Object get(ResultSet rs, int index, JdbcColumn col)
53: throws SQLException, JDOFatalDataStoreException {
54: return rs.getBytes(index);
55: }
56:
57: /**
58: * Set parameter index on ps to value (for col).
59: * @exception java.sql.SQLException on SQL errors
60: * @exception javax.jdo.JDOFatalDataStoreException if value is invalid
61: */
62: public void set(PreparedStatement ps, int index, JdbcColumn col,
63: Object value) throws SQLException,
64: JDOFatalDataStoreException {
65: if (value == null) {
66: if (col.jdbcType == Types.BLOB) {
67: ps.setNull(index, Types.BINARY);
68: } else {
69: ps.setNull(index, col.jdbcType);
70: }
71: } else {
72: ps.setBytes(index, (byte[]) value);
73: }
74: }
75:
76: /**
77: * Get the type of our expected value objects (e.g. java.util.Locale
78: * for a converter for Locale's).
79: */
80: public Class getValueType() {
81: return byte[].class;
82: }
83:
84: }
|