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/01 22:21:40 UTC

svn commit: r1463286 - in /jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc: JenaJdbcStatement.java results/QueryExecutionResults.java results/StreamedResults.java results/TripleIteratorResults.java

Author: rvesse
Date: Mon Apr  1 20:21:40 2013
New Revision: 1463286

URL: http://svn.apache.org/r1463286
Log:
Add missing files from previous commit

Added:
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java
Modified:
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/JenaJdbcStatement.java

Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/JenaJdbcStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/JenaJdbcStatement.java?rev=1463286&r1=1463285&r2=1463286&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/JenaJdbcStatement.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/JenaJdbcStatement.java Mon Apr  1 20:21:40 2013
@@ -31,6 +31,7 @@ import java.util.Queue;
 
 import org.apache.jena.jdbc.results.AskResults;
 import org.apache.jena.jdbc.results.SelectResults;
+import org.apache.jena.jdbc.results.TripleIteratorResults;
 
 import com.hp.hpl.jena.query.Query;
 import com.hp.hpl.jena.query.QueryExecution;
@@ -186,15 +187,14 @@ public abstract class JenaJdbcStatement 
                 this.currResults = new AskResults(this, askRes);
                 return true;
             } else if (q.isDescribeType()) {
-                
+                this.currResults = new TripleIteratorResults(this, qe, qe.execDescribeTriples());
+                return true;
             } else if (q.isConstructType()) {
-                
+                this.currResults = new TripleIteratorResults(this, qe, qe.execConstructTriples());
+                return true;
             } else {
                 throw new SQLException("Unknown SPARQL Query type");
             }
-            // TODO: Implement result set return for other query types
-            qe.close();
-            throw new SQLException("Not yet implemented");
         } catch (Exception e) {
             throw new SQLException("Error occurred during SPARQL query evaluation", e);
         }

Added: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java?rev=1463286&view=auto
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java (added)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java Mon Apr  1 20:21:40 2013
@@ -0,0 +1,69 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.jdbc.results;
+
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import com.hp.hpl.jena.query.QueryExecution;
+
+/**
+ * Abstract base class for result sets that are backed by a {@link QueryExecution} 
+ *
+ */
+public abstract class QueryExecutionResults extends JenaJdbcResultSet {
+
+    private QueryExecution qe;
+
+    /**
+     * Creates new Query Execution backed results
+     * @param statement Statement
+     * @param qe Query Execution
+     * @throws SQLException Thrown if there is an issue creating the results
+     */
+    public QueryExecutionResults(Statement statement, QueryExecution qe) throws SQLException {
+        super(statement);
+        if (qe == null) throw new SQLException("Query Execution cannot be null");
+        this.qe = qe;
+    }
+
+    /**
+     * Closes the results which also closes the underlying {@link QueryExecution}
+     */
+    public final void close() throws SQLException {
+        if (this.qe != null) {
+            this.qe.close();
+            this.qe = null;
+        }
+        this.closeInternal();
+    }
+    
+    /**
+     * Method which derived classes must implement to provide their own close logic
+     * @throws SQLException Thrown if there is an issue closing the results
+     */
+    protected abstract void closeInternal() throws SQLException;
+
+    public final boolean isClosed() throws SQLException {
+        return this.qe == null;
+    }
+    
+    
+
+}

Added: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java?rev=1463286&view=auto
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java (added)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java Mon Apr  1 20:21:40 2013
@@ -0,0 +1,239 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.jdbc.results;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.Statement;
+import com.hp.hpl.jena.query.QueryExecution;
+
+/**
+ * Represents a set of streamed results backed by some {@link QueryExecution}, streamed results are considered to be forward only
+ * @param <T> Type of the underlying result rows
+ *
+ */
+public abstract class StreamedResults<T> extends QueryExecutionResults {
+    
+    private T currItem;
+    private boolean started = false;
+    private boolean finished = false;
+    private int currRow = 0;
+    
+    /**
+     * Creates new select results
+     * @param statement Statement that created the result set
+     * @param qe Query Execution
+     * @throws SQLException Thrown if the arguments are invalid
+     */
+    public StreamedResults(Statement statement, QueryExecution qe) throws SQLException {
+        super(statement, qe);
+    }
+    
+    /**
+     * Gets the current result row (if any)
+     * @return Result row, null if not at a row
+     * @throws SQLException Thrown if the result set is closed
+     */
+    protected T getCurrentRow() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result set is closed");
+        return this.currItem;
+    }
+    
+    /**
+     * Method which derived classes must implement to indicate whether they have further rows available
+     * @return True if further rows are available, false otherwise
+     * @throws SQLException Thrown if an error determining whether rows are available
+     */
+    protected abstract boolean hasNext() throws SQLException;
+    
+    /**
+     * Method which derived classes must implement to provide the next row available
+     * @return Next row available
+     * @throws SQLException Thrown if this method is invoked when no further rows are available
+     */
+    protected abstract T moveNext() throws SQLException;
+
+    public final boolean absolute(int row) throws SQLException {
+        if (this.isClosed()) {
+            throw new SQLException("Cannot move to a row after the result set has been closed");
+        } else if (row == 1) {
+            // Try and move to the first row
+            return this.first();
+        } else if (row == -1) {
+            // Try and move to the last row
+            return this.last();
+        } else if (row <= 0) {
+            //Can't move to an arbitrary relative row from the end of the results
+            throw new SQLException("Jena JDBC result sets are forward only, cannot move to a row which is relative to the end of the result set since the number of result rows is not known in advance");
+        } else if (row == this.currRow) {
+            // Already at the desired row
+            return true;
+        } else if (row < this.currRow) {
+            throw new SQLException("Jena JDBC result sets are forward only, cannot move backwards");
+        } else {
+            // Before the desired row
+            while (this.hasNext() && this.currRow < row) {
+                this.currItem = this.moveNext();
+                this.currRow++;
+            }
+            // If we didn't reach it we hit the end of the result set
+            if (this.currRow < row) {
+                this.finished = true;
+                this.currItem = null;
+            }
+            return (row == this.currRow);
+        }
+    }
+
+    public final void afterLast() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        if (finished) return;
+        
+        // Move to end of results
+        while (this.hasNext()) {
+            this.currItem = this.moveNext();
+            this.currRow++;
+        }
+        this.currItem = null;
+        this.finished = true;
+    }
+
+    public final void beforeFirst() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        //If we've started throw an error as we can't move backwards
+        if (started) throw new SQLException("Jena JDBC result sets are forward only, can't move to before the start of the result set after navigation through the result set has begun");
+        //Otherwise OK
+        this.currItem = null;
+    }
+
+    @Override
+    protected final void closeInternal() throws SQLException {
+        this.currItem = null;
+        this.finished = true;
+        this.closeStreamInternal();
+    }
+    
+    protected abstract void closeStreamInternal() throws SQLException;
+
+    public final boolean first() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        if (this.started && this.currRow == 1) return true;
+        throw new SQLException("Jena JDBC result sets are forward only, can't move backwards to the first row after the first row has been passed");
+    }
+
+    public final int getFetchDirection() throws SQLException {
+        return ResultSet.FETCH_FORWARD;
+    }
+
+    public final int getFetchSize() throws SQLException {
+        // TODO Need a buffering wrapper around ResultSet to make this configurable
+        return 0;
+    }
+
+    public final int getRow() throws SQLException {
+        return this.currRow;
+    }
+
+    public final int getType() throws SQLException {
+        return ResultSet.TYPE_FORWARD_ONLY;
+    }
+
+    public final boolean isAfterLast() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        return this.finished;
+    }
+
+    public final boolean isBeforeFirst() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        return !this.started;
+    }
+
+    public final boolean isFirst() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        return this.currRow == 1;
+    }
+
+    public final boolean isLast() throws SQLException {
+        if (this.isClosed()) throw new SQLException("Result Set is closed");
+        return !this.hasNext();
+    }
+
+    public final boolean last() throws SQLException {
+        if (this.isClosed() || this.finished) {
+            throw new SQLException("Jena JDBC Result Sets are forward-only");
+        } else {
+            while (this.hasNext()) {
+                this.currItem = this.moveNext();
+                this.currRow++;
+            }
+            return true;
+        }
+    }
+
+    public final boolean next() throws SQLException {
+        if (this.isClosed()) {
+            throw new SQLException("Cannot move to the next row in a closed result set");
+        } else {
+            if (this.hasNext()) {
+                this.currItem = this.moveNext();
+                this.currRow++;
+                return true;
+            } else {
+                if (!this.finished) this.currRow++;
+                this.finished = true;
+                return false;
+            }
+        }
+    }
+
+    public final boolean relative(int rows) throws SQLException {
+        if (this.isClosed()) {
+            throw new SQLException("Cannot move to a row after the result set has been closed");
+        } else if (rows == 0) {
+            // Already at the desired row
+            return true;
+        } else if (rows < 0) {
+            throw new SQLException("Jena JDBC result sets are forward only, cannot move backwards");
+        } else {
+            // Before the desired row
+            int moved = 0;
+            while (this.hasNext() && moved < rows) {
+                this.currItem = this.moveNext();
+                this.currRow++;
+                moved++;
+            }
+            // If we didn't reach it we hit the end of the result set
+            if (moved < rows) {
+                this.finished = true;
+                this.currItem = null;
+            }
+            return (rows == moved);
+        }
+    }
+
+    public final void setFetchDirection(int direction) throws SQLException {
+        if (direction != ResultSet.FETCH_FORWARD) throw new SQLFeatureNotSupportedException("Jena JDBC Result Sets only support forward fetch");
+    }
+
+    public final void setFetchSize(int rows) throws SQLException {
+        // TODO Need to provide some buffering wrapper over a ResultSet to make this possible
+        throw new SQLFeatureNotSupportedException();
+    }
+}

Added: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java?rev=1463286&view=auto
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java (added)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java Mon Apr  1 20:21:40 2013
@@ -0,0 +1,137 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.jdbc.results;
+
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Iterator;
+
+import org.apache.jena.atlas.lib.Closeable;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.sparql.core.Var;
+
+/**
+ * Represents results of a CONSTRUCT/DESCRIBE query where the results are
+ * streamed
+ * 
+ */
+public class TripleIteratorResults extends StreamedResults<Triple> {
+
+    public static final int NUM_COLUMNS = 3;
+
+    public static final String COLUMN_LABEL_SUBJECT = "Subject";
+    public static final String COLUMN_LABEL_PREDICATE = "Subject";
+    public static final String COLUMN_LABEL_OBJECT = "Subject";
+
+    private Iterator<Triple> triples;
+
+    /**
+     * Creates a new result set which is backed by a triple iterator
+     * 
+     * @param statement
+     *            Statement
+     * @param qe
+     *            Query Execution
+     * @param ts
+     *            Triple Iterator
+     * @throws SQLException
+     *             Thrown if there is a problem creating the results
+     */
+    public TripleIteratorResults(Statement statement, QueryExecution qe, Iterator<Triple> ts) throws SQLException {
+        super(statement, qe);
+        if (ts == null)
+            throw new SQLException("Triple Iterator cannot be null");
+        this.triples = ts;
+    }
+
+    public int findColumn(String columnLabel) throws SQLException {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    @Override
+    protected boolean hasNext() throws SQLException {
+        // No null check here because superclass will not call us after we are
+        // closed and set to null
+        return this.triples.hasNext();
+    }
+
+    @Override
+    protected Triple moveNext() throws SQLException {
+        return this.triples.next();
+    }
+
+    @Override
+    protected void closeStreamInternal() throws SQLException {
+        if (this.triples != null) {
+            if (this.triples instanceof Closeable) {
+                ((Closeable) this.triples).close();
+            }
+            this.triples = null;
+        }
+    }
+
+    @Override
+    public ResultSetMetaData getMetaData() throws SQLException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    protected String findColumnLabel(int columnIndex) throws SQLException {
+        if (this.isClosed())
+            throw new SQLException("Result Set is closed");
+        if (columnIndex >= 1 && columnIndex <= NUM_COLUMNS) {
+            switch (columnIndex) {
+            case 1:
+                return COLUMN_LABEL_SUBJECT;
+            case 2:
+                return COLUMN_LABEL_PREDICATE;
+            case 3:
+                return COLUMN_LABEL_OBJECT;
+            default:
+                throw new SQLException("Column Index is out of bounds");
+            }
+        } else {
+            throw new SQLException("Column Index is out of bounds");
+        }
+    }
+
+    @Override
+    protected Node getNode(String columnLabel) throws SQLException {
+        if (this.isClosed())
+            throw new SQLException("Result Set is closed");
+        if (this.getCurrentRow() == null)
+            throw new SQLException("Not currently at a row");
+        Triple t = this.getCurrentRow();
+        if (COLUMN_LABEL_SUBJECT.equals(columnLabel)) {
+            return t.getSubject();
+        } else if (COLUMN_LABEL_PREDICATE.equals(columnLabel)) {
+            return t.getPredicate();
+        } else if (COLUMN_LABEL_OBJECT.equals(columnLabel)) {
+            return t.getObject();
+        } else {
+            throw new SQLException("Unknown column label");
+        }
+    }
+}