src/Foundation/DatabaseManager.php
protected function getDBRouter($tableName, $suffix)
    {
        $rs = ['prefix' => '', 'key' => '', 'connection' => null, 'isNoSuffix' => false];

        $defaultMap = !empty($this->configs['tables']['__default__'])
            ? $this->configs['tables']['__default__'] : [];
        $tableMap = !empty($this->configs['tables'][$tableName])
            ? $this->configs['tables'][$tableName] : $defaultMap;

        if (empty($tableMap)) {
            throw new InternalServerError(
                T('No table map config for {tableName}', ['tableName' => $tableName])
            );
        }

        $dbKey = null;
        $dbDefaultKey = null;
        if (!isset($tableMap['map'])) {
            $tableMap['map'] = [];
        }
        foreach ($tableMap['map'] as $map) {
            $isMatch = false;

            if ((isset($map['start']) && isset($map['end']))) {
                if ($suffix !== null && $suffix >= $map['start'] && $suffix <= $map['end']) {
                    $isMatch = true;
                }
            } else {
                $dbDefaultKey = $map['db'];
                if ($suffix === null) {
                    $isMatch = true;
                }
            }

            if ($isMatch) {
                $dbKey = isset($map['db']) ? trim($map['db']) : null;
                break;
            }
        }
        //try to use default map if no perfect match
        if ($dbKey === null) {
            $dbKey = $dbDefaultKey;
            $rs['isNoSuffix'] = true;
        }

        if ($dbKey === null) {
            throw new InternalServerError(
                T('No db router match for {tableName}', ['tableName' => $tableName])
            );
        }

        $rs['connection'] = $this->getConnection($dbKey);
        $rs['prefix'] = isset($tableMap['prefix']) ? trim($tableMap['prefix']) : '';
        $rs['key'] = isset($tableMap['key']) ? trim($tableMap['key']) : 'id';

        return $rs;
    }
protected function getDBRouter($tableName, $suffix)
    {
        $rs = ['prefix' => '', 'key' => '', 'connection' => null, 'isNoSuffix' => false];

        $defaultMap = !empty($this->configs['tables']['__default__'])
            ? $this->configs['tables']['__default__'] : [];
        $tableMap = !empty($this->configs['tables'][$tableName])
            ? $this->configs['tables'][$tableName] : $defaultMap;

        if (empty($tableMap)) {
            throw new InternalServerError(
                T('No table map config for {tableName}', ['tableName' => $tableName])
            );
        }

        $dbKey = null;
        $dbDefaultKey = null;
        if (!isset($tableMap['map'])) {
            $tableMap['map'] = [];
        }
        foreach ($tableMap['map'] as $map) {
            $isMatch = false;

            if ((isset($map['start']) && isset($map['end']))) {
                if ($suffix !== null && $suffix >= $map['start'] && $suffix <= $map['end']) {
                    $isMatch = true;
                }
            } else {
                $dbDefaultKey = $map['db'];
                if ($suffix === null) {
                    $isMatch = true;
                }
            }

            if ($isMatch) {
                $dbKey = isset($map['db']) ? trim($map['db']) : null;
                break;
            }
        }
        //try to use default map if no perfect match
        if ($dbKey === null) {
            $dbKey = $dbDefaultKey;
            $rs['isNoSuffix'] = true;
        }

        if ($dbKey === null) {
            throw new InternalServerError(
                T('No db router match for {tableName}', ['tableName' => $tableName])
            );
        }

        $rs['connection'] = $this->getConnection($dbKey);
        $rs['prefix'] = isset($tableMap['prefix']) ? trim($tableMap['prefix']) : '';
        $rs['key'] = isset($tableMap['key']) ? trim($tableMap['key']) : 'id';

        return $rs;
    }
获取分布式数据库路由

@param string $tableName 数据库表名
@param string $suffix 分布式下的表后缀
@throws InternalServerError
@return array 数据库配置