Search in sources :

Example 6 with SqlMapCondition

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

the class SqlMapConditionParser method parse.

/**
	 * Condition 단위의 Element를 통해 SqlMapCondition를 생성한다.
	 *
	 * @param conditionElement Condition 단위의 Element
	 * @param query
	 * @throws Exception
	 */
public SqlMapCondition parse(Element conditionElement, SqlMapQuery query) throws Exception {
    SqlMapConditionParser conditionParser = new SqlMapConditionParser();
    SqlMapParameterParser parameterParser = new SqlMapParameterParser();
    StringBuffer modifiedStatement = new StringBuffer();
    // 최상위 Element의 attribute로 Condition 생성
    SqlMapCondition condition = createSqlMapCondition(conditionElement);
    if (condition != null) {
        parameterParser.parse(conditionElement, query);
        // rootElement 하위의 모든 Node를 읽어온다
        // Node 타입이 TEXT, ELEMENT인 경우에 따라 정보 수집
        Iterator<?> nodeIterator = conditionElement.nodeIterator();
        while (nodeIterator.hasNext()) {
            Node node = (Node) nodeIterator.next();
            switch(node.getNodeType()) {
                case Node.TEXT_NODE:
                case Node.CDATA_SECTION_NODE:
                    // 변환된 statement 취합
                    modifiedStatement.append(node.getText());
                    // Parameter 파싱
                    parameterParser.parse(node, query);
                    break;
                case Node.ELEMENT_NODE:
                    // recursive
                    // 생성된 condition을 childConditionList에서 추가
                    SqlMapCondition childCondition = conditionParser.parse((Element) node, query);
                    if (childCondition != null) {
                        // MyBatis
                        initSqlMapCondition(node, childCondition);
                        condition.getChildConditionList().add(childCondition);
                        // 변환된 statement 취합
                        modifiedStatement.append(childCondition.getKey());
                    }
                    // Parameter 파싱
                    parameterParser.parse(node, query);
                    break;
                default:
                    break;
            }
        }
        // 변경된 statement 저장
        condition.setModifiedStatement(modifiedStatement.toString());
        // include 처리용 statement 저장
        Element copiedElement = conditionElement.createCopy();
        copiedElement.clearContent();
        copiedElement.setText(modifiedStatement.toString());
        condition.setIncludedStatement(copiedElement.asXML());
    }
    return condition;
}
Also used : Node(org.dom4j.Node) Element(org.dom4j.Element) SqlMapCondition(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)

Example 7 with SqlMapCondition

use of com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition 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 8 with SqlMapCondition

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

the class SqlMapConditionLoader method getSqlMapConditionTag.

/**
	 * Dynamic 쿼리의 Tag에 해당하는 Class를 동적으로 생성
	 *
	 * @param tagName Dynamic 쿼리의 Tag 이름 (isNull, isNotNull, isEmpty, ...)
	 * @return 해당  Tag 이름에 맞는 SqlMapCondition 객체
	 * @throws Exception 오류
	 */
public static SqlMapCondition getSqlMapConditionTag(String tagName) throws Exception {
    if (StringUtils.isEmpty(tagName)) {
        throw new Exception("It has no value on the tagName parameter.");
    }
    String classFullName = getClassFullName(tagName);
    try {
        ClassLoader classLoader = SqlMapConditionLoader.class.getClassLoader();
        Class<?> conditionClass = classLoader.loadClass(classFullName);
        SqlMapCondition conditionTag = (SqlMapCondition) conditionClass.newInstance();
        return conditionTag;
    } catch (ClassNotFoundException e) {
        throw new Exception("The SqlMapConditionTag was not defined. [" + classFullName + "]");
    } catch (InstantiationException e) {
        throw new Exception("The SqlMapConditionTag was not defined. [" + classFullName + "]");
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SqlMapCondition(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)

Example 9 with SqlMapCondition

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

the class SqlMapQueryParser method parse.

/**
	 * Query 단위의 Element를 통해 SqlMapQuery를 생성한다.
	 *
	 * @param queryElement Query 단위의 Element
	 * @throws Exception
	 */
public SqlMapQuery parse(Element queryElement) throws Exception {
    StringBuffer modifiedQuery = new StringBuffer();
    SqlMapConditionParser conditionParser = new SqlMapConditionParser();
    SqlMapParameterParser parameterParser = new SqlMapParameterParser();
    // Element 타입의 node에서 Query 정보를 읽어 SqlMapQuery를 생성한다.
    SqlMapQuery query = createSqlMapQuery(queryElement);
    // queryElement 하위의 모든 Node를 읽어온다
    // Node 타입이 TEXT, ELEMENT인 경우에 따라 정보 수집
    Iterator<?> nodeIterator = queryElement.nodeIterator();
    while (nodeIterator.hasNext()) {
        Node node = (Node) nodeIterator.next();
        switch(node.getNodeType()) {
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                modifiedQuery.append(node.getText());
                // Parameter 파싱
                parameterParser.parse(node, query);
                break;
            case Node.ELEMENT_NODE:
                SqlMapCondition condition = conditionParser.parse((Element) node, query);
                if (condition != null) {
                    query.getConditionList().add(condition);
                    modifiedQuery.append(condition.getKey());
                    if (!condition.getChildConditionList().isEmpty()) {
                        for (SqlMapCondition childCondition : condition.getChildConditionList()) {
                            if (childCondition != null) {
                                query.getConditionList().add(childCondition);
                            //									modifiedQuery.append(childCondition.getKey());
                            }
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
    // MyBatis test 속성 내의 조건식을 쿼리 조합시 사용하도록 parameter list에 입력
    extractMyBatisTestConditions(query, query.getConditionList());
    query.setModifiedQuery(modifiedQuery.toString());
    logger.debug(query.getModifiedQuery());
    if (query.isPrimitiveTypeParameter() && query.isDynamicQuery() == false && query.getConditionList().size() > 0) {
        query.addDefaultDynamicParameter();
    }
    return query;
}
Also used : Node(org.dom4j.Node) SqlMapQuery(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapQuery) SqlMapCondition(com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)

Aggregations

SqlMapCondition (com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)9 SqlMapQuery (com.nhn.dbtool.query.parser.sqlmap.model.SqlMapQuery)3 MyBatisTestCondition (com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition)2 SqlMapParameter (com.nhn.dbtool.query.parser.sqlmap.model.SqlMapParameter)2 Node (org.dom4j.Node)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 ArrayList (java.util.ArrayList)1 Document (org.dom4j.Document)1 DocumentException (org.dom4j.DocumentException)1 Element (org.dom4j.Element)1