Skip to content

Commit

Permalink
Fix #7342
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 15, 2020
1 parent a2c64ce commit 8adec17
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
Expand Up @@ -53,7 +53,7 @@ protected final List<MemoryQueryResultRow> init(final ShardingRule shardingRule,
String actualTableName = memoryResultSetRow.getCell(1).toString();
Optional<TableRule> tableRule = shardingRule.findTableRuleByActualTable(actualTableName);
if (!tableRule.isPresent()) {
if (shardingRule.getTableRules().isEmpty() || schemaMetaData.containsTable(actualTableName) && tableNames.add(actualTableName)) {
if (shardingRule.getTableRules().isEmpty() || tableNames.add(actualTableName)) {
result.add(memoryResultSetRow);
}
} else if (tableNames.add(tableRule.get().getLogicTable())) {
Expand Down
Expand Up @@ -82,10 +82,4 @@ public void assertNextForActualTableNameInTableRule() throws SQLException {
LogicTablesMergedResult actual = new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schemaMetaData, Collections.singletonList(createQueryResult("table_0")));
assertTrue(actual.next());
}

@Test
public void assertNextForActualTableNameNotInTableRule() throws SQLException {
LogicTablesMergedResult actual = new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schemaMetaData, Collections.singletonList(createQueryResult("table_3")));
assertFalse(actual.next());
}
}
Expand Up @@ -17,19 +17,20 @@

package org.apache.shardingsphere.infra.route;

import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
import org.apache.shardingsphere.infra.route.decorator.UnconfiguredSchemaRouteDecorator;
import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -89,7 +90,7 @@ private RouteContext executeRoute(final SQLStatement sqlStatement, final List<Ob
for (Entry<ShardingSphereRule, RouteDecorator> entry : decorators.entrySet()) {
result = entry.getValue().decorate(result, metaData, entry.getKey(), props);
}
return result;
return new UnconfiguredSchemaRouteDecorator().decorate(result, metaData);
}

private RouteContext createRouteContext(final SQLStatement sqlStatement, final List<Object> parameters) {
Expand Down
@@ -0,0 +1,54 @@
/*
* 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.infra.route.decorator;

import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;

import java.util.Collections;

/**
* Unconfigured schema route decorator.
*/
public final class UnconfiguredSchemaRouteDecorator {

/**
* Decorate route context.
*
* @param routeContext route context
* @param metaData meta data of ShardingSphere
* @return decorated route context
*/
public RouteContext decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData) {
if (isNeedUnconfiguredSchema(routeContext.getSqlStatementContext().getSqlStatement())) {
for (String each : metaData.getRuleSchemaMetaData().getUnconfiguredSchemaMetaDataMap().keySet()) {
routeContext.getRouteResult().getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
}
}
return routeContext;
}

// TODO use dynamic config to judge UnconfiguredSchema
private boolean isNeedUnconfiguredSchema(final SQLStatement sqlStatement) {
return sqlStatement instanceof MySQLShowTablesStatement;
}
}

0 comments on commit 8adec17

Please sign in to comment.