KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > protocol > Field


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): Nicolas Modrzyk, Marc Herbert.
22  */

23
24 package org.continuent.sequoia.common.protocol;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 import org.continuent.sequoia.common.stream.DriverBufferedInputStream;
30 import org.continuent.sequoia.common.stream.DriverBufferedOutputStream;
31 import org.continuent.sequoia.driver.ResultSetMetaData;
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.continuent.sequoia.driver.DriverResultSet
40  * @see org.continuent.sequoia.controller.backend.result.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 static final long serialVersionUID = 1050843622547803111L;
62
63   private String JavaDoc tableName;
64   private String JavaDoc fieldName;
65   private String JavaDoc fieldLabel;
66   private int columnDisplaySize;
67   private int sqlType;
68   private String JavaDoc typeName;
69   private String JavaDoc columnClassName;
70   private boolean isAutoIncrement;
71   private boolean isCaseSensitive;
72   private boolean isCurrency;
73   private int isNullable;
74   private boolean isReadOnly;
75   private boolean isWritable;
76   private boolean isDefinitelyWritable;
77   private boolean isSearchable;
78   private boolean isSigned;
79   private int precision;
80   private int scale;
81   private String JavaDoc encoding;
82
83   /**
84    * Create a new field with some default common values.
85    *
86    * @param table the table name
87    * @param columnName the field name
88    * @param columnDisplaySize the column display size
89    * @param sqlType the SQL type
90    * @param typeName the type name
91    * @param columnClassName the column class name
92    */

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

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

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

195   public void sendToStream(DriverBufferedOutputStream out) throws IOException JavaDoc
196   {
197     if (null == this.tableName)
198       out.writeBoolean(false);
199     else
200     {
201       out.writeBoolean(true);
202       out.writeLongUTF(this.tableName);
203     }
204
205     out.writeLongUTF(this.fieldName);
206     out.writeLongUTF(this.fieldLabel);
207     out.writeInt(this.columnDisplaySize);
208     out.writeInt(this.sqlType);
209     out.writeLongUTF(this.typeName);
210     out.writeLongUTF(this.columnClassName);
211     out.writeBoolean(this.isAutoIncrement);
212     out.writeBoolean(this.isCaseSensitive);
213     out.writeBoolean(this.isCurrency);
214     out.writeInt(this.isNullable);
215     out.writeBoolean(this.isReadOnly);
216     out.writeBoolean(this.isWritable);
217     out.writeBoolean(this.isDefinitelyWritable);
218     out.writeBoolean(this.isSearchable);
219     out.writeBoolean(this.isSigned);
220     out.writeInt(this.precision);
221     out.writeInt(this.scale);
222     out.writeLongUTF(this.encoding);
223
224   }
225
226   /**
227    * Returns the fieldLabel value.
228    *
229    * @return Returns the fieldLabel.
230    */

231   public final String JavaDoc getFieldLabel()
232   {
233     return fieldLabel;
234   }
235
236   /**
237    * Gets the field name.
238    *
239    * @return a <code>String</code> value
240    */

241   public String JavaDoc getFieldName()
242   {
243     return fieldName;
244   }
245
246   /**
247    * Gets the full name: "tableName.fieldName"
248    *
249    * @return a <code>String</code> value
250    */

251   public String JavaDoc getFullName()
252   {
253     return tableName + "." + fieldName;
254   }
255
256   /**
257    * @see java.sql.ResultSetMetaData#getPrecision(int)
258    */

259   public int getPrecision()
260   {
261     return precision;
262   }
263
264   /**
265    * @see java.sql.ResultSetMetaData#getScale(int)
266    */

267   public int getScale()
268   {
269     return scale;
270   }
271
272   /**
273    * Returns the JDBC type code.
274    *
275    * @return int Type according to {@link java.sql.Types}
276    * @see java.sql.ResultSetMetaData#getColumnType(int)
277    */

278   public int getSqlType()
279   {
280     return sqlType;
281   }
282
283   /**
284    * Gets the table name.
285    *
286    * @return a <code>String</code> value
287    */

288   public String JavaDoc getTableName()
289   {
290     return tableName;
291   }
292
293   /**
294    * Returns the SQL type name used by the database.
295    *
296    * @return the SQL type name
297    * @see java.sql.ResultSetMetaData#getColumnTypeName(int)
298    */

299   public String JavaDoc getTypeName()
300   {
301     return typeName;
302   }
303
304   /**
305    * Returns the Java class used by the mapping.
306    *
307    * @see java.sql.ResultSetMetaData#getColumnClassName(int)
308    */

309   public String JavaDoc getColumnClassName()
310   {
311     return columnClassName;
312   }
313
314   /**
315    * @see java.sql.ResultSetMetaData#getColumnDisplaySize(int)
316    */

317   public int getColumnDisplaySize()
318   {
319     return columnDisplaySize;
320   }
321
322   /**
323    * @return the encoding used for this field
324    */

325   public String JavaDoc getEncoding()
326   {
327     return encoding;
328   }
329
330   /**
331    * @see java.sql.ResultSetMetaData#isAutoIncrement(int)
332    */

333   public boolean isAutoIncrement()
334   {
335     return isAutoIncrement;
336   }
337
338   /**
339    * @see java.sql.ResultSetMetaData#isCaseSensitive(int)
340    */

341   public boolean isCaseSensitive()
342   {
343     return isCaseSensitive;
344   }
345
346   /**
347    * @see java.sql.ResultSetMetaData#isCurrency(int)
348    */

349   public boolean isCurrency()
350   {
351     return isCurrency;
352   }
353
354   /**
355    * @see java.sql.ResultSetMetaData#isDefinitelyWritable(int)
356    */

357   public boolean isDefinitelyWritable()
358   {
359     return isDefinitelyWritable;
360   }
361
362   /**
363    * @see java.sql.ResultSetMetaData#isNullable(int)
364    */

365   public int isNullable()
366   {
367     return isNullable;
368   }
369
370   /**
371    * @see java.sql.ResultSetMetaData#isReadOnly(int)
372    */

373   public boolean isReadOnly()
374   {
375     return isReadOnly;
376   }
377
378   /**
379    * @see java.sql.ResultSetMetaData#isSearchable(int)
380    */

381   public boolean isSearchable()
382   {
383     return isSearchable;
384   }
385
386   /**
387    * @see java.sql.ResultSetMetaData#isSigned(int)
388    */

389   public boolean isSigned()
390   {
391     return isSigned;
392   }
393
394   /**
395    * @see java.sql.ResultSetMetaData#isWritable(int)
396    */

397   public boolean isWritable()
398   {
399     return isWritable;
400   }
401
402   /**
403    * Returns the full name.
404    *
405    * @return <code>String</code> value
406    * @see #getFullName()
407    */

408   public String JavaDoc toString()
409   {
410     return getFullName();
411   }
412
413 }
Popular Tags