Search in sources :

Example 11 with IgnoreKeyCaseMap

use of org.sagacity.sqltoy.model.IgnoreKeyCaseMap in project sagacity-sqltoy by chenrenfei.

the class MacroUtilsTest method main.

public static void main(String[] args) {
    String macroStr = "@substr(${corpId},0,2)-SE@substr(@day(yyMMdd),0,4)";
    IgnoreKeyCaseMap keyValues = new IgnoreKeyCaseMap();
    keyValues.put("corpId", "HX02");
    keyValues.put("bizDate", DateUtil.parseString("2020-02-12"));
    System.err.println(MacroUtils.replaceMacros(macroStr, keyValues));
}
Also used : IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap)

Example 12 with IgnoreKeyCaseMap

use of org.sagacity.sqltoy.model.IgnoreKeyCaseMap in project sagacity-sqltoy by chenrenfei.

the class RedisIdGenerator method getId.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.plugins.id.IdGenerator#getId(java.lang.String,
	 * java.lang.String, java.lang.Object[], int)
	 */
@Override
public Object getId(String tableName, String signature, String[] relatedColumns, Object[] relatedColValue, Date bizDate, String idJavaType, int length, int sequencSize) {
    String key = (signature == null ? "" : signature);
    // 主键生成依赖业务的相关字段值
    IgnoreKeyCaseMap<String, Object> keyValueMap = new IgnoreKeyCaseMap<String, Object>();
    if (relatedColumns != null && relatedColumns.length > 0) {
        for (int i = 0; i < relatedColumns.length; i++) {
            keyValueMap.put(relatedColumns[i], relatedColValue[i]);
        }
    }
    // 替换signature中的@df() 和@case()等宏表达式
    String realKey = MacroUtils.replaceMacros(key, keyValueMap);
    // 没有宏
    if (realKey.equals(key)) {
        // 长度够放下6位日期 或没有设置长度且流水长度小于6,则默认增加一个6位日期作为前置
        if ((length <= 0 && sequencSize < 6) || (length - realKey.length() > 6)) {
            Date realBizDate = (bizDate == null ? new Date() : bizDate);
            realKey = realKey.concat(DateUtil.formatDate(realBizDate, (dateFormat == null) ? "yyMMdd" : dateFormat));
        }
    }
    // 参数替换
    if (!keyValueMap.isEmpty()) {
        realKey = MacroUtils.replaceParams(realKey, keyValueMap);
    }
    // 结合redis计数取末尾几位顺序数
    Long result;
    // update 2019-1-24 key命名策略改为SQLTOY_GL_ID:tableName:xxx 便于redis检索
    if (tableName != null) {
        result = generateId(realKey.equals("") ? tableName : tableName.concat(":").concat(realKey));
    } else {
        result = generateId(realKey);
    }
    return realKey.concat(StringUtil.addLeftZero2Len("" + result, (sequencSize > 0) ? sequencSize : length - realKey.length()));
}
Also used : RedisAtomicLong(org.springframework.data.redis.support.atomic.RedisAtomicLong) Date(java.util.Date) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap)

Example 13 with IgnoreKeyCaseMap

use of org.sagacity.sqltoy.model.IgnoreKeyCaseMap in project sagacity-sqltoy by chenrenfei.

the class BeanUtil method reflectBeansToList.

/**
 * @todo 利用java.lang.reflect并结合页面的property, 从对象中取出对应方法的值,组成一个List
 * @param datas
 * @param properties
 * @param reflectPropsHandler
 * @return
 * @throws Exception
 */
public static List reflectBeansToList(List datas, String[] properties, ReflectPropsHandler reflectPropsHandler) throws RuntimeException {
    if (null == datas || datas.isEmpty() || null == properties || properties.length < 1) {
        return null;
    }
    List resultList = new ArrayList();
    try {
        Object rowObject = null;
        int methodLength = properties.length;
        // 判断是否存在属性值处理反调
        boolean hasHandler = (reflectPropsHandler != null) ? true : false;
        // 存在反调,则将对象的属性和属性所在的顺序放入hashMap中,便于后面反调中通过属性调用
        if (hasHandler) {
            HashMap<String, Integer> propertyIndexMap = new HashMap<String, Integer>();
            for (int i = 0; i < methodLength; i++) {
                propertyIndexMap.put(properties[i].toLowerCase(), i);
            }
            reflectPropsHandler.setPropertyIndexMap(propertyIndexMap);
        }
        // 判断是否有级联
        boolean hasInnerClass = false;
        for (String prop : properties) {
            if (prop.contains(".")) {
                hasInnerClass = true;
                break;
            }
        }
        // 级联含子对象模式(属性名称:staff.name 模式)
        if (hasInnerClass) {
            Object[] rowAry;
            for (int i = 0, n = datas.size(); i < n; i++) {
                rowObject = datas.get(i);
                if (rowObject != null) {
                    List rowList = new ArrayList();
                    rowAry = reflectBeanToAry(rowObject, properties, null, reflectPropsHandler);
                    for (Object cell : rowAry) {
                        rowList.add(cell);
                    }
                    resultList.add(rowList);
                }
            }
            return resultList;
        }
        // 非级联模式
        Method[] realMethods = null;
        boolean inited = false;
        Object[] params = new Object[] {};
        // 增加map类型支持
        boolean isMap = false;
        if (datas.get(0) != null) {
            Class valueClass = datas.get(0).getClass();
            if (valueClass.equals(HashMap.class) || valueClass.equals(ConcurrentHashMap.class) || valueClass.equals(Map.class) || valueClass.equals(ConcurrentMap.class) || HashMap.class.equals(valueClass) || LinkedHashMap.class.equals(valueClass) || ConcurrentHashMap.class.equals(valueClass) || Map.class.equals(valueClass)) {
                isMap = true;
            }
        }
        Iterator iter;
        String fieldLow;
        Map.Entry<String, Object> entry;
        Map rowMap;
        for (int i = 0, n = datas.size(); i < n; i++) {
            rowObject = datas.get(i);
            if (null != rowObject) {
                List dataList = new ArrayList();
                // 2021-10-09 支持map类型
                if (isMap) {
                    if (rowObject instanceof IgnoreKeyCaseMap) {
                        rowMap = (IgnoreKeyCaseMap) rowObject;
                        for (int j = 0; j < methodLength; j++) {
                            dataList.add(rowMap.get(properties[j]));
                        }
                    } else {
                        rowMap = (Map) rowObject;
                        // 考虑key大小写兼容
                        for (int j = 0; j < methodLength; j++) {
                            fieldLow = properties[j].toLowerCase();
                            iter = rowMap.entrySet().iterator();
                            while (iter.hasNext()) {
                                entry = (Map.Entry<String, Object>) iter.next();
                                if (entry.getKey().toLowerCase().equals(fieldLow)) {
                                    dataList.add(entry.getValue());
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    // 第一行数据
                    if (!inited) {
                        realMethods = matchGetMethods(rowObject.getClass(), properties);
                        inited = true;
                    }
                    for (int j = 0; j < methodLength; j++) {
                        if (realMethods[j] != null) {
                            dataList.add(realMethods[j].invoke(rowObject, params));
                        } else {
                            dataList.add(null);
                        }
                    }
                }
                // 反调对数据值进行加工处理
                if (hasHandler) {
                    reflectPropsHandler.setRowIndex(i);
                    reflectPropsHandler.setRowList(dataList);
                    reflectPropsHandler.process();
                    resultList.add(reflectPropsHandler.getRowList());
                } else {
                    resultList.add(dataList);
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("BeanUtil.reflectBeansToList 方法,第:{}行数据为null,如果是sql查询请检查写法是否正确!", i);
                } else {
                    err.println("BeanUtil.reflectBeansToList 方法,第:{" + i + "}行数据为null,如果是sql查询请检查写法是否正确!");
                }
                resultList.add(null);
            }
        }
    } catch (Exception e) {
        logger.error("反射Java Bean获取数据组装List集合异常!{}", e.getMessage());
        e.printStackTrace();
        throw new RuntimeException("反射Java Bean获取数据组装List集合异常!" + e.getMessage());
    }
    return resultList;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) BigInteger(java.math.BigInteger) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

IgnoreKeyCaseMap (org.sagacity.sqltoy.model.IgnoreKeyCaseMap)13 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 ArrayList (java.util.ArrayList)6 SqlLoop (org.sagacity.sqltoy.plugins.id.macro.impl.SqlLoop)5 Map (java.util.Map)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 TableCascadeModel (org.sagacity.sqltoy.config.model.TableCascadeModel)2 StaffInfoVO (org.sagacity.sqltoy.demo.vo.StaffInfoVO)2 ReverseList (org.sagacity.sqltoy.plugins.calculator.ReverseList)2 UnpivotList (org.sagacity.sqltoy.plugins.calculator.UnpivotList)2 AbstractMacro (org.sagacity.sqltoy.plugins.id.macro.AbstractMacro)2 Method (java.lang.reflect.Method)1 BigInteger (java.math.BigInteger)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)1