KickJava   Java API By Example, From Geeks To Geeks.

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


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.JdbcConverterFactory;
15 import com.versant.core.jdbc.metadata.JdbcColumn;
16
17 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
18
import java.sql.PreparedStatement JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21
22 /**
23  * <p>This is a base class for converters that convert some type to/from String
24  * and store it in a column compatible with getString/setString. Subclasses
25  * must implement:</p>
26  *
27  * <ul>
28  * <li>{@link #fromString(String) }
29  * <li>{@link #getValueType() }
30  * </ul>
31  *
32  * <p>Subclasses may also implement {@link #toString(Object) } if the
33  * toString method of the value type itself is not suitable. A
34  * {@link JdbcConverterFactory} must also be written.</p>
35  *
36  * @keep-all
37  */

38 public abstract class TypeAsStringConverterBase extends JdbcConverterBase {
39
40     /**
41      * Get the value of col from rs at position index.
42      * @exception SQLException on SQL errors
43      * @exception JDOFatalDataStoreException if the ResultSet value is invalid
44      */

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

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

70     protected abstract Object JavaDoc fromString(String JavaDoc s);
71
72     /**
73      * Convert an instance of our type to a String.
74      * @param value Value to convert (never null)
75      */

76     protected String JavaDoc toString(Object JavaDoc value) {
77         return value.toString();
78     }
79
80 }
81
82
Popular Tags