KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.versant.core.metadata.MDStatics;
19
20 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
21
import java.sql.PreparedStatement JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24
25 /**
26  * This converter converts char and Character's to and from SQL. It assumes
27  * that the value is stored in a column compatible with ResultSet.getString and
28  * PreparedStatement.setString. The value is stored as a String containing
29  * the character.
30  * @keep-all
31  */

32 public class CharConverter extends JdbcConverterBase {
33     private char defaultChar;
34
35     public static class Factory extends NoArgJdbcConverterFactory {
36
37         private CharConverter converter;
38
39         /**
40          * Create a converter for col using props 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 CharConverter();
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         String JavaDoc s = rs.getString(index);
59         if (s == null || s.length() == 0) {
60             if (rs.wasNull()) {
61                 if (col.javaTypeCode == MDStatics.CHAR) {
62                     return new Character JavaDoc(defaultChar);
63                 } else {
64                     return null;
65                 }
66             } else {
67                 return new Character JavaDoc(defaultChar);
68             }
69         }
70         return new Character JavaDoc(s.charAt(0));
71     }
72
73
74
75
76     /**
77      * Set parameter index on ps to value (for col).
78      * @exception SQLException on SQL errors
79      * @exception JDOFatalDataStoreException if value is invalid
80      */

81     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col,
82                     Object JavaDoc value)
83         throws SQLException JavaDoc, JDOFatalDataStoreException {
84         if (value == null) {
85             ps.setNull(index, col.jdbcType);
86         } else {
87
88
89             {
90                 Character JavaDoc c = (Character JavaDoc)value;
91                 String JavaDoc s = new String JavaDoc(new char[]{c.charValue()});
92                 ps.setString(index, s);
93             }
94
95         }
96     }
97
98     /**
99      * Get the type of our expected value objects (e.g. java.util.Locale
100      * for a converter for Locale's).
101      */

102     public Class JavaDoc getValueType() {
103         return Character JavaDoc.class;
104     }
105
106 }
107
108
Popular Tags