You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/02/22 05:09:31 UTC
[shardingsphere] branch master updated: Add
ExtraTextProtocolBackendHandler to extend process via SPI (#9465)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 35f378c Add ExtraTextProtocolBackendHandler to extend process via SPI (#9465)
35f378c is described below
commit 35f378c3227f3441d3de1b8bd67b55f98db2f25c
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Feb 22 13:09:00 2021 +0800
Add ExtraTextProtocolBackendHandler to extend process via SPI (#9465)
---
.../text/TextProtocolBackendHandlerFactory.java | 14 +++++++++
.../extra/ExtraTextProtocolBackendHandler.java | 35 ++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index c702453..e6d3f11 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -24,11 +24,13 @@ import org.apache.shardingsphere.infra.audit.SQLCheckEngine;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.admin.DatabaseAdminBackendHandlerFactory;
import org.apache.shardingsphere.proxy.backend.text.data.DatabaseBackendHandlerFactory;
import org.apache.shardingsphere.proxy.backend.text.distsql.DistSQLBackendHandlerFactory;
+import org.apache.shardingsphere.proxy.backend.text.extra.ExtraTextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.sctl.ShardingCTLBackendHandlerFactory;
import org.apache.shardingsphere.proxy.backend.text.sctl.utils.SCTLUtils;
import org.apache.shardingsphere.proxy.backend.text.skip.SkipBackendHandler;
@@ -46,6 +48,10 @@ import java.util.Optional;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TextProtocolBackendHandlerFactory {
+ static {
+ ShardingSphereServiceLoader.register(ExtraTextProtocolBackendHandler.class);
+ }
+
/**
* Create new instance of text protocol backend handler.
*
@@ -65,6 +71,10 @@ public final class TextProtocolBackendHandlerFactory {
return ShardingCTLBackendHandlerFactory.newInstance(trimSQL, backendConnection);
}
SQLStatement sqlStatement = new ShardingSphereSQLParserEngine(getBackendDatabaseType(databaseType, backendConnection).getName()).parse(sql, false);
+ Optional<ExtraTextProtocolBackendHandler> extraHandler = findExtraTextProtocolBackendHandler(sqlStatement);
+ if (extraHandler.isPresent()) {
+ return extraHandler.get();
+ }
sqlCheck(backendConnection, sqlStatement);
if (sqlStatement instanceof TCLStatement) {
return TransactionBackendHandlerFactory.newInstance((TCLStatement) sqlStatement, sql, backendConnection);
@@ -86,4 +96,8 @@ public final class TextProtocolBackendHandlerFactory {
return Strings.isNullOrEmpty(backendConnection.getSchemaName())
? defaultDatabaseType : ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType();
}
+
+ private static Optional<ExtraTextProtocolBackendHandler> findExtraTextProtocolBackendHandler(final SQLStatement sqlStatement) {
+ return ShardingSphereServiceLoader.newServiceInstances(ExtraTextProtocolBackendHandler.class).stream().filter(each -> each.accept(sqlStatement)).findFirst();
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/extra/ExtraTextProtocolBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/extra/ExtraTextProtocolBackendHandler.java
new file mode 100644
index 0000000..cd2be4d
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/extra/ExtraTextProtocolBackendHandler.java
@@ -0,0 +1,35 @@
+/*
+ * 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.text.extra;
+
+import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+/**
+ * Extra text protocol backend handler.
+ */
+public interface ExtraTextProtocolBackendHandler extends TextProtocolBackendHandler {
+
+ /**
+ * Whether accept to process SQL statement.
+ *
+ * @param sqlStatement SQL statement to be judge
+ * @return accept or not
+ */
+ boolean accept(SQLStatement sqlStatement);
+}