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: import com.versant.core.metadata.MDStatics;
18:
19: import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
20: import java.sql.SQLException;
21: import java.sql.ResultSet;
22: import java.sql.PreparedStatement;
23:
24: /**
25: * This converter converts boolean's and Boolean's to and from SQL. It assumes
26: * that the value is stored in a column compatible with ResultSet.getInt and
27: * PreparedStatement.setInt. The value is stored 0 or 1.
28: * @keep-all
29: */
30: public class BooleanConverter extends JdbcConverterBase {
31:
32: public static class Factory extends NoArgJdbcConverterFactory {
33:
34: private BooleanConverter converter;
35:
36: /**
37: * Create a converter for col using props as parameters. Return null if
38: * no converter is required.
39: */
40: public JdbcConverter createJdbcConverter(JdbcColumn col,
41: Object args, JdbcTypeRegistry jdbcTypeRegistry) {
42: if (converter == null)
43: converter = new BooleanConverter();
44: return converter;
45: }
46:
47: }
48:
49: /**
50: * Get the value of col from rs at position index.
51: * @exception SQLException on SQL errors
52: * @exception JDOFatalDataStoreException if the ResultSet value is invalid
53: */
54: public Object get(ResultSet rs, int index, JdbcColumn col)
55: throws SQLException, JDOFatalDataStoreException {
56: int i = rs.getInt(index);
57: if (rs.wasNull()) {
58: if (col.javaTypeCode == MDStatics.BOOLEAN) {
59: return Boolean.FALSE;
60: } else {
61: return null;
62: }
63: }
64: return i == 0 ? Boolean.FALSE : Boolean.TRUE;
65: }
66:
67: /**
68: * Set parameter index on ps to value (for col).
69: * @exception SQLException on SQL errors
70: * @exception JDOFatalDataStoreException if value is invalid
71: */
72: public void set(PreparedStatement ps, int index, JdbcColumn col,
73: Object value) throws SQLException,
74: JDOFatalDataStoreException {
75: if (value == null) {
76: ps.setNull(index, col.jdbcType);
77: } else {
78:
79: {
80: boolean v = ((Boolean) value).booleanValue();
81: ps.setInt(index, v ? 1 : 0);
82: }
83: }
84: }
85:
86: /**
87: * Get the type of our expected value objects (e.g. java.util.Locale
88: * for a converter for Locale's).
89: */
90: public Class getValueType() {
91: return Boolean.class;
92: }
93:
94: }
|