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.JdbcTypeRegistry;
15: import com.versant.core.jdbc.metadata.JdbcColumn;
16:
17: import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
18: import java.sql.SQLException;
19: import java.sql.ResultSet;
20: import java.sql.PreparedStatement;
21: import java.sql.Timestamp;
22:
23: /**
24: * This converts java.sql.Timestamp's to and from a column using setTimestamp
25: * and getTimestamp.
26: * @keep-all
27: */
28: public class TimestampConverter extends JdbcConverterBase {
29:
30: public static class Factory extends NoArgJdbcConverterFactory {
31:
32: private TimestampConverter converter;
33:
34: /**
35: * Create a converter for col using props as parameters. Return null if
36: * no converter is required.
37: */
38: public JdbcConverter createJdbcConverter(JdbcColumn col,
39: Object args, JdbcTypeRegistry jdbcTypeRegistry) {
40: if (converter == null)
41: converter = new TimestampConverter();
42: return converter;
43: }
44:
45: }
46:
47: /**
48: * Get the value of col from rs at position index.
49: * @exception SQLException on SQL errors
50: * @exception JDOFatalDataStoreException if the ResultSet value is invalid
51: */
52: public Object get(ResultSet rs, int index, JdbcColumn col)
53: throws SQLException, JDOFatalDataStoreException {
54: return rs.getTimestamp(index);
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.setTimestamp(index, (Timestamp) value);
69: }
70: }
71:
72: /**
73: * Get the type of our expected value objects (e.g. java.util.Locale
74: * for a converter for Locale's).
75: */
76: public Class getValueType() {
77: return Timestamp.class;
78: }
79:
80: }
|