KickJava   Java API By Example, From Geeks To Geeks.

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


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.PreparedStatement JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.sql.Types JavaDoc;
39
40 /**
41  * Represents a primitive Java byte type
42  */

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

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

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

72   public boolean isNumeric()
73   {
74     return true;
75   }
76
77   /**
78    * Returns the foreign key type.
79    */

80   public Type getForeignType()
81   {
82     return ByteType.create();
83   }
84
85   /**
86    * Generates the type for the table.
87    */

88   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager,
89                                         int length,
90                                         int precision,
91                                         int scale)
92   {
93     return manager.getCreateColumnSQL(Types.TINYINT, length, precision, scale);
94   }
95
96   /**
97    * Generates a string to load the property.
98    */

99   public int generateLoad(JavaWriter out, String JavaDoc rs,
100                           String JavaDoc indexVar, int index)
101     throws IOException JavaDoc
102   {
103     out.print(rs + ".getByte(" + indexVar + " + " + index + ")");
104
105     return index + 1;
106   }
107
108   /**
109    * Generates a string to set the property.
110    */

111   public void generateSet(JavaWriter out, String JavaDoc pstmt,
112                           String JavaDoc index, String JavaDoc value)
113     throws IOException JavaDoc
114   {
115     out.println(pstmt + ".setByte(" + index + "++, " + value + ");");
116   }
117
118   /**
119    * Generates a string to set the property.
120    */

121   public void generateSetNull(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
122     throws IOException JavaDoc
123   {
124     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.TINYINT);");
125   }
126
127   /**
128    * Converts to an object.
129    */

130   public String JavaDoc toObject(String JavaDoc value)
131   {
132     return "new Byte(" + value + ")";
133   }
134
135   /**
136    * Converts the value.
137    */

138   public String JavaDoc generateCastFromObject(String JavaDoc value)
139   {
140     return "((Number) " + value + ").byteValue()";
141   }
142
143   /**
144    * Sets the value.
145    */

146   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
147     throws SQLException JavaDoc
148   {
149     if (value instanceof Number JavaDoc)
150       pstmt.setString(index, value.toString());
151     else
152       throw new IllegalArgumentException JavaDoc("Invalid argument for setParameter.");
153   }
154 }
155
Popular Tags