KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > EmbedResultSetMetaData


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.EmbedResultSetMetaData
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. 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  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import org.apache.derby.iapi.sql.ResultDescription;
25 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
26 import org.apache.derby.iapi.types.DataTypeDescriptor;
27 import org.apache.derby.iapi.types.DataTypeUtilities;
28 import org.apache.derby.iapi.types.TypeId;
29
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31
32 import org.apache.derby.iapi.reference.SQLState;
33
34 import java.sql.ResultSetMetaData JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Types JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38
39 /**
40  * A ResultSetMetaData object can be used to find out about the types
41  * and properties of the columns in a ResultSet.
42  *
43  * <p>
44  * We take the (cloudscape) ResultDescription and examine it, to return
45  * the appropriate information.
46
47    <P>
48    This class can be used outside of this package to convert a
49    ResultDescription into a ResultSetMetaData object.
50  *
51  * @author ames
52  */

53 public class EmbedResultSetMetaData
54     implements ResultSetMetaData JavaDoc {
55
56     private final ResultColumnDescriptor[] columnInfo;
57
58     //
59
// constructor
60
//
61
public EmbedResultSetMetaData(ResultColumnDescriptor[] columnInfo) {
62         this.columnInfo = columnInfo;
63     }
64
65     //
66
// ResultSetMetaData interface
67
//
68

69     /**
70      * What's the number of columns in the ResultSet?
71      *
72      * @return the number
73      */

74     public int getColumnCount() {
75         return columnInfo == null ? 0 : columnInfo.length;
76     }
77
78     /**
79      * Is the column automatically numbered, thus read-only?
80      *
81      * @param column the first column is 1, the second is 2, ...
82      * @return true if so
83      * @exception SQLException thrown on failure
84      *
85      */

86     public boolean isAutoIncrement(int column) throws SQLException JavaDoc {
87
88         ResultColumnDescriptor rcd = columnInfo[column - 1];
89         return rcd.isAutoincrement();
90     }
91
92     /**
93      * Does a column's case matter?
94      *
95      * @param column the first column is 1, the second is 2, ...
96      * @return true if so
97      * @exception SQLException thrown on failure
98      */

99     public boolean isCaseSensitive(int column) throws SQLException JavaDoc {
100       return DataTypeUtilities.isCaseSensitive(getColumnTypeDescriptor(column));
101     }
102
103
104     /**
105      * Can the column be used in a where clause?
106      *
107      * @param column the first column is 1, the second is 2, ...
108      * @return true if so
109      * @exception SQLException thrown on failure
110      */

111     public boolean isSearchable(int column) throws SQLException JavaDoc {
112         validColumnNumber(column);
113
114         // we have no restrictions yet, so this is always true
115
// might eventually be false for e.g. extra-long columns?
116
return true;
117     }
118
119     /**
120      * Is the column a cash value?
121      *
122      * @param column the first column is 1, the second is 2, ...
123      * @return true if so
124      * @exception SQLException thrown on failure
125      */

126     public boolean isCurrency(int column) throws SQLException JavaDoc {
127
128         return DataTypeUtilities.isCurrency(getColumnTypeDescriptor(column));
129     }
130
131     /**
132      * Can you put a NULL in this column?
133      *
134      * @param column the first column is 1, the second is 2, ...
135      * @return columnNoNulls, columnNullable or columnNullableUnknown
136      * @exception SQLException thrown on failure
137      */

138     public int isNullable(int column) throws SQLException JavaDoc {
139         return DataTypeUtilities.isNullable(getColumnTypeDescriptor(column));
140     }
141
142     /**
143      * Is the column a signed number?
144      *
145      * @param column the first column is 1, the second is 2, ...
146      * @return true if so
147      * @exception SQLException thrown on failure
148      */

149     public boolean isSigned(int column) throws SQLException JavaDoc {
150         return DataTypeUtilities.isSigned(getColumnTypeDescriptor(column));
151     }
152
153
154     /**
155      * What's the column's normal max width in chars?
156      *
157      * @param column the first column is 1, the second is 2, ...
158      * @return max width
159      * @exception SQLException thrown on failure
160      */

161     public int getColumnDisplaySize(int column) throws SQLException JavaDoc {
162         return DataTypeUtilities.getColumnDisplaySize(getColumnTypeDescriptor(column));
163     }
164
165     /**
166      * What's the suggested column title for use in printouts and
167      * displays?
168      *
169      * @param column the first column is 1, the second is 2, ...
170      * @return true if so
171      * @exception SQLException thrown on failure
172      */

173     public String JavaDoc getColumnLabel(int column) throws SQLException JavaDoc {
174         ResultColumnDescriptor cd = columnInfo[column - 1];
175         String JavaDoc s = cd.getName();
176
177         // we could get fancier than this, but it's simple
178
return (s==null? "Column"+Integer.toString(column) : s);
179     }
180
181
182     /**
183      * What's a column's name?
184      *
185      * @param column the first column is 1, the second is 2, ...
186      * @return column name
187      * @exception SQLException thrown on failure
188      */

189     public String JavaDoc getColumnName(int column) throws SQLException JavaDoc {
190         ResultColumnDescriptor cd = columnInfo[column - 1];
191         String JavaDoc s = cd.getName();
192         // database returns null when no column name to differentiate from empty name
193
return (s==null? "" : s);
194
195     }
196
197
198     /**
199      * What's a column's table's schema?
200      *
201      * @param column the first column is 1, the second is 2, ...
202      * @return schema name or "" if not applicable
203      * @exception SQLException thrown on failure
204      */

205     public String JavaDoc getSchemaName(int column) throws SQLException JavaDoc {
206         ResultColumnDescriptor cd = columnInfo[column - 1];
207
208         String JavaDoc s = cd.getSourceSchemaName();
209         // database returns null when no schema name to differentiate from empty name
210
return (s==null? "" : s);
211     }
212
213     /**
214      * What's a column's number of decimal digits?
215      *
216      * @param column the first column is 1, the second is 2, ...
217      * @return precision
218      * @exception SQLException thrown on failure
219      */

220     public int getPrecision(int column) throws SQLException JavaDoc {
221         return DataTypeUtilities.getDigitPrecision(getColumnTypeDescriptor(column));
222     }
223
224
225     /**
226      * What's a column's number of digits to right of the decimal point?
227      *
228      * @param column the first column is 1, the second is 2, ...
229      * @return scale
230      * @exception SQLException thrown on failure
231      */

232     public int getScale(int column) throws SQLException JavaDoc {
233         DataTypeDescriptor dtd = getColumnTypeDescriptor(column);
234         // REMIND -- check it is valid to ask for scale
235
return dtd.getScale();
236     }
237
238     /**
239      * What's a column's table name?
240      *
241      * @return table name or "" if not applicable
242      * @exception SQLException thrown on failure
243      */

244     public String JavaDoc getTableName(int column) throws SQLException JavaDoc {
245         ResultColumnDescriptor cd = columnInfo[column - 1];
246         String JavaDoc s = cd.getSourceTableName();
247
248         // database returns null when no table name to differentiate from empty name
249
return (s==null? "" : s);
250     }
251
252     /**
253      * What's a column's table's catalog name?
254      *
255      * @param column the first column is 1, the second is 2, ...
256      * @return column name or "" if not applicable.
257      * @exception SQLException thrown on failure
258      */

259     public String JavaDoc getCatalogName(int column) throws SQLException JavaDoc {
260         validColumnNumber(column);
261         return "";
262     }
263
264     /**
265      * What's a column's SQL type?
266      *
267      * @param column the first column is 1, the second is 2, ...
268      * @return SQL type
269      * @see Types
270      * @exception SQLException thrown on failure
271      */

272     public int getColumnType(int column) throws SQLException JavaDoc {
273         DataTypeDescriptor dtd = getColumnTypeDescriptor(column);
274         return dtd.getTypeId().getJDBCTypeId();
275     }
276
277     /**
278      * What's a column's data source specific type name?
279      *
280      * @param column the first column is 1, the second is 2, ...
281      * @return type name
282      * @exception SQLException thrown on failure
283      */

284     public String JavaDoc getColumnTypeName(int column) throws SQLException JavaDoc {
285         DataTypeDescriptor dtd = getColumnTypeDescriptor(column);
286         return dtd.getTypeId().getSQLTypeName();
287     }
288
289     /**
290      * Is a column definitely not writable?
291      *
292      * @param column the first column is 1, the second is 2, ...
293      * @return true if so
294      * @exception SQLException thrown on failure
295      */

296     public boolean isReadOnly(int column) throws SQLException JavaDoc {
297         validColumnNumber(column);
298
299         // we just don't know if it is a base table column or not
300
return false;
301     }
302
303     /**
304      * Is it possible for a write on the column to succeed?
305      *
306      * @param column the first column is 1, the second is 2, ...
307      * @return true if so
308      * @exception SQLException thrown on failure
309      */

310     public boolean isWritable(int column) throws SQLException JavaDoc {
311         validColumnNumber(column);
312         return columnInfo[column - 1].updatableByCursor();
313     }
314
315     /**
316      * Will a write on the column definitely succeed?
317      *
318      * @param column the first column is 1, the second is 2, ...
319      * @return true if so
320      * @exception SQLException thrown on failure
321      */

322     public boolean isDefinitelyWritable(int column) throws SQLException JavaDoc {
323         validColumnNumber(column);
324
325         // we just don't know if it is a base table column or not
326
return false;
327     }
328
329     /*
330      * class interface
331      */

332
333     private void validColumnNumber(int column) throws SQLException JavaDoc {
334       if (column < 1 ||
335                 column > getColumnCount() )
336                 throw Util.generateCsSQLException(
337                       SQLState.COLUMN_NOT_FOUND, new Integer JavaDoc(column));
338     }
339
340     public DataTypeDescriptor getColumnTypeDescriptor(int column) throws SQLException JavaDoc
341     {
342         validColumnNumber(column);
343
344         ResultColumnDescriptor cd = columnInfo[column - 1];
345
346         return cd.getType();
347     }
348
349     /////////////////////////////////////////////////////////////////////////
350
//
351
// JDBC 2.0 - New public methods
352
//
353
/////////////////////////////////////////////////////////////////////////
354

355     /**
356      * JDBC 2.0
357      *
358      * <p>Return the fully qualified name of the Java class whose instances
359      * are manufactured if ResultSet.getObject() is called to retrieve a value
360      * from the column. ResultSet.getObject() may return a subClass of the
361      * class returned by this method.
362      *
363      * @exception SQLException Feature not inplemented for now.
364      */

365     public String JavaDoc getColumnClassName(int column) throws SQLException JavaDoc {
366         
367         return getColumnTypeDescriptor(column).getTypeId().getResultSetMetaDataTypeName();
368     }
369
370
371     public static ResultColumnDescriptor getResultColumnDescriptor(String JavaDoc name, int jdcbTypeId, boolean nullable) {
372
373         return new org.apache.derby.impl.sql.GenericColumnDescriptor(
374             name, DataTypeDescriptor.getBuiltInDataTypeDescriptor(jdcbTypeId, nullable));
375     }
376     public static ResultColumnDescriptor getResultColumnDescriptor(String JavaDoc name, int jdcbTypeId, boolean nullable, int length) {
377
378         return new org.apache.derby.impl.sql.GenericColumnDescriptor(
379             name, DataTypeDescriptor.getBuiltInDataTypeDescriptor(jdcbTypeId, nullable, length));
380     }
381     public static ResultColumnDescriptor getResultColumnDescriptor(String JavaDoc name, DataTypeDescriptor dtd) {
382         return new org.apache.derby.impl.sql.GenericColumnDescriptor(name, dtd);
383     }
384 }
385
Popular Tags