Skip to content

Commit

Permalink
Remove ConnectionSessionAwareRULExecutor (apache#29875)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jan 27, 2024
1 parent 34b69b8 commit 9b19d14
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.distsql.handler.aware;

import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DatabaseConnectionManager;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;

/**
* DistSQL executor connection context aware.
*/
public interface DistSQLExecutorConnectionContextAware {

/**
* Set connection context.
*
* @param connectionContext connection context
*/
void setConnectionContext(ConnectionContext connectionContext);

/**
* Set database connection manager.
*
* @param databaseConnectionManager database connection manager
*/
@SuppressWarnings("rawtypes")
void setDatabaseConnectionManager(DatabaseConnectionManager databaseConnectionManager);

/**
* Set executor statement manager.
*
* @param executorStatementManager executor statement manager
*/
@SuppressWarnings("rawtypes")
void setStatementManager(ExecutorStatementManager executorStatementManager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.handler.distsql.rul.aware;
package org.apache.shardingsphere.distsql.handler.aware;

import org.apache.shardingsphere.distsql.handler.type.rul.RULExecutor;
import org.apache.shardingsphere.distsql.statement.rul.RULStatement;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;

/**
* Connection session aware RUL executor.
*
* @param <T> type of RUL statement
* DistSQL executor database protocol type Aware.
*/
public interface ConnectionSessionAwareRULExecutor<T extends RULStatement> extends RULExecutor<T> {
public interface DistSQLExecutorDatabaseProtocolTypeAware {

/**
* Set connection session.
* Set database protocol type.
*
* @param connectionSession connection session
* @param protocolType database protocol type
*/
void setConnectionSession(ConnectionSession connectionSession);
void setDatabaseProtocolType(DatabaseType protocolType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.rul;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorConnectionContextAware;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseProtocolTypeAware;
import org.apache.shardingsphere.distsql.handler.type.rul.RULExecutor;
import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
import org.apache.shardingsphere.distsql.statement.rul.RULStatement;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.rul.aware.ConnectionSessionAwareRULExecutor;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
Expand Down Expand Up @@ -67,8 +70,16 @@ private List<QueryHeader> createQueryHeader(final RULExecutor<RULStatement> exec
}

private MergedResult createMergedResult(final RULExecutor<RULStatement> executor) throws SQLException {
if (executor instanceof ConnectionSessionAwareRULExecutor) {
((ConnectionSessionAwareRULExecutor<RULStatement>) executor).setConnectionSession(connectionSession);
if (executor instanceof DistSQLExecutorDatabaseAware) {
((DistSQLExecutorDatabaseAware) executor).setDatabase(ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession.getDatabaseName())));
}
if (executor instanceof DistSQLExecutorDatabaseProtocolTypeAware) {
((DistSQLExecutorDatabaseProtocolTypeAware) executor).setDatabaseProtocolType(connectionSession.getProtocolType());
}
if (executor instanceof DistSQLExecutorConnectionContextAware) {
((DistSQLExecutorConnectionContextAware) executor).setConnectionContext(connectionSession.getConnectionContext());
((DistSQLExecutorConnectionContextAware) executor).setDatabaseConnectionManager(connectionSession.getDatabaseConnectionManager());
((DistSQLExecutorConnectionContextAware) executor).setStatementManager(connectionSession.getStatementManager());
}
return new LocalDataMergedResult(executor.getRows(sqlStatement, ProxyContext.getInstance().getContextManager()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.rul.type;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseProtocolTypeAware;
import org.apache.shardingsphere.distsql.handler.type.rul.RULExecutor;
import org.apache.shardingsphere.distsql.statement.rul.sql.FormatStatement;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.handler.distsql.rul.aware.ConnectionSessionAwareRULExecutor;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.api.SQLFormatEngine;

Expand All @@ -35,9 +35,9 @@
* Format SQL executor.
*/
@Setter
public final class FormatSQLExecutor implements ConnectionSessionAwareRULExecutor<FormatStatement> {
public final class FormatSQLExecutor implements RULExecutor<FormatStatement>, DistSQLExecutorDatabaseProtocolTypeAware {

private ConnectionSession connectionSession;
private DatabaseType databaseProtocolType;

@Override
public Collection<String> getColumnNames() {
Expand All @@ -46,7 +46,7 @@ public Collection<String> getColumnNames() {

@Override
public Collection<LocalDataQueryResultRow> getRows(final FormatStatement sqlStatement, final ContextManager contextManager) {
return Collections.singleton(new LocalDataQueryResultRow(formatSQL(sqlStatement.getSql(), connectionSession.getProtocolType())));
return Collections.singleton(new LocalDataQueryResultRow(formatSQL(sqlStatement.getSql(), databaseProtocolType)));
}

private Object formatSQL(final String sql, final DatabaseType databaseType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.rul.type;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseProtocolTypeAware;
import org.apache.shardingsphere.distsql.handler.type.rul.RULExecutor;
import org.apache.shardingsphere.distsql.statement.rul.sql.ParseStatement;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.proxy.backend.handler.distsql.rul.aware.ConnectionSessionAwareRULExecutor;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.util.Arrays;
Expand All @@ -36,9 +37,9 @@
* Parse DistSQL executor.
*/
@Setter
public final class ParseDistSQLExecutor implements ConnectionSessionAwareRULExecutor<ParseStatement> {
public final class ParseDistSQLExecutor implements RULExecutor<ParseStatement>, DistSQLExecutorDatabaseProtocolTypeAware {

private ConnectionSession connectionSession;
private DatabaseType databaseProtocolType;

@Override
public Collection<String> getColumnNames() {
Expand All @@ -53,7 +54,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ParseStatement sqlState

private SQLStatement parseSQL(final ShardingSphereMetaData metaData, final ParseStatement sqlStatement) {
SQLParserRule sqlParserRule = metaData.getGlobalRuleMetaData().getSingleRule(SQLParserRule.class);
return sqlParserRule.getSQLParserEngine(connectionSession.getProtocolType()).parse(sqlStatement.getSql(), false);
return sqlParserRule.getSQLParserEngine(databaseProtocolType).parse(sqlStatement.getSql(), false);
}

@Override
Expand Down
Loading

0 comments on commit 9b19d14

Please sign in to comment.