boolean isExisted(final String actualTableName) { for (DataNode each : actualDataNodes) { if (each.getTableName().equalsIgnoreCase(actualTableName)) { return true; } } return false; } }
/** * Get actual table names via target data source name. * * @param targetDataSource target data source name * @return names of actual tables */ public Collection<String> getActualTableNames(final String targetDataSource) { Collection<String> result = new LinkedHashSet<>(actualDataNodes.size()); for (DataNode each : actualDataNodes) { if (targetDataSource.equals(each.getDataSourceName())) { result.add(each.getTableName()); } } return result; }
private Collection<TableMetaData> load(final String dataSourceName, final String catalog, final Collection<DataNode> dataNodes) throws SQLException { Collection<TableMetaData> result = new LinkedList<>(); try (Connection connection = connectionManager.getConnection(dataSourceName)) { for (DataNode each : dataNodes) { result.add(new TableMetaData( isTableExist(connection, catalog, each.getTableName()) ? getColumnMetaDataList(connection, catalog, each.getTableName()) : Collections.<ColumnMetaData>emptyList())); } } return result; }
private void processInsertShardingCondition(final TableUnit tableUnit, final InsertShardingCondition shardingCondition, final List<String> expressions, final List<Object> parameters) { for (DataNode each : shardingCondition.getDataNodes()) { if (each.getDataSourceName().equals(tableUnit.getDataSourceName()) && each.getTableName().equals(tableUnit.getRoutingTables().iterator().next().getActualTableName())) { expressions.add(shardingCondition.getInsertValueExpression()); parameters.addAll(shardingCondition.getParameters()); return; } } if (shardingCondition.getDataNodes().isEmpty()) { expressions.add(shardingCondition.getInsertValueExpression()); parameters.addAll(shardingCondition.getParameters()); } } }
private Collection<TableUnit> getAllTableUnits(final String logicTableName) { Collection<TableUnit> result = new LinkedList<>(); TableRule tableRule = shardingRule.getTableRule(logicTableName); for (DataNode each : tableRule.getActualDataNodes()) { TableUnit tableUnit = new TableUnit(each.getDataSourceName()); tableUnit.getRoutingTables().add(new RoutingTable(logicTableName, each.getTableName())); result.add(tableUnit); } return result; } }
/** * Deduce actual table name from other actual table name in same binding table rule. * * @param dataSource data source name * @param logicTable logic table name * @param otherActualTable other actual table name in same binding table rule * @return actual table name */ public String getBindingActualTable(final String dataSource, final String logicTable, final String otherActualTable) { int index = -1; for (TableRule each : tableRules) { index = each.findActualTableIndex(dataSource, otherActualTable); if (-1 != index) { break; } } if (-1 == index) { throw new ShardingConfigurationException("Actual table [%s].[%s] is not in table config", dataSource, otherActualTable); } for (TableRule each : tableRules) { if (each.getLogicTable().equals(logicTable.toLowerCase())) { return each.getActualDataNodes().get(index).getTableName().toLowerCase(); } } throw new ShardingConfigurationException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable); }
private RoutingResult generateRoutingResult(final Collection<DataNode> routedDataNodes) { RoutingResult result = new RoutingResult(); for (DataNode each : routedDataNodes) { TableUnit tableUnit = new TableUnit(each.getDataSourceName()); tableUnit.getRoutingTables().add(new RoutingTable(logicTableName, each.getTableName())); result.getTableUnits().getTableUnits().add(tableUnit); } return result; }
DataNode dataNode = shardingRule.getDataNode(logicTableName); TableUnit tableUnit = new TableUnit(dataNode.getDataSourceName()); tableUnit.getRoutingTables().add(new RoutingTable(logicTableName, dataNode.getTableName())); result.getTableUnits().getTableUnits().add(tableUnit); } else { TableRule tableRule = shardingRule.getTableRule(each); DataNode dataNode = tableRule.getActualDataNodes().get(0); routingTables.add(new RoutingTable(each, dataNode.getTableName())); Set<String> currentDataSourceNames = new HashSet<>(tableRule.getActualDatasourceNames().size()); for (DataNode eachDataNode : tableRule.getActualDataNodes()) {