?
SELECT
COUNT(*)
FROM
(
SELECT
XXX
FROM
XXX r
LEFT JOIN YYY d ON r.id = d.resource_position_id
AND d.tenant_code = '10001'
WHERE
1 = 1
AND r.custom_id = ?
AND r.is_deleted = 'N'
AND r.tenant_code = '10001'
GROUP BY
r.id
) TOTAL
?1、SelectStatementContext 初始化
public SelectStatementContext(SelectStatement sqlStatement, GroupByContext groupByContext, OrderByContext orderByContext, ProjectionsContext projectionsContext, PaginationContext paginationContext) {
super(sqlStatement);
this.tablesContext = new TablesContext(sqlStatement.getSimpleTableSegments());
this.groupByContext = groupByContext;
this.orderByContext = orderByContext;
this.projectionsContext = projectionsContext;
this.paginationContext = paginationContext;
this.containsSubquery = this.containsSubquery();
}
2、注意
this.tablesContext = new TablesContext(sqlStatement.getSimpleTableSegments());
SelectStatement.java
public Collection<SimpleTableSegment> getSimpleTableSegments() {
Collection<SimpleTableSegment> result = new LinkedList();
Iterator var2 = this.tableReferences.iterator();
while(var2.hasNext()) {
TableReferenceSegment each = (TableReferenceSegment)var2.next();
result.addAll(each.getTables());
}
return result;
}
TableReferenceSegment.java
public Collection<SimpleTableSegment> getTables() {
Collection<SimpleTableSegment> tables = new LinkedList();
if (null != this.tableFactor && this.tableFactor.getTable() instanceof SimpleTableSegment) {
tables.add((SimpleTableSegment)this.tableFactor.getTable());
}
if (!this.joinedTables.isEmpty()) {
Iterator var2 = this.joinedTables.iterator();
while(var2.hasNext()) {
JoinedTableSegment each = (JoinedTableSegment)var2.next();
if (null != each.getTable()) {
tables.add((SimpleTableSegment)each.getTable());
}
}
}
此时tableFactor的类型为SubquerySegment(startIndex=21, stopIndex=555, select=org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement@8a91e92),getTables拿到的是空集合
3、执行查询时,实例化路由引擎
ShardingRouteEngineFactory.java
public static ShardingRouteEngine newInstance(ShardingRule shardingRule, ShardingSphereMetaData metaData, SQLStatementContext sqlStatementContext, ShardingConditions shardingConditions, ConfigurationProperties properties) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
if (sqlStatement instanceof TCLStatement) {
return new ShardingDatabaseBroadcastRoutingEngine();
} else if (sqlStatement instanceof DDLStatement) {
return new ShardingTableBroadcastRoutingEngine(metaData.getSchema(), sqlStatementContext);
} else if (sqlStatement instanceof DALStatement) {
return getDALRoutingEngine(shardingRule, sqlStatement, tableNames);
} else if (sqlStatement instanceof DCLStatement) {
return getDCLRoutingEngine(sqlStatementContext, metaData);
} else if (shardingRule.isAllInDefaultDataSource(tableNames)) {
return new ShardingDefaultDatabaseRoutingEngine(tableNames);
} else if (shardingRule.isAllBroadcastTables(tableNames)) {
return (ShardingRouteEngine)(sqlStatement instanceof SelectStatement ? new ShardingUnicastRoutingEngine(tableNames) : new ShardingDatabaseBroadcastRoutingEngine());
} else if (sqlStatementContext.getSqlStatement() instanceof DMLStatement && tableNames.isEmpty() && shardingRule.hasDefaultDataSourceName()) {
return new ShardingDefaultDatabaseRoutingEngine(tableNames);
} else {
return (ShardingRouteEngine)((!(sqlStatementContext.getSqlStatement() instanceof DMLStatement) || !shardingConditions.isAlwaysFalse()) && !tableNames.isEmpty() && shardingRule.tableRuleExists(tableNames) ? getShardingRoutingEngine(shardingRule, sqlStatementContext, shardingConditions, tableNames, properties) : new ShardingUnicastRoutingEngine(tableNames));
}
}
此时,tableNames,拿到的是空,因为第1步,初始化时,tables是空,最后一步拿到的是默认随机路由引擎ShardingUnicastRoutingEngine
4、路由问题
ShardingUnicastRoutingEngine.java
public RouteResult route(ShardingRule shardingRule) {
RouteResult result = new RouteResult();
String dataSourceName = shardingRule.getShardingDataSourceNames().getRandomDataSourceName();
RouteMapper dataSourceMapper = new RouteMapper(dataSourceName, dataSourceName);
ArrayList tableMappers;
if (shardingRule.isAllBroadcastTables(this.logicTables)) {
tableMappers = new ArrayList(this.logicTables.size());
Iterator var6 = this.logicTables.iterator();
while(var6.hasNext()) {
String each = (String)var6.next();
tableMappers.add(new RouteMapper(each, each));
}
result.getRouteUnits().add(new RouteUnit(dataSourceMapper, tableMappers));
} else if (this.logicTables.isEmpty()) {
result.getRouteUnits().add(new RouteUnit(dataSourceMapper, Collections.emptyList()));
} else if (1 == this.logicTables.size()) {
String logicTableName = (String)this.logicTables.iterator().next();
if (!shardingRule.findTableRule(logicTableName).isPresent()) {
result.getRouteUnits().add(new RouteUnit(dataSourceMapper, Collections.emptyList()));
return result;
}
DataNode dataNode = shardingRule.getDataNode(logicTableName);
result.getRouteUnits().add(new RouteUnit(dataSourceMapper, Collections.singletonList(new RouteMapper(logicTableName, dataNode.getTableName()))));
} else {
tableMappers = new ArrayList(this.logicTables.size());
Set<String> availableDatasourceNames = null;
boolean first = true;
Iterator var8 = this.logicTables.iterator();
while(var8.hasNext()) {
String each = (String)var8.next();
TableRule tableRule = shardingRule.getTableRule(each);
DataNode dataNode = (DataNode)tableRule.getActualDataNodes().get(0);
tableMappers.add(new RouteMapper(each, dataNode.getTableName()));
Set<String> currentDataSourceNames = new HashSet(tableRule.getActualDatasourceNames().size());
Iterator var13 = tableRule.getActualDataNodes().iterator();
while(var13.hasNext()) {
DataNode eachDataNode = (DataNode)var13.next();
currentDataSourceNames.add(eachDataNode.getDataSourceName());
}
if (first) {
availableDatasourceNames = currentDataSourceNames;
first = false;
} else {
availableDatasourceNames = Sets.intersection((Set)availableDatasourceNames, currentDataSourceNames);
}
}
if (((Set)availableDatasourceNames).isEmpty()) {
throw new ShardingSphereConfigurationException("Cannot find actual datasource intersection for logic tables: %s", new Object[]{this.logicTables});
}
dataSourceName = shardingRule.getShardingDataSourceNames().getRandomDataSourceName((Collection)availableDatasourceNames);
result.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), tableMappers));
}
return result;
}
String dataSourceName = shardingRule.getShardingDataSourceNames().getRandomDataSourceName();
数据源是个随机出来的数据源,不走分库算法
|