KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > catalog > GetProcedureColumns


1 /*
2
3    Derby - Class org.apache.derby.catalog.GetProcedureColumns
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.catalog;
23
24 import java.sql.Types JavaDoc;
25 import java.lang.reflect.*;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.DatabaseMetaData JavaDoc;
29 import org.apache.derby.catalog.TypeDescriptor;
30
31 import org.apache.derby.iapi.types.DataTypeDescriptor;
32 import org.apache.derby.iapi.types.DataTypeUtilities;
33 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
34 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
35 import org.apache.derby.catalog.types.RoutineAliasInfo;
36
37 import org.apache.derby.shared.common.reference.JDBC40Translation;
38 /**
39     <P>Use of VirtualTableInterface to provide support for
40     DatabaseMetaData.getProcedureColumns().
41     
42
43     <P>This class is called from a Query constructed in
44     java/org.apache.derby.impl.jdbc/metadata.properties:
45 <PRE>
46
47
48     <P>The VTI will return columns 3-14, an extra column to the specification
49     METHOD_ID is returned to distinguish between overloaded methods.
50
51   <OL>
52         <LI><B>PROCEDURE_CAT</B> String => procedure catalog (may be null)
53         <LI><B>PROCEDURE_SCHEM</B> String => procedure schema (may be null)
54         <LI><B>PROCEDURE_NAME</B> String => procedure name
55         <LI><B>COLUMN_NAME</B> String => column/parameter name
56         <LI><B>COLUMN_TYPE</B> Short => kind of column/parameter:
57       <UL>
58       <LI> procedureColumnUnknown - nobody knows
59       <LI> procedureColumnIn - IN parameter
60       <LI> procedureColumnInOut - INOUT parameter
61       <LI> procedureColumnOut - OUT parameter
62       <LI> procedureColumnReturn - procedure return value
63       <LI> procedureColumnResult - result column in ResultSet
64       </UL>
65   <LI><B>DATA_TYPE</B> int => SQL type from java.sql.Types
66         <LI><B>TYPE_NAME</B> String => SQL type name, for a UDT type the
67   type name is fully qualified
68         <LI><B>PRECISION</B> int => precision
69         <LI><B>LENGTH</B> int => length in bytes of data
70         <LI><B>SCALE</B> short => scale
71         <LI><B>RADIX</B> short => radix
72         <LI><B>NULLABLE</B> short => can it contain NULL?
73       <UL>
74       <LI> procedureNoNulls - does not allow NULL values
75       <LI> procedureNullable - allows NULL values
76       <LI> procedureNullableUnknown - nullability unknown
77       </UL>
78         <LI><B>REMARKS</B> String => comment describing parameter/column
79         <LI><B>METHOD_ID</B> Short => cloudscape extra column (overloading)
80         <LI><B>PARAMETER_ID</B> Short => cloudscape extra column (output order)
81   </OL>
82
83 */

84
85 public class GetProcedureColumns extends org.apache.derby.vti.VTITemplate
86 {
87     private boolean isFunction;
88     private int translate(int val) {
89         if (!isFunction) { return val; }
90         switch (val) {
91         case DatabaseMetaData.procedureColumnUnknown:
92             return JDBC40Translation.FUNCTION_PARAMETER_UNKNOWN;
93         case DatabaseMetaData.procedureColumnIn:
94             return JDBC40Translation.FUNCTION_PARAMETER_IN;
95         case DatabaseMetaData.procedureColumnInOut:
96             return JDBC40Translation.FUNCTION_PARAMETER_INOUT;
97         case DatabaseMetaData.procedureColumnOut:
98             return JDBC40Translation.FUNCTION_PARAMETER_OUT;
99         case DatabaseMetaData.procedureColumnReturn:
100             return JDBC40Translation.FUNCTION_RETURN;
101         default:
102             return JDBC40Translation.FUNCTION_PARAMETER_UNKNOWN;
103         }
104     }
105
106     private boolean isProcedure;
107     // state for procedures.
108
private RoutineAliasInfo procedure;
109     private int paramCursor;
110     private short method_count;
111     private short param_number;
112
113     private TypeDescriptor sqlType;
114     private String JavaDoc columnName;
115     private short columnType;
116     private final short nullable;
117
118     public ResultSetMetaData JavaDoc getMetaData()
119     {
120         return metadata;
121     }
122
123     //
124
// Instantiates the vti given a class name and methodname.
125
//
126
// @exception SQLException Thrown if there is a SQL error.
127
//
128
//
129
public GetProcedureColumns(AliasInfo aliasInfo, String JavaDoc aliasType) throws SQLException JavaDoc
130     {
131         // compile time aliasInfo will be null.
132
if (aliasInfo != null) {
133             isProcedure = aliasType.equals("P");
134             isFunction = aliasType.equals("F");
135             procedure = (RoutineAliasInfo) aliasInfo;
136             method_count = (short) procedure.getParameterCount();
137         }
138         if (aliasType == null) {
139             nullable = 0;
140             return;
141         }
142
143         if (isFunction) {
144             nullable = (short) JDBC40Translation.FUNCTION_NULLABLE;
145             sqlType = procedure.getReturnType();
146             columnName = ""; // COLUMN_NAME is VARCHAR NOT NULL
147
columnType = (short) JDBC40Translation.FUNCTION_RETURN;
148             paramCursor = -2;
149             return;
150         }
151         nullable = (short) DatabaseMetaData.procedureNullable;
152
153         paramCursor = -1;
154     }
155
156     public boolean next() throws SQLException JavaDoc {
157         if (++paramCursor >= procedure.getParameterCount())
158             return false;
159
160         if (paramCursor > -1) {
161             sqlType = procedure.getParameterTypes()[paramCursor];
162             columnName = procedure.getParameterNames()[paramCursor];
163             columnType =
164                 (short)translate(procedure.getParameterModes()[paramCursor]);
165         }
166         param_number = (short) paramCursor;
167         return true;
168     }
169
170     //
171
// Get the value of the specified data type from a column.
172
//
173
// @exception SQLException Thrown if there is a SQL error.
174
//
175
//
176
public String JavaDoc getString(int column) throws SQLException JavaDoc
177     {
178         switch (column)
179         {
180         case 1: // COLUMN_NAME:
181
return columnName;
182
183         case 4: //_TYPE_NAME:
184
return sqlType.getTypeName();
185                
186         case 10: // REMARKS:
187
return null;
188
189             default:
190                 return super.getString(column); // throw exception
191
}
192     }
193
194     //
195
// Get the value of the specified data type from a column.
196
//
197
// @exception SQLException Thrown if there is a SQL error.
198
//
199
//
200
public int getInt(int column) throws SQLException JavaDoc
201     {
202         switch (column)
203         {
204         case 3: // DATA_TYPE:
205
if (sqlType != null) {
206                 return sqlType.getJDBCTypeId();
207             }
208             return java.sql.Types.JAVA_OBJECT;
209
210         case 5: // PRECISION:
211
if (sqlType != null)
212                 {
213                     int type = sqlType.getJDBCTypeId();
214                     if (DataTypeDescriptor.isNumericType(type))
215                         return sqlType.getPrecision();
216                     else if (type == Types.DATE || type == Types.TIME
217                              || type == Types.TIMESTAMP)
218                         return DataTypeUtilities.getColumnDisplaySize(type, -1);
219                     else
220                         return sqlType.getMaximumWidth();
221                 }
222
223                 // No corresponding SQL type
224
return 0;
225
226         case 6: // LENGTH (in bytes):
227
if (sqlType != null)
228                     return sqlType.getMaximumWidthInBytes();
229
230                 // No corresponding SQL type
231
return 0;
232           
233             default:
234                 return super.getInt(column); // throw exception
235
}
236     }
237
238     //
239
// Get the value of the specified data type from a column.
240
//
241
// @exception SQLException Thrown if there is a SQL error.
242
//
243
//
244
public short getShort(int column) throws SQLException JavaDoc
245     {
246         switch (column)
247         {
248         case 2: // COLUMN_TYPE:
249
return columnType;
250
251         case 7: // SCALE:
252
if (sqlType != null)
253                     return (short)sqlType.getScale();
254
255                 // No corresponding SQL type
256
return 0;
257
258         case 8: // RADIX:
259
if (sqlType != null)
260                 {
261                     int sqlTypeID = sqlType.getJDBCTypeId();
262                     if (sqlTypeID == java.sql.Types.REAL ||
263                         sqlTypeID == java.sql.Types.FLOAT ||
264                         sqlTypeID == java.sql.Types.DOUBLE)
265                     {
266                         return 2;
267                     }
268                     return 10;
269                 }
270
271                 // No corresponding SQL type
272
return 0;
273
274         //FIXME
275
case 9: // NULLABLE:
276
return nullable;
277
278         case 11: // METHOD_ID:
279
return method_count;
280
281         case 12: // PARAMETER_ID:
282
return param_number;
283
284             default:
285                 return super.getShort(column); // throw exception
286
}
287     }
288
289     public void close()
290     {
291     }
292
293     /*
294     ** Metadata
295     */

296     private static final ResultColumnDescriptor[] columnInfo = {
297
298         EmbedResultSetMetaData.getResultColumnDescriptor("COLUMN_NAME", Types.VARCHAR, false, 128),
299         EmbedResultSetMetaData.getResultColumnDescriptor("COLUMN_TYPE", Types.SMALLINT, false),
300         EmbedResultSetMetaData.getResultColumnDescriptor("DATA_TYPE", Types.INTEGER, false),
301         EmbedResultSetMetaData.getResultColumnDescriptor("TYPE_NAME", Types.VARCHAR, false, 22),
302         EmbedResultSetMetaData.getResultColumnDescriptor("PRECISION", Types.INTEGER, false),
303         EmbedResultSetMetaData.getResultColumnDescriptor("LENGTH", Types.INTEGER, false),
304         EmbedResultSetMetaData.getResultColumnDescriptor("SCALE", Types.SMALLINT, false),
305
306         EmbedResultSetMetaData.getResultColumnDescriptor("RADIX", Types.SMALLINT, false),
307         EmbedResultSetMetaData.getResultColumnDescriptor("NULLABLE", Types.SMALLINT, false),
308         EmbedResultSetMetaData.getResultColumnDescriptor("REMARKS", Types.VARCHAR, true, 22),
309         EmbedResultSetMetaData.getResultColumnDescriptor("METHOD_ID", Types.SMALLINT, false),
310         EmbedResultSetMetaData.getResultColumnDescriptor("PARAMETER_ID", Types.SMALLINT, false),
311     };
312     private static final ResultSetMetaData JavaDoc metadata = new EmbedResultSetMetaData(columnInfo);
313 }
314
Popular Tags