db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r382710 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ client/org/apache/derby/client/net/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/
Date Fri, 03 Mar 2006 07:52:29 GMT
Author: kahatlen
Date: Thu Mar  2 23:52:28 2006
New Revision: 382710

URL: http://svn.apache.org/viewcvs?rev=382710&view=rev
Log:
DERBY-948: Miscellaneous ResultSet methods added by JDBC 4

Implemented new methods defined by the JDBC 4 spec:

  * ResultSet.getHoldability()
  * ResultSet.isClosed()

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java
    db/derby/code/trunk/java/client/org/apache/derby/client/net/NetResultSet40.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet40.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java?rev=382710&r1=382709&r2=382710&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java Thu Mar  2 23:52:28
2006
@@ -5243,4 +5243,49 @@
             "JDBC 3 method called - not yet supported").getSQLException();
     }
     
+    // -------------------------- JDBC 4.0 --------------------------
+
+    /**
+     * Retrieves the holdability for this <code>ResultSet</code>
+     * object.
+     *
+     * @return either <code>ResultSet.HOLD_CURSORS_OVER_COMMIT</code>
+     * or <code>ResultSet.CLOSE_CURSORS_AT_COMMIT</code>
+     * @exception SQLException if a database error occurs
+     */
+    public final int getHoldability() throws SQLException {
+        if (agent_.loggingEnabled()) {
+            agent_.logWriter_.traceEntry(this, "getHoldability");
+        }
+        try {
+            checkForClosedResultSet();
+        } catch (SqlException se) {
+            throw se.getSQLException();
+        }
+        if (agent_.loggingEnabled()) {
+            agent_.logWriter_.traceExit(this, "getHoldability",
+                                        resultSetHoldability_);
+        }
+        return resultSetHoldability_;
+    }
+    
+    /**
+     * Checks whether this <code>ResultSet</code> object has been
+     * closed, either automatically or because <code>close()</code>
+     * has been called.
+     *
+     * @return <code>true</code> if the <code>ResultSet</code> is
+     * closed, <code>false</code> otherwise
+     * @exception SQLException if a database error occurs
+     */
+    public final boolean isClosed() throws SQLException {
+        if (agent_.loggingEnabled()) {
+            agent_.logWriter_.traceEntry(this, "isClosed");
+        }
+        final boolean isClosed = !openOnClient_;
+        if (agent_.loggingEnabled()) {
+            agent_.logWriter_.traceExit(this, "isClosed", isClosed);
+        }
+        return isClosed;
+    }
 }

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/NetResultSet40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/client/org/apache/derby/client/net/NetResultSet40.java?rev=382710&r1=382709&r2=382710&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/NetResultSet40.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/NetResultSet40.java Thu Mar
 2 23:52:28 2006
@@ -65,14 +65,6 @@
         throw SQLExceptionFactory.notImplemented ("updateRowId (String, RowId)");
     }
     
-    public int getHoldability() throws SQLException {
-        throw SQLExceptionFactory.notImplemented ("getHoldability ()");
-    }
-    
-    public boolean isClosed() throws SQLException {
-        throw SQLExceptionFactory.notImplemented ("isClosed ()");
-    }
-    
     public void updateNString(int columnIndex, String nString) throws SQLException {
         throw SQLExceptionFactory.notImplemented ("updateNString (int, String)");
     }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet40.java?rev=382710&r1=382709&r2=382710&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet40.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet40.java Thu Mar
 2 23:52:28 2006
@@ -25,7 +25,11 @@
 import java.sql.SQLException;
 import java.sql.SQLXML;
 import org.apache.derby.iapi.sql.ResultSet;
+import java.sql.Statement;
 
+/**
+ * Implementation of JDBC 4 specific ResultSet methods.
+ */
 public class EmbedResultSet40 extends org.apache.derby.impl.jdbc.EmbedResultSet20{
     
     /** Creates a new instance of EmbedResultSet40 */
@@ -56,12 +60,40 @@
         throw Util.notImplemented();
     }
     
-    public int getHoldability() throws SQLException {
-        throw Util.notImplemented();
-    }
-    
-    public boolean isClosed() throws SQLException {
-        throw Util.notImplemented();
+    /**
+     * Retrieves the holdability for this <code>ResultSet</code>
+     * object.
+     *
+     * @return either <code>ResultSet.HOLD_CURSORS_OVER_COMMIT</code>
+     * or <code>ResultSet.CLOSE_CURSORS_AT_COMMIT</code>
+     * @exception SQLException if a database error occurs
+     */
+    public final int getHoldability() throws SQLException {
+        checkIfClosed("getHoldability");
+        Statement statement = getStatement();
+        if (statement == null) {
+            // If statement is null, the result set is an internal
+            // result set created by getNewRowSet() or getOldRowSet()
+            // in InternalTriggerExecutionContext. These result sets
+            // are not exposed to the JDBC applications. Returning
+            // CLOSE_CURSORS_AT_COMMIT since the result set will be
+            // closed when the trigger has finished.
+            return CLOSE_CURSORS_AT_COMMIT;
+        }
+        return statement.getResultSetHoldability();
+    }
+    
+    /**
+     * Checks whether this <code>ResultSet</code> object has been
+     * closed, either automatically or because <code>close()</code>
+     * has been called.
+     *
+     * @return <code>true</code> if the <code>ResultSet</code> is
+     * closed, <code>false</code> otherwise
+     * @exception SQLException if a database error occurs
+     */
+    public final boolean isClosed() throws SQLException {
+        return isClosed;
     }
     
     public void updateNString(int columnIndex, String nString) throws SQLException {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java?rev=382710&r1=382709&r2=382710&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java
Thu Mar  2 23:52:28 2006
@@ -25,9 +25,11 @@
 import java.io.Reader;
 import java.io.InputStream;
 import java.sql.Connection;
+import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Statement;
 import org.apache.derby.shared.common.reference.SQLState;
 
 /**
@@ -40,6 +42,19 @@
     PreparedStatement ps=null;
     ResultSet rs=null;
     
+    /**
+     * Checks that a <code>boolean</code> value is
+     * <code>true</code>. Throws an exception if it is false.
+     *
+     * @param expr boolean expected to be true
+     * @param msg message when assertion fails
+     * @exception RuntimeException if <code>expr</code> is false
+     */
+    private static void assert_(boolean expr, String msg) {
+        if (!expr) {
+            throw new RuntimeException("Assertion failed: " + msg);
+        }
+    }
     
     void t_getRowId1() {
         try {
@@ -100,13 +115,40 @@
         }
     }
     
+    /**
+     * Tests that <code>ResultSet.getHoldability()</code> has the
+     * correct behaviour.
+     */
     void t_getHoldability() {
         try {
-            int i = rs.getHoldability();
-            System.out.println("unImplemented Exception not thrown in code");
-        } catch(SQLException e) {
-            if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
-                System.out.println("Unexpected SQLException"+e);
+            // test default holdability
+            Statement stmt = conn.createStatement();
+            ResultSet rs = stmt.executeQuery("values(1)");
+            assert_(rs.getHoldability() == ResultSet.HOLD_CURSORS_OVER_COMMIT,
+                    "default holdability is HOLD_CURSORS_OVER_COMMIT");
+            rs.close();
+            try {
+                rs.getHoldability();
+                assert_(false, "getHoldability() should fail when closed");
+            } catch (SQLException sqle) {
+                String sqlState = sqle.getSQLState();
+                // client driver throws exception with SQL state null
+                // when result set is closed
+                if (sqlState != null &&
+                    !sqlState.equals("XCL16")) {
+                    throw sqle;
+                }
+            }
+            stmt.close();
+            // test explicitly set holdability
+            for (int h : new int[] { ResultSet.HOLD_CURSORS_OVER_COMMIT,
+                                     ResultSet.CLOSE_CURSORS_AT_COMMIT }) {
+                stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
+                                            ResultSet.CONCUR_READ_ONLY, h);
+                rs = stmt.executeQuery("values(1)");
+                assert_(rs.getHoldability() == h, "holdability " + h);
+                rs.close();
+                stmt.close();
             }
             
         } catch(Exception e) {
@@ -115,14 +157,59 @@
         }
     }
     
+    /**
+     * Tests that <code>ResultSet.isClosed()</code> returns the
+     * correct value in different situations.
+     */
     void t_isClosed(){
         try {
-            boolean b = rs.isClosed();
-            System.out.println("unImplemented Exception not thrown in code");
-        } catch(SQLException e) {
-            if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
-                System.out.println("Unexpected SQLException"+e);
-            }
+            Statement stmt = conn.createStatement();
+
+            // simple open/read/close test
+            ResultSet rs = stmt.executeQuery("values(1)");
+            assert_(!rs.isClosed(), "rs should be open");
+            while (rs.next());
+            assert_(!rs.isClosed(), "rs should be open");
+            rs.close();
+            assert_(rs.isClosed(), "rs should be closed");
+
+            // execute and re-execute statement
+            rs = stmt.executeQuery("values(1)");
+            assert_(!rs.isClosed(), "rs should be open");
+            ResultSet rs2 = stmt.executeQuery("values(1)");
+            assert_(rs.isClosed(), "rs should be closed");
+            assert_(!rs2.isClosed(), "rs2 should be open");
+
+            // re-execute another statement on the same connection
+            Statement stmt2 = conn.createStatement();
+            rs = stmt2.executeQuery("values(1)");
+            assert_(!rs2.isClosed(), "rs2 should be open");
+            assert_(!rs.isClosed(), "rs should be open");
+
+            // retrieve multiple result sets
+            stmt.execute("create procedure retrieve_result_sets() " +
+                         "parameter style java language java external name " +
+                         "'org.apache.derbyTesting.functionTests.tests." +
+                         "jdbc4.TestResultSetMethods.threeResultSets' " +
+                         "dynamic result sets 3 reads sql data");
+            stmt.execute("call retrieve_result_sets()");
+            ResultSet[] rss = new ResultSet[3];
+            int count = 0;
+            do {
+                rss[count] = stmt.getResultSet();
+                assert_(!rss[count].isClosed(),
+                        "rss[" + count + "] should be open");
+                if (count > 0) {
+                    assert_(rss[count-1].isClosed(),
+                            "rss[" + (count-1) + "] should be closed");
+                }
+                ++count;
+            } while (stmt.getMoreResults());
+            assert_(count == 3, "expected three result sets");
+            stmt.execute("drop procedure retrieve_result_sets");
+
+            stmt.close();
+            stmt2.close();
             
         } catch(Exception e) {
             System.out.println("Unexpected exception caught"+e);
@@ -286,6 +373,29 @@
         t_updateSQLXML2();
     }
     
+    /**
+     * Method that is invoked by <code>t_isClosed()</code> (as a
+     * stored procedure) to retrieve three result sets.
+     *
+     * @param rs1 first result set
+     * @param rs2 second result set
+     * @param rs3 third result set
+     * @exception SQLException if a database error occurs
+     */
+    public static void threeResultSets(ResultSet[] rs1,
+                                       ResultSet[] rs2,
+                                       ResultSet[] rs3)
+        throws SQLException
+    {
+        Connection c = DriverManager.getConnection("jdbc:default:connection");
+        Statement stmt1 = c.createStatement();
+        rs1[0] = stmt1.executeQuery("values(1)");
+        Statement stmt2 = c.createStatement();
+        rs2[0] = stmt2.executeQuery("values(1)");
+        Statement stmt3 = c.createStatement();
+        rs3[0] = stmt3.executeQuery("values(1)");
+        c.close();
+    }
     
     public static void main(String args[]) {
         TestConnection tc=null;



Mime
View raw message