You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2014/12/04 02:29:49 UTC

phoenix git commit: PHOENIX-1500 Disallow mutations and queries using using PhoenixPreparedStatement.executeQuery() and executeUpdate() respectively (Samarth Jain)

Repository: phoenix
Updated Branches:
  refs/heads/4.0 e758b6d3b -> dcf1bc9c9


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/dcf1bc9c
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/dcf1bc9c
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/dcf1bc9c

Branch: refs/heads/4.0
Commit: dcf1bc9c9072fe36ebf75f30d35d773f9251f0eb
Parents: e758b6d
Author: James Taylor <ja...@apache.org>
Authored: Wed Dec 3 17:31:17 2014 -0800
Committer: James Taylor <ja...@apache.org>
Committed: Wed Dec 3 17:31:17 2014 -0800

----------------------------------------------------------------------
 .../phoenix/exception/SQLExceptionCode.java     | 14 ++++----
 .../phoenix/jdbc/PhoenixPreparedStatement.java  |  8 +++++
 .../ExecuteQueryNotApplicableException.java     |  7 +++-
 .../ExecuteUpdateNotApplicableException.java    |  7 +++-
 .../phoenix/compile/QueryCompilerTest.java      |  2 +-
 .../jdbc/PhoenixPreparedStatementTest.java      | 34 ++++++++++++++++++--
 6 files changed, 59 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/dcf1bc9c/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 b776007..0be1c28 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
@@ -265,18 +265,18 @@ 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());
         }
     }),
-    CANNOT_SPLIT_LOCAL_INDEX(1109,"XCL08", "Local index may not be pre-split"),
-    CANNOT_SALT_LOCAL_INDEX(1110,"XCL09", "Local index may not be salted"),
+    CANNOT_SPLIT_LOCAL_INDEX(1109,"XCL09", "Local index may not be pre-split"),
+    CANNOT_SALT_LOCAL_INDEX(1110,"XCL10", "Local index may not be salted"),
     
     /**
      * Implementation defined class. Phoenix internal error. (errorcode 20, sqlstate INT).

http://git-wip-us.apache.org/repos/asf/phoenix/blob/dcf1bc9c/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/dcf1bc9c/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/dcf1bc9c/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/dcf1bc9c/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 314431a..d0baaee 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/dcf1bc9c/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());
+        }
+    }
 
 }