Search in sources :

Example 1 with MapperFile

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

use of com.navercorp.dbtools.sqlmap.parser.MapperFile 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)

Aggregations

MapperFile (com.navercorp.dbtools.sqlmap.parser.MapperFile)2 MapperParserImpl (com.navercorp.dbtools.sqlmap.parser.MapperParserImpl)2 QueryCondition (com.navercorp.dbtools.sqlmap.parser.QueryCondition)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 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 IEditorPart (org.eclipse.ui.IEditorPart)1 IViewPart (org.eclipse.ui.IViewPart)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1