shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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 com.iplatform.gather.service;
 
import com.iplatform.model.po.S_host;
import com.iplatform.model.po.Sdc_meta_db;
import com.iplatform.model.po.Sdc_meta_table;
import com.iplatform.model.po.Sdc_store;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class StoreServiceImpl extends BaseServiceImpl {
 
//    private MetaDataEngine metaDataEngine;
//    @Autowired
//    public StoreServiceImpl(MetaDataEngine metaDataEngine){
//        this.metaDataEngine = metaDataEngine;
//    }
 
    public GenericPager<Sdc_store> queryPageStore() {
        return this.selectSplit(new Sdc_store(), null, null);
    }
 
    public Sdc_store queryOne(String storeId) {
        return this.get(new Sdc_store(storeId));
    }
 
    public List<Sdc_meta_db> queryAllMetaDbs() {
        List<S_host> list = this.selectAll(new S_host());
        List<Sdc_meta_db> dbList = this.selectAll(new Sdc_meta_db());
 
        // 把存储数据库的连接“用户名、密码”信息也写入,方面后面使用
        for (Sdc_meta_db md : dbList) {
            String[] hostInfo = md.getHost_info().split(StringUtils.SEPARATOR_COLON);
            for (S_host h : list) {
                if (h.getUrl().equals(hostInfo[0]) && h.getPort() == Integer.parseInt(hostInfo[1])) {
                    md.setUsername(h.getAuthentication());
                    md.setPassword(h.getCertification());
                }
            }
        }
        return dbList;
    }
 
    public List<Sdc_meta_table> queryAllMetaTables() {
        return this.selectAll(new Sdc_meta_table());
    }
 
    public List<Sdc_meta_table> queryMetaTables(long dbId) {
        Sdc_meta_table table = new Sdc_meta_table();
        table.setDb_id(dbId);
        return this.select(new Sdc_meta_table(), "where db_id=?", new Object[]{dbId});
    }
 
    /**
     * 根据表名前缀,查询所有符合条件的表集合。如:base_custdept_ -->
     * @param prefix
     * @return
     */
    public List<Sdc_meta_table> queryMetaTablesByName(String prefix) {
        GenericPager<Sdc_meta_table> pager = this.selectSplit("select * from sdc_meta_table where table_name like ?"
                , new Object[]{prefix+"%"}, 0, 1024, new Sdc_meta_table());
        return pager.getDatas();
    }
 
    public GenericPager<Sdc_meta_db> queryPageMetaDbs(String storeId) {
        return this.selectSplit("select * from sdc_meta_db where store_id=? order by create_time", new Object[]{storeId}, new Sdc_meta_db());
    }
 
    public GenericPager<Sdc_meta_table> queryPageMetaTables(String storeId) {
        return this.selectSplit("select * from sdc_meta_table where store_id=? order by create_time", new Object[]{}, new Sdc_meta_table());
    }
 
//    public GenericPager<Sdc_meta_table> queryPageMetaTables(String storeId, long metaDbId) {
//        // 查出来本页所有表中的数据量大小
//        GenericPager<Sdc_meta_table> result = this.queryPageMetaTables(storeId);
//        List<Sdc_meta_table> datas = result.getDatas();
//
//        // 获取得到的数据表名字集合,为下面查询记录总数准备
//        List<String> tableNameList = new ArrayList<String>();
//        for (Sdc_meta_table mt : datas) {
//            tableNameList.add(mt.getTable_name());
//        }
//
//        // 把找到的每个表的记录数设置到属性中
//        // Address address = metaCache.getDatabaseAddress(storeId, metaDbId);
//        TableInfo ti = null;
//        try {
//            Map<String, TableInfo> map = metaDataEngine.getTableRows(storeId, metaDbId, tableNameList);
//            for (Sdc_meta_table mt : datas) {
//                ti = map.get(mt.getTable_name());
//                if (ti == null) {
//                    mt.setRow_count((long)-1);
//                } else {
//                    mt.setRow_count(ti.getRows());
//                }
//            }
//        } catch (Exception ex) {
//            log.error("获取表结构元数据出现错误,可能物理表不存在", ex);
//            for (Sdc_meta_table mt : datas) {
//                mt.setRow_count((long)-1);
//            }
//        }
//        return result;
//    }
 
    /**
     * 添加记录,告诉系统,在元数据中加入了新的数据库信息
     *
     * @param metaDb
     */
    public void execSaveMetaDb(Sdc_meta_db metaDb) {
        this.update("update sdc_meta_db set is_using=0 where store_id=?", new Object[]{metaDb.getStore_id()});
        this.insert(metaDb);
    }
 
    /**
     * 添加表元数据信息,同时增加数据库中记录的表数量
     *
     * @param metaTable
     */
    public void execSaveMetaTable(Sdc_meta_table metaTable) {
        this.insert(metaTable);
//        Sdc_meta_db db = new Sdc_meta_db(metaTable.getDb_id());
//        db.setTable_count();
        this.update("update sdc_meta_db set table_count=table_count+1 where id=?", new Object[]{metaTable.getDb_id()});
    }
 
    /**
     * 删除存储引擎中的表记录,同时更新缓存。
     * </p>
     * 注意:该方法仅仅在测试时调用,在正式生产环境中是不会用到该方法。
     *
     * @param metaTableId
     */
    public void execRemoveTable(long metaTableId) {
        Sdc_meta_table mt = this.get(new Sdc_meta_table(metaTableId));
        Sdc_meta_db md = this.get(new Sdc_meta_db(mt.getDb_id()));
        this.delete(new Sdc_meta_table(metaTableId));
 
        // 在controller中调用缓存删除,2022-09-20
//        String[] hostPort = MetaDataUtils.getHostUrlPort(md.getHostInfo());
//        Address address = new Address();
//        address.setUrl(hostPort[0]);
//        address.setPort(Integer.parseInt(hostPort[1]));
//        address.setService(md.getDatabaseName());
//        metaCache.removeTable(mt.getStoreId(), address, mt.getTableName());
    }
 
    /**
     * 对存储项目进行编辑更新操作
     *
     * @param entity
     */
    public void execUpdateStore(Sdc_store entity) {
//        String storeId = entity.getId();
//        Store store = storeDao.get(storeId);
//        if (StringUtils.isNotEmpty(entity.getDescription())) {
//            store.setDescription(entity.getDescription());
//        }
//        if (StringUtils.isNotEmpty(entity.getDefineName())) {
//            store.setDefineName(entity.getDefineName());
//        }
//        store.setType(entity.getType());
//        store.setStrategy(entity.getStrategy());
//        store.setInnerUse(entity.getInnerUse());
//        store.setSelectHosts(entity.getSelectHosts());
//        store.setDatabaseType(entity.getDatabaseType());
//        store.setDeleted(entity.getDeleted());
//        store.setCreateTime(entity.getCreateTime());
//        store.setCreateUser(entity.getCreateUser());
//        storeDao.save(store);
        this.update(entity);
    }
 
}