KickJava   Java API By Example, From Geeks To Geeks.

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


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.JdbcConverter;
15 import com.versant.core.jdbc.JdbcConverterFactory;
16 import com.versant.core.jdbc.JdbcTypeRegistry;
17 import com.versant.core.jdbc.metadata.JdbcColumn;
18 import com.versant.core.metadata.MDStatics;
19
20 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
21
import java.sql.SQLException JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24
25 /**
26  * This converter converts boolean's and Boolean's to and from SQL. It assumes
27  * that the value is stored in a column compatible with ResultSet.getInt and
28  * PreparedStatement.setInt. The value is stored 0 or 1.
29  * @keep-all
30  */

31 public class BooleanConverter extends JdbcConverterBase {
32
33     public static class Factory extends NoArgJdbcConverterFactory {
34
35         private BooleanConverter converter;
36
37         /**
38          * Create a converter for col using props as parameters. Return null if
39          * no converter is required.
40          */

41         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
42                 JdbcTypeRegistry jdbcTypeRegistry) {
43             if (converter == null) 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 JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
55             throws SQLException JavaDoc, 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
69
70     /**
71      * Set parameter index on ps to value (for col).
72      * @exception SQLException on SQL errors
73      * @exception JDOFatalDataStoreException if value is invalid
74      */

75     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
76             throws SQLException JavaDoc, JDOFatalDataStoreException {
77         if (value == null) {
78             ps.setNull(index, col.jdbcType);
79         } else {
80
81             {
82                 boolean v = ((Boolean JavaDoc)value).booleanValue();
83                 ps.setInt(index, v ? 1 : 0);
84             }
85         }
86     }
87
88     /**
89      * Get the type of our expected value objects (e.g. java.util.Locale
90      * for a converter for Locale's).
91      */

92     public Class JavaDoc getValueType() {
93         return Boolean JavaDoc.class;
94     }
95
96 }
97
98
Popular Tags