1 24 25 package org.objectweb.cjdbc.scenario.raidb1.driver; 26 27 import java.sql.Connection ; 28 import java.sql.DatabaseMetaData ; 29 import java.sql.ResultSet ; 30 import java.sql.ResultSetMetaData ; 31 32 import org.objectweb.cjdbc.scenario.templates.Raidb1Template; 33 34 40 public class Raidb1MetaDataScenario extends Raidb1Template 41 { 42 43 48 public void testMetaDataTables() throws Exception 49 { 50 System.out.println("Tables:"); 51 Connection conn = getCJDBCConnection(); 52 DatabaseMetaData dbmd = conn.getMetaData(); 53 ResultSet rs = dbmd.getTables(null, null, null, null); 54 ResultSetMetaData rsmd = rs.getMetaData(); 55 int columnCount = rsmd.getColumnCount(); 56 String [] columnNames = new String [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 74 while (rs.next()) 75 { 76 String catalog = rs.getString(1); 77 String schema = rs.getString(2); 78 String name = rs.getString(3); 79 String type = rs.getString(4); 80 String remarks = rs.getString(5); 81 System.out.println(" " + catalog + "." + schema + "." + name + " : " 82 + type + " (" + remarks + ")"); 83 } 84 85 conn.close(); 86 } 87 88 93 public void testMetaDataSampleOutput() throws Exception 94 { 95 Connection conn = getCJDBCConnection(); 96 DatabaseMetaData dbmd = conn.getMetaData(); 97 assertEquals("Catalog term different than expected", dbmd.getCatalogTerm(), 98 "catalog"); 99 String 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 rs = dbmd.getCatalogs(); 105 while (rs.next()) 106 { 107 String 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 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 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 catalog = rs.getString(1); 140 String schema = rs.getString(2); 141 String name = rs.getString(3); 142 String grantor = rs.getString(4); 143 String grantee = rs.getString(5); 144 String privilege = rs.getString(6); 145 String 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 161 public void testMetaDataProcedures() throws Exception 162 { 163 System.out.println("Procedures:"); 164 Connection conn = getCJDBCConnection(); 165 DatabaseMetaData dbmd = conn.getMetaData(); 166 ResultSet rs = dbmd.getProcedures(null, null, null); 167 ResultSetMetaData rsmd = rs.getMetaData(); 168 int columnCount = rsmd.getColumnCount(); 169 String [] columnNames = new String [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 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 201 public void testMetaDataTablePrivileges() throws Exception 202 { 203 System.out.println("Table Privileges:"); 204 Connection conn = getCJDBCConnection(); 205 DatabaseMetaData dbmd = conn.getMetaData(); 206 ResultSet rs = dbmd.getTablePrivileges(null, null, null); 207 ResultSetMetaData rsmd = rs.getMetaData(); 208 int columnCount = rsmd.getColumnCount(); 209 String [] columnNames = new String [columnCount]; 210 for (int i = 1; i <= columnCount; i++) 211 columnNames[i - 1] = rsmd.getColumnName(i); 212 213 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 239 public void testMetaDataColumns() throws Exception 240 { 241 System.out.println("Columns:"); 242 Connection conn = getCJDBCConnection(); 243 DatabaseMetaData dbmd = conn.getMetaData(); 244 ResultSet rs = dbmd.getColumns(null, null, null, null); 245 ResultSetMetaData rsmd = rs.getMetaData(); 246 int columnCount = rsmd.getColumnCount(); 247 String [] columnNames = new String [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 306 conn.close(); 307 } 308 309 314 public void testMetaDataSchema() throws Exception 315 { 316 System.out.println("Schema:"); 317 Connection conn = getCJDBCConnection(); 318 DatabaseMetaData dbmd = conn.getMetaData(); 319 ResultSet rs = dbmd.getSchemas(); 320 ResultSetMetaData rsmd = rs.getMetaData(); 321 int columnCount = rsmd.getColumnCount(); 322 String [] columnNames = new String [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 336 public void testMetaDataCatalogs() throws Exception 337 { 338 System.out.println("Catalogs:"); 339 Connection conn = getCJDBCConnection(); 340 DatabaseMetaData dbmd = conn.getMetaData(); 341 ResultSet rs = dbmd.getCatalogs(); 342 ResultSetMetaData rsmd = rs.getMetaData(); 343 int columnCount = rsmd.getColumnCount(); 344 String [] columnNames = new String [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 358 public void testMetaDataTableTypes() throws Exception 359 { 360 System.out.println("Table Types:"); 361 Connection conn = getCJDBCConnection(); 362 DatabaseMetaData dbmd = conn.getMetaData(); 363 ResultSet rs = dbmd.getTableTypes(); 364 ResultSetMetaData rsmd = rs.getMetaData(); 365 int columnCount = rsmd.getColumnCount(); 366 String [] columnNames = new String [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 380 public void testMetaDataProcedureColumns() throws Exception 381 { 382 System.out.println("Procedures Columns:"); 383 Connection conn = getCJDBCConnection(); 384 DatabaseMetaData dbmd = conn.getMetaData(); 385 ResultSet rs = dbmd.getProcedureColumns(null, null, null, null); 386 ResultSetMetaData rsmd = rs.getMetaData(); 387 int columnCount = rsmd.getColumnCount(); 388 String [] columnNames = new String [columnCount]; 389 for (int i = 1; i <= columnCount; i++) 390 columnNames[i - 1] = rsmd.getColumnName(i); 391 392 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 442 public void testMetaDataPrimaryKeys() throws Exception 443 { 444 System.out.println("Primary Keys:"); 445 Connection conn = getCJDBCConnection(); 446 DatabaseMetaData dbmd = conn.getMetaData(); 447 ResultSet rs = dbmd.getPrimaryKeys(null, null, "ADDRESS"); 448 ResultSetMetaData rsmd = rs.getMetaData(); 449 int columnCount = rsmd.getColumnCount(); 450 String [] columnNames = new String [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 pkName = rs.getString("PK_NAME"); 456 assertEquals("PK Name different than expected", "ID", pkName); 457 458 conn.close(); 459 } 460 } | Popular Tags |