KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > MResultSetMetaData


1 /**
2  * com.mckoi.database.jdbc.MResultSetMetaData 23 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import com.mckoi.database.global.ColumnDescription;
28 import com.mckoi.database.global.SQLTypes;
29 import java.sql.*;
30 import java.math.BigDecimal JavaDoc;
31
32 /**
33  * An implementation of JDBC's ResultSetmetaData.
34  *
35  * @author Tobias Downer
36  */

37
38 public class MResultSetMetaData implements ResultSetMetaData {
39
40   /**
41    * The parent MResultSet object.
42    */

43   private MResultSet result_set;
44
45   /**
46    * Constructs the ResultSetMetaData over the given result set.
47    */

48   MResultSetMetaData(MResultSet result_set) {
49     this.result_set = result_set;
50   }
51
52   /**
53    * Returns the object class that a given sql_type will produce by the
54    * 'getObject' call in ResultSet.
55    */

56   private static Class JavaDoc jdbcSQLClass(int sql_type) {
57     switch (sql_type) {
58       case(SQLTypes.BIT):
59         return Boolean JavaDoc.class;
60       case(SQLTypes.TINYINT):
61         return Byte JavaDoc.class;
62       case(SQLTypes.SMALLINT):
63         return Short JavaDoc.class;
64       case(SQLTypes.INTEGER):
65         return Integer JavaDoc.class;
66       case(SQLTypes.BIGINT):
67         return Long JavaDoc.class;
68       case(SQLTypes.FLOAT):
69         return Double JavaDoc.class;
70       case(SQLTypes.REAL):
71         return Float JavaDoc.class;
72       case(SQLTypes.DOUBLE):
73         return Double JavaDoc.class;
74       case(SQLTypes.NUMERIC):
75         return BigDecimal JavaDoc.class;
76       case(SQLTypes.DECIMAL):
77         return BigDecimal JavaDoc.class;
78       case(SQLTypes.CHAR):
79         return String JavaDoc.class;
80       case(SQLTypes.VARCHAR):
81         return String JavaDoc.class;
82       case(SQLTypes.LONGVARCHAR):
83         return String JavaDoc.class;
84       case(SQLTypes.DATE):
85         return java.sql.Date JavaDoc.class;
86       case(SQLTypes.TIME):
87         return java.sql.Time JavaDoc.class;
88       case(SQLTypes.TIMESTAMP):
89         return java.sql.Timestamp JavaDoc.class;
90       case(SQLTypes.BINARY):
91         return byte[].class;
92       case(SQLTypes.VARBINARY):
93         return byte[].class;
94       case(SQLTypes.LONGVARBINARY):
95         return byte[].class;
96       case(SQLTypes.NULL):
97         return Object JavaDoc.class;
98       case(SQLTypes.OTHER):
99         return Object JavaDoc.class;
100       case(SQLTypes.JAVA_OBJECT):
101         return Object JavaDoc.class;
102       case(SQLTypes.DISTINCT):
103         // (Not supported)
104
return Object JavaDoc.class;
105       case(SQLTypes.STRUCT):
106         // (Not supported)
107
return Object JavaDoc.class;
108       case(SQLTypes.ARRAY):
109         // (Not supported)
110
return Object JavaDoc.class;
111 //#IFDEF(JDBC2.0)
112
case(SQLTypes.BLOB):
113         return java.sql.Blob JavaDoc.class;
114       case(SQLTypes.CLOB):
115         return java.sql.Clob JavaDoc.class;
116       case(SQLTypes.REF):
117         // (Not supported)
118
return Object JavaDoc.class;
119 //#ENDIF
120
default:
121         return Object JavaDoc.class;
122     }
123   }
124   
125   // ---------- Implemented from ResultSetMetaData ----------
126

127   public int getColumnCount() throws SQLException {
128     return result_set.columnCount();
129   }
130
131   public boolean isAutoIncrement(int column) throws SQLException {
132     // There are no hard-coded auto increment columns but you can make one
133
// with the UNIQUEKEY function.
134
return false;
135   }
136
137   public boolean isCaseSensitive(int column) throws SQLException {
138     return true;
139   }
140
141   public boolean isSearchable(int column) throws SQLException {
142     return result_set.getColumn(column -1).isQuantifiable();
143   }
144
145   public boolean isCurrency(int column) throws SQLException {
146     // Currency not supported by the driver or the database.
147
return false;
148   }
149
150   public int isNullable(int column) throws SQLException {
151     if (result_set.getColumn(column - 1).isNotNull()) {
152       return columnNoNulls;
153     }
154     else {
155       return columnNullable;
156     }
157   }
158
159   public boolean isSigned(int column) throws SQLException {
160     // There are no unsigned numbers....
161
if (result_set.getColumn(column - 1).isNumericType()) {
162       return true;
163     }
164     else {
165       // All other types aren't signed (strings, dates, etc)
166
return false;
167     }
168   }
169
170   public int getColumnDisplaySize(int column) throws SQLException {
171     // How can we implement this when strings and numbers
172
// can be any length?
173
return 64;
174   }
175
176   public String JavaDoc getColumnLabel(int column) throws SQLException {
177     // ISSUE: Should this process be cached? Could be a problem if this
178
// method is used in an inner loop. (A string object is created)
179
String JavaDoc encoded_name = result_set.getColumn(column - 1).getName();
180     if (encoded_name.startsWith("@a")) {
181       // Strip any control characters and return
182
return encoded_name.substring(2);
183     }
184     else if (encoded_name.startsWith("@f")) {
185       // Return only the column name, not the schema.table part
186
int p = encoded_name.lastIndexOf(".");
187       if (p > -1) {
188         return encoded_name.substring(p + 1);
189       }
190       else {
191         return encoded_name.substring(2);
192       }
193     }
194     // No encoding (must be an older version of the database engine).
195
return encoded_name;
196   }
197
198   public String JavaDoc getColumnName(int column) throws SQLException {
199     // If the JDBC driver is set to succinct column names (the default) then
200
// return what 'getColumnLabel' tells us.
201
if (!result_set.verboseColumnNames()) {
202       return getColumnLabel(column);
203     }
204     else {
205       // ISSUE: Should this process be cached? Could be a problem if this
206
// method is used in an inner loop. (A string object is created)
207
String JavaDoc encoded_name = result_set.getColumn(column - 1).getName();
208       if (encoded_name.startsWith("@")) {
209         // Strip any control characters and return
210
return encoded_name.substring(2);
211       }
212       // No encoding (must be an older version of the database engine).
213
return encoded_name;
214     }
215   }
216
217   public String JavaDoc getSchemaName(int column) throws SQLException {
218     ColumnDescription col = result_set.getColumn(column - 1);
219     String JavaDoc name = col.getName();
220
221     // Do we have a column code. If not default to 'f'
222
char col_code = 'f';
223     int name_start = 0;
224     if (name.startsWith("@")) {
225       col_code = name.charAt(1);
226       name_start = 2;
227     }
228
229     if (col_code == 'a') {
230       // This is an alias so there is no table name
231
return "";
232     }
233     else if (col_code == 'f') {
234       // Assume it is [schema_name].[table_name].[column_name]
235
int delim = name.lastIndexOf(".");
236       if (delim == -1) {
237         return "";
238       }
239       else {
240         delim = name.lastIndexOf(".", delim - 1);
241         if (delim == -1) {
242           return "";
243         }
244         else {
245           int end_point = delim;
246           delim = name.lastIndexOf(".", delim - 1);
247           if (delim == -1) {
248             return name.substring(name_start, end_point);
249           }
250           else {
251             return name.substring(delim + 1, end_point);
252           }
253         }
254       }
255     }
256     else {
257       throw new SQLException("Unknown column code: '" + col_code + "'");
258     }
259   }
260
261   public int getPrecision(int column) throws SQLException {
262     // HACK: Precision is not a property we define for columns yet....
263
// For *CHAR columns, we make this return the max size of the string
264
int size = result_set.getColumn(column - 1).getSize();
265     if (size == -1) {
266       size = 32;
267     }
268     return size;
269   }
270
271   public int getScale(int column) throws SQLException {
272     int scale = result_set.getColumn(column - 1).getScale();
273     if (scale == -1) {
274       scale = 0;
275     }
276     return scale;
277   }
278
279   public String JavaDoc getTableName(int column) throws SQLException {
280     ColumnDescription col = result_set.getColumn(column - 1);
281     String JavaDoc name = col.getName();
282
283     // Do we have a column code. If not default to 'f'
284
char col_code = 'f';
285     int name_start = 0;
286     if (name.startsWith("@")) {
287       col_code = name.charAt(1);
288       name_start = 2;
289     }
290
291     if (col_code == 'a') {
292       // This is an alias so there is no table name
293
return "";
294     }
295     else if (col_code == 'f') {
296       // Assume it is [schema_name].[table_name].[column_name]
297
int delim = name.lastIndexOf(".");
298       if (delim == -1) {
299         return "";
300       }
301       else {
302         int end_point = delim;
303         delim = name.lastIndexOf(".", end_point - 1);
304         if (delim == -1) {
305           return name.substring(name_start, end_point);
306         }
307         else {
308           return name.substring(delim + 1, end_point);
309         }
310       }
311     }
312     else {
313       throw new SQLException("Unknown column code: '" + col_code + "'");
314     }
315   }
316
317   public String JavaDoc getCatalogName(int column) throws SQLException {
318     // No support for catalogs
319
return "";
320   }
321
322   public int getColumnType(int column) throws SQLException {
323     return result_set.getColumn(column - 1).getSQLType();
324   }
325
326   public String JavaDoc getColumnTypeName(int column) throws SQLException {
327     return result_set.getColumn(column - 1).getSQLTypeName();
328   }
329
330   public boolean isReadOnly(int column) throws SQLException {
331     return false;
332   }
333
334   public boolean isWritable(int column) throws SQLException {
335     return true;
336   }
337
338   public boolean isDefinitelyWritable(int column) throws SQLException {
339     return false;
340   }
341
342 //#IFDEF(JDBC2.0)
343

344     //--------------------------JDBC 2.0-----------------------------------
345

346   public String JavaDoc getColumnClassName(int column) throws SQLException {
347     // PENDING: This should return the instance class name set as the
348
// constraint for a JAVA_OBJECT column.
349
return jdbcSQLClass(result_set.getColumn(column - 1).getSQLType()).getName();
350 // return result_set.getColumn(column - 1).classType().getName();
351
}
352
353 //#ENDIF
354

355 }
356
Popular Tags