KickJava   Java API By Example, From Geeks To Geeks.

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


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.JdbcConverterFactory;
16 import com.versant.core.jdbc.JdbcTypeRegistry;
17 import com.versant.core.jdbc.metadata.JdbcColumn;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.io.*;
23
24 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws-clause
25

26 import com.versant.core.common.BindingSupportImpl;
27
28 /**
29  * This converter converts Strings stored in TEXT or LONGVARCHAR columns to
30  * and from SQL using rs.getAsciiStream and ps.setAsciiStream.
31  * @keep-all
32  */

33 public class AsciiStreamConverter extends JdbcConverterBase {
34
35     public static class Factory extends NoArgJdbcConverterFactory {
36
37         private AsciiStreamConverter converter;
38
39         /**
40          * Create a converter for col using args as parameters. Return null if
41          * no converter is required.
42          */

43         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
44                 JdbcTypeRegistry jdbcTypeRegistry) {
45             if (converter == null) converter = new AsciiStreamConverter();
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 JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
57             throws SQLException JavaDoc, JDOFatalDataStoreException {
58         try {
59             InputStream in = rs.getAsciiStream(index);
60             if (in == null) return null;
61             return StreamUtils.readAll(in, "UTF8");
62         } catch (IOException x) {
63             throw BindingSupportImpl.getInstance().fatalDatastore(
64                 "Error reading " + col + ": " +
65                 x.getClass().getName() + ": " + x.getMessage(), x);
66         }
67     }
68
69     /**
70      * Set parameter index on ps to value (for col).
71      * @exception SQLException on SQL errors
72      * @exception JDOFatalDataStoreException if value is invalid
73      */

74     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
75             throws SQLException JavaDoc, JDOFatalDataStoreException {
76         if (value == null) {
77             ps.setNull(index, col.jdbcType);
78             return;
79         }
80         String JavaDoc s = (String JavaDoc)value;
81         byte[] b = s.getBytes();
82         ps.setAsciiStream(index, new ByteArrayInputStream(b), b.length);
83     }
84
85     /**
86      * Get the type of our expected value objects (e.g. java.util.Locale
87      * for a converter for Locale's).
88      */

89     public Class JavaDoc getValueType() {
90         return String JavaDoc.class;
91     }
92 }
93
94
Popular Tags