You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/25 07:13:09 UTC
[shardingsphere] branch master updated: ShardingProxy supports
checking table exists for createTable SQL (#7051)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 d161512 ShardingProxy supports checking table exists for createTable SQL (#7051)
d161512 is described below
commit d161512b38d1672d9b20119c87225713e69bd235
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Tue Aug 25 15:12:48 2020 +0800
ShardingProxy supports checking table exists for createTable SQL (#7051)
* ShardingProxy support checking table exists for createTable SQL
* check style
---
.../db/protocol/mysql/constant/MySQLServerErrorCode.java | 4 +++-
.../sharding/route/engine/exception/TableExistsException.java | 5 +++++
.../route/engine/validator/ShardingStatementValidatorFactory.java | 5 +++++
.../validator/impl/ShardingCreateTableStatementValidator.java | 4 ++--
.../backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java | 3 ++-
.../shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java | 6 +++++-
6 files changed, 22 insertions(+), 5 deletions(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java
index 90f02d2..b2a707c 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java
@@ -42,7 +42,9 @@ public enum MySQLServerErrorCode implements SQLErrorCode {
ER_UNSUPPORTED_PS(1295, "HY000", "This command is not supported in the prepared statement protocol yet"),
- ER_DB_CREATE_EXISTS(1007, "HY000", "Message: Can't create database '%s'; database exists"),
+ ER_DB_CREATE_EXISTS_ERROR(1007, "HY000", "Can't create database '%s'; database exists"),
+
+ ER_TABLE_EXISTS_ERROR(1050, "42S01", "Table '%s' already exists"),
ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE(3176, "HY000",
"Please do not modify the %s table with an XA transaction. This is an internal system table used to store GTIDs for committed transactions. "
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/TableExistsException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/TableExistsException.java
index 6117b27..e5beb5a 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/TableExistsException.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/TableExistsException.java
@@ -17,16 +17,21 @@
package org.apache.shardingsphere.sharding.route.engine.exception;
+import lombok.Getter;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
/**
* Table exists exception.
*/
+@Getter
public final class TableExistsException extends ShardingSphereException {
private static final long serialVersionUID = 6056681626545854214L;
+ private final String tableName;
+
public TableExistsException(final String tableName) {
super(String.format("Table '%s' already exists.", tableName));
+ this.tableName = tableName;
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
index 42179dc..4eabcfb 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
@@ -19,10 +19,12 @@ package org.apache.shardingsphere.sharding.route.engine.validator;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingCreateTableStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingDeleteStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingInsertStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingUpdateStatementValidator;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.UpdateStatement;
@@ -51,6 +53,9 @@ public final class ShardingStatementValidatorFactory {
if (sqlStatement instanceof DeleteStatement) {
return Optional.of(new ShardingDeleteStatementValidator());
}
+ if (sqlStatement instanceof CreateTableStatement) {
+ return Optional.of(new ShardingCreateTableStatementValidator());
+ }
return Optional.empty();
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java
index af847d2..5380a69 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java
@@ -31,7 +31,7 @@ import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateTableStateme
* Sharding create table statement validator.
*/
public final class ShardingCreateTableStatementValidator implements ShardingStatementValidator<CreateTableStatement> {
-
+
@Override
public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext, final ShardingSphereMetaData metaData) {
CreateTableStatementContext sqlStatementContext = (CreateTableStatementContext) routeContext.getSqlStatementContext();
@@ -40,7 +40,7 @@ public final class ShardingCreateTableStatementValidator implements ShardingStat
throw new TableExistsException(tableName);
}
}
-
+
@Override
public void postValidate(final SQLStatement sqlStatement, final RouteResult routeResult) {
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
index e8e4a0e..ad02b07 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
@@ -42,6 +42,7 @@ import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
+import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
@@ -87,7 +88,7 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat
SQLLogger.logSQL(sql, ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
}
return execute(executionContext);
- } catch (final SQLException ex) {
+ } catch (final TableExistsException | SQLException ex) {
return new ErrorResponse(ex);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
index 7081060..88b3797 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactio
import org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
import org.apache.shardingsphere.proxy.backend.text.sctl.ShardingCTLErrorCode;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.ShardingCTLException;
+import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import java.sql.SQLException;
@@ -64,7 +65,10 @@ public final class MySQLErrPacketFactory {
return new MySQLErrPacket(sequenceId, MySQLServerErrorCode.ER_NO_DB_ERROR);
}
if (cause instanceof DBCreateExistsException) {
- return new MySQLErrPacket(sequenceId, MySQLServerErrorCode.ER_DB_CREATE_EXISTS, ((DBCreateExistsException) cause).getDatabaseName());
+ return new MySQLErrPacket(sequenceId, MySQLServerErrorCode.ER_DB_CREATE_EXISTS_ERROR, ((DBCreateExistsException) cause).getDatabaseName());
+ }
+ if (cause instanceof TableExistsException) {
+ return new MySQLErrPacket(sequenceId, MySQLServerErrorCode.ER_TABLE_EXISTS_ERROR, ((TableExistsException) cause).getTableName());
}
return new MySQLErrPacket(sequenceId, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage());
}