KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > DatabaseField


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, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23
24 import java.io.*;
25 import oracle.toplink.essentials.internal.databaseaccess.*;
26
27 /**
28  * INTERNAL:
29  * <p><b>Purpose</b>:
30  * Define a fully qualified field name.<p>
31  * <b>Responsibilities</b>: <ul>
32  * <li> Know its name and its table.
33  * </ul>
34  * @see DatabaseTable
35  */

36 public class DatabaseField implements Cloneable JavaDoc, Serializable {
37     /** Variables used for generating DDL **/
38     protected int scale;
39     protected int length;
40     protected int precision;
41     protected boolean isUnique;
42     protected boolean isNullable;
43     protected boolean isUpdatable;
44     protected boolean isInsertable;
45     protected String JavaDoc columnDefinition;
46     
47     /** Column name of the field. */
48     protected String JavaDoc name;
49
50     /** Fields table (encapsulates name + creator). */
51     protected DatabaseTable table;
52
53     /** Respective Java type desired for the field's value, used to optimize performance and for binding. */
54     public transient Class JavaDoc type;
55     
56     /**
57      * Respective JDBC type of the field's value.
58      * This overrides the class type, which the JDBC type is normally computed from.
59      */

60     public int sqlType;
61
62     /** Store normal index of field in result set to optimize performance. */
63     protected int index;
64
65     public DatabaseField() {
66         this("", new DatabaseTable());
67     }
68
69     public DatabaseField(String JavaDoc qualifiedName) {
70         this.index = -1;
71         this.sqlType = -1;
72         int index = qualifiedName.lastIndexOf('.');
73
74         if (index == -1) {
75             this.name = qualifiedName;
76             this.table = new DatabaseTable();
77         } else {
78             this.name = qualifiedName.substring(index + 1, qualifiedName.length());
79             this.table = new DatabaseTable(qualifiedName.substring(0, index));
80         }
81         
82         initDDLFields();
83     }
84
85     public DatabaseField(String JavaDoc fieldName, String JavaDoc tableName) {
86         this(fieldName, new DatabaseTable(tableName));
87     }
88
89     public DatabaseField(String JavaDoc fieldName, DatabaseTable databaseTable) {
90         this.index = -1;
91         this.sqlType = -1;
92         this.name = fieldName;
93         this.table = databaseTable;
94         initDDLFields();
95     }
96     
97     /**
98      * Inits the DDL generation fields. Currently equivalent to the defaults
99      * from the EJB 3.0 spec.
100      */

101     public void initDDLFields() {
102         scale = 0;
103         length = 255;
104         precision = 0;
105         isUnique = false;
106         isNullable = true;
107         isUpdatable = true;
108         isInsertable = true;
109         columnDefinition = "";
110     }
111
112     /**
113      * The table is not cloned because it is treated as an automic value.
114      */

115     public Object JavaDoc clone() {
116         try {
117             return super.clone();
118         } catch (CloneNotSupportedException JavaDoc exception) {
119         }
120
121         return null;
122     }
123
124     /**
125      * Determine whether the receiver is equal to a DatabaseField.
126      * Return true if the receiver and field have the same name and table.
127      * Also return true if the table of the receiver or field are unspecfied,
128      * ie. have no name.
129      */

130     public boolean equals(DatabaseField field) {
131         if (this == field) {
132             return true;
133         }
134
135         if (field != null) {
136             if (DatabasePlatform.shouldIgnoreCaseOnFieldComparisons()) {
137                 if (getName().equalsIgnoreCase(field.getName())) {
138                     if ((getTableName().length() == 0) || (field.getTableName().length() == 0)) {
139                         return true;
140                     }
141                     return (getTable().equals(field.getTable()));
142                 }
143             } else {
144                 if (getName().equals(field.getName())) {
145                     if ((getTableName().length() == 0) || (field.getTableName().length() == 0)) {
146                         return true;
147                     }
148                     return (getTable().equals(field.getTable()));
149                 }
150             }
151         }
152
153         return false;
154     }
155     
156     /**
157      * Determine whether the receiver is equal to a DatabaseField.
158      * Return true if the receiver and field have the same name and table.
159      * Also return true if the table of the receiver or field are unspecfied,
160      * ie. have no name.
161      */

162     public boolean equals(Object JavaDoc object) {
163         if (!(object instanceof DatabaseField)) {
164             return false;
165         }
166
167         return equals((DatabaseField)object);
168     }
169     
170     /**
171      * Get the SQL fragment that is used when generating the DDL for the column.
172      */

173     public String JavaDoc getColumnDefinition() {
174         return this.columnDefinition;
175     }
176     
177     /**
178      * Return the expected index that this field will occur in the result set
179      * row. This is used to optimize performance of database row field lookups.
180      */

181     public int getIndex() {
182         return index;
183     }
184     
185     /**
186      * Used to specify the column length when generating DDL.
187      */

188     public int getLength() {
189         return this.length;
190     }
191     
192     /**
193      * Return the unqualified name of the field.
194      */

195     public String JavaDoc getName() {
196         return name;
197     }
198     
199     /**
200      * Returns the precision for a decimal column when generating DDL.
201      */

202     public int getPrecision() {
203         return this.precision;
204     }
205     
206     /**
207      * Return the qualified name of the field.
208      */

209     public String JavaDoc getQualifiedName() {
210         if (hasTableName()) {
211             return getTable().getQualifiedName() + "." + getName();
212         } else {
213             return getName();
214         }
215     }
216     
217     /**
218      * Returns the scale for a decimal column when generating DDL.
219      */

220     public int getScale() {
221         return this.scale;
222     }
223     
224     /**
225      * Return the JDBC type that coresponds to the field.
226      * The JDBC type is normally determined from the class type,
227      * but this allows it to be overriden for types that do not match directly
228      * to a Java type, such as MONEY or ARRAY, STRUCT, XMLTYPE, etc.
229      * This can be used for binding or stored procedure usage.
230      */

231     public int getSqlType() {
232         return sqlType;
233     }
234
235     /**
236      *
237      */

238     public DatabaseTable getTable() {
239         return table;
240     }
241
242     /**
243      *
244      */

245     public String JavaDoc getTableName() {
246         return getTable().getName();
247     }
248
249     /**
250      *
251      */

252     public Class JavaDoc getType() {
253         return type;
254     }
255     
256     /**
257      * Return the hashcode of the name, because it is fairly unqiue.
258      */

259     public int hashCode() {
260         return getName().hashCode();
261     }
262     
263     public boolean hasTableName() {
264         if (getTable() == null) {
265             return false;
266         }
267         if (getTable().getName() == null) {
268             return false;
269         }
270         return !(getTable().getName().equals(""));
271     }
272
273     /**
274      * Used to specify whether the column should be included in SQL UPDATE
275      * statements.
276      */

277     public boolean isInsertable() {
278         return this.isInsertable;
279     }
280     
281     /**
282      * Used for generatating DDL. Returns true if the database column is
283      * nullable.
284      */

285     public boolean isNullable() {
286         return this.isNullable;
287     }
288     
289     /**
290      * Used for generatating DDL. Returns true if the field is a unique key.
291      */

292     public boolean isUnique() {
293         return this.isUnique;
294     }
295     
296     /**
297      * Returns true is this database field should be read only.
298      */

299     public boolean isReadOnly() {
300         return (! isUpdatable && ! isInsertable);
301     }
302     
303     /**
304      * Returns whether the column should be included in SQL INSERT
305      * statements.
306      */

307     public boolean isUpdatable() {
308         return this.isUpdatable;
309     }
310     
311     /**
312      * Reset the field's name and table from the qualified name.
313      */

314     public void resetQualifiedName(String JavaDoc qualifiedName) {
315         setIndex(-1);
316         int index = qualifiedName.lastIndexOf('.');
317
318         if (index == -1) {
319             setName(qualifiedName);
320             getTable().setName("");
321             getTable().setTableQualifier("");
322         } else {
323             setName(qualifiedName.substring(index + 1, qualifiedName.length()));
324             getTable().setPossiblyQualifiedName(qualifiedName.substring(0, index));
325         }
326     }
327     
328     /**
329      * Set the SQL fragment that is used when generating the DDL for the column.
330      */

331     public void setColumnDefinition(String JavaDoc columnDefinition) {
332         this.columnDefinition = columnDefinition;
333     }
334     
335     /**
336      * Set the expected index that this field will occur in the result set row.
337      * This is used to optimize performance of database row field lookups.
338      */

339     public void setIndex(int index) {
340         this.index = index;
341     }
342     
343     /**
344      * Used to specify whether the column should be included in SQL UPDATE
345      * statements.
346      */

347     public void setInsertable(boolean isInsertable) {
348         this.isInsertable = isInsertable;
349     }
350     
351     /**
352      * Used to specify the column length when generating DDL.
353      */

354     public void setLength(int length) {
355         this.length = length;
356     }
357     
358     /**
359      * Set the unqualified name of the field.
360      */

361     public void setName(String JavaDoc name) {
362         this.name = name;
363     }
364     
365     /**
366      * Used for generatating DDL. Set to true if the database column is
367      * nullable.
368      */

369     public void setNullable(boolean isNullable) {
370         this.isNullable = isNullable;
371     }
372     
373     /**
374      * Used to specify the precision for a decimal column when generating DDL.
375      */

376     public void setPrecision(int precision) {
377         this.precision = precision;
378     }
379     
380     /**
381      * Used to specify the scale for a decimal column when generating DDL.
382      */

383     public void setScale(int scale) {
384         this.scale = scale;
385     }
386     
387     /**
388      * Set the JDBC type that coresponds to the field.
389      * The JDBC type is normally determined from the class type,
390      * but this allows it to be overriden for types that do not match directly
391      * to a Java type, such as MONEY or ARRAY, STRUCT, XMLTYPE, etc.
392      * This can be used for binding or stored procedure usage.
393      */

394     public void setSqlType(int sqlType) {
395         this.sqlType = sqlType;
396     }
397     
398     /**
399      * Set the table for the field.
400      */

401     public void setTable(DatabaseTable table) {
402         this.table = table;
403     }
404     
405     /**
406      * Set the table name for this field.
407      */

408     public void setTableName(String JavaDoc tableName) {
409         setTable(new DatabaseTable(tableName));
410     }
411
412     /**
413      * Set the Java class type that coresponds to the field.
414      * The JDBC type is determined from the class type,
415      * this is used to optimize performance, and for binding.
416      */

417     public void setType(Class JavaDoc type) {
418         this.type = type;
419     }
420
421     /**
422      * Used for generatating DDL. Set to true if the field is a unique key.
423      */

424     public void setUnique(boolean isUnique) {
425         this.isUnique = isUnique;
426     }
427     
428     /**
429      * Used to specify whether the column should be included in SQL INSERT
430      * statements.
431      */

432     public void setUpdatable(boolean isUpdatable) {
433         this.isUpdatable = isUpdatable;
434     }
435     
436     public String JavaDoc toString() {
437         return this.getQualifiedName();
438     }
439 }
440
Popular Tags