KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
24
25 /**
26  * <p>This is a base class for converters that convert some type to/from String
27  * and store it in a column compatible with getString/setString. The string
28  * retrieved is trimmed before the value instance is created. Subclasses
29  * must implement:</p>
30  *
31  * <ul>
32  * <li>{@link #fromString(String) }
33  * <li>{@link #getValueType() }
34  * </ul>
35  *
36  * <p>Subclasses may also implement {@link #toString(Object) } if the
37  * toString method of the value type itself is not suitable. A
38  * {@link JdbcConverterFactory} must also be written.</p>
39  *
40  * @keep-all
41  */

42 public abstract class TypeAsTrimStringConverterBase extends JdbcConverterBase {
43
44     /**
45      * Get the value of col from rs at position index.
46      * @exception SQLException on SQL errors
47      * @exception JDOFatalDataStoreException if the ResultSet value is invalid
48      */

49     public Object JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
50             throws SQLException JavaDoc, JDOFatalDataStoreException {
51         String JavaDoc s = rs.getString(index);
52         if (s == null) return null;
53         return fromString(s.trim());
54     }
55
56     /**
57      * Set parameter index on ps to value (for col).
58      * @exception SQLException on SQL errors
59      * @exception JDOFatalDataStoreException if value is invalid
60      */

61     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
62             throws SQLException JavaDoc, JDOFatalDataStoreException {
63         if (value == null) {
64             ps.setNull(index, col.jdbcType);
65         } else {
66             ps.setString(index, toString(value));
67         }
68     }
69
70     /**
71      * Create an instance of our type from a String.
72      * @param s String to use (never null)
73      */

74     protected abstract Object JavaDoc fromString(String JavaDoc s);
75
76     /**
77      * Convert an instance of our type to a String.
78      * @param value Value to convert (never null)
79      */

80     protected String JavaDoc toString(Object JavaDoc value) {
81         return value.toString();
82     }
83
84 }
85
86
Popular Tags