ludc
2023-03-16 86b6157299a50579f454e4fb45a09ff21d252dab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.core.tenant.dynamic;
 
import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springblade.core.tool.utils.StringUtil;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
 
import static org.springblade.core.tenant.constant.TenantBaseConstant.TENANT_DATASOURCE_GROUP_STATEMENT;
 
/**
 * 租户数据源初始加载
 *
 * @author Chill
 */
public class TenantDataSourceJdbcProvider extends AbstractJdbcDataSourceProvider {
 
    private final String driverClassName;
    private final String url;
    private final String username;
    private final String password;
    private final DynamicDataSourceProperties dynamicDataSourceProperties;
 
    public TenantDataSourceJdbcProvider(DynamicDataSourceProperties dynamicDataSourceProperties, String driverClassName, String url, String username, String password) {
        super(driverClassName, url, username, password);
        this.dynamicDataSourceProperties = dynamicDataSourceProperties;
        this.driverClassName = driverClassName;
        this.url = url;
        this.username = username;
        this.password = password;
    }
 
    @Override
    protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
        // 构建数据源集合
        Map<String, DataSourceProperty> map = new HashMap<>(16);
        // 构建主数据源
        DataSourceProperty masterProperty = new DataSourceProperty();
        masterProperty.setDriverClassName(driverClassName);
        masterProperty.setUrl(url);
        masterProperty.setUsername(username);
        masterProperty.setPassword(password);
        masterProperty.setDruid(dynamicDataSourceProperties.getDruid());
        map.put(dynamicDataSourceProperties.getPrimary(), masterProperty);
        // 构建yml数据源
        Map<String, DataSourceProperty> datasource = dynamicDataSourceProperties.getDatasource();
        if (datasource.size() > 0) {
            map.putAll(datasource);
        }
        // 构建动态数据源
        ResultSet rs = statement.executeQuery(TENANT_DATASOURCE_GROUP_STATEMENT);
        while (rs.next()) {
            String tenantId = rs.getString("tenantId");
            String driver = rs.getString("driverClass");
            String url = rs.getString("url");
            String username = rs.getString("username");
            String password = rs.getString("password");
            if (StringUtil.isNoneBlank(tenantId, driver, url, username, password)) {
                DataSourceProperty jdbcProperty = new DataSourceProperty();
                jdbcProperty.setDriverClassName(driver);
                jdbcProperty.setUrl(url);
                jdbcProperty.setUsername(username);
                jdbcProperty.setPassword(password);
                jdbcProperty.setDruid(dynamicDataSourceProperties.getDruid());
                map.put(tenantId, jdbcProperty);
            }
        }
        return map;
    }
}