1 20 package org.apache.derbyTesting.junit; 21 22 import java.sql.*; 23 24 import junit.framework.Assert; 25 26 30 public class JDBC { 31 32 36 private static final boolean HAVE_DRIVER 37 = haveClass("java.sql.Driver"); 38 39 43 private static final boolean HAVE_SAVEPOINT 44 = haveClass("java.sql.Savepoint"); 45 46 49 private static final boolean HAVE_SQLXML 50 = haveClass("java.sql.SQLXML"); 51 52 57 private static boolean haveClass(String className) 58 { 59 try { 60 Class.forName(className); 61 return true; 62 } catch (Exception e) { 63 return false; 64 } 65 } 66 72 public static boolean vmSupportsJDBC4() 73 { 74 return HAVE_DRIVER 75 && HAVE_SQLXML; 76 } 77 83 public static boolean vmSupportsJDBC3() 84 { 85 return HAVE_DRIVER 86 && HAVE_SAVEPOINT; 87 } 88 89 95 public static boolean vmSupportsJDBC2() 96 { 97 return HAVE_DRIVER; 98 } 99 105 public static boolean vmSupportsJSR169() 106 { 107 return !HAVE_DRIVER 108 && HAVE_SAVEPOINT; 109 } 110 111 123 public static void cleanup(Connection conn) throws SQLException 124 { 125 if (conn == null) 126 return; 127 if (conn.isClosed()) 128 return; 129 130 SQLException sqle = null; 131 try { 132 conn.rollback(); 133 } catch (SQLException e) { 134 sqle = e; 135 } 136 137 try { 138 conn.close(); 139 } catch (SQLException e) { 140 if (sqle == null) 141 sqle = e; 142 else 143 sqle.setNextException(e); 144 throw sqle; 145 } 146 } 147 148 162 public static void dropSchema(DatabaseMetaData dmd, String schema) throws SQLException 163 { 164 Connection conn = dmd.getConnection(); 165 Assert.assertFalse(conn.getAutoCommit()); 166 Statement s = dmd.getConnection().createStatement(); 167 168 PreparedStatement psf = conn.prepareStatement( 170 "SELECT ALIAS FROM SYS.SYSALIASES A, SYS.SYSSCHEMAS S" + 171 " WHERE A.SCHEMAID = S.SCHEMAID " + 172 " AND A.ALIASTYPE = 'F' " + 173 " AND S.SCHEMANAME = ?"); 174 psf.setString(1, schema); 175 ResultSet rs = psf.executeQuery(); 176 dropUsingDMD(s, rs, schema, "ALIAS", "FUNCTION"); 177 psf.close(); 178 179 rs = dmd.getProcedures((String ) null, 181 schema, (String ) null); 182 183 dropUsingDMD(s, rs, schema, "PROCEDURE_NAME", "PROCEDURE"); 184 185 rs = dmd.getTables((String ) null, schema, (String ) null, 187 new String [] {"VIEW"}); 188 189 dropUsingDMD(s, rs, schema, "TABLE_NAME", "VIEW"); 190 191 rs = dmd.getTables((String ) null, schema, (String ) null, 193 new String [] {"TABLE"}); 194 195 dropUsingDMD(s, rs, schema, "TABLE_NAME", "TABLE"); 196 197 rs = dmd.getTables((String ) null, schema, (String ) null, 200 new String [] {"AA_DERBY-1790-SYNONYM"}); 201 202 dropUsingDMD(s, rs, schema, "TABLE_NAME", "SYNONYM"); 203 204 if (!schema.equals("APP")) { 206 s.execute("DROP SCHEMA " + JDBC.escape(schema) + " RESTRICT"); 207 } 208 conn.commit(); 209 s.close(); 210 } 211 212 227 private static void dropUsingDMD( 228 Statement s, ResultSet rs, String schema, 229 String mdColumn, 230 String dropType) throws SQLException 231 { 232 String dropLeadIn = "DROP " + dropType + " "; 233 234 s.clearBatch(); 235 int batchCount = 0; 236 while (rs.next()) 237 { 238 String objectName = rs.getString(mdColumn); 239 s.addBatch(dropLeadIn + JDBC.escape(schema, objectName)); 240 batchCount++; 241 } 242 rs.close(); 243 int[] results; 244 try { 245 results = s.executeBatch(); 246 Assert.assertNotNull(results); 247 Assert.assertEquals("Incorrect result length from executeBatch", 248 batchCount, results.length); 249 } catch (BatchUpdateException batchException) { 250 results = batchException.getUpdateCounts(); 251 Assert.assertNotNull(results); 252 Assert.assertTrue("Too many results in BatchUpdateException", 253 results.length <= batchCount); 254 } 255 256 boolean hadError = false; 257 boolean didDrop = false; 258 for (int i = 0; i < results.length; i++) 259 { 260 int result = results[i]; 261 if (result == -3 ) 262 hadError = true; 263 else if (result == -2) 264 didDrop = true; 265 else if (result >= 0) 266 didDrop = true; 267 else 268 Assert.fail("Negative executeBatch status"); 269 } 270 271 s.getConnection().commit(); 273 s.clearBatch(); 274 } 275 276 287 public static void assertMetaDataMatch(DatabaseMetaData dmd, 288 ResultSetMetaData rsmd) throws SQLException 289 { 290 for (int col = 1; col <= rsmd.getColumnCount(); col++) 291 { 292 ResultSet column = dmd.getColumns( 294 rsmd.getCatalogName(col), 295 rsmd.getSchemaName(col), 296 rsmd.getTableName(col), 297 rsmd.getColumnName(col)); 298 299 Assert.assertTrue("Column missing " + rsmd.getColumnName(col), 300 column.next()); 301 302 Assert.assertEquals(column.getInt("DATA_TYPE"), 303 rsmd.getColumnType(col)); 304 305 Assert.assertEquals(column.getInt("NULLABLE"), 306 rsmd.isNullable(col)); 307 308 Assert.assertEquals(column.getString("TYPE_NAME"), 309 rsmd.getColumnTypeName(col)); 310 311 column.close(); 312 } 313 } 314 315 325 public static void assertDrainResults(ResultSet rs) 326 throws SQLException 327 { 328 ResultSetMetaData rsmd = rs.getMetaData(); 329 330 while (rs.next()) { 331 for (int col = 1; col <= rsmd.getColumnCount(); col++) 332 { 333 String s = rs.getString(col); 334 Assert.assertEquals(s == null, rs.wasNull()); 335 } 336 } 337 rs.close(); 338 } 339 340 344 public static String escape(String name) 345 { 346 return "\"" + name + "\""; 347 } 348 352 public static String escape(String schema, String name) 353 { 354 return "\"" + schema + "\".\"" + name + "\""; 355 } 356 } 357
| Popular Tags
|