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/04 23:05:52 UTC
svn commit: r1464765 - in /jena/Experimental/jena-jdbc:
jena-jdbc-core/src/main/java/org/apache/jena/jdbc/connections/
jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/
jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/ jena-j...
Author: rvesse
Date: Thu Apr 4 21:05:52 2013
New Revision: 1464765
URL: http://svn.apache.org/r1464765
Log:
More plumbing towards JDBC compatibility levels
Added:
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java
Removed:
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaJdbcStatement.java
Modified:
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/connections/JenaConnection.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/AskResults.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaResultSet.java
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/SelectResults.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
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/AskResultsMetadata.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/JenaResultsMetadata.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/SelectResultsMetadata.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/TripleResultsMetadata.java
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/DatasetStatement.java
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/connections/JenaConnection.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/connections/JenaConnection.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/connections/JenaConnection.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/connections/JenaConnection.java Thu Apr 4 21:05:52 2013
@@ -102,6 +102,17 @@ public abstract class JenaConnection imp
public int getJdbcCompatibilityLevel() {
return this.compatibilityLevel;
}
+
+ /**
+ * Sets the JDBC compatibility level that is in use, see {@link JdbcCompatibility} for explanations.
+ * <p>
+ * Changing the level may not effect existing open objects, behaviour in this case will be implementation specific.
+ * </p>
+ * @param level Compatibility level
+ */
+ public void setJdbcCompatibilityLevel(int level) {
+ this.compatibilityLevel = JdbcCompatibility.normalizeLevel(level);
+ }
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
throw new SQLFeatureNotSupportedException();
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/AskResults.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/AskResults.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/AskResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/AskResults.java Thu Apr 4 21:05:52 2013
@@ -22,9 +22,8 @@ import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
-import java.sql.Statement;
-
import org.apache.jena.jdbc.results.metadata.AskResultsMetadata;
+import org.apache.jena.jdbc.statements.JenaStatement;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.graph.Node;
@@ -39,6 +38,7 @@ public class AskResults extends JenaResu
private boolean result, closed = false;
private int currRow = 0;
private boolean needsCommit = false;
+ private AskResultsMetadata metadata;
/**
* Creates a new ASK result
@@ -47,10 +47,11 @@ public class AskResults extends JenaResu
* @param commit Whether a commit is required when the results are closed
* @throws SQLException Thrown if the arguments are invalid
*/
- public AskResults(Statement statement, boolean result, boolean commit) throws SQLException {
+ public AskResults(JenaStatement statement, boolean result, boolean commit) throws SQLException {
super(statement);
this.result = result;
this.needsCommit = commit;
+ this.metadata = new AskResultsMetadata(this);
}
public boolean absolute(int row) throws SQLException {
@@ -177,7 +178,7 @@ public class AskResults extends JenaResu
@Override
public ResultSetMetaData getMetaData() throws SQLException {
- return new AskResultsMetadata();
+ return this.metadata;
}
@Override
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaResultSet.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaResultSet.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaResultSet.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaResultSet.java Thu Apr 4 21:05:52 2013
@@ -41,6 +41,7 @@ import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;
+import org.apache.jena.jdbc.statements.JenaStatement;
import org.apache.jena.jdbc.utils.JenaJdbcNodeUtils;
import com.hp.hpl.jena.graph.Node;
@@ -67,7 +68,7 @@ public abstract class JenaResultSet impl
* @throws SQLException
* Thrown if the arguments are invalid
*/
- public JenaResultSet(Statement statement) throws SQLException {
+ public JenaResultSet(JenaStatement statement) throws SQLException {
if (statement == null)
throw new SQLException("Statement for a Result Set cannot be null");
this.statement = statement;
Modified: 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=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/QueryExecutionResults.java Thu Apr 4 21:05:52 2013
@@ -19,7 +19,7 @@
package org.apache.jena.jdbc.results;
import java.sql.SQLException;
-import java.sql.Statement;
+import org.apache.jena.jdbc.statements.JenaStatement;
import com.hp.hpl.jena.query.QueryExecution;
@@ -39,7 +39,7 @@ public abstract class QueryExecutionResu
* @param commit Whether a commit is needed when the results are closed
* @throws SQLException Thrown if there is an issue creating the results
*/
- public QueryExecutionResults(Statement statement, QueryExecution qe, boolean commit) throws SQLException {
+ public QueryExecutionResults(JenaStatement statement, QueryExecution qe, boolean commit) throws SQLException {
super(statement);
if (qe == null) throw new SQLException("Query Execution cannot be null");
this.qe = qe;
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=1464765&r1=1464764&r2=1464765&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 Thu Apr 4 21:05:52 2013
@@ -20,12 +20,12 @@ package org.apache.jena.jdbc.results;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.jena.atlas.lib.Closeable;
import org.apache.jena.jdbc.results.metadata.SelectResultsMetadata;
+import org.apache.jena.jdbc.statements.JenaStatement;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.QueryExecution;
@@ -40,6 +40,7 @@ public class SelectResults extends Strea
private com.hp.hpl.jena.query.ResultSet innerResults;
private List<String> columns;
+ private SelectResultsMetadata metadata;
/**
* Creates new select results
@@ -55,13 +56,14 @@ public class SelectResults extends Strea
* @throws SQLException
* Thrown if the arguments are invalid
*/
- public SelectResults(Statement statement, QueryExecution qe, com.hp.hpl.jena.query.ResultSet results, boolean commit)
+ public SelectResults(JenaStatement statement, QueryExecution qe, com.hp.hpl.jena.query.ResultSet results, boolean commit)
throws SQLException {
super(statement, qe, commit);
if (results == null)
throw new SQLException("SPARQL Results cannot be null");
this.innerResults = results;
this.columns = new ArrayList<String>(this.innerResults.getResultVars());
+ this.metadata = new SelectResultsMetadata(this, this.innerResults, this.columns);
}
@Override
@@ -108,7 +110,7 @@ public class SelectResults extends Strea
@Override
public ResultSetMetaData getMetaData() throws SQLException {
- return new SelectResultsMetadata(this.innerResults, this.columns);
+ return this.metadata;
}
/**
Modified: 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=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/StreamedResults.java Thu Apr 4 21:05:52 2013
@@ -21,7 +21,8 @@ package org.apache.jena.jdbc.results;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
-import java.sql.Statement;
+import org.apache.jena.jdbc.statements.JenaStatement;
+
import com.hp.hpl.jena.query.QueryExecution;
/**
@@ -43,7 +44,7 @@ public abstract class StreamedResults<T>
* @param commit Whether a commit is necessary when the results are closed
* @throws SQLException Thrown if the arguments are invalid
*/
- public StreamedResults(Statement statement, QueryExecution qe, boolean commit) throws SQLException {
+ public StreamedResults(JenaStatement statement, QueryExecution qe, boolean commit) throws SQLException {
super(statement, qe, commit);
}
Modified: 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=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/TripleIteratorResults.java Thu Apr 4 21:05:52 2013
@@ -20,11 +20,11 @@ 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 org.apache.jena.jdbc.results.metadata.TripleResultsMetadata;
+import org.apache.jena.jdbc.statements.JenaStatement;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
@@ -37,7 +37,7 @@ import com.hp.hpl.jena.query.QueryExecut
*/
public class TripleIteratorResults extends StreamedResults<Triple> {
- private static final TripleResultsMetadata metadata = new TripleResultsMetadata();
+ private TripleResultsMetadata metadata;
private Iterator<Triple> triples;
@@ -55,11 +55,12 @@ public class TripleIteratorResults exten
* @throws SQLException
* Thrown if there is a problem creating the results
*/
- public TripleIteratorResults(Statement statement, QueryExecution qe, Iterator<Triple> ts, boolean commit) throws SQLException {
+ public TripleIteratorResults(JenaStatement statement, QueryExecution qe, Iterator<Triple> ts, boolean commit) throws SQLException {
super(statement, qe, commit);
if (ts == null)
throw new SQLException("Triple Iterator cannot be null");
this.triples = ts;
+ this.metadata = new TripleResultsMetadata(this);
}
public int findColumn(String columnLabel) throws SQLException {
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/AskResultsMetadata.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/AskResultsMetadata.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/AskResultsMetadata.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/AskResultsMetadata.java Thu Apr 4 21:05:52 2013
@@ -21,18 +21,35 @@ package org.apache.jena.jdbc.results.met
import java.sql.SQLException;
import java.sql.Types;
+import org.apache.jena.jdbc.JdbcCompatibility;
import org.apache.jena.jdbc.results.AskResults;
+import org.apache.jena.jdbc.results.JenaResultSet;
/**
* Meta data for {@link AskResults}
- *
+ * <p>
+ * Note that ASK results are something of a special case because they contain
+ * only a single column and we know exactly what the type of the column is, with
+ * other forms of results we don't have this luxury and so the
+ * {@link JdbcCompatibility} levels are used to determine how we report types.
+ * </p>
+ *
*/
public class AskResultsMetadata extends JenaResultsMetadata {
-
+
/**
* Constant for the ASK results column label
*/
public static final String COLUMN_LABEL_ASK = "ASK";
+
+ /**
+ * Creates new ASK results metadata
+ * @param results Results
+ * @throws SQLException Thrown if the metadata cannot be created
+ */
+ public AskResultsMetadata(JenaResultSet results) throws SQLException {
+ super(results);
+ }
@Override
public int getColumnCount() throws SQLException {
@@ -41,19 +58,22 @@ public class AskResultsMetadata extends
@Override
public String getColumnLabel(int column) throws SQLException {
- if (column == 1) return COLUMN_LABEL_ASK;
+ if (column == 1)
+ return COLUMN_LABEL_ASK;
throw new SQLException("Column Index is out of bounds");
}
-
+
@Override
public int getColumnType(int column) throws SQLException {
- if (column == 1) return Types.BOOLEAN;
+ if (column == 1)
+ return Types.BOOLEAN;
throw new SQLException("Column Index is out of bounds");
}
-
+
@Override
public String getColumnTypeName(int column) throws SQLException {
- if (column == 1) return Boolean.class.getCanonicalName();
+ if (column == 1)
+ return Boolean.class.getCanonicalName();
throw new SQLException("Column Index is out of bounds");
}
@@ -75,7 +95,8 @@ public class AskResultsMetadata extends
@Override
public int getColumnDisplaySize(int column) throws SQLException {
- if (column == 1) return 10;
+ if (column == 1)
+ return 10;
throw new SQLException("Column Index is out of bounds");
}
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/JenaResultsMetadata.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/JenaResultsMetadata.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/JenaResultsMetadata.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/JenaResultsMetadata.java Thu Apr 4 21:05:52 2013
@@ -23,6 +23,8 @@ import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
+import org.apache.jena.jdbc.results.JenaResultSet;
+
import com.hp.hpl.jena.graph.Node;
/**
@@ -30,6 +32,18 @@ import com.hp.hpl.jena.graph.Node;
*
*/
public abstract class JenaResultsMetadata implements ResultSetMetaData {
+
+ private JenaResultSet results;
+
+ /**
+ * Creates new result set metadata
+ * @param results Result Set
+ * @throws SQLException
+ */
+ public JenaResultsMetadata(JenaResultSet results) throws SQLException {
+ if (results == null) throw new SQLException("Result Set cannot be null");
+ this.results = results;
+ }
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLFeatureNotSupportedException();
@@ -40,7 +54,7 @@ public abstract class JenaResultsMetadat
}
public String getCatalogName(int column) throws SQLException {
- throw new SQLFeatureNotSupportedException();
+ return this.results.getStatement().getConnection().getCatalog();
}
public String getColumnClassName(int column) throws SQLException {
@@ -106,11 +120,13 @@ public abstract class JenaResultsMetadat
}
public String getSchemaName(int column) throws SQLException {
- throw new SQLFeatureNotSupportedException();
+ // Not applicable so return empty string
+ return "";
}
public String getTableName(int column) throws SQLException {
- throw new SQLFeatureNotSupportedException();
+ // Not applicable so return empty string
+ return "";
}
public boolean isAutoIncrement(int column) throws SQLException {
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/SelectResultsMetadata.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/SelectResultsMetadata.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/SelectResultsMetadata.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/SelectResultsMetadata.java Thu Apr 4 21:05:52 2013
@@ -21,6 +21,7 @@ package org.apache.jena.jdbc.results.met
import java.sql.SQLException;
import java.util.List;
+import org.apache.jena.jdbc.results.JenaResultSet;
import org.apache.jena.jdbc.results.SelectResults;
import com.hp.hpl.jena.query.ResultSet;
@@ -37,10 +38,13 @@ public class SelectResultsMetadata exten
/**
* Creates new SELECT results metadata
- * @param rset Underlying results
+ * @param results JDBC result set
+ * @param rset Underlying SPARQL results
* @param columns Columns
+ * @throws SQLException
*/
- public SelectResultsMetadata(ResultSet rset, List<String> columns) {
+ public SelectResultsMetadata(JenaResultSet results, ResultSet rset, List<String> columns) throws SQLException {
+ super(results);
this.innerResults = rset;
this.columns = columns;
}
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/TripleResultsMetadata.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/TripleResultsMetadata.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/TripleResultsMetadata.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/metadata/TripleResultsMetadata.java Thu Apr 4 21:05:52 2013
@@ -20,6 +20,7 @@ package org.apache.jena.jdbc.results.met
import java.sql.SQLException;
+import org.apache.jena.jdbc.results.JenaResultSet;
import org.apache.jena.jdbc.results.TripleIteratorResults;
/**
@@ -44,6 +45,15 @@ public class TripleResultsMetadata exten
* Constant for the number of columns in triple results
*/
public static final int NUM_COLUMNS = 3;
+
+ /**
+ * Creates new results metadata for triple (CONSTRUCT/DESCRIBE) results
+ * @param results Result Set
+ * @throws SQLException Thrown if the metadata cannot be created
+ */
+ public TripleResultsMetadata(JenaResultSet results) throws SQLException {
+ super(results);
+ }
@Override
public int getColumnCount() throws SQLException {
Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/DatasetStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/DatasetStatement.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/DatasetStatement.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/DatasetStatement.java Thu Apr 4 21:05:52 2013
@@ -36,7 +36,7 @@ import com.hp.hpl.jena.update.UpdateRequ
* A Jena JDBC statement over a {@link Dataset}
*
*/
-public class DatasetStatement extends JenaJdbcStatement {
+public class DatasetStatement extends JenaStatement {
private DatasetConnection dsConn;
Added: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java?rev=1464765&view=auto
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java (added)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaStatement.java Thu Apr 4 21:05:52 2013
@@ -0,0 +1,662 @@
+/**
+ * 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.statements;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.SQLWarning;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.jena.jdbc.connections.JenaConnection;
+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;
+import com.hp.hpl.jena.query.QueryFactory;
+import com.hp.hpl.jena.query.ReadWrite;
+import com.hp.hpl.jena.update.UpdateFactory;
+import com.hp.hpl.jena.update.UpdateProcessor;
+import com.hp.hpl.jena.update.UpdateRequest;
+
+/**
+ * Abstract Jena JDBC implementation of a statement that only permits read
+ * operations
+ *
+ */
+public abstract class JenaStatement implements Statement {
+
+ protected static final int DEFAULT_HOLDABILITY = ResultSet.CLOSE_CURSORS_AT_COMMIT;
+ protected static final int DEFAULT_FETCH_DIRECTION = ResultSet.FETCH_FORWARD;
+ protected static final int DEFAULT_FETCH_SIZE = 0;
+ protected static final boolean DEFAULT_AUTO_COMMIT = JenaConnection.DEFAULT_AUTO_COMMIT;
+ protected static final int DEFAULT_TRANSACTION_LEVEL = JenaConnection.DEFAULT_ISOLATION_LEVEL;
+ protected static final int NO_LIMIT = 0;
+
+ private List<String> commands = new ArrayList<String>();
+ private SQLWarning warnings = null;
+ private JenaConnection connection;
+ private ResultSet currResults = null;
+ private Queue<ResultSet> results = new LinkedList<ResultSet>();
+ private List<ResultSet> openResults = new ArrayList<ResultSet>();
+ private boolean closed = false;
+ private int fetchDirection = DEFAULT_FETCH_DIRECTION;
+ private int fetchSize = DEFAULT_FETCH_SIZE;
+ private int holdability = DEFAULT_HOLDABILITY;
+ private int updateCount = 0;
+ private boolean autoCommit = DEFAULT_AUTO_COMMIT;
+ private int transactionLevel = DEFAULT_TRANSACTION_LEVEL;
+ private int maxRows = NO_LIMIT;
+ @SuppressWarnings("unused")
+ private boolean escapeProcessing = false;
+ private int timeout = NO_LIMIT;
+
+ /**
+ * Creates a new statement
+ *
+ * @param connection
+ * Connection
+ * @throws SQLException
+ * Thrown if the arguments are invalid
+ */
+ public JenaStatement(JenaConnection connection) throws SQLException {
+ this(connection, DEFAULT_FETCH_DIRECTION, DEFAULT_FETCH_SIZE, DEFAULT_HOLDABILITY, DEFAULT_AUTO_COMMIT,
+ DEFAULT_TRANSACTION_LEVEL);
+ }
+
+ /**
+ * Creates a new statement
+ *
+ * @param connection
+ * Connection
+ * @param fetchDir
+ * Fetch Direction
+ * @param fetchSize
+ * Fetch Size
+ * @param holdability
+ * Result Set holdability
+ * @param autoCommit
+ * Auto-commit behaviour
+ * @param transactionLevel
+ * Transaction level
+ * @throws SQLException
+ * Thrown if there is an error with the statement parameters
+ *
+ */
+ public JenaStatement(JenaConnection connection, int fetchDir, int fetchSize, int holdability, boolean autoCommit,
+ int transactionLevel) throws SQLException {
+ if (connection == null)
+ throw new SQLException("Cannot create a Statement with a null connection");
+ this.connection = connection;
+ this.checkFetchDirection(fetchDir);
+ this.fetchDirection = fetchDir;
+ this.fetchSize = fetchSize;
+ this.checkHoldability(holdability);
+ this.holdability = holdability;
+ this.autoCommit = autoCommit;
+ this.transactionLevel = transactionLevel;
+ }
+
+ /**
+ * Gets the underlying {@link JenaConnection} implementation, useful for
+ * accessing Jena JDBC specific information such as desired JDBC
+ * compatibility level
+ *
+ * @return Underlying Jena Connection
+ */
+ public JenaConnection getJenaConnection() {
+ return this.connection;
+ }
+
+ public boolean isWrapperFor(Class<?> iface) throws SQLException {
+ throw new SQLFeatureNotSupportedException();
+ }
+
+ public <T> T unwrap(Class<T> iface) throws SQLException {
+ throw new SQLFeatureNotSupportedException();
+ }
+
+ public void addBatch(String sql) throws SQLException {
+ this.commands.add(sql);
+ }
+
+ public void cancel() throws SQLException {
+
+ }
+
+ public void clearBatch() throws SQLException {
+ this.commands.clear();
+ }
+
+ public void clearWarnings() throws SQLException {
+ this.warnings = null;
+ }
+
+ public void close() throws SQLException {
+ if (this.closed)
+ return;
+ this.closed = true;
+ if (this.currResults != null) {
+ this.currResults.close();
+ this.currResults = null;
+ }
+ while (!this.results.isEmpty()) {
+ ResultSet rset = this.results.poll();
+ if (rset != null)
+ rset.close();
+ }
+ for (ResultSet rset : this.openResults) {
+ rset.close();
+ }
+ this.openResults.clear();
+ }
+
+ public final boolean execute(String sql) throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+
+ Query q = null;
+ UpdateRequest u = null;
+ try {
+ // Start by assuming a query
+ q = QueryFactory.create(sql);
+ } catch (Exception e) {
+ try {
+ // If that fails try as an update instead
+ u = UpdateFactory.create(sql);
+ } catch (Exception e2) {
+ throw new SQLException("Not a valid SPARQL query/update", e);
+ }
+ }
+
+ if (q != null) {
+ // Execute as a query
+ return this.executeQuery(q);
+ } else if (u != null) {
+ // Execute as an update
+ this.executeUpdate(u);
+ return false;
+ } else {
+ throw new SQLException("Unable to create a SPARQL query/update");
+ }
+ }
+
+ private boolean executeQuery(Query q) throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+
+ // Do we need transactions?
+ boolean needsBegin = (!this.autoCommit && this.transactionLevel != Connection.TRANSACTION_NONE && !this
+ .hasActiveTransaction());
+ boolean needsCommit = (this.autoCommit && this.transactionLevel != Connection.TRANSACTION_NONE);
+
+ try {
+ // Start a transaction if necessary
+ if (needsCommit) {
+ this.beginTransaction(ReadWrite.READ);
+ } else if (needsBegin) {
+ this.beginTransaction(ReadWrite.WRITE);
+ }
+
+ // Manipulate the query if appropriate
+ if (this.maxRows > NO_LIMIT) {
+ // If we have no LIMIT or the LIMIT is greater than the
+ // permitted max rows
+ // then we will set the LIMIT to the max rows
+ if (!q.hasLimit() || q.getLimit() > this.maxRows) {
+ q.setLimit(this.maxRows);
+ }
+ }
+
+ // Create the query execution
+ QueryExecution qe = this.createQueryExecution(q);
+
+ // Manipulate the query execution if appropriate
+ if (this.timeout > NO_LIMIT) {
+ qe.setTimeout(this.timeout, TimeUnit.SECONDS);
+ }
+
+ // Return the appropriate result set type
+ if (q.isSelectType()) {
+ this.currResults = new SelectResults(this, qe, qe.execSelect(), needsCommit);
+ return true;
+ } else if (q.isAskType()) {
+ boolean askRes = qe.execAsk();
+ qe.close();
+ this.currResults = new AskResults(this, askRes, needsCommit);
+ return true;
+ } else if (q.isDescribeType()) {
+ this.currResults = new TripleIteratorResults(this, qe, qe.execDescribeTriples(), needsCommit);
+ return true;
+ } else if (q.isConstructType()) {
+ this.currResults = new TripleIteratorResults(this, qe, qe.execConstructTriples(), needsCommit);
+ return true;
+ } else {
+ throw new SQLException("Unknown SPARQL Query type");
+ }
+ } catch (SQLException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new SQLException("Error occurred during SPARQL query evaluation", e);
+ }
+ }
+
+ /**
+ * Helper method which derived classes must implement to provide a query
+ * execution
+ *
+ * @param q
+ * Query
+ * @return Query Execution
+ * @throws SQLException
+ * Thrown if there is a problem creating a query execution
+ */
+ protected abstract QueryExecution createQueryExecution(Query q) throws SQLException;
+
+ private int executeUpdate(UpdateRequest u) throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+ if (this.connection.isReadOnly())
+ throw new SQLException("The JDBC connection is currently in read-only mode, updates are not permitted");
+
+ // Do we need transactions?
+ boolean needsBegin = (!this.autoCommit && this.transactionLevel != Connection.TRANSACTION_NONE && !this
+ .hasActiveTransaction());
+ boolean needsCommit = (this.autoCommit && this.transactionLevel != Connection.TRANSACTION_NONE);
+
+ boolean commit = false;
+ try {
+ // Start a Transaction if necessary
+ if (needsCommit || needsBegin) {
+ this.beginTransaction(ReadWrite.WRITE);
+ }
+
+ UpdateProcessor processor = this.createUpdateProcessor(u);
+ processor.execute();
+ commit = true;
+ return 0;
+ } catch (SQLException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new SQLException("Error occurred during SPARQL update evaluation", e);
+ } finally {
+ if (needsCommit) {
+ if (commit) {
+ this.commitTransaction();
+ } else {
+ this.rollbackTransaction();
+ }
+ }
+ }
+ }
+
+ /**
+ * Helper method which derived classes must implement to provide an update
+ * processor
+ *
+ * @param u
+ * Update
+ * @return Update Processor
+ */
+ protected abstract UpdateProcessor createUpdateProcessor(UpdateRequest u) throws SQLException;
+
+ protected abstract boolean hasActiveTransaction() throws SQLException;
+
+ protected abstract void beginTransaction(ReadWrite type) throws SQLException;
+
+ protected abstract void commitTransaction() throws SQLException;
+
+ protected abstract void rollbackTransaction() throws SQLException;
+
+ public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
+ throw new SQLFeatureNotSupportedException("Use the single argument form of execute()");
+ }
+
+ public boolean execute(String sql, int[] columnIndexes) throws SQLException {
+ throw new SQLFeatureNotSupportedException("Use the single argument form of execute()");
+ }
+
+ public boolean execute(String sql, String[] columnNames) throws SQLException {
+ // TODO: Assuming a SELECT * or DESCRIBE * could replace the * with the
+ // specific column names
+ throw new SQLFeatureNotSupportedException("Use the single argument form of execute()");
+ }
+
+ public final int[] executeBatch() throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+
+ int[] rets = new int[this.commands.size()];
+ ResultSet curr = this.currResults;
+ for (int i = 0; i < this.commands.size(); i++) {
+ if (this.execute(this.commands.get(i))) {
+ // True means it returned a ResultSet
+ this.results.add(this.getResultSet());
+ this.currResults = null;
+ rets[i] = 0;
+ } else {
+ rets[i] = this.getUpdateCount();
+ }
+ }
+ this.currResults = curr;
+ // Make the next available results the current results if there
+ // are no current results
+ if (this.currResults == null && !this.results.isEmpty()) {
+ this.currResults = this.results.poll();
+ }
+ return rets;
+ }
+
+ public final ResultSet executeQuery(String sql) throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+
+ Query q = null;
+ try {
+ q = QueryFactory.create(sql);
+ } catch (Exception e) {
+ throw new SQLException("Not a valid SPARQL query", e);
+ }
+
+ if (q == null)
+ throw new SQLException("Unable to create a SPARQL Query");
+ if (this.executeQuery(q)) {
+ return this.currResults;
+ } else {
+ throw new SQLException("Query did not produce a result set");
+ }
+ }
+
+ public final int executeUpdate(String sql) throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+ if (this.connection.isReadOnly())
+ throw new SQLException("The JDBC connection is currently in read-only mode, updates are not permitted");
+ UpdateRequest u = null;
+ try {
+ u = UpdateFactory.create(sql);
+ } catch (Exception e) {
+ throw new SQLException("Not a valid SPARQL Update", e);
+ }
+
+ if (u == null)
+ throw new SQLException("Unable to create a SPARQL Update Request");
+ return this.executeUpdate(u);
+ }
+
+ public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
+ throw new SQLFeatureNotSupportedException("Use the single argument form of executeUpdate()");
+ }
+
+ public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
+ throw new SQLFeatureNotSupportedException("Use the single argument form of executeUpdate()");
+ }
+
+ public int executeUpdate(String sql, String[] columnNames) throws SQLException {
+ throw new SQLFeatureNotSupportedException("Use the single argument form of executeUpdate()");
+ }
+
+ public final Connection getConnection() throws SQLException {
+ return this.connection;
+ }
+
+ public int getFetchDirection() throws SQLException {
+ return this.fetchDirection;
+ }
+
+ public int getFetchSize() throws SQLException {
+ return this.fetchSize;
+ }
+
+ public ResultSet getGeneratedKeys() throws SQLException {
+ throw new SQLFeatureNotSupportedException();
+ }
+
+ public int getMaxFieldSize() throws SQLException {
+ return NO_LIMIT;
+ }
+
+ public int getMaxRows() throws SQLException {
+ return maxRows;
+ }
+
+ public boolean getMoreResults() throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+
+ if (this.currResults != null) {
+ this.currResults.close();
+ this.currResults = null;
+ }
+ if (!this.results.isEmpty()) {
+ this.currResults = this.results.poll();
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public boolean getMoreResults(int current) throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+
+ switch (current) {
+ case Statement.CLOSE_CURRENT_RESULT:
+ return this.getMoreResults();
+ case Statement.CLOSE_ALL_RESULTS:
+ for (ResultSet rset : this.openResults) {
+ rset.close();
+ }
+ return this.getMoreResults();
+ case Statement.KEEP_CURRENT_RESULT:
+ if (this.currResults != null) {
+ this.openResults.add(this.currResults);
+ this.currResults = null;
+ }
+ return this.getMoreResults();
+ default:
+ throw new SQLFeatureNotSupportedException(
+ "Unsupported mode for dealing with current results, only Statement.CLOSE_CURRENT_RESULT, Statement.CLOSE_ALL_RESULTS and Statement.KEEP_CURRENT_RESULT are supported");
+ }
+ }
+
+ public int getQueryTimeout() throws SQLException {
+ return this.timeout;
+ }
+
+ public final ResultSet getResultSet() throws SQLException {
+ if (this.isClosed())
+ throw new SQLException("The Statement is closed");
+ return this.currResults;
+ }
+
+ /**
+ * Helper method for use in execute() method implementations to set the
+ * current results
+ *
+ * @param results
+ * Results
+ * @throws SQLException
+ * Thrown if there is an error closing the previous results
+ */
+ protected void setCurrentResults(ResultSet results) throws SQLException {
+ if (this.currResults != null) {
+ this.currResults.close();
+ }
+ this.currResults = results;
+ }
+
+ /**
+ * Gets that result sets are read-only
+ */
+ public final int getResultSetConcurrency() throws SQLException {
+ return ResultSet.CONCUR_READ_ONLY;
+ }
+
+ public int getResultSetHoldability() throws SQLException {
+ return this.holdability;
+ }
+
+ protected void checkHoldability(int h) throws SQLException {
+ switch (h) {
+ case ResultSet.CLOSE_CURSORS_AT_COMMIT:
+ case ResultSet.HOLD_CURSORS_OVER_COMMIT:
+ return;
+ default:
+ throw new SQLException(String.format("Holdability %d is supported for Jena JDBC statements", h));
+ }
+ }
+
+ public final int getResultSetType() throws SQLException {
+ return ResultSet.TYPE_FORWARD_ONLY;
+ }
+
+ public int getUpdateCount() throws SQLException {
+ return this.updateCount;
+ }
+
+ /**
+ * Helper method which derived classes may use to set the update count
+ *
+ * @param count
+ * Update Count
+ */
+ protected void setUpdateCount(int count) {
+ this.updateCount = count;
+ }
+
+ public SQLWarning getWarnings() throws SQLException {
+ return this.warnings;
+ }
+
+ /**
+ * Helper method that derived classes may use to set warnings
+ *
+ * @param warning
+ * Warning
+ */
+ protected void setWarning(SQLWarning warning) {
+ if (this.warnings == null) {
+ this.warnings = warning;
+ } else {
+ // Chain with existing warnings
+ warning.setNextWarning(this.warnings);
+ this.warnings = warning;
+ }
+ }
+
+ /**
+ * Helper method that derived classes may use to set warnings
+ *
+ * @param warning
+ * Warning
+ */
+ protected void setWarning(String warning) {
+ this.setWarning(new SQLWarning(warning));
+ }
+
+ /**
+ * Helper method that derived classes may use to set warnings
+ *
+ * @param warning
+ * Warning
+ * @param cause
+ * Cause
+ */
+ protected void setWarning(String warning, Throwable cause) {
+ this.setWarning(new SQLWarning(warning, cause));
+ }
+
+ public final boolean isClosed() throws SQLException {
+ return this.closed;
+ }
+
+ public final boolean isPoolable() throws SQLException {
+ return true;
+ }
+
+ public void setCursorName(String name) throws SQLException {
+ throw new SQLFeatureNotSupportedException();
+ }
+
+ public void setEscapeProcessing(boolean enable) throws SQLException {
+ this.escapeProcessing = enable;
+ }
+
+ public void setFetchDirection(int direction) throws SQLException {
+ this.checkFetchDirection(direction);
+ this.fetchDirection = direction;
+ }
+
+ /**
+ * Helper method which checks whether a given fetch direction is valid
+ * throwing an error if it is not
+ *
+ * @param dir
+ * Fetch Direction
+ * @throws SQLException
+ * Thrown if the direction is not valid
+ */
+ protected void checkFetchDirection(int dir) throws SQLException {
+ switch (dir) {
+ case ResultSet.FETCH_FORWARD:
+ return;
+ default:
+ throw new SQLFeatureNotSupportedException("Only ResultSet.FETCH_FORWARD is supported as a fetch direction");
+ }
+ }
+
+ public void setFetchSize(int rows) throws SQLException {
+ this.fetchSize = rows;
+ }
+
+ public void setMaxFieldSize(int max) throws SQLException {
+ // Ignored
+ this.setWarning("setMaxFieldSize() was called but there is no field size limit for Jena JDBC connections");
+ }
+
+ public void setMaxRows(int max) throws SQLException {
+ if (max <= NO_LIMIT) {
+ this.maxRows = NO_LIMIT;
+ } else {
+ this.maxRows = max;
+ }
+ }
+
+ public void setPoolable(boolean poolable) throws SQLException {
+ // Ignored
+ this.setWarning("setPoolable() was called but Jena JDBC statements are always considered poolable");
+ }
+
+ public void setQueryTimeout(int seconds) throws SQLException {
+ if (seconds <= NO_LIMIT) {
+ this.timeout = NO_LIMIT;
+ } else {
+ this.timeout = seconds;
+ }
+ }
+}
Modified: jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java?rev=1464765&r1=1464764&r2=1464765&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/statements/RemoteEndpointStatement.java Thu Apr 4 21:05:52 2013
@@ -23,7 +23,7 @@ import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import org.apache.jena.jdbc.remote.connections.RemoteEndpointConnection;
-import org.apache.jena.jdbc.statements.JenaJdbcStatement;
+import org.apache.jena.jdbc.statements.JenaStatement;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
@@ -37,7 +37,7 @@ import com.hp.hpl.jena.update.UpdateRequ
* A Jena JDBC statement against a remote endpoint
*
*/
-public class RemoteEndpointStatement extends JenaJdbcStatement {
+public class RemoteEndpointStatement extends JenaStatement {
private RemoteEndpointConnection remoteConn;