KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > metadata


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.metadata
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.derbyTesting.functionTests.tests.jdbcapi;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.CallableStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Types JavaDoc;
33 import java.sql.Timestamp JavaDoc;
34 import java.sql.Time JavaDoc;
35 import java.sql.Date JavaDoc;
36 import java.math.BigDecimal JavaDoc;
37
38 import java.util.Properties JavaDoc;
39
40 import org.apache.derby.tools.ij;
41
42 /**
43  * Test of database meta-data. This program simply calls each of the meta-data
44  * methods, one by one, and prints the results. The test passes if the printed
45  * results match a previously stored "master". Thus this test cannot actually
46  * discern whether it passes or not.
47  *
48  * @author alan
49  */

50
51 public class metadata extends metadata_test {
52
53     public metadata() {
54     }
55
56     /**
57      * Constructor:
58      * Just intializes the Connection and Statement fields
59      * to be used through the test.
60      */

61     public metadata(String JavaDoc [] args) {
62
63         try {
64
65             ij.getPropertyArg(args);
66             con = ij.startJBMS();
67             s = con.createStatement();
68
69         } catch (SQLException JavaDoc e) {
70             dumpSQLExceptions(e);
71         }
72         catch (Throwable JavaDoc e) {
73             System.out.println("FAIL -- unexpected exception:");
74             e.printStackTrace(System.out);
75         }
76
77     }
78
79     /**
80      * Makes a call to the "runTest" method in metadata_test.java,
81      * which will in turn call back here for implementations of
82      * the abstract methods.
83      */

84     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
85
86         new metadata(args).runTest();
87
88     }
89
90     /**
91      * This method is responsible for executing a metadata query and returning
92      * a result set that complies with the JDBC specification.
93      */

94     protected ResultSet JavaDoc getMetaDataRS(DatabaseMetaData JavaDoc dmd, int procId,
95         String JavaDoc [] sArgs, String JavaDoc [] argArray, int [] iArgs, boolean [] bArgs)
96         throws SQLException JavaDoc
97     {
98
99         switch (procId) {
100
101             case GET_PROCEDURES:
102                 return dmd.getProcedures(sArgs[0], sArgs[1], sArgs[2]);
103
104             case GET_PROCEDURE_COLUMNS:
105                 return dmd.getProcedureColumns(sArgs[0], sArgs[1], sArgs[2], sArgs[3]);
106
107             case GET_TABLES:
108                 return dmd.getTables(sArgs[0], sArgs[1], sArgs[2], argArray);
109
110             case GET_COLUMNS:
111                 return dmd.getColumns(sArgs[0], sArgs[1], sArgs[2], sArgs[3]);
112
113             case GET_COLUMN_PRIVILEGES:
114                 return dmd.getColumnPrivileges(sArgs[0], sArgs[1], sArgs[2], sArgs[3]);
115
116             case GET_TABLE_PRIVILEGES:
117                 return dmd.getTablePrivileges(sArgs[0], sArgs[1], sArgs[2]);
118
119             case GET_BEST_ROW_IDENTIFIER:
120                 return dmd.getBestRowIdentifier(sArgs[0], sArgs[1], sArgs[2],
121                     iArgs[0], bArgs[0]);
122
123             case GET_VERSION_COLUMNS:
124                 return dmd.getVersionColumns(sArgs[0], sArgs[1], sArgs[2]);
125
126             case GET_PRIMARY_KEYS:
127                 return dmd.getPrimaryKeys(sArgs[0], sArgs[1], sArgs[2]);
128
129             case GET_IMPORTED_KEYS:
130                 return dmd.getImportedKeys(sArgs[0], sArgs[1], sArgs[2]);
131
132             case GET_EXPORTED_KEYS:
133                 return dmd.getExportedKeys(sArgs[0], sArgs[1], sArgs[2]);
134
135             case GET_CROSS_REFERENCE:
136                 return dmd.getCrossReference(sArgs[0], sArgs[1], sArgs[2],
137                     sArgs[3], sArgs[4], sArgs[5]);
138
139             case GET_TYPE_INFO:
140                 return dmd.getTypeInfo();
141
142             case GET_INDEX_INFO:
143                 return dmd.getIndexInfo(sArgs[0], sArgs[1], sArgs[2],
144                     bArgs[0], bArgs[1]);
145
146             default:
147             // shouldn't get here.
148

149                 System.out.println("*** UNEXPECTED PROCEDURE ID ENCOUNTERED: " + procId + ".");
150                 return null;
151         }
152
153     }
154
155     protected void dumpRS(int procId, ResultSet JavaDoc s) throws SQLException JavaDoc {
156
157         ResultSetMetaData JavaDoc rsmd = s.getMetaData ();
158
159         // Get the number of columns in the result set
160
int numCols = rsmd.getColumnCount ();
161         String JavaDoc[] headers = new String JavaDoc[numCols];
162         if (numCols <= 0) {
163             System.out.println("(no columns!)");
164             return;
165         }
166         
167         // Display column headings, and include column types
168
// as part of those headings.
169
for (int i=1; i<=numCols; i++) {
170             if (i > 1) System.out.print(",");
171             headers[i-1] = rsmd.getColumnLabel(i);
172             System.out.print(headers[i-1]);
173             System.out.print("[" + rsmd.getColumnTypeName(i) + "]");
174
175         }
176         System.out.println();
177     
178         // Display data, fetching until end of the result set
179
StringBuffer JavaDoc errorColumns;
180         while (s.next()) {
181             // Loop through each column, getting the
182
// column data and displaying
183
errorColumns = new StringBuffer JavaDoc();
184             String JavaDoc value;
185             for (int i=1; i<=numCols; i++) {
186                 if (i > 1) System.out.print(",");
187                 value = s.getString(i);
188                 if (headers[i-1].equals("DATA_TYPE"))
189                 {
190                     if (((org.apache.derbyTesting.functionTests.util.TestUtil.getJDBCMajorVersion(s.getStatement().getConnection()) >= 3) &&
191                          (Integer.valueOf(value).intValue() == 16)) ||
192                         (Integer.valueOf(value).intValue() == -7))
193                         System.out.print("**BOOLEAN_TYPE for VM**");
194                     else
195                         System.out.print(value);
196                 }
197                 else
198                     System.out.print(value);
199
200             }
201             System.out.println();
202         }
203         s.close();
204     }
205
206     /** dummy method to test getProcedureColumns
207      */

208     public static byte[] getpc(String JavaDoc a, BigDecimal JavaDoc b, short c, byte d, short e, int f, long g, float h, double i, byte[] j, Date JavaDoc k, Time JavaDoc l, Timestamp JavaDoc T)
209     {
210         return j;
211     }
212
213     /** overload getpc to further test getProcedureColumns
214     */

215     public static void getpc(int a, long[] b)
216     {
217     }
218
219     /** overload getpc to further test getProcedureColumns
220      * private method shouldn't be returned with alias, ok with procedure
221      */

222     private static void getpc(int a, long b)
223     {
224     }
225
226     // instance method
227
// with method alias, this should not be returned by getProcedureColumns
228
// but DB2 returns this with a java procedure
229
public void getpc(String JavaDoc a, String JavaDoc b) {
230     }
231
232     // this method should notbe seen by getProcedureColumns as
233
// it has no parameters and no return value.
234
public static void getpc4a() {
235     }
236
237     // check a method with no paramters and a return value works
238
// for getProcedureColumns.
239
public static int getpc4b() {
240         return 4;
241     }
242
243     // check for nested connection working ok
244
public static void isro() throws SQLException JavaDoc {
245         DriverManager.getConnection("jdbc:default:connection").getMetaData().isReadOnly();
246     }
247
248 }
249
250
Popular Tags