Skip to content

Commit

Permalink
Refactor PreviewHandler (#25941)
Browse files Browse the repository at this point in the history
* Refactor PreviewHandler

* Update
  • Loading branch information
yx9o authored May 30, 2023
1 parent 618548e commit 0f6bb7e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 163 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,16 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.handler.rul.RULExecutor;
import org.apache.shardingsphere.distsql.parser.statement.rul.RULStatement;
import org.apache.shardingsphere.distsql.parser.statement.rul.sql.PreviewStatement;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.rul.sql.PreviewHandler;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;

import java.util.HashMap;
import java.util.Map;

/**
* RUL backend handler factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RULBackendHandlerFactory {

private static final Map<Class<? extends RULStatement>, Class<? extends RULBackendHandler<?>>> HANDLERS = new HashMap<>();

static {
HANDLERS.put(PreviewStatement.class, PreviewHandler.class);
}

/**
* Create new instance of RUL backend handler.
*
Expand All @@ -52,26 +37,7 @@ public final class RULBackendHandlerFactory {
* @return created instance
*/
public static ProxyBackendHandler newInstance(final RULStatement sqlStatement, final ConnectionSession connectionSession) {
if (TypedSPILoader.contains(RULExecutor.class, sqlStatement.getClass().getName())) {
RULBackendHandler<?> result = new SQLRULBackendHandler<>();
result.init(sqlStatement, connectionSession);
return result;
}
return createRULBackendHandler(sqlStatement, connectionSession);
}

private static RULBackendHandler<?> newInstance(final Class<? extends RULBackendHandler<?>> clazz) {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (final ReflectiveOperationException ignored) {
throw new UnsupportedSQLOperationException(String.format("Can not find public constructor for class `%s`", clazz.getName()));
}
}

private static RULBackendHandler<?> createRULBackendHandler(final RULStatement sqlStatement, final ConnectionSession connectionSession) {
Class<? extends RULBackendHandler<?>> clazz = HANDLERS.get(sqlStatement.getClass());
ShardingSpherePreconditions.checkState(null != clazz, () -> new UnsupportedSQLOperationException(String.format("Unsupported SQL statement : %s", sqlStatement.getClass().getName())));
RULBackendHandler<?> result = newInstance(clazz);
RULBackendHandler<?> result = new SQLRULBackendHandler<>();
result.init(sqlStatement, connectionSession);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class SQLRULBackendHandler<T extends RULStatement> extends RULBacke
private MergedResult mergedResult;

@Override
public ResponseHeader execute() {
public ResponseHeader execute() throws SQLException {
RULExecutor<T> executor = TypedSPILoader.getService(RULExecutor.class, getSqlStatement().getClass().getName());
queryHeaders = createQueryHeader(executor);
mergedResult = createMergedResult(executor);
Expand All @@ -60,7 +60,7 @@ private List<QueryHeader> createQueryHeader(final RULExecutor<T> executor) {
return executor.getColumnNames().stream().map(each -> new QueryHeader("", "", each, each, Types.CHAR, "CHAR", 255, 0, false, false, false, false)).collect(Collectors.toList());
}

private MergedResult createMergedResult(final RULExecutor<T> executor) {
private MergedResult createMergedResult(final RULExecutor<T> executor) throws SQLException {
if (executor instanceof ConnectionSessionRequiredRULExecutor) {
ShardingSphereMetaData metaData = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData();
return new LocalDataMergedResult(((ConnectionSessionRequiredRULExecutor<T>) executor).getRows(metaData, getConnectionSession(), getSqlStatement()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;

import java.sql.SQLException;
import java.util.Collection;

/**
Expand All @@ -39,6 +40,7 @@ public interface ConnectionSessionRequiredRULExecutor<T extends RULStatement> ex
* @param connectionSession connectionSession connection session
* @param sqlStatement SQL statement
* @return query result rows
* @throws SQLException SQL exception
*/
Collection<LocalDataQueryResultRow> getRows(ShardingSphereMetaData metaData, ConnectionSession connectionSession, T sqlStatement);
Collection<LocalDataQueryResultRow> getRows(ShardingSphereMetaData metaData, ConnectionSession connectionSession, T sqlStatement) throws SQLException;
}
Loading

0 comments on commit 0f6bb7e

Please sign in to comment.