Search in sources :

Example 1 with MyBatisTestCondition

use of com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition in project cubrid-manager by CUBRID.

the class SqlMapQueryParser method extractMyBatisTestConditions.

/**
	 * MyBatis test 속성 내의 조건식을 쿼리 조합시 사용하도록 parameter list에 입력
	 *
	 * @param query
	 * @param conditionList
	 */
private void extractMyBatisTestConditions(SqlMapQuery query, List<SqlMapCondition> conditionList) {
    if (conditionList == null) {
        return;
    }
    for (SqlMapCondition condition : conditionList) {
        if (condition == null || condition.getMyBatisTestConditions() == null) {
            continue;
        }
        for (MyBatisTestCondition mCondition : condition.getMyBatisTestConditions()) {
            String property = mCondition.getProperty();
            SqlMapParameter sqlmapParameter = new SqlMapParameter();
            sqlmapParameter.setName(property);
            sqlmapParameter.setDynamic(true);
            query.addParameter(sqlmapParameter);
        }
        if ("foreach".equals(condition.getType())) {
            SqlMapParameter sqlmapParameter = new SqlMapParameter();
            sqlmapParameter.setName(condition.getCollection());
            sqlmapParameter.setDynamic(true);
            query.addParameter(sqlmapParameter);
        }
        if (condition.getChildConditionList() != null) {
            extractMyBatisTestConditions(query, condition.getChildConditionList());
        }
    }
}
Also used : SqlMapParameter(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapParameter) MyBatisTestCondition(com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition) SqlMapCondition(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)

Example 2 with MyBatisTestCondition

use of com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition in project cubrid-manager by CUBRID.

the class MapperFileImpl method prepareQueryConditionList.

private void prepareQueryConditionList(Map<String, QueryCondition> queryConditionMap, List<SqlMapCondition> sqlMapConditionList) {
    for (SqlMapCondition sqlMapCondition : sqlMapConditionList) {
        if (sqlMapCondition == null) {
            continue;
        }
        if (sqlMapCondition.getMyBatisTestConditions() == null || sqlMapCondition.getMyBatisTestConditions().size() == 0) {
            String value = sqlMapCondition.getValue();
            if (sqlMapCondition instanceof SelectKeyTag) {
                continue;
            }
            if (!(sqlMapCondition instanceof DynamicTag)) {
                if (sqlMapCondition instanceof IsNotEmptyTag) {
                    value = "isNotEmpty";
                } else if (sqlMapCondition instanceof IsEmptyTag) {
                    value = "isEmpty";
                } else if (sqlMapCondition.getCompareValue() != null && sqlMapCondition.getCompareValue().length() > 0) {
                    value = sqlMapCondition.getCompareValue();
                }
                if (sqlMapCondition instanceof IterateTag) {
                // skip
                } else if (!"dynamic_param".equals(sqlMapCondition.getProperty())) {
                    String key = sqlMapCondition.getProperty() + ":" + value;
                    queryConditionMap.put(key, new QueryCondition(sqlMapCondition.getProperty(), value));
                }
            }
        } else {
            for (MyBatisTestCondition mybatisTestCondition : sqlMapCondition.getMyBatisTestConditions()) {
                if (mybatisTestCondition != null) {
                    String key = mybatisTestCondition.getProperty() + ":" + mybatisTestCondition.getValue();
                    queryConditionMap.put(key, new QueryCondition(mybatisTestCondition.getProperty(), mybatisTestCondition.getValue()));
                }
            }
        }
        if (sqlMapCondition.getChildConditionList() != null) {
            prepareQueryConditionList(queryConditionMap, sqlMapCondition.getChildConditionList());
        }
    }
}
Also used : IsNotEmptyTag(com.nhn.dbtool.query.parser.sqlmap.model.IsNotEmptyTag) DynamicTag(com.nhn.dbtool.query.parser.sqlmap.model.DynamicTag) IsEmptyTag(com.nhn.dbtool.query.parser.sqlmap.model.IsEmptyTag) IterateTag(com.nhn.dbtool.query.parser.sqlmap.model.IterateTag) QueryCondition(com.navercorp.dbtools.sqlmap.parser.QueryCondition) MyBatisTestCondition(com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition) SqlMapCondition(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition) SelectKeyTag(com.nhn.dbtool.query.parser.sqlmap.model.SelectKeyTag)

Example 3 with MyBatisTestCondition

use of com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition in project cubrid-manager by CUBRID.

the class MyBatisTestConditionParser method parse.

public List<MyBatisTestCondition> parse(String testString) {
    int type = TYPE_PROPERTY;
    boolean hasPreWhitespace = true;
    List<MyBatisTestCondition> list = new ArrayList<MyBatisTestCondition>();
    MyBatisTestCondition tc = new MyBatisTestCondition();
    list.add(tc);
    // There should be added additional white-spaces due to guarantee a safety at the end of string.
    String test = StringEscapeUtils.unescapeHtml(testString) + "          ";
    StringBuilder buf = new StringBuilder();
    char ch = 0;
    for (int i = 0, len = test.length(); i < len; i++) {
        ch = test.charAt(i);
        boolean hasConcatenation = // ||
        (ch == '|' && test.charAt(i + 1) == '|') || // &&
        (ch == '&' && test.charAt(i + 1) == '&') || // AaNnDd
        (hasPreWhitespace && (ch == 'a' || ch == 'A') && (test.charAt(i + 1) == 'n' || test.charAt(i + 1) == 'N') && (test.charAt(i + 2) == 'd' || test.charAt(i + 2) == 'D') && (test.charAt(i + 3) == ' ' || test.charAt(i + 3) == '\t')) || // OoRr
        (hasPreWhitespace && (ch == 'o' || ch == 'O') && (test.charAt(i + 1) == 'r' || test.charAt(i + 1) == 'R') && (test.charAt(i + 2) == ' ' || test.charAt(i + 2) == '\t'));
        if (hasConcatenation) {
            String concatenationName;
            switch(ch) {
                case 'a':
                case 'A':
                    i += 3;
                    concatenationName = "and";
                    break;
                case '&':
                    i++;
                    concatenationName = "and";
                    break;
                case 'o':
                case 'O':
                    i += 2;
                    concatenationName = "or";
                    break;
                case '|':
                    i++;
                    concatenationName = "or";
                    break;
                default:
                    throw new RuntimeException("It was failed to parse a MyBatis XML file.");
            }
            if (type == TYPE_VALUE) {
                // 이항연산에서 비교할 값
                tc.setValue(buf.toString().trim());
                tc = new MyBatisTestCondition();
                tc.setConcatenation(concatenationName);
                list.add(tc);
            } else if (type == TYPE_PROPERTY) {
                // 단항연산
                tc.setProperty(buf.toString().trim());
                tc = new MyBatisTestCondition();
                tc.setConcatenation(concatenationName);
                list.add(tc);
            } else {
                tc.setConcatenation(concatenationName);
                tc.setProperty(buf.toString().trim());
                tc.setOperator(null);
                tc.setValue(null);
            }
            buf.delete(0, buf.length());
            type = TYPE_PROPERTY;
        } else if (ch == '=' && test.charAt(i + 1) == '=' || ch == '!' && test.charAt(i + 1) == '=' || ch == '>' && test.charAt(i + 1) == '=' || ch == '<' && test.charAt(i + 1) == '=' || ch == '<' && test.charAt(i + 1) == '>' || ch == '>' || ch == '<') {
            String operatorName = ch + "";
            if (test.charAt(i + 1) == '=' || test.charAt(i + 1) == '>') {
                operatorName += test.charAt(i + 1) + "";
                i += 1;
            }
            tc.setOperator(operatorName);
            tc.setProperty(buf.toString().trim());
            buf.delete(0, buf.length());
            type = TYPE_VALUE;
        } else {
            // Prepare the property or the value string
            hasPreWhitespace = ch == ' ' || ch == '\t';
            buf.append(ch);
            if (i >= len - 1) {
                if (type == TYPE_PROPERTY) {
                    tc.setProperty(buf.toString().trim());
                } else if (type == TYPE_VALUE) {
                    tc.setValue(buf.toString().trim());
                }
            }
        }
    }
    // property로 단항 연산자(사용자 정의 함수)가 사용된 경우 value에 true/false 대입
    for (MyBatisTestCondition condition : list) {
        String property = condition.getProperty();
        if (StringUtils.isBlank(property)) {
            // TODO 에러처리 필요?
            continue;
        }
        if (condition.getOperator() != null && condition.getValue() != null) {
            condition.setValue(condition.getOperator() + condition.getValue());
        }
        if (condition.getValue() != null || condition.getOperator() != null) {
            continue;
        }
        if (property.charAt(0) == '!') {
            condition.setProperty(property.substring(1));
            condition.setValue("false");
        } else {
            condition.setValue("true");
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) MyBatisTestCondition(com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition)

Aggregations

MyBatisTestCondition (com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition)3 SqlMapCondition (com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)2 QueryCondition (com.navercorp.dbtools.sqlmap.parser.QueryCondition)1 DynamicTag (com.nhn.dbtool.query.parser.sqlmap.model.DynamicTag)1 IsEmptyTag (com.nhn.dbtool.query.parser.sqlmap.model.IsEmptyTag)1 IsNotEmptyTag (com.nhn.dbtool.query.parser.sqlmap.model.IsNotEmptyTag)1 IterateTag (com.nhn.dbtool.query.parser.sqlmap.model.IterateTag)1 SelectKeyTag (com.nhn.dbtool.query.parser.sqlmap.model.SelectKeyTag)1 SqlMapParameter (com.nhn.dbtool.query.parser.sqlmap.model.SqlMapParameter)1 ArrayList (java.util.ArrayList)1