KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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

43 public class ByteType extends Type {
44   private static final L10N L = new L10N(ByteType.class);
45
46   private static final ByteType BYTE_TYPE = new ByteType();
47
48   private ByteType()
49   {
50   }
51
52   /**
53    * Returns the singleton Byte type.
54    */

55   public static ByteType create()
56   {
57     return BYTE_TYPE;
58   }
59
60   /**
61    * Returns the type name.
62    */

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

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

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

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

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

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

128   public static Byte JavaDoc toByte(int value, boolean wasNull)
129   {
130     if (wasNull)
131       return null;
132     else
133       return new Byte JavaDoc((byte) value);
134   }
135
136   /**
137    * Gets the value.
138    */

139   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
140     throws SQLException JavaDoc
141   {
142     int value = rs.getByte(index);
143
144     return rs.wasNull() ? null : new Byte JavaDoc((byte) value);
145   }
146 }
147
Popular Tags