KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > BooleanType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.type;
31
32 import com.caucho.amber.manager.AmberPersistenceUnit;
33 import com.caucho.java.JavaWriter;
34 import com.caucho.util.L10N;
35
36 import java.io.IOException JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Types JavaDoc;
40
41 /**
42  * The type of a property.
43  */

44 public class BooleanType extends Type {
45   private static final L10N L = new L10N(BooleanType.class);
46
47   private static final BooleanType BOOLEAN_TYPE = new BooleanType();
48
49   private BooleanType()
50   {
51   }
52
53   /**
54    * Returns the string type.
55    */

56   public static BooleanType create()
57   {
58     return BOOLEAN_TYPE;
59   }
60
61   /**
62    * Returns the type name.
63    */

64   public String JavaDoc getName()
65   {
66     return "java.lang.Boolean";
67   }
68
69   /**
70    * Returns true for a boolean type.
71    */

72   public boolean isBoolean()
73   {
74     return true;
75   }
76
77   /**
78    * Generates the type for the table.
79    */

80   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
81   {
82     return manager.getCreateColumnSQL(Types.BOOLEAN, length, precision, scale);
83   }
84
85   /**
86    * Generates a string to load the property.
87    */

88   public int generateLoad(JavaWriter out, String JavaDoc rs,
89                           String JavaDoc indexVar, int index)
90     throws IOException JavaDoc
91   {
92     out.print("com.caucho.amber.type.BooleanType.toBoolean(" +
93               rs + ".getBoolean(" + indexVar + " + " + index + "), " +
94               rs + ".wasNull())");
95
96     return index + 1;
97   }
98
99   /**
100    * Generates a string to set the property.
101    */

102   public void generateSet(JavaWriter out, String JavaDoc pstmt,
103                           String JavaDoc index, String JavaDoc value)
104     throws IOException JavaDoc
105   {
106     out.println("if (" + value + " == null)");
107     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.BIT);");
108     out.println("else");
109     out.println(" " + pstmt + ".setBoolean(" + index + "++, " +
110                 value + ".booleanValue());");
111   }
112
113   /**
114    * Generates a string to set the property.
115    */

116   public void generateSetNull(JavaWriter out, String JavaDoc pstmt,
117                               String JavaDoc index)
118     throws IOException JavaDoc
119   {
120     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.BIT);");
121   }
122
123   /**
124    * Converts a value to a boolean.
125    */

126   public static Boolean JavaDoc toBoolean(boolean value, boolean wasNull)
127   {
128     if (wasNull)
129       return null;
130     else
131       return new Boolean JavaDoc(value);
132   }
133
134   /**
135    * Gets the value.
136    */

137   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
138     throws SQLException JavaDoc
139   {
140     boolean value = rs.getBoolean(index);
141
142     return rs.wasNull() ? null : (value ? Boolean.TRUE : Boolean.FALSE);
143   }
144 }
145
Popular Tags