1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.ResultSetMetaData ; 29 import java.sql.SQLException ; 30 import java.sql.Statement ; 31 32 import org.apache.derby.tools.ij; 33 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 34 import org.apache.derby.shared.common.reference.JDBC40Translation; 35 36 39 public class TestDbMetaData { 40 41 public static void main(String [] args) { 42 try 43 { 44 ij.getPropertyArg(args); 47 48 Connection conn_main = ij.startJBMS(); 49 50 runTests( conn_main ); 51 } 52 catch (SQLException e) { 53 dumpSQLExceptions(e); 54 } 55 catch (Throwable e) { 56 System.out.println("FAIL -- unexpected exception:"); 57 e.printStackTrace(System.out); 58 } 59 } 60 61 private static void runTests(Connection con) throws Exception { 63 testDatabaseMetaDataMethods(con); 64 testStoredProcEscapeSyntax(con); 65 testAutoCommitFailure(con); 66 con.close(); 67 } 68 69 private static void testDatabaseMetaDataMethods(Connection con) 71 throws Exception 72 { 73 con.setAutoCommit(true); Statement s = con.createStatement(); 75 DatabaseMetaData met = con.getMetaData(); 76 77 if (!met.supportsStoredFunctionsUsingCallSyntax()) { 78 System.out.println 79 ("FAIL: supportsStoredFunctionsUsingCallSyntax() " + 80 "should return true"); 81 } 82 83 if (met.autoCommitFailureClosesAllResultSets()) { 84 System.out.println 85 ("FAIL: autoCommitFailureClosesAllResultSets() " + 86 "should return false"); 87 } 88 89 checkEmptyRS(met.getClientInfoProperties()); 90 91 System.out.println(""+(JDBC40Translation.FUNCTION_PARAMETER_UNKNOWN == 93 DatabaseMetaData.functionColumnUnknown)); 94 System.out.println(""+(JDBC40Translation.FUNCTION_PARAMETER_IN == 95 DatabaseMetaData.functionColumnIn)); 96 System.out.println(""+(JDBC40Translation.FUNCTION_PARAMETER_INOUT == 97 DatabaseMetaData.functionColumnInOut)); 98 System.out.println(""+(JDBC40Translation.FUNCTION_PARAMETER_OUT == 99 DatabaseMetaData.functionColumnOut)); 100 System.out.println(""+(JDBC40Translation.FUNCTION_RETURN == 101 DatabaseMetaData.functionReturn)); 102 103 System.out.println(""+(JDBC40Translation.FUNCTION_NO_NULLS == 104 DatabaseMetaData.functionNoNulls)); 105 System.out.println(""+(JDBC40Translation.FUNCTION_NULLABLE == 106 DatabaseMetaData.functionNullable)); 107 System.out.println(""+(JDBC40Translation.FUNCTION_NULLABLE_UNKNOWN == 108 DatabaseMetaData.functionNullableUnknown)); 109 110 System.out.println(""+(DatabaseMetaData.functionNullable == 116 DatabaseMetaData.procedureNullable)); 117 118 s.execute("CREATE FUNCTION DUMMY1 ( X SMALLINT ) RETURNS SMALLINT "+ 122 "PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA EXTERNAL "+ 123 "NAME 'java.some.func'"); 124 s.execute("CREATE FUNCTION DUMMY2 ( X INTEGER, Y SMALLINT ) RETURNS"+ 125 " INTEGER PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA "+ 126 "EXTERNAL NAME 'java.some.func'"); 127 s.execute("CREATE FUNCTION DUMMY3 ( X VARCHAR(16), Y INTEGER ) "+ 128 "RETURNS VARCHAR(16) PARAMETER STYLE JAVA NO SQL LANGUAGE"+ 129 " JAVA EXTERNAL NAME 'java.some.func'"); 130 s.execute("CREATE FUNCTION DUMMY4 ( X VARCHAR(128), Y INTEGER ) "+ 131 "RETURNS INTEGER PARAMETER STYLE JAVA NO SQL LANGUAGE "+ 132 "JAVA EXTERNAL NAME 'java.some.func'"); 133 134 dumpRS(met.getFunctions(null, null, null)); 136 dumpRS(met.getFunctions("Dummy Catalog", null, null)); 140 dumpRS(met.getFunctions(null, "SYS%", null)); 142 dumpRS(met.getFunctions(null, null, "%GET%")); 145 checkEmptyRS(met.getFunctions("", "", null)); 148 149 dumpRS(met.getFunctionColumns(null,null,"DUMMY%",null)); 152 153 dumpRS(met.getFunctionColumns(null,null,"DUMMY%","")); 155 156 dumpRS(met.getSchemas(null, null)); 158 dumpRS(met.getSchemas(null, "SYS%")); 160 dumpRS(met.getSchemas(null, "APP")); 162 checkEmptyRS(met.getSchemas(null, "BLAH")); 165 166 t_wrapper(met); 167 168 s.close(); 169 } 170 171 176 private static boolean usingEmbeddedClient() { 177 return "embedded".equals( System.getProperty( "framework" ) ); 178 } 179 180 188 private static void testStoredProcEscapeSyntax(Connection con) 189 throws SQLException 190 { 191 con.setAutoCommit(false); 192 String call = "{CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(0)}"; 193 Statement stmt = con.createStatement(); 194 195 boolean success; 196 try { 197 stmt.execute(call); 198 success = true; 199 } catch (SQLException e) { 200 success = false; 201 } 202 203 DatabaseMetaData dmd = con.getMetaData(); 204 boolean supported = dmd.supportsStoredFunctionsUsingCallSyntax(); 205 if (success != supported) { 206 System.out.println("supportsStoredFunctionsUsingCallSyntax() " + 207 "returned " + supported + ", but executing " + 208 call + (success ? " succeeded." : " failed.")); 209 } 210 stmt.close(); 211 con.rollback(); 212 } 213 214 222 private static void testAutoCommitFailure(Connection con) 223 throws SQLException 224 { 225 DatabaseMetaData dmd = con.getMetaData(); 226 boolean shouldBeClosed = dmd.autoCommitFailureClosesAllResultSets(); 227 228 con.setAutoCommit(true); 229 230 Statement s1 = 231 con.createStatement(ResultSet.TYPE_FORWARD_ONLY, 232 ResultSet.CONCUR_READ_ONLY, 233 ResultSet.HOLD_CURSORS_OVER_COMMIT); 234 ResultSet resultSet = s1.executeQuery("VALUES (1, 2), (3, 4)"); 235 236 Statement s2 = con.createStatement(); 237 try { 238 String query = 239 "SELECT dummy, nonexistent, phony FROM imaginarytable34521"; 240 s2.execute(query); 241 System.out.println("\"" + query + "\" is expected to fail, " + 242 "but it didn't."); 243 } catch (SQLException e) { 244 } 246 247 boolean isClosed = resultSet.isClosed(); 248 if (isClosed != shouldBeClosed) { 249 System.out.println("autoCommitFailureClosesAllResultSets() " + 250 "returned " + shouldBeClosed + 251 ", but ResultSet is " + 252 (isClosed ? "closed." : "not closed.")); 253 } 254 resultSet.close(); 255 s1.close(); 256 s2.close(); 257 } 258 259 static private void dumpSQLExceptions (SQLException se) { 260 System.out.println("FAIL -- unexpected exception"); 261 while (se != null) { 262 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 263 se.printStackTrace(System.out); 264 se = se.getNextException(); 265 } 266 } 267 268 static void dumpRS(ResultSet s) throws SQLException { 269 ResultSetMetaData rsmd = s.getMetaData (); 270 271 int numCols = rsmd.getColumnCount (); 273 274 if (numCols <= 0) { 275 System.out.println("(no columns!)"); 276 return; 277 } 278 279 for (int i=1; i<=numCols; i++) { 281 if (i > 1) System.out.print(","); 282 System.out.print(rsmd.getColumnLabel(i)); 283 } 284 System.out.println(); 285 286 while (s.next()) { 288 for (int i=1; i<=numCols; i++) { 291 if (i > 1) System.out.print(","); 292 System.out.print(s.getString(i)); 293 } 294 System.out.println(); 295 } 296 s.close(); 297 } 298 299 303 static void checkEmptyRS(ResultSet rs) throws Exception 304 { 305 boolean passed = false; 306 307 try { 308 if ( rs == null ) 309 { 310 throw new Exception ("Metadata result set can not be null"); 311 } 312 int numrows = 0; 313 while (rs.next()) 314 numrows++; 315 if (numrows != 0) { 317 throw new Exception ("Result set is not empty"); 318 } 319 } 320 catch (SQLException e) 321 { 322 throw new Exception ("Unexpected SQL Exception: " + e.getMessage(), e); 323 } 324 } 325 326 327 336 337 static void t_wrapper(DatabaseMetaData dmd) { 338 Class <DatabaseMetaData > wrap_class = DatabaseMetaData .class; 341 342 try { 344 if(dmd.isWrapperFor(wrap_class)) { 345 DatabaseMetaData dmd1 = 346 (DatabaseMetaData )dmd.unwrap(wrap_class); 347 } 348 else { 349 System.out.println("isWrapperFor wrongly returns false"); 350 } 351 } 352 catch(SQLException sqle) { 353 dumpSQLExceptions(sqle); 354 } 355 356 361 Class <PreparedStatement > wrap_class1 = PreparedStatement .class; 362 363 try { 364 if(dmd.isWrapperFor(wrap_class1)) { 367 System.out.println("isWrapperFor wrongly returns true"); 368 } 369 else { 370 PreparedStatement ps1 = (PreparedStatement ) 371 dmd.unwrap(wrap_class1); 372 System.out.println("unwrap does not throw the expected " + 373 "exception"); 374 } 375 } 376 catch (SQLException sqle) { 377 if(!SQLStateConstants.UNABLE_TO_UNWRAP.equals(sqle.getSQLState())) { 381 sqle.printStackTrace(); 382 } 383 } 384 } 385 } 386 | Popular Tags |