1821349743@qq.com
2023-02-20 93dd9bc3f16b0f626761ec624f2dc78037568897
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
package com.product.data.utli;
 
 
import com.google.common.collect.Maps;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.data.config.CmnConst;
import com.product.data.config.DatabaseType;
import com.product.data.config.ErrorCode;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Author cheng
 * @Date 2022/2/8 16:36
 * @Desc 通用工具类
 */
public class CommonUtils {
 
 
    public static String getFieldName(String fieldName, DatabaseType dbe) {
        switch (dbe) {
            case MySql:
                return "`" + fieldName + "`";
            case SqlServer:
                return "[" + fieldName + "]";
            case Oracle:
            case PSQL:
                return "\"" + fieldName + '\"';
            case Informix:
                //todo
        }
        return fieldName;
    }
 
    /**
     * 获取字段缓存根据表名
     *
     * @param tableName 表名
     * @return
     * @throws BaseException
     */
    public static Map<String, Map<Object, Object>> getSystemFieldByCache(String tableName) throws BaseException {
        DataPoolCacheImpl dataPoolCache = DataPoolCacheImpl.getInstance();
        DataTableEntity tableInfo = dataPoolCache.getCacheData("所有表信息", new String[]{tableName});
        if (DataTableEntity.isEmpty(tableInfo) || StringUtils.isEmpty(tableInfo.getString(0, CmnConst.UUID))) {
            throw new BaseException(ErrorCode.GET_TABLE_CACHE_FAIL.getValue(), ErrorCode.GET_TABLE_CACHE_FAIL.getText());
        }
        String table_uuid = tableInfo.getString(0, CmnConst.UUID);
        DataTableEntity fieldInfo = dataPoolCache.getCacheData("表字段信息", new String[]{table_uuid});
        if (DataTableEntity.isEmpty(fieldInfo)) {
            throw new BaseException(ErrorCode.GET_FIELD_CACHE_FAIL.getValue(), ErrorCode.GET_FIELD_CACHE_FAIL.getText());
        }
        Map<String, Map<Object, Object>> resultMap = new HashMap<>();
        for (int i = 0; i < fieldInfo.getRows(); i++) {
            FieldSetEntity fs = fieldInfo.getFieldSetEntity(i);
            Map<Object, Object> values = fs.getValues();
            String field_name = fs.getString(CmnConst.FIELD_NAME);
            resultMap.put(field_name, Maps.newHashMap(values));
        }
 
        return resultMap;
    }
 
}