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.ResultSet;
20: import java.sql.SQLException;
21: import java.sql.PreparedStatement;
22: import java.math.BigDecimal;
23:
24: /**
25: * BigDecimal converter for Interbase and Firebird.
26: */
27: public class BigDecimalConverter extends JdbcConverterBase {
28:
29: public static class Factory extends NoArgJdbcConverterFactory {
30:
31: private BigDecimalConverter converter;
32:
33: /**
34: * Create a converter for col using props as parameters. Return null if
35: * no converter is required.
36: */
37: public JdbcConverter createJdbcConverter(JdbcColumn col,
38: Object args, JdbcTypeRegistry jdbcTypeRegistry) {
39: if (converter == null)
40: converter = new BigDecimalConverter();
41: return converter;
42: }
43:
44: }
45:
46: /**
47: * Get the value of col from rs at position index.
48: * @exception java.sql.SQLException on SQL errors
49: * @exception javax.jdo.JDOFatalDataStoreException if the ResultSet value is invalid
50: */
51: public Object get(ResultSet rs, int index, JdbcColumn col)
52: throws SQLException, JDOFatalDataStoreException {
53: double d = rs.getDouble(index);
54: if (rs.wasNull())
55: return null;
56: return new BigDecimal(d);
57: }
58:
59: /**
60: * Set parameter index on ps to value (for col).
61: * @exception java.sql.SQLException on SQL errors
62: * @exception javax.jdo.JDOFatalDataStoreException if value is invalid
63: */
64: public void set(PreparedStatement ps, int index, JdbcColumn col,
65: Object value) throws SQLException,
66: JDOFatalDataStoreException {
67: if (value == null) {
68: ps.setNull(index, col.jdbcType);
69: } else {
70: BigDecimal bigDecimal = (BigDecimal) value;
71: ps.setDouble(index, bigDecimal.doubleValue());
72: }
73: }
74:
75: /**
76: * Get the type of our expected value objects (e.g. java.util.Locale
77: * for a converter for Locale's).
78: */
79: public Class getValueType() {
80: return BigDecimal.class;
81: }
82:
83: }
|