You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2013/04/05 23:54:08 UTC

svn commit: r1465139 - in /jena/Experimental/jena-jdbc/jena-jdbc-core/src: main/java/org/apache/jena/jdbc/results/SelectResults.java test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java

Author: rvesse
Date: Fri Apr  5 21:54:07 2013
New Revision: 1465139

URL: http://svn.apache.org/r1465139
Log:
Fix a bug with losing the first row of results when detecting column types

Modified:
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java

Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java?rev=1465139&r1=1465138&r2=1465139&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/SelectResults.java Fri Apr  5 21:54:07 2013
@@ -29,8 +29,10 @@ import org.apache.jena.jdbc.statements.J
 
 import com.hp.hpl.jena.graph.Node;
 import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.query.ResultSetFactory;
 import com.hp.hpl.jena.sparql.core.Var;
 import com.hp.hpl.jena.sparql.engine.binding.Binding;
+import com.hp.hpl.jena.sparql.resultset.ResultSetPeekable;
 
 /**
  * Represents SPARQL SELECT results
@@ -38,7 +40,7 @@ import com.hp.hpl.jena.sparql.engine.bin
  */
 public class SelectResults extends StreamedResults<Binding> {
 
-    private com.hp.hpl.jena.query.ResultSet innerResults;
+    private ResultSetPeekable innerResults;
     private List<String> columns;
     private SelectResultsMetadata metadata;
 
@@ -56,7 +58,7 @@ public class SelectResults extends Strea
      * @throws SQLException
      *             Thrown if the arguments are invalid
      */
-    public SelectResults(JenaStatement statement, QueryExecution qe, com.hp.hpl.jena.query.ResultSet results, boolean commit)
+    public SelectResults(JenaStatement statement, QueryExecution qe, ResultSetPeekable results, boolean commit)
             throws SQLException {
         super(statement, qe, commit);
         if (results == null)
@@ -65,6 +67,24 @@ public class SelectResults extends Strea
         this.columns = new ArrayList<String>(this.innerResults.getResultVars());
         this.metadata = new SelectResultsMetadata(this, this.innerResults);
     }
+    
+    /**
+     * Creates new select results
+     * 
+     * @param statement
+     *            Statement that created the result set
+     * @param qe
+     *            Query Execution
+     * @param results
+     *            SPARQL Results
+     * @param commit
+     *            Whether a commit is necessary when the results are closed
+     * @throws SQLException
+     *             Thrown if the arguments are invalid
+     */
+    public SelectResults(JenaStatement statement, QueryExecution qe, com.hp.hpl.jena.query.ResultSet results, boolean commit) throws SQLException {
+        this(statement, qe, ResultSetFactory.makePeekable(results), commit);
+    }
 
     @Override
     public void closeStreamInternal() throws SQLException {

Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java?rev=1465139&r1=1465138&r2=1465139&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java Fri Apr  5 21:54:07 2013
@@ -24,6 +24,7 @@ import java.sql.SQLException;
 import java.sql.Statement;
 import java.sql.Types;
 
+import org.apache.jena.jdbc.JdbcCompatibility;
 import org.apache.jena.jdbc.connections.JenaConnection;
 import org.apache.jena.jdbc.results.metadata.TripleResultsMetadata;
 import org.apache.jena.jdbc.utils.TestUtils;
@@ -117,27 +118,29 @@ public abstract class AbstractJenaConnec
         conn.close();
         Assert.assertTrue(conn.isClosed());
     }
-    
+
     /**
      * Trying to retrieve a statement from a closed connection is an error
      * 
      * @throws SQLException
      */
-    @Test(expected=SQLException.class)
+    @Test(expected = SQLException.class)
     public void connection_get_statement_02() throws SQLException {
         JenaConnection conn = this.getConnection();
         conn.close();
         Assert.assertTrue(conn.isClosed());
-        
-        // Trying to create a statement after the connection is closed is an error
+
+        // Trying to create a statement after the connection is closed is an
+        // error
         conn.createStatement();
     }
-    
+
     /**
      * Trying to use a statement from a closed connection is an error
+     * 
      * @throws SQLException
      */
-    @Test(expected=SQLException.class)
+    @Test(expected = SQLException.class)
     public void connection_get_statement_03() throws SQLException {
         JenaConnection conn = this.getConnection();
         Statement stmt = conn.createStatement();
@@ -145,7 +148,7 @@ public abstract class AbstractJenaConnec
         Assert.assertFalse(stmt.isClosed());
         conn.close();
         Assert.assertTrue(conn.isClosed());
-        
+
         // Trying to use a statement after the connection is closed is an error
         stmt.execute("SELECT * { ?s ?p ?o }");
     }
@@ -269,10 +272,56 @@ public abstract class AbstractJenaConnec
         conn.close();
         Assert.assertTrue(conn.isClosed());
     }
-    
+
     /**
-     * Runs a SELECT query on a non-empty database with max rows set and checks that
-     * the appropriate number of rows are returned
+     * Runs a SELECT query on a non-empty database and checks it returns
+     * non-empty results. Uses high compatibility level to ensure that column
+     * type detection doesn't consume the first row.
+     * 
+     * @throws SQLException
+     */
+    @Test
+    public void connection_statement_query_select_04() throws SQLException {
+        // Prepare a dataset
+        Dataset ds = DatasetFactory.createMem();
+        ds.asDatasetGraph().add(
+                new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
+                        NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
+
+        // Work with the connection
+        JenaConnection conn = this.getConnection(ds);
+        conn.setJdbcCompatibilityLevel(JdbcCompatibility.HIGH);
+        Statement stmt = conn.createStatement();
+        ResultSet rset = stmt.executeQuery("SELECT * WHERE { GRAPH ?g { ?s ?p ?o } }");
+        Assert.assertNotNull(rset);
+        Assert.assertFalse(rset.isClosed());
+        Assert.assertTrue(rset.isBeforeFirst());
+
+        // Check result set metadata
+        checkSelectMetadata(rset, 4);
+
+        // Should have a row
+        Assert.assertTrue(rset.next());
+        Assert.assertTrue(rset.isFirst());
+        Assert.assertEquals(1, rset.getRow());
+
+        // Should be no further rows
+        Assert.assertFalse(rset.next());
+        Assert.assertTrue(rset.isAfterLast());
+        Assert.assertFalse(rset.isClosed());
+
+        // Close things
+        rset.close();
+        Assert.assertTrue(rset.isClosed());
+        stmt.close();
+        Assert.assertTrue(stmt.isClosed());
+        conn.close();
+        Assert.assertTrue(conn.isClosed());
+    }
+
+    /**
+     * Runs a SELECT query on a non-empty database with max rows set and checks
+     * that the appropriate number of rows are returned
      * 
      * @throws SQLException
      */
@@ -313,10 +362,10 @@ public abstract class AbstractJenaConnec
         conn.close();
         Assert.assertTrue(conn.isClosed());
     }
-    
+
     /**
-     * Runs a SELECT query on a non-empty database with max rows set and checks that
-     * the appropriate number of rows are returned
+     * Runs a SELECT query on a non-empty database with max rows set and checks
+     * that the appropriate number of rows are returned
      * 
      * @throws SQLException
      */
@@ -328,8 +377,9 @@ public abstract class AbstractJenaConnec
         // Work with the connection
         JenaConnection conn = this.getConnection(ds);
         Statement stmt = conn.createStatement();
-        
-        // Set max rows to 10, note that if the query specifies a lower limit then that is respected
+
+        // Set max rows to 10, note that if the query specifies a lower limit
+        // then that is respected
         stmt.setMaxRows(10);
         ResultSet rset = stmt.executeQuery("SELECT * WHERE { GRAPH ?g { ?s ?p ?o } } LIMIT 1");
         Assert.assertNotNull(rset);
@@ -567,9 +617,10 @@ public abstract class AbstractJenaConnec
             Assert.assertEquals(Types.NVARCHAR, metadata.getColumnType(i));
         }
     }
-    
+
     /**
      * Does a basic read transaction
+     * 
      * @throws SQLException
      */
     @Test
@@ -580,18 +631,18 @@ public abstract class AbstractJenaConnec
         Assume.assumeTrue(conn.getMetaData().supportsTransactions());
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
         conn.setAutoCommit(true);
-        
+
         // Make a read operation
         Statement stmt = conn.createStatement();
         ResultSet rset = stmt.executeQuery("SELECT * WHERE { ?s ?p ?o }");
         Assert.assertNotNull(rset);
         Assert.assertFalse(rset.isClosed());
         Assert.assertTrue(rset.isBeforeFirst());
-        
+
         // Should have no rows
         Assert.assertFalse(rset.next());
         Assert.assertTrue(rset.isAfterLast());
-        
+
         // Close things
         rset.close();
         Assert.assertTrue(rset.isClosed());
@@ -600,9 +651,10 @@ public abstract class AbstractJenaConnec
         conn.close();
         Assert.assertTrue(conn.isClosed());
     }
-    
+
     /**
      * Does a basic write transaction
+     * 
      * @throws SQLException
      */
     @Test
@@ -614,21 +666,21 @@ public abstract class AbstractJenaConnec
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
         conn.setAutoCommit(true);
         conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
-        
+
         // Make a write operation
         Statement stmt = conn.createStatement();
         stmt.executeUpdate("INSERT DATA { <http://x> <http://y> <http://z> }");
-        
+
         // Make a subsequent read, with auto-commit we should see some data
         ResultSet rset = stmt.executeQuery("SELECT * WHERE { ?s ?p ?o }");
         Assert.assertNotNull(rset);
         Assert.assertFalse(rset.isClosed());
         Assert.assertTrue(rset.isBeforeFirst());
-        
+
         // Should have one row
         Assert.assertTrue(rset.next());
         Assert.assertFalse(rset.next());
-        
+
         // Close things
         rset.close();
         Assert.assertTrue(rset.isClosed());
@@ -637,9 +689,10 @@ public abstract class AbstractJenaConnec
         conn.close();
         Assert.assertTrue(conn.isClosed());
     }
-    
+
     /**
      * Does a basic write transaction without auto-commit and then commits it
+     * 
      * @throws SQLException
      */
     @Test
@@ -651,17 +704,17 @@ public abstract class AbstractJenaConnec
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
         conn.setAutoCommit(false);
         conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
-        
+
         // Make a write operation
         Statement stmt = conn.createStatement();
         stmt.executeUpdate("INSERT DATA { <http://x> <http://y> <http://z> }");
-        
+
         // Make a subsequent read, with auto-commit we should see some data
         ResultSet rset = stmt.executeQuery("SELECT * WHERE { ?s ?p ?o }");
         Assert.assertNotNull(rset);
         Assert.assertFalse(rset.isClosed());
         Assert.assertTrue(rset.isBeforeFirst());
-        
+
         // Should have one row
         Assert.assertTrue(rset.next());
         Assert.assertFalse(rset.next());
@@ -670,28 +723,29 @@ public abstract class AbstractJenaConnec
 
         // Commit the transaction
         conn.commit();
-        
+
         // Check we still can read the data
         rset = stmt.executeQuery("SELECT * WHERE { ?s ?p ?o }");
         Assert.assertNotNull(rset);
         Assert.assertFalse(rset.isClosed());
         Assert.assertTrue(rset.isBeforeFirst());
-        
+
         // Should have one row
         Assert.assertTrue(rset.next());
         Assert.assertFalse(rset.next());
         rset.close();
         Assert.assertTrue(rset.isClosed());
-        
+
         // Close things
         stmt.close();
         Assert.assertTrue(stmt.isClosed());
         conn.close();
         Assert.assertTrue(conn.isClosed());
     }
-    
+
     /**
      * Does a basic write transaction without auto-commit and then rolls it back
+     * 
      * @throws SQLException
      */
     @Test
@@ -703,17 +757,17 @@ public abstract class AbstractJenaConnec
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
         conn.setAutoCommit(false);
         conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
-        
+
         // Make a write operation
         Statement stmt = conn.createStatement();
         stmt.executeUpdate("INSERT DATA { <http://x> <http://y> <http://z> }");
-        
+
         // Make a subsequent read, with auto-commit we should see some data
         ResultSet rset = stmt.executeQuery("SELECT * WHERE { ?s ?p ?o }");
         Assert.assertNotNull(rset);
         Assert.assertFalse(rset.isClosed());
         Assert.assertTrue(rset.isBeforeFirst());
-        
+
         // Should have one row
         Assert.assertTrue(rset.next());
         Assert.assertFalse(rset.next());
@@ -722,18 +776,18 @@ public abstract class AbstractJenaConnec
 
         // Rollback the transaction
         conn.rollback();
-        
+
         // Check we can no longer read the data
         rset = stmt.executeQuery("SELECT * WHERE { ?s ?p ?o }");
         Assert.assertNotNull(rset);
         Assert.assertFalse(rset.isClosed());
         Assert.assertTrue(rset.isBeforeFirst());
-        
+
         // Should have no rows
         Assert.assertFalse(rset.next());
         rset.close();
         Assert.assertTrue(rset.isClosed());
-        
+
         // Close things
         stmt.close();
         Assert.assertTrue(stmt.isClosed());