Search in sources :

Example 1 with BindParameter

use of com.cubrid.common.ui.query.sqlmap.BindParameter 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

BindParameter (com.cubrid.common.ui.query.sqlmap.BindParameter)1 MapperFile (com.navercorp.dbtools.sqlmap.parser.MapperFile)1 MapperParser (com.navercorp.dbtools.sqlmap.parser.MapperParser)1 MapperParserImpl (com.navercorp.dbtools.sqlmap.parser.MapperParserImpl)1 QueryCondition (com.navercorp.dbtools.sqlmap.parser.QueryCondition)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 IViewPart (org.eclipse.ui.IViewPart)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 PartInitException (org.eclipse.ui.PartInitException)1