KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > Field


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Nicolas Modrzyk, Marc Herbert.
23  */

24
25 package org.objectweb.cjdbc.driver;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 import org.objectweb.cjdbc.common.stream.CJDBCInputStream;
31 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream;
32
33 /**
34  * Field is our private implementation of <code>ResultSetMetaData</code>,
35  * holding the information for one column.
36  * <p>
37  * The first version was inspired from the MM MySQL driver by Mark Matthews.
38  *
39  * @see org.objectweb.cjdbc.driver.DriverResultSet
40  * @see org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
43  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert </a>
44  * @version 1.0
45  */

46 public class Field implements Serializable JavaDoc
47 {
48   //
49
// This object is manually (de-)serialized below for compatibility with C.
50
// It also implements Serializable for the convenience of Java-Java
51
// communication (typically between controllers).
52
//
53
// Ideally:
54
// (1) unneeded fields for Java-Java communication are all tagged as
55
// "transient"
56
// (2) C-Java and Java-Java need to send the exact same fields.
57
// And so:
58
// (3) keeping up-to-date manual serialization below is easy: just check
59
// "transient" tags.
60

61   private String JavaDoc tableName;
62   private String JavaDoc fieldName;
63   private int columnDisplaySize;
64   private int sqlType;
65   private String JavaDoc typeName;
66   private String JavaDoc columnClassName;
67   private boolean isAutoIncrement;
68   private boolean isCaseSensitive;
69   private boolean isCurrency;
70   private int isNullable;
71   private boolean isReadOnly;
72   private boolean isWritable;
73   private boolean isDefinitelyWritable;
74   private boolean isSearchable;
75   private boolean isSigned;
76   private int precision;
77   private int scale;
78
79   /**
80    * Create a new field with some default common values.
81    *
82    * @param table the table name
83    * @param name the field name
84    * @param columnDisplaySize the column display size
85    * @param sqlType the SQL type
86    * @param typeName the type name
87    * @param columnClassName the column class name
88    */

89   public Field(String JavaDoc table, String JavaDoc name, int columnDisplaySize, int sqlType,
90       String JavaDoc typeName, String JavaDoc columnClassName)
91   {
92     this(table, name, columnDisplaySize, sqlType, typeName, columnClassName,
93         false, true, false, ResultSetMetaData.columnNullable, true, false,
94         false, false, false, 0, 0);
95   }
96
97   /**
98    * Creates a new <code>Field</code> instance.
99    *
100    * @param table the table name
101    * @param name the field name
102    * @param columnDisplaySize the column display size
103    * @param sqlType the SQL type
104    * @param typeName the type name
105    * @param columnClassName the column class name
106    * @param isAutoIncrement true if field is auto incremented
107    * @param isCaseSensitive true if field is case sensitive
108    * @param isCurrency true if field is currency
109    * @param isNullable indicates the nullability of the field
110    * @param isReadOnly true if field is read only
111    * @param isWritable true if field is writable
112    * @param isDefinitelyWritable true if field is definetly writable
113    * @param isSearchable true if field is searchable
114    * @param isSigned true if field is signed
115    * @param precision decimal precision
116    * @param scale number of digits to right of decimal point
117    */

118   public Field(String JavaDoc table, String JavaDoc name, int columnDisplaySize, int sqlType,
119       String JavaDoc typeName, String JavaDoc columnClassName, boolean isAutoIncrement,
120       boolean isCaseSensitive, boolean isCurrency, int isNullable,
121       boolean isReadOnly, boolean isWritable, boolean isDefinitelyWritable,
122       boolean isSearchable, boolean isSigned, int precision, int scale)
123   {
124     if (table == null)
125       this.tableName = null;
126     else
127       this.tableName = new String JavaDoc(table);
128     this.fieldName = new String JavaDoc(name);
129     this.columnDisplaySize = columnDisplaySize;
130     this.sqlType = sqlType;
131     this.typeName = typeName;
132     this.columnClassName = columnClassName;
133     this.isAutoIncrement = isAutoIncrement;
134     this.isCaseSensitive = isCaseSensitive;
135     this.isCurrency = isCurrency;
136     this.isNullable = isNullable;
137     this.isReadOnly = isReadOnly;
138     this.isWritable = isWritable;
139     this.isDefinitelyWritable = isDefinitelyWritable;
140     this.isSearchable = isSearchable;
141     this.isSigned = isSigned;
142     this.precision = precision;
143     this.scale = scale;
144   }
145
146   /**
147    * Creates a new <code>Field</code> object, deserializing it from an input
148    * stream. Has to mirror the serialization method below.
149    *
150    * @param in input stream
151    * @throws IOException if a stream error occurs
152    */

153   public Field(CJDBCInputStream in) throws IOException JavaDoc
154   {
155     if (in.readBoolean())
156       this.tableName = in.readUTF();
157     else
158       this.tableName = null;
159
160     this.fieldName = in.readUTF();
161     this.columnDisplaySize = in.readInt();
162     this.sqlType = in.readInt();
163     this.typeName = in.readUTF();
164     this.columnClassName = in.readUTF();
165     this.isAutoIncrement = in.readBoolean();
166     this.isCaseSensitive = in.readBoolean();
167     this.isCurrency = in.readBoolean();
168     this.isNullable = in.readInt();
169     this.isReadOnly = in.readBoolean();
170     this.isWritable = in.readBoolean();
171     this.isDefinitelyWritable = in.readBoolean();
172     this.isSearchable = in.readBoolean();
173     this.isSigned = in.readBoolean();
174     this.precision = in.readInt();
175     this.scale = in.readInt();
176   }
177
178   /**
179    * Serialize the <code>Field</code> on the output stream by sending only the
180    * needed parameters to reconstruct it on the controller. Has to mirror the
181    * deserialization method above.
182    *
183    * @param out destination stream
184    * @throws IOException if a stream error occurs
185    */

186   public void sendToStream(CJDBCOutputStream out) throws IOException JavaDoc
187   {
188     if (null == this.tableName)
189       out.writeBoolean(false);
190     else
191     {
192       out.writeBoolean(true);
193       out.writeUTF(this.tableName);
194     }
195
196     out.writeUTF(this.fieldName);
197     out.writeInt(this.columnDisplaySize);
198     out.writeInt(this.sqlType);
199     out.writeUTF(this.typeName);
200     out.writeUTF(this.columnClassName);
201     out.writeBoolean(this.isAutoIncrement);
202     out.writeBoolean(this.isCaseSensitive);
203     out.writeBoolean(this.isCurrency);
204     out.writeInt(this.isNullable);
205     out.writeBoolean(this.isReadOnly);
206     out.writeBoolean(this.isWritable);
207     out.writeBoolean(this.isDefinitelyWritable);
208     out.writeBoolean(this.isSearchable);
209     out.writeBoolean(this.isSigned);
210     out.writeInt(this.precision);
211     out.writeInt(this.scale);
212
213   }
214
215   /**
216    * Gets the table name.
217    *
218    * @return a <code>String</code> value
219    */

220   public String JavaDoc getTableName()
221   {
222     return tableName;
223   }
224
225   /**
226    * Gets the field name.
227    *
228    * @return a <code>String</code> value
229    * @see #setFieldName
230    */

231   public String JavaDoc getFieldName()
232   {
233     return fieldName;
234   }
235
236   /**
237    * Gets the full name: "tableName.fieldName"
238    *
239    * @return a <code>String</code> value
240    */

241   public String JavaDoc getFullName()
242   {
243     return tableName + "." + fieldName;
244   }
245
246   /**
247    * Sets the field name.
248    *
249    * @param name the new field name
250    * @see #getFieldName
251    */

252   public void setFieldName(String JavaDoc name)
253   {
254     fieldName = name;
255   }
256
257   /**
258    * Returns the full name.
259    *
260    * @return <code>String</code> value
261    * @see #getFullName()
262    */

263   public String JavaDoc toString()
264   {
265     return getFullName();
266   }
267
268   /**
269    * Returns the JDBC type code.
270    *
271    * @return int Type according to {@link java.sql.Types}
272    * @see java.sql.ResultSetMetaData#getColumnType(int)
273    */

274   public int getSqlType()
275   {
276     return sqlType;
277   }
278
279   /**
280    * Returns the SQL type name used by the database.
281    *
282    * @return the SQL type name
283    * @see java.sql.ResultSetMetaData#getColumnTypeName(int)
284    */

285   public String JavaDoc getTypeName()
286   {
287     return typeName;
288   }
289
290   /**
291    * Returns the Java class used by the mapping.
292    *
293    * @see java.sql.ResultSetMetaData#getColumnClassName(int)
294    */

295   public String JavaDoc getColumnClassName()
296   {
297     return columnClassName;
298   }
299
300   /**
301    * @see java.sql.ResultSetMetaData#getColumnDisplaySize(int)
302    */

303   public int getColumnDisplaySize()
304   {
305     return columnDisplaySize;
306   }
307
308   /**
309    * @see java.sql.ResultSetMetaData#isAutoIncrement(int)
310    */

311   public boolean isAutoIncrement()
312   {
313     return isAutoIncrement;
314   }
315
316   /**
317    * @see java.sql.ResultSetMetaData#isCaseSensitive(int)
318    */

319   public boolean isCaseSensitive()
320   {
321     return isCaseSensitive;
322   }
323
324   /**
325    * @see java.sql.ResultSetMetaData#isCurrency(int)
326    */

327   public boolean isCurrency()
328   {
329     return isCurrency;
330   }
331
332   /**
333    * @see java.sql.ResultSetMetaData#isDefinitelyWritable(int)
334    */

335   public boolean isDefinitelyWritable()
336   {
337     return isDefinitelyWritable;
338   }
339
340   /**
341    * @see java.sql.ResultSetMetaData#isNullable(int)
342    */

343   public int isNullable()
344   {
345     return isNullable;
346   }
347
348   /**
349    * @see java.sql.ResultSetMetaData#isReadOnly(int)
350    */

351   public boolean isReadOnly()
352   {
353     return isReadOnly;
354   }
355
356   /**
357    * @see java.sql.ResultSetMetaData#isWritable(int)
358    */

359   public boolean isWritable()
360   {
361     return isWritable;
362   }
363
364   /**
365    * @see java.sql.ResultSetMetaData#isSearchable(int)
366    */

367   public boolean isSearchable()
368   {
369     return isSearchable;
370   }
371
372   /**
373    * @see java.sql.ResultSetMetaData#isSigned(int)
374    */

375   public boolean isSigned()
376   {
377     return isSigned;
378   }
379
380   /**
381    * @see java.sql.ResultSetMetaData#getPrecision(int)
382    */

383   public int getPrecision()
384   {
385     return precision;
386   }
387
388   /**
389    * @see java.sql.ResultSetMetaData#getScale(int)
390    */

391   public int getScale()
392   {
393     return scale;
394   }
395
396 }
Popular Tags