01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.conv;
12:
13: import com.versant.core.jdbc.JdbcConverter;
14: import com.versant.core.jdbc.JdbcConverterFactory;
15: import com.versant.core.jdbc.JdbcTypeRegistry;
16: import com.versant.core.jdbc.metadata.JdbcColumn;
17:
18: import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
19: import java.sql.PreparedStatement;
20: import java.sql.SQLException;
21: import java.sql.ResultSet;
22: import java.io.File;
23:
24: /**
25: * <p>This is a base class for converters that convert some type to/from String
26: * and store it in a column compatible with getString/setString. The string
27: * retrieved is trimmed before the value instance is created. Subclasses
28: * must implement:</p>
29: *
30: * <ul>
31: * <li>{@link #fromString(String) }
32: * <li>{@link #getValueType() }
33: * </ul>
34: *
35: * <p>Subclasses may also implement {@link #toString(Object) } if the
36: * toString method of the value type itself is not suitable. A
37: * {@link JdbcConverterFactory} must also be written.</p>
38: *
39: * @keep-all
40: */
41: public abstract class TypeAsTrimStringConverterBase extends
42: 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 get(ResultSet rs, int index, JdbcColumn col)
50: throws SQLException, JDOFatalDataStoreException {
51: String s = rs.getString(index);
52: if (s == null)
53: return null;
54: return fromString(s.trim());
55: }
56:
57: /**
58: * Set parameter index on ps to value (for col).
59: * @exception SQLException on SQL errors
60: * @exception JDOFatalDataStoreException if value is invalid
61: */
62: public void set(PreparedStatement ps, int index, JdbcColumn col,
63: Object value) throws SQLException,
64: JDOFatalDataStoreException {
65: if (value == null) {
66: ps.setNull(index, col.jdbcType);
67: } else {
68: ps.setString(index, toString(value));
69: }
70: }
71:
72: /**
73: * Create an instance of our type from a String.
74: * @param s String to use (never null)
75: */
76: protected abstract Object fromString(String s);
77:
78: /**
79: * Convert an instance of our type to a String.
80: * @param value Value to convert (never null)
81: */
82: protected String toString(Object value) {
83: return value.toString();
84: }
85:
86: }
|