KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > driver > Raidb1MetaDataScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.driver;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DatabaseMetaData JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.ResultSetMetaData JavaDoc;
31
32 import org.objectweb.cjdbc.scenario.templates.Raidb1Template;
33
34 /**
35  * This class defines a Raidb1MetaDataScenario
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
38  * @version 1.0
39  */

40 public class Raidb1MetaDataScenario extends Raidb1Template
41 {
42
43   /**
44    * Test the meta data return by the driver for the tables
45    *
46    * @throws Exception if fails
47    */

48   public void testMetaDataTables() throws Exception JavaDoc
49   {
50     System.out.println("Tables:");
51     Connection JavaDoc conn = getCJDBCConnection();
52     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
53     ResultSet JavaDoc rs = dbmd.getTables(null, null, null, null);
54     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
55     int columnCount = rsmd.getColumnCount();
56     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
57     for (int i = 1; i <= columnCount; i++)
58       columnNames[i - 1] = rsmd.getColumnName(i);
59
60     assertEquals("TABLE_CAT invalid", "TABLE_CAT", columnNames[0]);
61     assertEquals("TABLE_SCHEM invalid", "TABLE_SCHEM", columnNames[1]);
62     assertEquals("TABLE_NAME invalid", "TABLE_NAME", columnNames[2]);
63     assertEquals("TABLE_TYPE invalid", "TABLE_TYPE", columnNames[3]);
64     assertEquals("REMARKS invalid", "REMARKS", columnNames[4]);
65
66     // # TABLE_CAT String => table catalog (may be null)
67
// # TABLE_SCHEM String => table schema (may be null)
68
// # TABLE_NAME String => table name
69
// # TABLE_TYPE String => table type. Typical types are "TABLE", "VIEW",
70
// "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",
71
// "SYNONYM".
72
// # REMARKS String => explanatory comment on the table
73

74     while (rs.next())
75     {
76       String JavaDoc catalog = rs.getString(1);
77       String JavaDoc schema = rs.getString(2);
78       String JavaDoc name = rs.getString(3);
79       String JavaDoc type = rs.getString(4);
80       String JavaDoc remarks = rs.getString(5);
81       System.out.println(" " + catalog + "." + schema + "." + name + " : "
82           + type + " (" + remarks + ")");
83     }
84
85     conn.close();
86   }
87
88   /**
89    * Retrieve metadata from the driver, simple asserts, mainly for display
90    *
91    * @throws Exception when fails
92    */

93   public void testMetaDataSampleOutput() throws Exception JavaDoc
94   {
95     Connection JavaDoc conn = getCJDBCConnection();
96     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
97     assertEquals("Catalog term different than expected", dbmd.getCatalogTerm(),
98         "catalog");
99     String JavaDoc catalog1 = conn.getCatalog();
100     System.out.println("Current Catalog:" + catalog1);
101     assertEquals("Current catalog different than expected", catalog1, "myDB");
102
103     System.out.println("Catalog: (" + dbmd.getSchemaTerm() + "):");
104     ResultSet JavaDoc rs = dbmd.getCatalogs();
105     while (rs.next())
106     {
107       String JavaDoc s = rs.getString("TABLE_CAT");
108       System.out.println(" " + s);
109     }
110
111     System.out.println("Schemas (" + dbmd.getSchemaTerm() + "):");
112     rs = dbmd.getSchemas();
113     if (rs != null)
114     {
115       while (rs.next())
116       {
117         String JavaDoc s = rs.getString(1);
118         System.out.println(" " + s);
119       }
120     }
121
122     System.out.println("Tables types:");
123     rs = dbmd.getTableTypes();
124     if (rs != null)
125     {
126       while (rs.next())
127       {
128         String JavaDoc s = rs.getString(1);
129         System.out.println(" " + s);
130       }
131     }
132
133     System.out.println("Table Privileges:");
134     rs = dbmd.getTablePrivileges(null, null, null);
135
136     if (rs != null)
137       while (rs.next())
138       {
139         String JavaDoc catalog = rs.getString(1);
140         String JavaDoc schema = rs.getString(2);
141         String JavaDoc name = rs.getString(3);
142         String JavaDoc grantor = rs.getString(4);
143         String JavaDoc grantee = rs.getString(5);
144         String JavaDoc privilege = rs.getString(6);
145         String JavaDoc isGrantable = rs.getString(7);
146         System.out.println(" " + catalog + "." + schema + "." + name);
147         System.out.println(" Grantor: " + grantor);
148         System.out.println(" Grantee: " + grantee);
149         System.out.println(" Privilege: " + privilege);
150         System.out.println(" isGrantable: " + isGrantable);
151       }
152     
153     conn.close();
154   }
155
156   /**
157    * Test the meta data return by the driver for the procedures
158    *
159    * @throws Exception if fails
160    */

161   public void testMetaDataProcedures() throws Exception JavaDoc
162   {
163     System.out.println("Procedures:");
164     Connection JavaDoc conn = getCJDBCConnection();
165     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
166     ResultSet JavaDoc rs = dbmd.getProcedures(null, null, null);
167     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
168     int columnCount = rsmd.getColumnCount();
169     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
170     for (int i = 1; i <= columnCount; i++)
171     {
172       columnNames[i - 1] = rsmd.getColumnName(i);
173       System.out.println(columnNames[i - 1]);
174     }
175     // 1. PROCEDURE_CAT String => procedure catalog (may be null)
176
// 2. PROCEDURE_SCHEM String => procedure schema (may be null)
177
// 3. PROCEDURE_NAME String => procedure name
178
// 4. reserved for future use
179
// 5. reserved for future use
180
// 6. reserved for future use
181
// 7. REMARKS String => explanatory comment on the procedure
182
// 8. PROCEDURE_TYPE short => kind of procedure:
183
// * procedureResultUnknown - May return a result
184
// * procedureNoResult - Does not return a result
185
// * procedureReturnsResult - Returns a result
186

187     assertEquals("PROCEDURE_CAT invalid", "PROCEDURE_CAT", columnNames[0]);
188     assertEquals("PROCEDURE_SCHEM invalid", "PROCEDURE_SCHEM", columnNames[1]);
189     assertEquals("PROCEDURE_NAME invalid", "PROCEDURE_NAME", columnNames[2]);
190     assertEquals("REMARKS invalid", "REMARKS", columnNames[6]);
191     assertEquals("PROCEDURE_TYPE invalid", "PROCEDURE_TYPE", columnNames[7]);
192     
193     conn.close();
194   }
195
196   /**
197    * Test the meta data return by the driver for the table privileges
198    *
199    * @throws Exception if fails
200    */

201   public void testMetaDataTablePrivileges() throws Exception JavaDoc
202   {
203     System.out.println("Table Privileges:");
204     Connection JavaDoc conn = getCJDBCConnection();
205     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
206     ResultSet JavaDoc rs = dbmd.getTablePrivileges(null, null, null);
207     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
208     int columnCount = rsmd.getColumnCount();
209     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
210     for (int i = 1; i <= columnCount; i++)
211       columnNames[i - 1] = rsmd.getColumnName(i);
212
213     // # TABLE_CAT String => table catalog (may be null)
214
// # TABLE_SCHEM String => table schema (may be null)
215
// # TABLE_NAME String => table name
216
// # GRANTOR => grantor of access (may be null)
217
// # GRANTEE String => grantee of access
218
// # PRIVILEGE String => name of access (SELECT, INSERT, UPDATE, REFRENCES,
219
// ...)
220
// # IS_GRANTABLE String => "YES" if grantee is permitted to grant to
221
// others; "NO" if not; null if unknown
222

223     assertEquals("TABLE_CAT invalid", "TABLE_CAT", columnNames[0]);
224     assertEquals("TABLE_SCHEM invalid", "TABLE_SCHEM", columnNames[1]);
225     assertEquals("TABLE_NAME invalid", "TABLE_NAME", columnNames[2]);
226     assertEquals("GRANTOR invalid", "GRANTOR", columnNames[3]);
227     assertEquals("GRANTEE invalid", "GRANTEE", columnNames[4]);
228     assertEquals("PRIVILEGE invalid", "PRIVILEGE", columnNames[5]);
229     assertEquals("IS_GRANTABLE invalid", "IS_GRANTABLE", columnNames[6]);
230     
231     conn.close();
232   }
233
234   /**
235    * Test the meta data return by the driver for the columns
236    *
237    * @throws Exception if fails
238    */

239   public void testMetaDataColumns() throws Exception JavaDoc
240   {
241     System.out.println("Columns:");
242     Connection JavaDoc conn = getCJDBCConnection();
243     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
244     ResultSet JavaDoc rs = dbmd.getColumns(null, null, null, null);
245     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
246     int columnCount = rsmd.getColumnCount();
247     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
248     for (int i = 1; i <= columnCount; i++)
249       columnNames[i - 1] = rsmd.getColumnName(i);
250
251     assertEquals("TABLE_CAT invalid", "TABLE_CAT", columnNames[0]);
252     assertEquals("TABLE_SCHEM invalid", "TABLE_SCHEM", columnNames[1]);
253     assertEquals("TABLE_NAME invalid", "TABLE_NAME", columnNames[2]);
254     assertEquals("COLUMN_NAME invalid", "COLUMN_NAME", columnNames[3]);
255     assertEquals("DATA_TYPE invalid", "DATA_TYPE", columnNames[4]);
256
257     assertEquals("TYPE_NAME invalid", "TYPE_NAME", columnNames[5]);
258     assertEquals("COLUMN_SIZE invalid", "COLUMN_SIZE", columnNames[6]);
259     assertEquals("BUFFER_LENGTH invalid", "BUFFER_LENGTH", columnNames[7]);
260     assertEquals("DECIMAL_DIGITS invalid", "DECIMAL_DIGITS", columnNames[8]);
261     assertEquals("NUM_PREC_RADIX invalid", "NUM_PREC_RADIX", columnNames[9]);
262
263     assertEquals("NULLABLE invalid", "NULLABLE", columnNames[10]);
264     assertEquals("REMARKS invalid", "REMARKS", columnNames[11]);
265     assertEquals("COLUMN_DEF invalid", "COLUMN_DEF", columnNames[12]);
266     assertEquals("SQL_DATA_TYPE invalid", "SQL_DATA_TYPE", columnNames[13]);
267     assertEquals("SQL_DATETIME_SUB invalid", "SQL_DATETIME_SUB",
268         columnNames[14]);
269
270     assertEquals("CHAR_OCTET_LENGTH invalid", "CHAR_OCTET_LENGTH",
271         columnNames[15]);
272     assertEquals("ORDINAL_POSITION invalid", "ORDINAL_POSITION",
273         columnNames[16]);
274     assertEquals("IS_NULLABLE invalid", "IS_NULLABLE", columnNames[17]);
275
276     // # TABLE_CAT String => table catalog (may be null)
277
// # TABLE_SCHEM String => table schema (may be null)
278
// # TABLE_NAME String => table name
279
// # COLUMN_NAME String => column name
280
// # DATA_TYPE short => SQL type from java.sql.Types
281
// # TYPE_NAME String => Data source dependent type name, for a UDT the
282
// type name is fully qualified
283
// # COLUMN_SIZE int => column size. For char or date types this is the
284
// maximum number of characters, for numeric or decimal types this is
285
// precision.
286
// # BUFFER_LENGTH is not used.
287
// # DECIMAL_DIGITS int => the number of fractional digits
288
// # NUM_PREC_RADIX int => Radix (typically either 10 or 2)
289
// # NULLABLE int => is NULL allowed?
290
//
291
// * columnNoNulls - might not allow NULL values
292
// * columnNullable - definitely allows NULL values
293
// * columnNullableUnknown - nullability unknown
294
//
295
// # REMARKS String => comment describing column (may be null)
296
// # COLUMN_DEF String => default value (may be null)
297
// # SQL_DATA_TYPE int => unused
298
// # SQL_DATETIME_SUB int => unused
299
// # CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in
300
// the column
301
// # ORDINAL_POSITION int => index of column in table (starting at 1)
302
// # IS_NULLABLE String => "NO" means column definitely does not allow NULL
303
// values; "YES" means the column might allow NULL values. An empty string
304
// means nobody knows.
305

306     conn.close();
307   }
308
309   /**
310    * Test the meta data return by the driver for the schema
311    *
312    * @throws Exception if fails
313    */

314   public void testMetaDataSchema() throws Exception JavaDoc
315   {
316     System.out.println("Schema:");
317     Connection JavaDoc conn = getCJDBCConnection();
318     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
319     ResultSet JavaDoc rs = dbmd.getSchemas();
320     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
321     int columnCount = rsmd.getColumnCount();
322     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
323     for (int i = 1; i <= columnCount; i++)
324       columnNames[i - 1] = rsmd.getColumnName(i);
325
326     assertEquals("TABLE_SCHEM invalid", "TABLE_SCHEM", columnNames[0]);
327     
328     conn.close();
329   }
330
331   /**
332    * Test the meta data return by the driver for the catalogs
333    *
334    * @throws Exception if fails
335    */

336   public void testMetaDataCatalogs() throws Exception JavaDoc
337   {
338     System.out.println("Catalogs:");
339     Connection JavaDoc conn = getCJDBCConnection();
340     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
341     ResultSet JavaDoc rs = dbmd.getCatalogs();
342     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
343     int columnCount = rsmd.getColumnCount();
344     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
345     for (int i = 1; i <= columnCount; i++)
346       columnNames[i - 1] = rsmd.getColumnName(i);
347
348     assertEquals("TABLE_CAT invalid", "TABLE_CAT", columnNames[0]);
349     
350     conn.close();
351   }
352
353   /**
354    * Test the meta data return by the driver for the table types
355    *
356    * @throws Exception if fails
357    */

358   public void testMetaDataTableTypes() throws Exception JavaDoc
359   {
360     System.out.println("Table Types:");
361     Connection JavaDoc conn = getCJDBCConnection();
362     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
363     ResultSet JavaDoc rs = dbmd.getTableTypes();
364     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
365     int columnCount = rsmd.getColumnCount();
366     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
367     for (int i = 1; i <= columnCount; i++)
368       columnNames[i - 1] = rsmd.getColumnName(i);
369
370     assertEquals("TABLE_TYPE invalid", "TABLE_TYPE", columnNames[0]);
371     
372     conn.close();
373   }
374
375   /**
376    * Test the meta data return by the driver for the procedure columns
377    *
378    * @throws Exception if fails
379    */

380   public void testMetaDataProcedureColumns() throws Exception JavaDoc
381   {
382     System.out.println("Procedures Columns:");
383     Connection JavaDoc conn = getCJDBCConnection();
384     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
385     ResultSet JavaDoc rs = dbmd.getProcedureColumns(null, null, null, null);
386     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
387     int columnCount = rsmd.getColumnCount();
388     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
389     for (int i = 1; i <= columnCount; i++)
390       columnNames[i - 1] = rsmd.getColumnName(i);
391
392     // # PROCEDURE_CAT String => procedure catalog (may be null)
393
// # PROCEDURE_SCHEM String => procedure schema (may be null)
394
// # PROCEDURE_NAME String => procedure name
395
// # COLUMN_NAME String => column/parameter name
396
// # COLUMN_TYPE Short => kind of column/parameter:
397
//
398
// * procedureColumnUnknown - nobody knows
399
// * procedureColumnIn - IN parameter
400
// * procedureColumnInOut - INOUT parameter
401
// * procedureColumnOut - OUT parameter
402
// * procedureColumnReturn - procedure return value
403
// * procedureColumnResult - result column in ResultSet
404
//
405
// # DATA_TYPE short => SQL type from java.sql.Types
406
// # TYPE_NAME String => SQL type name, for a UDT type the type name is
407
// fully qualified
408
// # PRECISION int => precision
409
// # LENGTH int => length in bytes of data
410
// # SCALE short => scale
411
// # RADIX short => radix
412
// # NULLABLE short => can it contain NULL?
413
//
414
// * procedureNoNulls - does not allow NULL values
415
// * procedureNullable - allows NULL values
416
// * procedureNullableUnknown - nullability unknown
417
//
418
// # REMARKS String => comment describing parameter/column
419

420     assertEquals("PROCEDURE_CAT invalid", "PROCEDURE_CAT", columnNames[0]);
421     assertEquals("PROCEDURE_SCHEM invalid", "PROCEDURE_SCHEM", columnNames[1]);
422     assertEquals("PROCEDURE_NAME invalid", "PROCEDURE_NAME", columnNames[2]);
423     assertEquals("COLUMN_NAME invalid", "COLUMN_NAME", columnNames[3]);
424     assertEquals("COLUMN_TYPE invalid", "COLUMN_TYPE", columnNames[4]);
425     assertEquals("DATA_TYPE invalid", "DATA_TYPE", columnNames[5]);
426     assertEquals("TYPE_NAME invalid", "TYPE_NAME", columnNames[6]);
427     assertEquals("PRECISION invalid", "PRECISION", columnNames[7]);
428     assertEquals("LENGTH invalid", "LENGTH", columnNames[8]);
429     assertEquals("SCALE invalid", "SCALE", columnNames[9]);
430     assertEquals("RADIX invalid", "RADIX", columnNames[10]);
431     assertEquals("NULLABLE invalid", "NULLABLE", columnNames[11]);
432     assertEquals("REMARKS invalid", "REMARKS", columnNames[12]);
433     
434     conn.close();
435   }
436
437   /**
438    * Test the meta data return by the driver for the primary keys
439    *
440    * @throws Exception if fails
441    */

442   public void testMetaDataPrimaryKeys() throws Exception JavaDoc
443   {
444     System.out.println("Primary Keys:");
445     Connection JavaDoc conn = getCJDBCConnection();
446     DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
447     ResultSet JavaDoc rs = dbmd.getPrimaryKeys(null, null, "ADDRESS");
448     ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
449     int columnCount = rsmd.getColumnCount();
450     String JavaDoc[] columnNames = new String JavaDoc[columnCount];
451     for (int i = 1; i <= columnCount; i++)
452       columnNames[i - 1] = rsmd.getColumnName(i);
453
454     assertTrue("Could not get result set for primary keys", rs.next());
455     String JavaDoc pkName = rs.getString("PK_NAME");
456     assertEquals("PK Name different than expected", "ID", pkName);
457     
458     conn.close();
459   }
460 }
Popular Tags