ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
package jnpf.base.util;
 
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import jnpf.database.model.entity.DbLinkEntity;
import jnpf.database.source.DbBase;
import jnpf.database.source.impl.DbPostgre;
import jnpf.database.util.*;
import jnpf.util.StringUtil;
import jnpf.util.TenantHolder;
 
 
public class SourceUtil {
    public static DataSourceConfig dbConfig(String dbName, DataSourceUtil linkEntity) {
        boolean isLink = false;
        DbType mpDbType;
        String userName;
        String password;
        String dbSchema;
        String url;
        if (linkEntity == null) {
            if (TenantDataSourceUtil.isTenantAssignDataSource()) {
                linkEntity = TenantDataSourceUtil.getTenantAssignDataSource(TenantHolder.getDatasourceId()).toDbLink(new DbLinkEntity());
            } else {
                linkEntity = DynamicDataSourceUtil.dataSourceUtil.init();
            }
            if (!"KingbaseES".equals(linkEntity.getDbType()) && !"PostgreSQL".equals(linkEntity.getDbType()) && StringUtil.isNotEmpty(dbName)) {
                linkEntity.setDbName(dbName);
            }
        }
        try {
            DbBase dbBase = DbTypeUtil.getDb(linkEntity);
            mpDbType = dbBase.getMpDbType();
            userName = linkEntity.getUserName();
            password = linkEntity.getPassword();
            dbSchema = linkEntity.getDbSchema();
 
            // oracle 默认 schema = username
            if (StringUtil.isEmpty(dbSchema) && (
                    mpDbType.getDb().equalsIgnoreCase(DbType.ORACLE.getDb())
                            || mpDbType.getDb().equalsIgnoreCase(DbType.KINGBASE_ES.getDb())
                            || mpDbType.getDb().equalsIgnoreCase(DbType.DM.getDb())
            )) {
                dbSchema = linkEntity.getUserName();
                if (StringUtil.isNotEmpty(dbName) && !isLink) {
                    dbSchema = dbName;
                }
            }
            //postgre默认 public
            if (mpDbType.getDb().equalsIgnoreCase(DbType.POSTGRE_SQL.getDb())) {
                if (StringUtil.isNotEmpty(dbName) && !isLink) {
                    dbSchema = dbName;
                } else if (StringUtil.isNotEmpty(linkEntity.getDbSchema())) {
                    dbSchema = linkEntity.getDbSchema();
                } else {
                    dbSchema = DbPostgre.DEF_SCHEMA;
                }
            }
            //兼容 SQL_SERVER 默认dbo模式
            if (StringUtil.isEmpty(dbSchema) && mpDbType.getDb().equalsIgnoreCase(DbType.SQL_SERVER.getDb())) {
                dbSchema = "dbo";
            }
            url = ConnUtil.getUrl(linkEntity);
            DataSourceConfig dsc = new DataSourceConfig.Builder(url, userName, password)
                    .schema(dbSchema)
                    .keyWordsHandler(new MySqlKeyWordsHandler())
                    .build();
            return dsc;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }
 
    static class MyPostgreSqlQuery extends PostgreSqlQuery {
 
        @Override
        public String tableFieldsSql() {
            return "SELECT A.attname AS name,format_type (A.atttypid,A.atttypmod) AS type,col_description (A.attrelid,A.attnum) AS comment,\n" +
                    "(CASE WHEN (SELECT COUNT (*) FROM pg_constraint AS PC WHERE PC.conrelid = C.oid AND A.attnum = PC.conkey[1] AND PC.contype = 'p') > 0 THEN 'PRI' ELSE '' END) AS key \n" +
                    "FROM pg_class AS C,pg_attribute AS A WHERE A.attrelid='%s'::regclass AND A.attrelid= C.oid AND A.attnum> 0 AND NOT A.attisdropped ORDER  BY A.attnum";
        }
    }
 
}