Java Code Examples for org.apache.commons.lang3.StringUtils#wrapIfMissing()

The following examples show how to use org.apache.commons.lang3.StringUtils#wrapIfMissing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryDescriptor.java    From bird-java with MIT License 5 votes vote down vote up
public static QueryDescriptor parseClass(Class<?> tClass) {
    Map<String, String> fieldMap = new HashMap<>(16);

    if (tClass == null) return null;
    Class tempClass = tClass;
    StringBuilder sb = new StringBuilder();

    while (tempClass != null && !StringUtils.equals(tempClass.getName(), OBJECT_CLASS_NAME)) {
        Field[] tempFields = tempClass.getDeclaredFields();
        for (Field field : tempFields) {
            if(EXCLUDE_FIELD_NAMES.contains(field.getName()))continue;

            TableField tableField = field.getAnnotation(TableField.class);
            if (tableField != null && !tableField.exist()) continue;

            String fieldName = StringUtils.wrapIfMissing(field.getName(), '`');
            String dbFieldName = getDbFieldName(field);
            fieldMap.putIfAbsent(fieldName, dbFieldName);
        }

        tempClass = tempClass.getSuperclass();
    }

    for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
        sb.append(entry.getValue());
        sb.append(" AS ");
        sb.append(entry.getKey());
        sb.append(",");
    }

    String select = StringUtils.stripEnd(sb.toString(), ",");

    String from = "";
    TableName tableName = tClass.getAnnotation(TableName.class);
    if (tableName != null) {
        from = tableName.value();
    }
    return new QueryDescriptor(select, from, fieldMap);
}
 
Example 2
Source File: QueryDescriptor.java    From bird-java with MIT License 5 votes vote down vote up
public static String getDbFieldName(Field field) {
    TableField tableField = field.getAnnotation(TableField.class);
    String dbFieldName = tableField == null ? field.getName() : tableField.value();

    if (StringUtils.startsWith(dbFieldName, "{") && StringUtils.endsWith(dbFieldName, "}")) {
        dbFieldName = StringUtils.removeStart(dbFieldName, "{");
        dbFieldName = StringUtils.removeEnd(dbFieldName, "}");
    } else {
        dbFieldName = StringUtils.wrapIfMissing(dbFieldName, '`');
    }
    return dbFieldName;
}
 
Example 3
Source File: QueryDescriptor.java    From bird-java with MIT License 5 votes vote down vote up
String getDbFieldName(String field) {
    String emptyField = "''";
    String dbDelimiter = ".";
    if (StringUtils.isBlank(field)) return emptyField;
    if (field.contains(dbDelimiter)) return field;

    String fieldName = StringUtils.wrapIfMissing(field, '`');
    return this.fieldMap.getOrDefault(fieldName, fieldName);
}
 
Example 4
Source File: FilterGroup.java    From bird-java with MIT License 4 votes vote down vote up
private String formatRules(List<FilterRule> rules, Map<String, String> fieldNameMap) {
    if (CollectionUtils.isEmpty(rules)) return StringUtils.EMPTY;

    StringBuilder sb = new StringBuilder();

    boolean isStart = true;
    for (FilterRule rule : rules) {
        if (rule == null) continue;

        String field = StringUtils.strip(rule.getField());
        String value = StringUtils.strip(rule.getValue());
        if (StringUtils.isBlank(field) || StringUtils.isBlank(value)) continue;
        if (!isStart) {
            sb.append(" and ");
        }

        FilterOperate ruleOperate = FilterOperate.resolve(rule.getOperate());
        if (ruleOperate == null) {
            ruleOperate = FilterOperate.EQUAL;
        }

        String dbFieldName = this.getDbFieldName(fieldNameMap, field);

        if (ruleOperate == FilterOperate.IN) {
            sb.append(String.format("FIND_IN_SET(%s,%s)", dbFieldName, StringUtils.wrapIfMissing(StringUtils.strip(value, ","), "'")));
        } else {
            switch (ruleOperate) {
                case START_WITH:
                    value = value + "%";
                    break;
                case END_WITH:
                    value = "%" + value;
                    break;
                case CONTAINS:
                    value = "%" + value + "%";
                    break;
                default:
                    break;
            }
            value = StringUtils.wrapIfMissing(value, "'");
            sb.append(dbFieldName).append(" ").append(ruleOperate.getDbValue()).append(" ").append(value);
        }
        isStart = false;
    }
    String where = sb.toString();
    return StringUtils.isNotBlank(where) ? "(" + where + ")" : where;
}