KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
20
import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.Types JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 /**
27  * This converter trims Strings returned by ResultSet.getString(). It is
28  * used for Informix SE which only have support for space padded CHAR columns
29  * i.e. no VARCHAR support. For nulls the JDBC type is always set to CHAR.
30  * @keep-all
31  */

32 public class InformixSETrimStringConverter extends JdbcConverterBase {
33
34     public static class Factory extends NoArgJdbcConverterFactory {
35
36         private InformixSETrimStringConverter converter;
37
38         /**
39          * Create a converter for col using args 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 InformixSETrimStringConverter();
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) return null;
59         int n = s.length() - 1;
60         int pos;
61         for (pos = n; pos >= 0 && s.charAt(pos) <= ' '; --pos);
62         return pos == n ? s : s.substring(0, pos + 1);
63     }
64
65     /**
66      * Set parameter index on ps to value (for col).
67      * @exception SQLException on SQL errors
68      * @exception JDOFatalDataStoreException if value is invalid
69      */

70     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
71             throws SQLException JavaDoc, JDOFatalDataStoreException {
72         if (value == null) {
73             ps.setNull(index, Types.CHAR);
74         } else {
75             ps.setString(index, (String JavaDoc)value);
76         }
77     }
78
79     /**
80      * Get the type of our expected value objects (e.g. java.util.Locale
81      * for a converter for Locale's).
82      */

83     public Class JavaDoc getValueType() {
84         return String JavaDoc.class;
85     }
86
87 }
88
89
Popular Tags