shikeying
2024-05-08 8924870a053f0b882ada86421c062cbdb9cff093
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
package com.iplatform.base;
 
import com.walker.connector.LocalAddress;
import com.walker.db.DatabaseType;
import com.walker.db.TableInfo;
import com.walker.db.page.GenericPager;
import com.walker.dbmeta.DatabaseMetaEngine;
import com.walker.dbmeta.FieldInfo;
import com.walker.dbmeta.support.DamengMetaEngine;
import com.walker.dbmeta.support.MySQLMetaEngine;
import com.walker.dbmeta.support.OracleMetaEngine;
import com.walker.dbmeta.support.PostgresMetaEngine;
import com.walker.dbmeta.support.SqlserverMetaEngine;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 定义本地数据库元数据引擎对象。<p></p>
 * 用于查询数据库中表和字段记录。
 * @author 时克英
 * @date 2022-11-22
 */
public class LocalDatabaseMetaEngine {
 
    private DatabaseMetaEngine engine;
 
    private LocalAddress localAddress;
 
    public void setLocalAddress(LocalAddress localAddress) {
        if(localAddress == null){
            throw new IllegalArgumentException("LocalAddress 必须提供");
        }
        this.localAddress = localAddress;
    }
 
    public void setDatabaseType(DatabaseType databaseType){
        if(databaseType == DatabaseType.DERBY){
            throw new UnsupportedOperationException("不支持该数据库:" + databaseType.name());
        } else if(databaseType == DatabaseType.MYSQL){
            engine = new MySQLMetaEngine();
 
        } else if(databaseType == DatabaseType.ORACLE){
            engine = new OracleMetaEngine();
 
        } else if(databaseType == DatabaseType.POSTGRES){
            engine = new PostgresMetaEngine();
 
        } else if(databaseType == DatabaseType.SQLSERVER){
            engine = new SqlserverMetaEngine();
 
        } else if(databaseType == DatabaseType.DAMENG){
            // 2023-03-03
            engine = new DamengMetaEngine();
 
        } else
            throw new IllegalArgumentException("unsupported database type: " + databaseType.name());
        System.out.println("创建:" + engine.getClass().getName());
    }
 
    public List<FieldInfo> getFieldsObject(String tableName){
        return engine.getFieldsObject(this.localAddress, tableName);
    }
 
    public List<String> getTableNamesByLike(String tableNameLike){
        return engine.getTableNamesByLike(this.localAddress, tableNameLike);
    }
 
    /**
     * 分页返回表信息集合。当前页信息通过对象: ListPageContext.getCurrentPageIndex()方法获得。
     * @param tableNameLike 表名模糊查询,如果为空字符串则查询所有。
     * @return
     * @date 2022-11-22
     */
    public GenericPager<TableInfo> queryPageTableNamesByLike(String tableNameLike
//            , int pageIndex, int pageSize
    ){
        return this.engine.queryPageTableNamesByLike(this.localAddress, tableNameLike);
    }
 
    /**
     * 返回指定表元数据信息。
     * @param tableName 表名称
     * @return
     * @date 2022-11-26
     */
    public TableInfo queryOneTableInfo(String tableName){
        List<String> tableList = new ArrayList<>(2);
        tableList.add(tableName);
        Map<String, TableInfo> map = this.engine.getTableRows(this.localAddress, tableList);
        if(map == null || map.size() == 0){
            return null;
        }
        return map.get(tableName);
    }
}