@Override public String apply(final TableRule input) { return input.getLogicTable(); } }));
@Override public String apply(final TableRule input) { return input.getLogicTable().toLowerCase(); } });
/** * Judge contains this logic table in this rule. * * @param logicTableName logic table name * @return contains this logic table or not */ public boolean hasLogicTable(final String logicTableName) { for (TableRule each : tableRules) { if (each.getLogicTable().equals(logicTableName.toLowerCase())) { return true; } } return false; }
/** * Find table rule. * * @param logicTableName logic table name * @return table rule */ public Optional<TableRule> findTableRule(final String logicTableName) { for (TableRule each : tableRules) { if (each.getLogicTable().equalsIgnoreCase(logicTableName)) { return Optional.of(each); } } return Optional.absent(); }
private Map<String, TableMetaData> loadShardingTables(final ShardingRule shardingRule) throws SQLException { Map<String, TableMetaData> result = new HashMap<>(shardingRule.getTableRules().size(), 1); for (TableRule each : shardingRule.getTableRules()) { result.put(each.getLogicTable(), tableMetaDataLoader.load(each.getLogicTable(), shardingRule)); } return result; }
/** * Get logic table name base on logic index name. * * @param logicIndexName logic index name * @return logic table name */ public String getLogicTableName(final String logicIndexName) { for (TableRule each : tableRules) { if (logicIndexName.equals(each.getLogicIndex())) { return each.getLogicTable(); } } throw new ShardingConfigurationException("Cannot find logic table name with logic index name: '%s'", logicIndexName); }
/** * Judge is sharding column or not. * * @param column column object * @return is sharding column or not */ public boolean isShardingColumn(final Column column) { for (TableRule each : tableRules) { if (each.getLogicTable().equalsIgnoreCase(column.getTableName()) && isShardingColumn(each, column)) { return true; } } return false; }
/** * Find column of generated key. * * @param logicTableName logic table name * @return generated key's column */ public Optional<Column> findGenerateKeyColumn(final String logicTableName) { for (TableRule each : tableRules) { if (each.getLogicTable().equalsIgnoreCase(logicTableName) && null != each.getGenerateKeyColumn()) { return Optional.of(new Column(each.getGenerateKeyColumn(), logicTableName)); } } return Optional.absent(); }
private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException { List<MemoryQueryResultRow> result = new LinkedList<>(); for (QueryResult each : queryResults) { while (each.next()) { MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(each); String actualTableName = memoryResultSetRow.getCell(1).toString(); Optional<TableRule> tableRule = shardingRule.findTableRuleByActualTable(actualTableName); if (!tableRule.isPresent()) { if (shardingRule.getTableRules().isEmpty() || shardingTableMetaData.containsTable(actualTableName) && tableNames.add(actualTableName)) { result.add(memoryResultSetRow); } } else if (tableNames.add(tableRule.get().getLogicTable())) { memoryResultSetRow.setCell(1, tableRule.get().getLogicTable()); setCellValue(memoryResultSetRow, tableRule.get().getLogicTable(), actualTableName); result.add(memoryResultSetRow); } } } if (!result.isEmpty()) { setCurrentResultSetRow(result.get(0)); } return result.iterator(); }
if (tableRule.isPresent()) { if (!bindingTableNames.contains(each)) { result.add(new StandardRoutingEngine(shardingRule, tableRule.get().getLogicTable(), shardingConditions).route());
/** * 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); }