Search in sources :

Example 6 with QueryTokenizer

use of org.talend.sqlbuilder.util.QueryTokenizer in project tdi-studio-se by Talend.

the class DB2ExplainPlanAction method run.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.sqlbuilder.actions.AbstractEditorAction#run()
     */
//$NON-NLS-1$
@SuppressWarnings("unchecked")
@Override
public void run() {
    RepositoryNode node = editor.getRepositoryNode();
    SessionTreeNodeManager nodeManager = new SessionTreeNodeManager();
    SessionTreeNode runNode = null;
    try {
        runNode = nodeManager.getSessionTreeNode(node, editor.getDialog().getSelectedContext());
    } catch (Exception e) {
        //$NON-NLS-1$
        MessageDialog.openError(null, Messages.getString("AbstractSQLExecution.Executing.Error"), e.getMessage());
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage1"), e);
        return;
    }
    Preferences prefs = SqlBuilderPlugin.getDefault().getPluginPreferences();
    String queryDelimiter = prefs.getString(IConstants.QUERY_DELIMITER);
    String alternateDelimiter = prefs.getString(IConstants.ALTERNATE_DELIMITER);
    String commentDelimiter = prefs.getString(IConstants.COMMENT_DELIMITER);
    QueryTokenizer qt = new QueryTokenizer(getSQLToBeExecuted(), queryDelimiter, alternateDelimiter, commentDelimiter);
    final List queryStrings = new ArrayList();
    while (qt.hasQuery()) {
        final String querySql = qt.nextQuery();
        // ignore commented lines.
        if (!querySql.startsWith("--")) {
            //$NON-NLS-1$
            queryStrings.add(querySql);
        }
    }
    // check if we can run explain plans
    try {
        Statement st = runNode.getInteractiveConnection().createStatement();
        boolean createPlanTable = false;
        boolean notFoundTable = true;
        try {
            //$NON-NLS-1$
            ResultSet rs = st.executeQuery("select queryno from SYSTOOLS.EXPLAIN_STATEMENT");
            notFoundTable = false;
            rs.close();
        } catch (Throwable e) {
            createPlanTable = MessageDialog.openQuestion(null, //$NON-NLS-1$
            Messages.getString("db2.editor.actions.explain.notFound.Title"), //$NON-NLS-1$
            Messages.getString("db2.editor.actions.explain.notFound"));
        } finally {
            try {
                st.close();
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage2"), e);
            }
        }
        if (notFoundTable && !createPlanTable) {
            return;
        }
        if (notFoundTable && createPlanTable) {
            SQLConnection conn = runNode.getInteractiveConnection();
            st = conn.createStatement();
            try {
                st.execute(createPlanScript1);
                st.execute(createPlanScript2);
                st.execute(createPlanScript3);
                st.execute(createPlanScript4);
                st.execute(createPlanScript5);
                st.execute(createPlanScript6);
                st.execute(createPlanScript7);
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage3"), e);
                //$NON-NLS-1$
                MessageDialog.openError(//$NON-NLS-1$
                null, //$NON-NLS-1$
                Messages.getString("db2.editor.actions.explain.createError.Title"), //$NON-NLS-1$
                Messages.getString("db2.editor.actions.explain.createError"));
                try {
                    st.close();
                } catch (Throwable e1) {
                    //$NON-NLS-1$
                    SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage2"), e1);
                }
                return;
            }
            try {
                st.close();
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage2"), e);
            }
        }
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage4"), e);
    }
    try {
        while (!queryStrings.isEmpty()) {
            String querySql = (String) queryStrings.remove(0);
            if (querySql != null) {
                resultDisplayer.addSQLExecution(new DB2ExplainPlanExecution(querySql, runNode));
            }
        }
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage5"), e);
    }
}
Also used : Statement(java.sql.Statement) SessionTreeNode(org.talend.sqlbuilder.sessiontree.model.SessionTreeNode) SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) ArrayList(java.util.ArrayList) RepositoryNode(org.talend.repository.model.RepositoryNode) QueryTokenizer(org.talend.sqlbuilder.util.QueryTokenizer) SessionTreeNodeManager(org.talend.sqlbuilder.dbstructure.SessionTreeNodeManager) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Preferences(org.eclipse.core.runtime.Preferences)

Aggregations

QueryTokenizer (org.talend.sqlbuilder.util.QueryTokenizer)6 ArrayList (java.util.ArrayList)5 Preferences (org.eclipse.core.runtime.Preferences)3 RepositoryNode (org.talend.repository.model.RepositoryNode)3 SessionTreeNodeManager (org.talend.sqlbuilder.dbstructure.SessionTreeNodeManager)3 SessionTreeNode (org.talend.sqlbuilder.sessiontree.model.SessionTreeNode)3 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 List (java.util.List)2 SQLConnection (net.sourceforge.squirrel_sql.fw.sql.SQLConnection)2 Shell (org.eclipse.swt.widgets.Shell)2 CTabItem (org.eclipse.swt.custom.CTabItem)1 IRepositoryNode (org.talend.repository.model.IRepositoryNode)1 MultiPageSqlBuilderEditor (org.talend.sqlbuilder.editors.MultiPageSqlBuilderEditor)1 ErDiagramDialog (org.talend.sqlbuilder.erdiagram.ui.ErDiagramDialog)1 SQLExecution (org.talend.sqlbuilder.sqlcontrol.SQLExecution)1 ISQLBuilderDialog (org.talend.sqlbuilder.ui.ISQLBuilderDialog)1 SQLBuilderDialog (org.talend.sqlbuilder.ui.SQLBuilderDialog)1 SqlEditDialog (org.talend.sqlbuilder.ui.SqlEditDialog)1