未验证 提交 aca45af3 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Add checking for creating the same database. (#7028)

上级 8e5e83b4
......@@ -42,6 +42,8 @@ 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_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. "
+ "Although modifying it can lead to an inconsistent GTID state, if neccessary you can modify it with a non-XA transaction.");
......
/*
* 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.shardingsphere.proxy.backend.exception;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* DB create exists exception.
*/
@RequiredArgsConstructor
@Getter
public final class DBCreateExistsException extends BackendException {
private static final long serialVersionUID = 779787160167652641L;
private final String databaseName;
}
......@@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
......@@ -72,7 +73,10 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler {
}
private BackendResponse execute(final CreateDatabaseStatementContext context) {
SchemaNameCallback.getInstance().run(context.getSqlStatement().getSchemaName(), true);
if (ProxySchemaContexts.getInstance().getSchemaNames().contains(context.getSqlStatement().getDatabaseName())) {
return new ErrorResponse(new DBCreateExistsException(context.getSqlStatement().getDatabaseName()));
}
SchemaNameCallback.getInstance().run(context.getSqlStatement().getDatabaseName(), true);
// TODO Need to get the executed feedback from registry center for returning.
UpdateResponse result = new UpdateResponse();
result.setType("CREATE");
......
......@@ -41,7 +41,7 @@ public final class OrchestrationSchemaContextsFixture implements SchemaContexts
@Override
public Map<String, SchemaContext> getSchemaContexts() {
return Collections.emptyMap();
return Collections.singletonMap("schema", mock(SchemaContext.class));
}
@Override
......
......@@ -25,6 +25,7 @@ import org.apache.shardingsphere.kernel.context.SchemaContext;
import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.OrchestrationSchemaContextsFixture;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
......@@ -69,6 +70,19 @@ public final class RDLBackendHandlerTest {
assertThat(response, instanceOf(UpdateResponse.class));
}
@Test
public void assertExecuteCreateDatabaseContextWithException() {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
RDLBackendHandler executeEngine = new RDLBackendHandler(connection, new CreateDatabaseStatement("schema"));
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
setOrchestrationSchemaContexts(true);
response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
assertThat(((ErrorResponse) response).getCause(), instanceOf(DBCreateExistsException.class));
}
private Map<String, SchemaContext> getSchemaContextMap() {
SchemaContext result = new SchemaContext("schema", null, null);
return Collections.singletonMap("schema", result);
......@@ -103,7 +117,7 @@ public final class RDLBackendHandlerTest {
Field schemaContexts = ProxySchemaContexts.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
if (isOrchestration) {
schemaContexts.set(ProxySchemaContexts.getInstance(), mock(OrchestrationSchemaContextsFixture.class));
schemaContexts.set(ProxySchemaContexts.getInstance(), new OrchestrationSchemaContextsFixture());
} else {
schemaContexts.set(ProxySchemaContexts.getInstance(), new StandardSchemaContexts());
}
......
......@@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactionException;
import org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
......@@ -62,6 +63,9 @@ public final class MySQLErrPacketFactory {
if (cause instanceof NoDatabaseSelectedException) {
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, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage());
}
}
......@@ -27,5 +27,5 @@ import lombok.RequiredArgsConstructor;
@Getter
public final class CreateDatabaseStatement extends DDLStatement {
private final String schemaName;
private final String databaseName;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册