KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > NullBytesAsBinaryConverter


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.conv;
13
14 import com.versant.core.jdbc.JdbcConverter;
15 import com.versant.core.jdbc.JdbcTypeRegistry;
16 import com.versant.core.jdbc.metadata.JdbcColumn;
17
18 import javax.jdo.JDOFatalDataStoreException;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.Types JavaDoc;
23
24 /**
25  * This converter converts byte[] to and from SQL. It assumes the data is
26  * accessable using ResultSet.getBytes and PreparedStatement.setBytes.
27  * This is special Converter for mssql that handles null blobs as Binary.
28  */

29 public class NullBytesAsBinaryConverter extends JdbcConverterBase {
30
31     public static class Factory extends NoArgJdbcConverterFactory {
32
33         private NullBytesAsBinaryConverter asBinaryConverter;
34
35         /**
36          * Create a converter for col using props as parameters. Return null if
37          * no converter is required.
38          */

39         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
40                 JdbcTypeRegistry jdbcTypeRegistry) {
41             if (asBinaryConverter == null) 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 JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
53             throws SQLException JavaDoc, 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 JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
63             throws SQLException JavaDoc, JDOFatalDataStoreException {
64         if (value == null) {
65             if (col.jdbcType == Types.BLOB) {
66                 ps.setNull(index, Types.BINARY);
67             } else {
68                 ps.setNull(index, col.jdbcType);
69             }
70         } else {
71             ps.setBytes(index, (byte[])value);
72         }
73     }
74
75     /**
76      * Get the type of our expected value objects (e.g. java.util.Locale
77      * for a converter for Locale's).
78      */

79     public Class JavaDoc getValueType() {
80         return byte[].class;
81     }
82
83 }
84
85
Popular Tags