懒羊羊
2023-11-14 8286c62256f23bc2367a6729c0f46f84215e380b
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cn.stylefeng.guns.base.db.util;
 
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.base.db.dao.sqls.CreateDatabaseSql;
import cn.stylefeng.guns.base.db.dao.sqls.TableFieldListSql;
import cn.stylefeng.guns.base.db.dao.sqls.TableListSql;
import cn.stylefeng.guns.base.db.entity.DatabaseInfo;
import cn.stylefeng.guns.base.db.exception.DataSourceInitException;
import cn.stylefeng.guns.base.db.model.TableFieldInfo;
import cn.stylefeng.roses.core.config.properties.DruidProperties;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 数据库操作工具类
 *
 * @author fengshuonan
 * @Date 2019/1/13 18:34
 */
@Slf4j
public class DbUtil {
 
    /**
     * 查询某个数据库连接的所有表
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:30
     */
    public static List<Map<String, Object>> selectTables(DatabaseInfo dbInfo) {
        List<Map<String, Object>> tables = new ArrayList<>();
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(
                    dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
 
            //获取数据库名称
            String dbName = getDbName(dbInfo);
 
            //构造查询语句
            PreparedStatement preparedStatement = conn.prepareStatement(new TableListSql().getSql(dbInfo.getJdbcUrl()));
 
            //拼接设置数据库名称
            if (!dbInfo.getJdbcUrl().contains("sqlserver") && !dbInfo.getJdbcUrl().contains("postgresql")) {
                preparedStatement.setString(1, dbName);
            }
 
            ResultSet resultSet = preparedStatement.executeQuery();
 
            while (resultSet.next()) {
                HashMap<String, Object> map = new HashMap<>();
                String tableName = resultSet.getString("tableName");
                String tableComment = resultSet.getString("tableComment");
                map.put("tableName", tableName);
                map.put("tableComment", tableComment);
                tables.add(map);
            }
            return tables;
        } catch (Exception ex) {
            log.error("查询所有表错误!", ex);
            throw new DataSourceInitException(DataSourceInitException.ExEnum.QUERY_DATASOURCE_INFO_ERROR);
        }
    }
 
    /**
     * 查询某个表的所有字段
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:31
     */
    public static List<TableFieldInfo> getTableFields(DatabaseInfo dbInfo, String tableName) {
        ArrayList<TableFieldInfo> fieldList = new ArrayList<>();
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(
                    dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
 
            PreparedStatement preparedStatement = conn.prepareStatement(new TableFieldListSql().getSql(dbInfo.getJdbcUrl()));
 
            if (dbInfo.getJdbcUrl().contains("oracle")) {
                preparedStatement.setString(1, tableName);
            } else if (dbInfo.getJdbcUrl().contains("postgresql")) {
                preparedStatement.setString(1, tableName);
            } else if (dbInfo.getJdbcUrl().contains("sqlserver")) {
                preparedStatement.setString(1, tableName);
            } else {
                String dbName = getDbName(dbInfo);
                preparedStatement.setString(1, tableName);
                preparedStatement.setString(2, dbName);
            }
 
            //执行查询
            ResultSet resultSet = preparedStatement.executeQuery();
 
            while (resultSet.next()) {
                TableFieldInfo tableFieldInfo = new TableFieldInfo();
                String columnName = resultSet.getString("columnName");
                String columnComment = resultSet.getString("columnComment");
                tableFieldInfo.setColumnName(columnName);
                tableFieldInfo.setColumnComment(columnComment);
                tableFieldInfo.setCamelFieldName(StrUtil.toCamelCase(columnName));
                fieldList.add(tableFieldInfo);
            }
            return fieldList;
        } catch (Exception ex) {
            log.error("查询表的所有字段错误!", ex);
            throw new DataSourceInitException(DataSourceInitException.ExEnum.QUERY_DATASOURCE_INFO_ERROR);
        }
    }
 
    /**
     * 创建数据库
     *
     * @author fengshuonan
     * @Date 2019-06-18 15:29
     */
    public static void createDatabase(DruidProperties druidProperties, String databaseName) {
        try {
            Class.forName(druidProperties.getDriverClassName());
            Connection conn = DriverManager.getConnection(druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
 
            //创建sql
            String sql = new CreateDatabaseSql().getSql(druidProperties.getUrl());
            sql = sql.replaceAll("\\?", databaseName);
 
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
 
            int i = preparedStatement.executeUpdate();
            log.info("创建数据库!数量:" + i);
 
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            throw new ServiceException(500, "创建多租户-执行sql出现问题!");
        }
    }
 
    /**
     * 获取数据库名称
     *
     * @author fengshuonan
     * @Date 2019-06-18 15:25
     */
    private static String getDbName(DatabaseInfo dbInfo) {
 
        if (dbInfo.getJdbcUrl().contains("oracle")) {
 
            //如果是oracle,直接返回username
            return dbInfo.getUserName();
 
        } else if (dbInfo.getJdbcUrl().contains("postgresql")) {
 
            //postgresql,直接返回最后一个/后边的字符
            int first = dbInfo.getJdbcUrl().lastIndexOf("/") + 1;
            return dbInfo.getJdbcUrl().substring(first);
 
        } else if (dbInfo.getJdbcUrl().contains("sqlserver")) {
 
            //sqlserver,直接返回最后一个=后边的字符
            int first = dbInfo.getJdbcUrl().lastIndexOf("=") + 1;
            return dbInfo.getJdbcUrl().substring(first);
 
        } else {
 
            //mysql,返回/和?之间的字符
            String jdbcUrl = dbInfo.getJdbcUrl();
            int first = jdbcUrl.lastIndexOf("/") + 1;
            int last = jdbcUrl.indexOf("?");
            return jdbcUrl.substring(first, last);
        }
    }
}