KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > tools > schemaframework > FieldDefinition


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.tools.schemaframework;
23
24 import java.io.*;
25 import oracle.toplink.essentials.internal.helper.*;
26 import oracle.toplink.essentials.internal.databaseaccess.*;
27 import oracle.toplink.essentials.exceptions.*;
28 import oracle.toplink.essentials.internal.sessions.AbstractSession;
29
30 /**
31  * <p>
32  * <b>Purpose</b>: Define a database field definition for creation within a table.
33  * This differs from DatabaseField in that it is used only table creation not a runtime.
34  * <p>
35  * <b>Responsibilities</b>:
36  * <ul>
37  * <li> Store the name, java type, size and sub-size.
38  * The sizes are optional and the name of the java class is used for the type.
39  * </ul>
40  */

41 public class FieldDefinition implements Serializable, Cloneable JavaDoc {
42     protected String JavaDoc name;
43     protected Class JavaDoc type;
44     protected String JavaDoc typeName;
45     protected int size;
46     protected int subSize;
47     protected boolean shouldAllowNull;
48     protected boolean isIdentity;
49     protected boolean isPrimaryKey;
50     protected boolean isUnique;
51     protected String JavaDoc additional;
52     protected String JavaDoc constraint;
53     protected String JavaDoc foreignKeyFieldName;
54
55     public FieldDefinition() {
56         this.name = "";
57         this.size = 0;
58         this.subSize = 0;
59         this.shouldAllowNull = true;
60         this.isIdentity = false;
61         this.isPrimaryKey = false;
62         this.isUnique = false;
63     }
64
65     public FieldDefinition(String JavaDoc name, Class JavaDoc type) {
66         this.name = name;
67         this.type = type;
68         this.size = 0;
69         this.subSize = 0;
70         shouldAllowNull = true;
71         isIdentity = false;
72         isPrimaryKey = false;
73         isUnique = false;
74     }
75
76     public FieldDefinition(String JavaDoc name, Class JavaDoc type, int size) {
77         this();
78         this.name = name;
79         this.type = type;
80         this.size = size;
81     }
82
83     public FieldDefinition(String JavaDoc name, Class JavaDoc type, int size, int subSize) {
84         this();
85         this.name = name;
86         this.type = type;
87         this.size = size;
88         this.subSize = subSize;
89     }
90
91     public FieldDefinition(String JavaDoc name, String JavaDoc typeName) {
92         this();
93         this.name = name;
94         this.typeName = typeName;
95     }
96
97     /**
98      * INTERNAL:
99      * Append the database field definition string to the table creation statement.
100      */

101     public void appendDBString(Writer writer, AbstractSession session, TableDefinition table) throws ValidationException {
102         FieldTypeDefinition fieldType;
103         if (getType() != null) {
104             fieldType = session.getPlatform().getFieldTypeDefinition(getType());
105             if (fieldType == null) {
106                 throw ValidationException.javaTypeIsNotAValidDatabaseType(getType());
107             }
108         } else {
109             fieldType = new FieldTypeDefinition(getTypeName());
110         }
111         try {
112             writer.write(getName());
113             writer.write(" ");
114             String JavaDoc qualifiedName = table.getFullName() + '.' + getName();
115             session.getPlatform().printFieldTypeSize(writer, this, fieldType, session, qualifiedName);
116             session.getPlatform().printFieldUnique(writer, isUnique(), session, qualifiedName);
117             if (isIdentity()) {
118                 String JavaDoc name = table.getFullName() + '.' + getName();
119                 session.getPlatform().printFieldIdentityClause(writer, session, name);
120             }
121             if (shouldAllowNull() && fieldType.shouldAllowNull()) {
122                 session.getPlatform().printFieldNullClause(writer);
123             } else {
124                 session.getPlatform().printFieldNotNullClause(writer);
125             }
126             if (getConstraint() != null) {
127                 writer.write(" " + getConstraint());
128             }
129             if (getAdditional() != null) {
130                 writer.write(" " + getAdditional());
131             }
132         } catch (IOException ioException) {
133             throw ValidationException.fileError(ioException);
134         }
135     }
136
137     /**
138      * INTERNAL:
139      * Append the database field definition string to the type creation statement.
140      * Types do not support constraints.
141      */

142     public void appendTypeString(Writer writer, AbstractSession session) throws ValidationException {
143         FieldTypeDefinition fieldType;
144         if (getType() != null) {
145             fieldType = session.getPlatform().getFieldTypeDefinition(getType());
146             if (fieldType == null) {
147                 throw ValidationException.javaTypeIsNotAValidDatabaseType(getType());
148             }
149         } else {
150             fieldType = new FieldTypeDefinition(getTypeName());
151         }
152         try {
153             writer.write(getName());
154             writer.write(" ");
155             writer.write(fieldType.getName());
156             if ((fieldType.isSizeAllowed()) && ((getSize() != 0) || (fieldType.isSizeRequired()))) {
157                 writer.write("(");
158                 if (getSize() == 0) {
159                     writer.write(new Integer JavaDoc(fieldType.getDefaultSize()).toString());
160                 } else {
161                     writer.write(new Integer JavaDoc(getSize()).toString());
162                 }
163                 if (getSubSize() != 0) {
164                     writer.write(",");
165                     writer.write(new Integer JavaDoc(getSubSize()).toString());
166                 } else if (fieldType.getDefaultSubSize() != 0) {
167                     writer.write(",");
168                     writer.write(new Integer JavaDoc(fieldType.getDefaultSubSize()).toString());
169                 }
170                 writer.write(")");
171             }
172             if (getAdditional() != null) {
173                 writer.write(" " + getAdditional());
174             }
175         } catch (IOException ioException) {
176             throw ValidationException.fileError(ioException);
177         }
178     }
179
180     /**
181      * PUBLIC:
182      */

183     public Object JavaDoc clone() {
184         try {
185             return super.clone();
186         } catch (CloneNotSupportedException JavaDoc impossible) {
187             return null;
188         }
189     }
190
191     /**
192      * PUBLIC:
193      * Return any additional information about this field to be given when the table is created.
194      */

195     public String JavaDoc getAdditional() {
196         return additional;
197     }
198
199     /**
200      * PUBLIC:
201      * Return any constraint of this field.
202      * i.e. "BETWEEN 0 AND 1000000".
203      */

204     public String JavaDoc getConstraint() {
205         return constraint;
206     }
207
208     public String JavaDoc getForeignKeyFieldName() {
209         return foreignKeyFieldName;
210     }
211
212     /**
213      * PUBLIC:
214      * Return the name of the field.
215      */

216     public String JavaDoc getName() {
217         return name;
218     }
219
220     /**
221      * PUBLIC:
222      * Return the size of the field, this is only required for some field types.
223      */

224     public int getSize() {
225         return size;
226     }
227
228     /**
229      * PUBLIC:
230      * Return the sub-size of the field.
231      * This is used as the decimal precision for numeric values only.
232      */

233     public int getSubSize() {
234         return subSize;
235     }
236
237     /**
238      * PUBLIC:
239      * Return the type of the field.
240      * This should be set to a java class, such as String.class, Integer.class or Date.class.
241      */

242     public Class JavaDoc getType() {
243         return type;
244     }
245
246     /**
247      * PUBLIC:
248      * Return the type of the field.
249      * This is the exact DB type name, which can be used instead of the Java class.
250      */

251     public String JavaDoc getTypeName() {
252         return typeName;
253     }
254
255     /**
256      * PUBLIC:
257      * Answer whether the receiver is an identity field.
258      * Identity fields are Sybase specific,
259      * they insure that on insert a unique sequencial value is store in the row.
260      */

261     public boolean isIdentity() {
262         return isIdentity;
263     }
264
265     /**
266      * PUBLIC:
267      * Answer whether the receiver is a primary key.
268      * If the table has a multipart primary key this should be set in each field.
269      */

270     public boolean isPrimaryKey() {
271         return isPrimaryKey;
272     }
273
274     /**
275      * PUBLIC:
276      * Answer whether the receiver is a unique constraint field.
277      */

278     public boolean isUnique() {
279         return isUnique;
280     }
281
282     /**
283      * PUBLIC:
284      * Set any additional information about this field to be given when the table is created.
285      */

286     public void setAdditional(String JavaDoc string) {
287         additional = string;
288     }
289
290     /**
291      * PUBLIC:
292      * Set any constraint of this field.
293      * i.e. "BETWEEN 0 AND 1000000".
294      */

295     public void setConstraint(String JavaDoc string) {
296         constraint = string;
297     }
298
299     public void setForeignKeyFieldName(String JavaDoc foreignKeyFieldName) {
300         this.foreignKeyFieldName = foreignKeyFieldName;
301     }
302
303     /**
304      * PUBLIC:
305      * Set whether the receiver is an identity field.
306      * Identity fields are Sybase specific,
307      * they insure that on insert a unique sequencial value is store in the row.
308      */

309     public void setIsIdentity(boolean value) {
310         isIdentity = value;
311         if (value) {
312             setShouldAllowNull(false);
313         }
314     }
315
316     /**
317      * PUBLIC:
318      * Set whether the receiver is a primary key.
319      * If the table has a multipart primary key this should be set in each field.
320      */

321     public void setIsPrimaryKey(boolean value) {
322         isPrimaryKey = value;
323         if (value) {
324             setShouldAllowNull(false);
325         }
326     }
327
328     /**
329      * PUBLIC:
330      * Set the name of the field.
331      */

332     public void setName(String JavaDoc name) {
333         this.name = name;
334     }
335
336     /**
337      * PUBLIC:
338      * Set whether the receiver should allow null values.
339      */

340     public void setShouldAllowNull(boolean value) {
341         shouldAllowNull = value;
342     }
343
344     /**
345      * PUBLIC:
346      * Set the size of the field, this is only required for some field types.
347      */

348     public void setSize(int size) {
349         this.size = size;
350     }
351
352     /**
353      * PUBLIC:
354      * Set the sub-size of the field.
355      * This is used as the decimal precision for numeric values only.
356      */

357     public void setSubSize(int subSize) {
358         this.subSize = subSize;
359     }
360
361     /**
362      * PUBLIC:
363      * Set the type of the field.
364      * This should be set to a java class, such as String.class, Integer.class or Date.class.
365      */

366     public void setType(Class JavaDoc type) {
367         this.type = type;
368     }
369
370     /**
371      * PUBLIC:
372      * Set the type of the field.
373      * This is the exact DB type name, which can be used instead of the Java class.
374      */

375     public void setTypeName(String JavaDoc typeName) {
376         this.typeName = typeName;
377     }
378
379     /**
380      * PUBLIC:
381      * Set whether the receiver is a unique constraint field.
382      */

383     public void setUnique(boolean value) {
384         isUnique = value;
385     }
386
387     /**
388      * PUBLIC:
389      * Return whether the receiver should allow null values.
390      */

391     public boolean shouldAllowNull() {
392         return shouldAllowNull;
393     }
394
395     public String JavaDoc toString() {
396         return Helper.getShortClassName(getClass()) + "(" + getName() + "(" + getType() + "))";
397     }
398 }
399
Popular Tags