Search in sources :

Example 1 with DsJdbcUrlEnum

use of com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum in project XHuiCloud by sindaZeng.

the class XHuiDynamicDataSourceProvider method executeStmt.

/**
 * 执行语句获得数据源参数
 * @param statement 语句
 * @return 数据源参数
 * @throws SQLException sql异常
 */
@SneakyThrows
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
    ResultSet rs = statement.executeQuery(properties.getQueryDsSql());
    Map<String, DataSourceProperty> map = new HashMap<>(8);
    List<GenDsInfo> sysTenantDs = populate(rs, GenDsInfo.class);
    for (GenDsInfo ds : sysTenantDs) {
        String url;
        DsJdbcUrlEnum dsJdbcUrlEnum = DsJdbcUrlEnum.get(ds.getType());
        if (DsJdbcUrlEnum.MSSQL == dsJdbcUrlEnum) {
            url = String.format(dsJdbcUrlEnum.getUrl(), ds.getHost(), ds.getPort(), ds.getName());
        } else {
            url = String.format(dsJdbcUrlEnum.getUrl(), ds.getHost(), ds.getPort(), ds.getName());
        }
        DataSourceProperty property = new DataSourceProperty();
        property.setUsername(ds.getUsername());
        property.setPassword(ds.getPassword());
        property.setUrl(url);
        map.put(ds.getName(), property);
    }
    // 添加默认主数据源
    DataSourceProperty property = new DataSourceProperty();
    property.setUsername(properties.getUsername());
    property.setPassword(properties.getPassword());
    property.setUrl(properties.getUrl());
    map.put("master", property);
    return map;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) DsJdbcUrlEnum(com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum) SneakyThrows(lombok.SneakyThrows)

Aggregations

DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)1 GenDsInfo (com.xhuicloud.common.datasource.entity.GenDsInfo)1 DsJdbcUrlEnum (com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 SneakyThrows (lombok.SneakyThrows)1