Repository: phoenix Updated Branches: refs/heads/3.0 8768902a6 -> 75ae0a7ee
PHOENIX-1500 Disallow mutations and queries using using PhoenixPreparedStatement.executeQuery() and executeUpdate() respectively (Samarth Jain) Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/75ae0a7e Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/75ae0a7e Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/75ae0a7e Branch: refs/heads/3.0 Commit: 75ae0a7eee513acb015bf0f0fa0d41f2fb5a4a13 Parents: 8768902 Author: James Taylor <[email protected]> Authored: Wed Dec 3 17:41:42 2014 -0800 Committer: James Taylor <[email protected]> Committed: Wed Dec 3 17:41:42 2014 -0800 ---------------------------------------------------------------------- .../phoenix/exception/SQLExceptionCode.java | 10 +++--- .../phoenix/jdbc/PhoenixPreparedStatement.java | 8 +++++ .../ExecuteQueryNotApplicableException.java | 7 +++- .../ExecuteUpdateNotApplicableException.java | 7 +++- .../phoenix/compile/QueryCompilerTest.java | 2 +- .../jdbc/PhoenixPreparedStatementTest.java | 34 ++++++++++++++++++-- 6 files changed, 57 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/75ae0a7e/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java index 1471830..5fa9a01 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java @@ -264,11 +264,11 @@ public enum SQLExceptionCode { RESULTSET_CLOSED(1101, "XCL01", "ResultSet is closed."), GET_TABLE_REGIONS_FAIL(1102, "XCL02", "Cannot get all table regions"), EXECUTE_QUERY_NOT_APPLICABLE(1103, "XCL03", "executeQuery may not be used."), - EXECUTE_UPDATE_NOT_APPLICABLE(1104, "XCL03", "executeUpdate may not be used."), - SPLIT_POINT_NOT_CONSTANT(1105, "XCL04", "Split points must be constants."), - BATCH_EXCEPTION(1106, "XCL05", "Exception while executing batch."), - EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH(1107, "XCL06", "An executeUpdate is prohibited when the batch is not empty. Use clearBatch to empty the batch first."), - STALE_REGION_BOUNDARY_CACHE(1108, "XCL07", "Cache of region boundaries are out of date.", new Factory() { + EXECUTE_UPDATE_NOT_APPLICABLE(1104, "XCL04", "executeUpdate may not be used."), + SPLIT_POINT_NOT_CONSTANT(1105, "XCL05", "Split points must be constants."), + BATCH_EXCEPTION(1106, "XCL06", "Exception while executing batch."), + EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH(1107, "XCL07", "An executeUpdate is prohibited when the batch is not empty. Use clearBatch to empty the batch first."), + STALE_REGION_BOUNDARY_CACHE(1108, "XCL08", "Cache of region boundaries are out of date.", new Factory() { @Override public SQLException newException(SQLExceptionInfo info) { return new StaleRegionBoundaryCacheException(info.getSchemaName(), info.getTableName()); http://git-wip-us.apache.org/repos/asf/phoenix/blob/75ae0a7e/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java index 09fcb98..985bffb 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java @@ -52,6 +52,8 @@ import org.apache.phoenix.compile.QueryPlan; import org.apache.phoenix.compile.StatementPlan; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.schema.ExecuteQueryNotApplicableException; +import org.apache.phoenix.schema.ExecuteUpdateNotApplicableException; import org.apache.phoenix.schema.PDataType; import org.apache.phoenix.schema.Sequence; import org.apache.phoenix.util.DateUtil; @@ -180,12 +182,18 @@ public class PhoenixPreparedStatement extends PhoenixStatement implements Prepar @Override public ResultSet executeQuery() throws SQLException { throwIfUnboundParameters(); + if (statement.getOperation().isMutation()) { + throw new ExecuteQueryNotApplicableException(statement.getOperation()); + } return executeQuery(statement); } @Override public int executeUpdate() throws SQLException { throwIfUnboundParameters(); + if (!statement.getOperation().isMutation()) { + throw new ExecuteUpdateNotApplicableException(statement.getOperation()); + } if (!batch.isEmpty()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH) .build().buildException(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/75ae0a7e/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteQueryNotApplicableException.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteQueryNotApplicableException.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteQueryNotApplicableException.java index a7ea6a4..9e93d93 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteQueryNotApplicableException.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteQueryNotApplicableException.java @@ -21,11 +21,16 @@ import java.sql.SQLException; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.jdbc.PhoenixStatement.Operation; public class ExecuteQueryNotApplicableException extends SQLException { private static final long serialVersionUID = 1L; private static SQLExceptionCode code = SQLExceptionCode.EXECUTE_QUERY_NOT_APPLICABLE; - + + public ExecuteQueryNotApplicableException(Operation op) { + super(new SQLExceptionInfo.Builder(code).setMessage("Disallowed operation: " + op.name()).build().toString(), code.getSQLState(), code.getErrorCode()); + } + public ExecuteQueryNotApplicableException(String query) { super(new SQLExceptionInfo.Builder(code).setMessage("Query: " + query).build().toString(), code.getSQLState(), code.getErrorCode()); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/75ae0a7e/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteUpdateNotApplicableException.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteUpdateNotApplicableException.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteUpdateNotApplicableException.java index 5745d9e..8df1e06 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteUpdateNotApplicableException.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/ExecuteUpdateNotApplicableException.java @@ -21,10 +21,15 @@ import java.sql.SQLException; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.jdbc.PhoenixStatement.Operation; public class ExecuteUpdateNotApplicableException extends SQLException { private static final long serialVersionUID = 1L; - private static SQLExceptionCode code = SQLExceptionCode.EXECUTE_QUERY_NOT_APPLICABLE; + private static SQLExceptionCode code = SQLExceptionCode.EXECUTE_UPDATE_NOT_APPLICABLE; + + public ExecuteUpdateNotApplicableException(Operation op) { + super(new SQLExceptionInfo.Builder(code).setMessage("Disallowed operation: " + op.name()).build().toString(), code.getSQLState(), code.getErrorCode()); + } public ExecuteUpdateNotApplicableException(String query) { super(new SQLExceptionInfo.Builder(code).setMessage("Query: " + query).build().toString(), code.getSQLState(), code.getErrorCode()); http://git-wip-us.apache.org/repos/asf/phoenix/blob/75ae0a7e/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java index 58d7710..ac601d7 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java @@ -1368,7 +1368,7 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest { } conn.close(); try { - PreparedStatement stmt = conn.prepareStatement("SELECT * FROM atable"); + PreparedStatement stmt = conn.prepareStatement("UPSERT INTO atable VALUES('000000000000000','000000000000000')"); stmt.addBatch(); stmt.executeUpdate(); fail(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/75ae0a7e/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixPreparedStatementTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixPreparedStatementTest.java b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixPreparedStatementTest.java index c2bbdfb..bf16c21 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixPreparedStatementTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixPreparedStatementTest.java @@ -17,8 +17,8 @@ */ package org.apache.phoenix.jdbc; -import org.apache.phoenix.query.BaseConnectionlessQueryTest; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.sql.Connection; import java.sql.DriverManager; @@ -26,7 +26,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties; -import static org.junit.Assert.fail; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.query.BaseConnectionlessQueryTest; +import org.junit.Test; public class PhoenixPreparedStatementTest extends BaseConnectionlessQueryTest { @@ -57,5 +59,31 @@ public class PhoenixPreparedStatementTest extends BaseConnectionlessQueryTest { // Expected exception } } + + @Test + public void testMutationUsingExecuteQueryShouldFail() throws Exception { + Properties connectionProperties = new Properties(); + Connection connection = DriverManager.getConnection(getUrl(), connectionProperties); + PreparedStatement stmt = connection.prepareStatement("DELETE FROM " + ATABLE); + try { + stmt.executeQuery(); + fail(); + } catch(SQLException e) { + assertEquals(SQLExceptionCode.EXECUTE_QUERY_NOT_APPLICABLE.getErrorCode(), e.getErrorCode()); + } + } + + @Test + public void testQueriesUsingExecuteUpdateShouldFail() throws Exception { + Properties connectionProperties = new Properties(); + Connection connection = DriverManager.getConnection(getUrl(), connectionProperties); + PreparedStatement stmt = connection.prepareStatement("SELECT * FROM " + ATABLE); + try { + stmt.executeUpdate(); + fail(); + } catch(SQLException e) { + assertEquals(SQLExceptionCode.EXECUTE_UPDATE_NOT_APPLICABLE.getErrorCode(), e.getErrorCode()); + } + } }
