Search in sources :

Example 1 with QueryCondition

use of com.navercorp.dbtools.sqlmap.parser.QueryCondition 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 2 with QueryCondition

use of com.navercorp.dbtools.sqlmap.parser.QueryCondition in project cubrid-manager by CUBRID.

the class SqlmapNavigatorView method refreshView.

public void refreshView() {
    String queryId = getSelectedQueryId();
    List<String> condListForQuery = new ArrayList<String>();
    List<QueryCondition> condList = SqlmapPersistUtil.getInstance().getConditions(queryId);
    List<Map<String, String>> condData = new ArrayList<Map<String, String>>();
    for (QueryCondition cond : condList) {
        Map<String, String> item = new HashMap<String, String>();
        String condNameValue = cond.getConditionKey() + ":" + cond.getConditionBody();
        item.put("1", condNameValue);
        condData.add(item);
        if (SqlmapPersistUtil.getInstance().isUsedCondition(queryId, condNameValue)) {
            condListForQuery.add(condNameValue);
        }
    }
    condView.setInput(condData);
    List<Map<String, String>> paramData = new ArrayList<Map<String, String>>();
    Map<String, BindParameter> bindParams = SqlmapPersistUtil.getInstance().getBindParameters(queryId);
    for (Map.Entry<String, BindParameter> entry : bindParams.entrySet()) {
        Map<String, String> item = new HashMap<String, String>();
        item.put("0", entry.getValue().getName());
        item.put("1", entry.getValue().getValue());
        item.put("2", entry.getValue().getType().name());
        paramData.add(item);
    }
    paramView.setInput(paramData);
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null) {
        return;
    }
    IEditorPart editorPart = window.getActivePage().getActiveEditor();
    if (!(editorPart instanceof QueryEditorPart)) {
        return;
    }
    QueryEditorPart queryEditorPart = (QueryEditorPart) editorPart;
    MapperFile mapperFile = null;
    try {
        mapperFile = new MapperParserImpl().parse(queryEditorPart.getAllQueries());
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return;
    }
    String generatedQuery = mapperFile.generateQuery(queryId, condListForQuery);
    List<String> bindParamList = QueryUtil.extractBindParameters(mapperFile.generateRawQuery(queryId));
    for (String bindParam : bindParamList) {
        String paramRawName = bindParam;
        String paramName = QueryUtil.extractBindParameterName(paramRawName);
        BindParameter bindValue = bindParams.get(paramName);
        if (bindValue != null) {
            String value = bindValue.getType() == BindParameterType.STRING ? "'" + bindValue.getValue() + "'" : bindValue.getValue();
            generatedQuery = generatedQuery.replace(paramRawName, value);
        }
    }
    sqlView.setText(formator.format(generatedQuery));
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) MapperParserImpl(com.navercorp.dbtools.sqlmap.parser.MapperParserImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryCondition(com.navercorp.dbtools.sqlmap.parser.QueryCondition) Map(java.util.Map) HashMap(java.util.HashMap) MapperFile(com.navercorp.dbtools.sqlmap.parser.MapperFile)

Example 3 with QueryCondition

use of com.navercorp.dbtools.sqlmap.parser.QueryCondition in project cubrid-manager by CUBRID.

the class SqlmapPersistUtil method setConditions.

public void setConditions(String queryId, List<QueryCondition> queryConditions) {
    condValues.put(queryId, queryConditions);
    Set<String> oldUsedConditions = usedConditions.get(queryId);
    Set<String> newUsedConditions = new HashSet<String>();
    for (QueryCondition condition : queryConditions) {
        String conditionValue = condition.getConditionKey() + ":" + condition.getConditionBody();
        if (oldUsedConditions != null && oldUsedConditions.contains(conditionValue)) {
            newUsedConditions.add(conditionValue);
        }
    }
    usedConditions.put(queryId, newUsedConditions);
}
Also used : QueryCondition(com.navercorp.dbtools.sqlmap.parser.QueryCondition) HashSet(java.util.HashSet)

Example 4 with QueryCondition

use of com.navercorp.dbtools.sqlmap.parser.QueryCondition in project cubrid-manager by CUBRID.

the class QueryEditorPart method runQuery.

/**
	 * Execute all the selected SQL script on editor, if not, execute all the
	 * script on editor
	 *
	 * @param isOnlyQueryPlan boolean
	 * @param isSqlmapQuery boolean whether or not it needs parsing a sqlmap query
	 * @param queries String
	 * @param rowParameterList List<List<PstmtParameter>>
	 */
private void runQuery(boolean isOnlyQueryPlan, boolean isSqlmapQuery, String queries, List<List<PstmtParameter>> rowParameterList) {
    // FIXME move this logic to core module
    if (!isConnected()) {
        CommonUITool.openErrorBox(Messages.qedit_tip_run_query);
        return;
    }
    if (StringUtil.isEmpty(queries)) {
        return;
    }
    try {
        connection.getConnection(true);
    } catch (SQLException e) {
        CommonUITool.openErrorBox(e.getLocalizedMessage());
        return;
    }
    try {
        connection.setAutoCommit(isAutocommit);
    } catch (SQLException e) {
        CommonUITool.openErrorBox(e.getLocalizedMessage());
        return;
    }
    if (!connection.hasConnection()) {
        return;
    }
    MapperParser mapperParser = null;
    boolean isXmlQueries = isSqlmapQuery && QueryUtil.isXml(queries);
    if (isXmlQueries) {
        try {
            mapperParser = new MapperParserImpl();
        } catch (Exception ignored) {
            isXmlQueries = false;
        }
    }
    if (isXmlQueries) {
        boolean isNotFirstDisplayed = false;
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            IViewPart view = page.findView(SqlmapNavigatorView.ID);
            if (view == null) {
                showToolTip(qeToolBar, runItem, Messages.titleSqlmapSupports, Messages.msgSqlmapSupports, 3);
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(SqlmapNavigatorView.ID, null, IWorkbenchPage.VIEW_ACTIVATE);
                isNotFirstDisplayed = true;
            }
        } catch (PartInitException e) {
            LOGGER.error(e.getMessage(), e);
        }
        // TODO #664 find query id on current position of xml content
        // TODO #664 position
        String queryId = QueryUtil.findNearbyQueryId(queries, queries.length() / 2);
        MapperFile mapperFile = null;
        try {
            mapperFile = mapperParser.parse(getAllQueries());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            // TODO #664 message
            CommonUITool.openErrorBox(e.getLocalizedMessage());
        }
        List<String> queryIdList = mapperFile.getQueryIdList();
        SqlmapNavigatorView.getInstance().updateQueryIdList(queryIdList, queryId);
        List<QueryCondition> condList = mapperFile.getConditionList(queryId);
        boolean isXmlUpdated = SqlmapPersistUtil.getInstance().isChanged(queryId, condList);
        if (isXmlUpdated) {
            SqlmapPersistUtil.getInstance().setConditions(queryId, condList);
        }
        String generatedQuery = SqlmapPersistUtil.getInstance().generateQuery(mapperFile, queryId);
        String rawQuery = mapperFile.generateRawQuery(queryId);
        if (rawQuery == null) {
            showToolTip(qeToolBar, runItem, Messages.titleSqlmapUpdated, "Cannot find a query by the query id.\nIt seems to be commented out a query.", // TODO i18n
            3);
        }
        List<String> bindParameters = QueryUtil.extractBindParameters(rawQuery);
        for (String parameterOfSqlmap : bindParameters) {
            String parameterName = QueryUtil.extractBindParameterName(parameterOfSqlmap);
            SqlmapPersistUtil.getInstance().addOrModifyBindParameter(queryId, parameterName, null, BindParameterType.STRING.name());
            BindParameter bindParameter = SqlmapPersistUtil.getInstance().getBindParameter(queryId, parameterName);
            if (bindParameter != null) {
                String value = bindParameter.getType().wrap(bindParameter.getValue());
                generatedQuery = generatedQuery.replace(parameterOfSqlmap, value);
            }
        }
        SqlmapNavigatorView.getInstance().refreshView();
        if (!isNotFirstDisplayed) {
            showToolTip(qeToolBar, runItem, Messages.titleSqlmapUpdated, Messages.msgSqlmapUpdated, 1);
        }
        return;
    }
    Vector<String> queryVector = QueryUtil.queriesToQuery(queries);
    if (GeneralPreference.isShowAlertModifiedQueryOnAutoCommit() && isAutocommit() && !isOnlyQueryPlan && checkModifiedQueriesAndAlert(queryVector)) {
        return;
    }
    if (isOnlyQueryPlan) {
        if (!queryPlanItem.isEnabled()) {
            return;
        }
    } else {
        if (!runItem.isEnabled()) {
            return;
        }
    }
    isRunning = true;
    queryPlanItem.setEnabled(false);
    autoCommitItem.setEnabled(false);
    setPstmtParaItem.setEnabled(false);
    if (isOnlyQueryPlan) {
        runQueryPlanOnly(queryVector);
    } else {
        runQuery(queryVector, rowParameterList, connection);
    }
}
Also used : MapperParserImpl(com.navercorp.dbtools.sqlmap.parser.MapperParserImpl) IViewPart(org.eclipse.ui.IViewPart) SQLException(java.sql.SQLException) BindParameter(com.cubrid.common.ui.query.sqlmap.BindParameter) PartInitException(org.eclipse.ui.PartInitException) SQLException(java.sql.SQLException) IOException(java.io.IOException) MapperParser(com.navercorp.dbtools.sqlmap.parser.MapperParser) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) QueryCondition(com.navercorp.dbtools.sqlmap.parser.QueryCondition) MapperFile(com.navercorp.dbtools.sqlmap.parser.MapperFile)

Example 5 with QueryCondition

use of com.navercorp.dbtools.sqlmap.parser.QueryCondition in project cubrid-manager by CUBRID.

the class SqlmapPersistUtil method isChanged.

public boolean isChanged(String queryId, List<QueryCondition> queryConditions) {
    List<QueryCondition> oldConditions = condValues.get(queryId);
    if (oldConditions == null || queryConditions == null || oldConditions.size() != queryConditions.size()) {
        return true;
    }
    Set<String> currentConditionNameSet = new HashSet<String>();
    for (QueryCondition queryCondition : oldConditions) {
        String condition = queryCondition.getConditionKey() + ":" + queryCondition.getConditionBody();
        currentConditionNameSet.add(condition);
    }
    for (int i = 0; i < queryConditions.size(); i++) {
        QueryCondition newCondition = queryConditions.get(i);
        String condition = newCondition.getConditionKey() + ":" + newCondition.getConditionBody();
        if (!currentConditionNameSet.contains(condition)) {
            return true;
        }
    }
    return false;
}
Also used : QueryCondition(com.navercorp.dbtools.sqlmap.parser.QueryCondition) HashSet(java.util.HashSet)

Aggregations

QueryCondition (com.navercorp.dbtools.sqlmap.parser.QueryCondition)6 MapperFile (com.navercorp.dbtools.sqlmap.parser.MapperFile)2 MapperParserImpl (com.navercorp.dbtools.sqlmap.parser.MapperParserImpl)2 HashSet (java.util.HashSet)2 PartInitException (org.eclipse.ui.PartInitException)2 QueryEditorPart (com.cubrid.common.ui.query.editor.QueryEditorPart)1 BindParameter (com.cubrid.common.ui.query.sqlmap.BindParameter)1 MapperParser (com.navercorp.dbtools.sqlmap.parser.MapperParser)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 MyBatisTestCondition (com.nhn.dbtool.query.parser.sqlmap.model.MyBatisTestCondition)1 SelectKeyTag (com.nhn.dbtool.query.parser.sqlmap.model.SelectKeyTag)1 SqlMapCondition (com.nhn.dbtool.query.parser.sqlmap.model.SqlMapCondition)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1