KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > databaseaccess > FieldTypeDefinition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.databaseaccess;
23
24 import java.io.*;
25 import oracle.toplink.essentials.internal.helper.*;
26
27 /**
28  * INTERNAL:
29  * <b>Purpose</b>: Define a database platform specific definition for a platform independant Java class type.
30  * This is used for the field creation within a table creation statement.
31  * <p><b>Responsibilities</b>:<ul>
32  * <li> Store a default size and know if the size option is required or optional.
33  * <li> Store the name of the real database type.
34  * <li> Maintain maximum precision and optionall min & max Scale.
35  * </ul>
36  */

37 public class FieldTypeDefinition implements Serializable {
38     protected String JavaDoc name;
39     protected int defaultSize;
40     protected int defaultSubSize;
41     protected boolean isSizeAllowed;
42     protected boolean isSizeRequired;
43     protected int maxPrecision;
44     protected int minScale;
45     protected int maxScale;
46     protected boolean shouldAllowNull; //allow for specific types/platforms to not allow null
47

48     public FieldTypeDefinition() {
49         defaultSize = 10;
50         isSizeRequired = false;
51         isSizeAllowed = true;
52         maxPrecision = 10;
53         minScale = 0;
54         maxScale = 0;
55         shouldAllowNull = true;
56     }
57
58     /**
59     * Return a new field type.
60     * @see #setName()
61     */

62     public FieldTypeDefinition(String JavaDoc databaseTypeName) {
63         this();
64         name = databaseTypeName;
65     }
66
67     /**
68      * Return a new field type with a required size defaulting to the defaultSize.
69      */

70     public FieldTypeDefinition(String JavaDoc databaseTypeName, int defaultSize) {
71         this();
72         this.name = databaseTypeName;
73         this.defaultSize = defaultSize;
74         this.isSizeRequired = true;
75         setMaxPrecision(defaultSize);
76     }
77
78     /**
79      * Return a new field type with a required size defaulting to the defaultSize.
80      */

81     public FieldTypeDefinition(String JavaDoc databaseTypeName, int defaultSize, int defaultSubSize) {
82         this();
83         this.name = databaseTypeName;
84         this.defaultSize = defaultSize;
85         this.defaultSubSize = defaultSubSize;
86         this.isSizeRequired = true;
87         setMaxPrecision(defaultSize);
88         setMaxScale(defaultSubSize);
89     }
90
91     /**
92      * Return a new field type with a required size defaulting to the defaultSize.
93      */

94     public FieldTypeDefinition(String JavaDoc databaseTypeName, boolean allowsSize) {
95         this();
96         this.name = databaseTypeName;
97         this.isSizeAllowed = allowsSize;
98     }
99     
100     /** Return a new field type with a required size defaulting to the defaultSize and
101      * shouldAllowNull set to allowsNull.
102      */

103     public FieldTypeDefinition(String JavaDoc databaseTypeName, boolean allowsSize, boolean allowsNull) {
104         this(databaseTypeName, allowsSize);
105         this.shouldAllowNull = allowsNull;
106     }
107
108     /**
109      * Return the default size for this type.
110      * This default size will be used if the database requires specification of a size,
111      * and the table definition did not provide one.
112      */

113     public int getDefaultSize() {
114         return defaultSize;
115     }
116
117     /**
118     * Return the default sub-size for this type.
119     * This default size will be used if the database requires specification of a size,
120     * and the table definition did not provide one.
121     */

122     public int getDefaultSubSize() {
123         return defaultSubSize;
124     }
125
126     public int getMaxPrecision() {
127         return maxPrecision;
128     }
129
130     public int getMaxScale() {
131         return maxScale;
132     }
133
134     public int getMinScale() {
135         return minScale;
136     }
137
138     /**
139     * Return the name.
140     * @param name can be any database primitive type name,
141     * this name will then be mapped to the Java primitive type,
142     * the datbase type varies by platform and the mappings can be found in the subclasses of DatabasePlatform.
143     *
144     * these Java names and their ODBC mappings include;
145     * - Integer -> SQL_INT
146     * - Float -> SQL_FLOAT
147     * - Double -> SQL_DOUBLE
148     * - Long -> SQL_LONG
149     * - Short -> SQL_INT
150     * - BigDecimal -> SQL_NUMERIC
151     * - BigInteger -> SQL_NUMERIC
152     * - String -> SQL_VARCHAR
153     * - Array -> BLOB
154     * - Character[] -> SQL_CHAR
155     * - Boolean -> SQL_BOOL
156     * - Text -> CLOB
157     * - Date -> SQL_DATE
158     * - Time -> SQL_TIME
159     * - Timestamp -> SQL_TIMESTAMP
160     *
161     * @see oracle.toplink.essentials.internal.databaseaccess.DatabasePlatform
162     */

163     public String JavaDoc getName() {
164         return name;
165     }
166
167     /**
168     * Return if this type can support a size specification.
169     */

170     public boolean isSizeAllowed() {
171         return isSizeAllowed;
172     }
173
174     /**
175     * Return if this type must have a size specification.
176     */

177     public boolean isSizeRequired() {
178         return isSizeRequired;
179     }
180
181     /**
182      * Return if this type is allowed to be null for this platform
183      */

184     public boolean shouldAllowNull() {
185         return this.shouldAllowNull;
186     }
187     
188     /**
189     * Set the default size for this type.
190     * This default size will be used if the database requires specification of a size,
191     * and the table definition did not provide one.
192     */

193     public void setDefaultSize(int defaultSize) {
194         this.defaultSize = defaultSize;
195     }
196
197     /**
198     * Set the default sub-size for this type.
199     * This default size will be used if the database requires specification of a size,
200     * and the table definition did not provide one.
201     */

202     public void setDefaultSubSize(int defaultSubSize) {
203         this.defaultSubSize = defaultSubSize;
204     }
205
206     /**
207     * Set if this type can support a size specification.
208     */

209     public void setIsSizeAllowed(boolean aBoolean) {
210         isSizeAllowed = aBoolean;
211     }
212
213     /**
214     * Set if this type must have a size specification.
215     */

216     public void setIsSizeRequired(boolean aBoolean) {
217         isSizeRequired = aBoolean;
218     }
219
220     /**
221      * Set if this type is allowed to be null for this platform
222      */

223     public void setShouldAllowNull(boolean allowsNull) {
224         this.shouldAllowNull = allowsNull;
225     }
226     
227     /**
228      * Set the maximum precision and the minimum and maximum scale.
229      * @return this Allowing the method to be invoked inline with constructor
230      */

231     public FieldTypeDefinition setLimits(int maxPrecision, int minScale, int maxScale) {
232         setMaxPrecision(maxPrecision);
233         setMinScale(minScale);
234         setMaxScale(maxScale);
235         return this;
236     }
237
238     public void setMaxPrecision(int maximum) {
239         maxPrecision = maximum;
240     }
241
242     public void setMaxScale(int maximum) {
243         maxScale = maximum;
244     }
245
246     public void setMinScale(int minimum) {
247         minScale = minimum;
248     }
249
250     /**
251     * Set the name.
252     * @param name can be any database primitive type name,
253     * this name will then be mapped to the Java primitive type,
254     * the datbase type varies by platform and the mappings can be found in the subclasses of DatabasePlatform.
255     *
256     * these Java names and their ODBC mappings include;
257     * - Integer -> SQL_INT
258     * - Float -> SQL_FLOAT
259     * - Double -> SQL_DOUBLE
260     * - Long -> SQL_LONG
261     * - Short -> SQL_INT
262     * - BigDecimal -> SQL_NUMERIC
263     * - BigInteger -> SQL_NUMERIC
264     * - String -> SQL_VARCHAR
265     * - Array -> BLOB
266     * - Character[] -> SQL_CHAR
267     * - Boolean -> SQL_BOOL
268     * - Text -> CLOB
269     * - Date -> SQL_DATE
270     * - Time -> SQL_TIME
271     * - Timestamp -> SQL_TIMESTAMP
272     *
273     * @see oracle.toplink.essentials.internal.databaseaccess.DatabasePlatform
274     */

275     public void setName(String JavaDoc name) {
276         this.name = name;
277     }
278
279     /**
280     * Set this type to not allow a size specification.
281     */

282     public void setSizeDisallowed() {
283         setIsSizeAllowed(false);
284     }
285
286     /**
287     * Set this type to optionally have a size specification.
288     */

289     public void setSizeOptional() {
290         setIsSizeRequired(false);
291         setIsSizeAllowed(true);
292     }
293
294     /**
295     * Set this type to require to have a size specification.
296     */

297     public void setSizeRequired() {
298         setIsSizeRequired(true);
299     }
300
301     public String JavaDoc toString() {
302         return Helper.getShortClassName(getClass()) + "(" + getName() + ")";
303     }
304 }
305
Popular Tags