You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2020/11/29 14:19:10 UTC
[shardingsphere] branch master updated: Move getTableName from
SQLStatementContext to TableModifyInTransactionException (#8411)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new b67c908 Move getTableName from SQLStatementContext to TableModifyInTransactionException (#8411)
b67c908 is described below
commit b67c908a2ecd9a6607e305d3020109c3c9152839
Author: Liang Zhang <te...@163.com>
AuthorDate: Sun Nov 29 22:18:53 2020 +0800
Move getTableName from SQLStatementContext to TableModifyInTransactionException (#8411)
---
.../proxy/backend/communication/ProxySQLExecutor.java | 12 ++----------
.../backend/exception/TableModifyInTransactionException.java | 12 ++++++++++--
.../proxy/frontend/mysql/MySQLErrPacketFactoryTest.java | 9 ++++++---
3 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java
index 90e0dfc..f924675 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.backend.communication;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.binder.type.TableAvailable;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
@@ -42,9 +41,9 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.type.RawExecutionRule;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.executor.ProxyJDBCExecutorCallback;
-import org.apache.shardingsphere.proxy.backend.communication.raw.ProxyRawExecutor;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.accessor.JDBCAccessor;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.TransactionStatus;
+import org.apache.shardingsphere.proxy.backend.communication.raw.ProxyRawExecutor;
import org.apache.shardingsphere.proxy.backend.context.BackendExecutorContext;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactionException;
@@ -95,7 +94,7 @@ public final class ProxySQLExecutor {
*/
public void checkExecutePrerequisites(final ExecutionContext executionContext) {
if (isExecuteDDLInXATransaction(executionContext.getSqlStatementContext().getSqlStatement())) {
- throw new TableModifyInTransactionException(getTableName(executionContext.getSqlStatementContext()));
+ throw new TableModifyInTransactionException(executionContext.getSqlStatementContext());
}
}
@@ -104,13 +103,6 @@ public final class ProxySQLExecutor {
return TransactionType.XA == transactionStatus.getTransactionType() && sqlStatement instanceof DDLStatement && transactionStatus.isInTransaction();
}
- private String getTableName(final SQLStatementContext<?> sqlStatementContext) {
- if (sqlStatementContext instanceof TableAvailable && !((TableAvailable) sqlStatementContext).getAllTables().isEmpty()) {
- return ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue();
- }
- return "unknown_table";
- }
-
/**
* Execute SQL.
*
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TableModifyInTransactionException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TableModifyInTransactionException.java
index ab6dfdb..2a1810d 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TableModifyInTransactionException.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TableModifyInTransactionException.java
@@ -18,16 +18,24 @@
package org.apache.shardingsphere.proxy.backend.exception;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.TableAvailable;
/**
* Table modify in transaction exception.
*/
-@RequiredArgsConstructor
@Getter
public final class TableModifyInTransactionException extends BackendException {
private static final long serialVersionUID = 5676889868293244575L;
private final String tableName;
+
+ public TableModifyInTransactionException(final SQLStatementContext<?> sqlStatementContext) {
+ if (sqlStatementContext instanceof TableAvailable && !((TableAvailable) sqlStatementContext).getAllTables().isEmpty()) {
+ tableName = ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue();
+ } else {
+ tableName = "unknown_table";
+ }
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
index 3a7a9bb..69e57b9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.mysql;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.proxy.backend.exception.CircuitBreakException;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
@@ -36,10 +37,11 @@ import org.junit.Test;
import java.sql.SQLException;
+import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.CoreMatchers.endsWith;
import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
public final class MySQLErrPacketFactoryTest {
@@ -90,11 +92,12 @@ public final class MySQLErrPacketFactoryTest {
@Test
public void assertNewInstanceWithTableModifyInTransactionException() {
- MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new TableModifyInTransactionException("tbl"));
+ MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new TableModifyInTransactionException(mock(SQLStatementContext.class)));
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getErrorCode(), is(3176));
assertThat(actual.getSqlState(), is("HY000"));
- assertThat(actual.getErrorMessage(), is("Please do not modify the tbl table with an XA transaction. This is an internal system table used to store GTIDs for committed transactions. "
+ assertThat(actual.getErrorMessage(), is("Please do not modify the unknown_table table with an XA transaction. "
+ + "This is an internal system table used to store GTIDs for committed transactions. "
+ "Although modifying it can lead to an inconsistent GTID state, if neccessary you can modify it with a non-XA transaction."));
}