提交 3df4aadf 编写于 作者: Z zhyee 提交者: Liang Zhang

Fix SCTL statement execute error with comments (#3093)

* Fix SCTL statement execute error with comment

* Add  SCTLUtils class to trim sql
上级 c91225b3
...@@ -38,6 +38,7 @@ import org.apache.shardingsphere.shardingproxy.backend.text.admin.UnicastBackend ...@@ -38,6 +38,7 @@ import org.apache.shardingsphere.shardingproxy.backend.text.admin.UnicastBackend
import org.apache.shardingsphere.shardingproxy.backend.text.admin.UseDatabaseBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.admin.UseDatabaseBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.query.QueryBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.query.QueryBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.sctl.ShardingCTLBackendHandlerFactory; import org.apache.shardingsphere.shardingproxy.backend.text.sctl.ShardingCTLBackendHandlerFactory;
import org.apache.shardingsphere.shardingproxy.backend.text.sctl.utils.SCTLUtils;
import org.apache.shardingsphere.shardingproxy.backend.text.transaction.SkipBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.transaction.SkipBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.transaction.TransactionBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.transaction.TransactionBackendHandler;
import org.apache.shardingsphere.spi.database.DatabaseType; import org.apache.shardingsphere.spi.database.DatabaseType;
...@@ -60,8 +61,9 @@ public final class TextProtocolBackendHandlerFactory { ...@@ -60,8 +61,9 @@ public final class TextProtocolBackendHandlerFactory {
* @return instance of text protocol backend handler * @return instance of text protocol backend handler
*/ */
public static TextProtocolBackendHandler newInstance(final DatabaseType databaseType, final String sql, final BackendConnection backendConnection) { public static TextProtocolBackendHandler newInstance(final DatabaseType databaseType, final String sql, final BackendConnection backendConnection) {
if (sql.toUpperCase().startsWith(ShardingCTLBackendHandlerFactory.SCTL)) { final String trimSql = SCTLUtils.trimComment(sql);
return ShardingCTLBackendHandlerFactory.newInstance(sql, backendConnection); if (trimSql.toUpperCase().startsWith(ShardingCTLBackendHandlerFactory.SCTL)) {
return ShardingCTLBackendHandlerFactory.newInstance(trimSql, backendConnection);
} }
SQLStatement sqlStatement = new SQLParseKernel(ParseRuleRegistry.getInstance(), databaseType, sql).parse(); SQLStatement sqlStatement = new SQLParseKernel(ParseRuleRegistry.getInstance(), databaseType, sql).parse();
if (sqlStatement instanceof TCLStatement) { if (sqlStatement instanceof TCLStatement) {
......
/*
* 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.shardingproxy.backend.text.sctl.utils;
public class SCTLUtils {
private static final String COMMENT_PREFIX = "/*";
private static final String COMMENT_SUFFIX = "*/";
private static final String SQL_END = ";";
/**
* Trim the comment of sql.
*
* @param sql SQL to be trim
* @return the sql remove comment
*/
public static String trimComment(final String sql) {
String result = sql;
if (sql.startsWith(COMMENT_PREFIX)) {
result = result.substring(sql.indexOf(COMMENT_SUFFIX) + 2);
}
if (sql.endsWith(SQL_END)) {
result = result.substring(0, result.length() - 1);
}
return result.trim();
}
}
...@@ -30,6 +30,7 @@ import org.apache.shardingsphere.shardingproxy.backend.text.admin.UnicastBackend ...@@ -30,6 +30,7 @@ import org.apache.shardingsphere.shardingproxy.backend.text.admin.UnicastBackend
import org.apache.shardingsphere.shardingproxy.backend.text.admin.UseDatabaseBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.admin.UseDatabaseBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.query.QueryBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.query.QueryBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.sctl.set.ShardingCTLSetBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.sctl.set.ShardingCTLSetBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.sctl.show.ShardingCTLShowBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.transaction.SkipBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.transaction.SkipBackendHandler;
import org.apache.shardingsphere.shardingproxy.backend.text.transaction.TransactionBackendHandler; import org.apache.shardingsphere.shardingproxy.backend.text.transaction.TransactionBackendHandler;
import org.apache.shardingsphere.spi.database.DatabaseType; import org.apache.shardingsphere.spi.database.DatabaseType;
...@@ -70,6 +71,13 @@ public final class TextProtocolBackendHandlerFactoryTest { ...@@ -70,6 +71,13 @@ public final class TextProtocolBackendHandlerFactoryTest {
TextProtocolBackendHandler actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection); TextProtocolBackendHandler actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection);
assertThat(actual, instanceOf(ShardingCTLSetBackendHandler.class)); assertThat(actual, instanceOf(ShardingCTLSetBackendHandler.class));
} }
@Test
public void assertNewInstanceSCTLWithComment() {
String sql = "/*ApplicationName=DataGrip 2018.1.4*/ sctl:show cached_connections;";
TextProtocolBackendHandler actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection);
assertThat(actual, instanceOf(ShardingCTLShowBackendHandler.class));
}
@Test @Test
public void assertNewInstanceWithBegin() { public void assertNewInstanceWithBegin() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册