KickJava   Java API By Example, From Geeks To Geeks.

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


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 is a converter for databases that do not handle
27  * (Character.MIN_VALUE) propertly (e.g. Postgres and Informix). A char with
28  * value must set as an empty String.
29  *
30  * @keep-all
31  */

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

42         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
43                 JdbcTypeRegistry jdbcTypeRegistry) {
44             if (converter == null) converter = new NoMinCharConverter();
45             return converter;
46         }
47
48     }
49
50     /**
51      * Get the value of col from rs at position index.
52      * @exception SQLException on SQL errors
53      * @exception JDOFatalDataStoreException if the ResultSet value is invalid
54      */

55     public Object JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
56             throws SQLException JavaDoc, JDOFatalDataStoreException {
57         String JavaDoc s = rs.getString(index);
58         if (s == null || s.length() == 0) {
59             if (rs.wasNull()) {
60                 if (col.javaTypeCode == MDStatics.CHAR) {
61                     return new Character JavaDoc(defaultChar);
62                 } else {
63                     return null;
64                 }
65             } else {
66                 return new Character JavaDoc(defaultChar);
67             }
68         }
69         return new Character JavaDoc(s.charAt(0));
70     }
71
72     /**
73      * Set parameter index on ps to value (for col).
74      * @exception SQLException on SQL errors
75      * @exception JDOFatalDataStoreException if value is invalid
76      */

77     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
78             throws SQLException JavaDoc, JDOFatalDataStoreException {
79         if (value == null) {
80             ps.setNull(index, col.jdbcType);
81         } else {
82             Character JavaDoc c = (Character JavaDoc)value;
83             char cv = c.charValue();
84             String JavaDoc s = cv == Character.MIN_VALUE ? "" : new String JavaDoc(new char[]{cv});
85             ps.setString(index, s);
86         }
87     }
88
89     /**
90      * Get the type of our expected value objects (e.g. java.util.Locale
91      * for a converter for Locale's).
92      */

93     public Class JavaDoc getValueType() {
94         return Character JavaDoc.class;
95     }
96
97 }
98
99
Popular Tags