Search in sources :

Example 1 with ExplainNode

use of net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainNode in project tdq-studio-se by Talend.

the class ExplainExecution method doExecution.

protected void doExecution(IProgressMonitor monitor) throws Exception {
    int numErrors;
    SQLException lastSQLException;
    Query query;
    numErrors = 0;
    lastSQLException = null;
    query = null;
    Iterator iter = getQueryParser().iterator();
    String id_;
    query = (Query) iter.next();
    if (monitor.isCanceled()) {
        return;
    }
    _stmt = _connection.createStatement();
    id_ = Integer.toHexString((new Random()).nextInt()).toUpperCase();
    _stmt.execute((new StringBuilder("delete plan_table where statement_id='")).append(id_).append("'").toString());
    _stmt.close();
    _stmt = null;
    if (monitor.isCanceled())
        return;
    ResultSet rs;
    ExplainNode nd_parent;
    try {
        _stmt = _connection.createStatement();
        _stmt.execute((new StringBuilder("EXPLAIN PLAN SET statement_id = '")).append(id_).append("' FOR ").append(query.getQuerySql()).toString());
        _stmt.close();
        _stmt = null;
        if (monitor.isCanceled())
            return;
    } catch (SQLException e) {
        debugLogQuery(query, e);
        boolean stopOnError = SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean("SQLEditor.StopOnError");
        logException(e, query, stopOnError);
        closeStatements();
        if (stopOnError)
            throw e;
        numErrors++;
        lastSQLException = e;
    } catch (Exception e) {
        closeStatements();
        throw e;
    }
    _prepStmt = _connection.prepareStatement("select level, object_type,operation,options,object_owner,object_name,optimizer,cardinality ,cost,id,parent_id,level  from  plan_table  start with id = 0 and statement_id=?  connect by prior id=parent_id and statement_id=?");
    _prepStmt.setString(1, id_);
    _prepStmt.setString(2, id_);
    rs = _prepStmt.executeQuery();
    if (monitor.isCanceled())
        return;
    HashMap mp = new HashMap();
    int level;
    ExplainNode nd;
    for (; rs.next(); nd.setLevel(level)) {
        String object_type = rs.getString("object_type");
        String operation = rs.getString("operation");
        String options = rs.getString("options");
        String object_owner = rs.getString("object_owner");
        String object_name = rs.getString("object_name");
        String optimizer = rs.getString("optimizer");
        int cardinality = rs.getInt("cardinality");
        if (rs.wasNull())
            cardinality = -1;
        int cost = rs.getInt("cost");
        if (rs.wasNull())
            cost = -1;
        int parentID = rs.getInt("parent_id");
        int id = rs.getInt("id");
        level = rs.getInt("level");
        nd = null;
        if (id == 0) {
            ExplainNode dummy = new ExplainNode(null);
            mp.put(new Integer(-1), dummy);
            dummy.setId(-1);
            nd = new ExplainNode(dummy);
            dummy.add(nd);
            nd.setId(0);
            mp.put(new Integer(0), nd);
        } else {
            nd_parent = (ExplainNode) mp.get(new Integer(parentID));
            nd = new ExplainNode(nd_parent);
            nd_parent.add(nd);
            mp.put(new Integer(id), nd);
        }
        nd.setCardinality(cardinality);
        nd.setCost(cost);
        nd.setObject_name(object_name);
        nd.setObject_owner(object_owner);
        nd.setObject_type(object_type);
        nd.setOperation(operation);
        nd.setOptimizer(optimizer);
        nd.setOptions(options);
        nd.setId(id);
    }
    rs.close();
    _prepStmt.close();
    _prepStmt = null;
    nd_parent = (ExplainNode) mp.get(new Integer(-1));
    if (monitor.isCanceled())
        return;
    displayResults(nd_parent, query);
    debugLogQuery(query, null);
    query = null;
    if (numErrors == 1)
        throw lastSQLException;
    if (numErrors > 1)
        MessageDialog.openError(getEditor().getSite().getShell(), "SQL Error", "One or more of your SQL statements failed - check the Messages log for details");
    return;
}
Also used : Query(net.sourceforge.sqlexplorer.parsers.Query) Random(java.util.Random) ExplainNode(net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainNode) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Example 2 with ExplainNode

use of net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainNode in project tdq-studio-se by Talend.

the class ExplainExecution method displayResults.

private void displayResults(final ExplainNode node, final Query query) {
    getEditor().getSite().getShell().getDisplay().asyncExec(new Runnable() {

        public void run() {
            ResultsTab resultsTab = allocateResultsTab(query);
            try {
                Composite composite = resultsTab.getParent();
                GridLayout gLayout = new GridLayout();
                gLayout.numColumns = 2;
                gLayout.marginLeft = 0;
                gLayout.horizontalSpacing = 0;
                gLayout.verticalSpacing = 0;
                gLayout.marginWidth = 0;
                gLayout.marginHeight = 0;
                composite.setLayout(gLayout);
                composite.setData("parenttab", resultsTab.getTabItem());
                Composite pp = new Composite(composite, 0);
                pp.setLayout(new FillLayout());
                pp.setLayoutData(new GridData(1808));
                TableTreeViewer tv = new TableTreeViewer(pp, 0x10800);
                Table table = tv.getTableTree().getTable();
                table.setLinesVisible(true);
                table.setHeaderVisible(true);
                TableColumn tc = new TableColumn(table, 0);
                tc.setText("");
                tc = new TableColumn(table, 0);
                tc.setText("Cost");
                tc = new TableColumn(table, 0);
                tc.setText("Cardinality");
                TableLayout tableLayout = new TableLayout();
                tableLayout.addColumnData(new ColumnWeightData(6, 150, true));
                tableLayout.addColumnData(new ColumnWeightData(1, 50, true));
                tableLayout.addColumnData(new ColumnWeightData(1, 50, true));
                table.setLayout(tableLayout);
                tv.setContentProvider(new ITreeContentProvider() {

                    public void dispose() {
                    }

                    public Object[] getChildren(Object parentElement) {
                        return ((ExplainNode) parentElement).getChildren();
                    }

                    public Object[] getElements(Object inputElement) {
                        ExplainNode nd = (ExplainNode) inputElement;
                        return nd.getChildren();
                    }

                    public Object getParent(Object element) {
                        return ((ExplainNode) element).getParent();
                    }

                    public boolean hasChildren(Object element) {
                        return ((ExplainNode) element).getChildren().length > 0;
                    }

                    public void inputChanged(Viewer viewer1, Object obj, Object obj1) {
                    }
                });
                tv.setLabelProvider(new TreeLabelProvider() {
                });
                tv.setInput(node);
                tv.refresh();
                tv.expandAll();
                for (int i = 0; i < table.getColumnCount(); i++) table.getColumn(i).pack();
                final ExplainPlanActionGroup actionGroup = new ExplainPlanActionGroup(tv, node.getChildren()[0]);
                MenuManager menuManager = new MenuManager("ExplainPlanContextMenu");
                menuManager.setRemoveAllWhenShown(true);
                org.eclipse.swt.widgets.Menu contextMenu = menuManager.createContextMenu(table);
                tv.getControl().setMenu(contextMenu);
                menuManager.addMenuListener(new IMenuListener() {

                    public void menuAboutToShow(IMenuManager manager) {
                        actionGroup.fillContextMenu(manager);
                    }
                });
                composite.layout();
                composite.redraw();
            } catch (Exception e) {
                if (resultsTab != null) {
                    Composite composite = resultsTab.getParent();
                    String message = e.getMessage();
                    Label errorLabel = new Label(composite, 4);
                    errorLabel.setText(message);
                    errorLabel.setLayoutData(new GridData(4, 128, true, false));
                }
                SQLExplorerPlugin.error("Error creating explain tab", e);
            }
        }
    });
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) Label(org.eclipse.swt.widgets.Label) TableTreeViewer(org.eclipse.jface.viewers.TableTreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) TableTreeViewer(org.eclipse.jface.viewers.TableTreeViewer) ExplainPlanActionGroup(net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainPlanActionGroup) GridLayout(org.eclipse.swt.layout.GridLayout) ResultsTab(net.sourceforge.sqlexplorer.sqleditor.results.ResultsTab) TableLayout(org.eclipse.jface.viewers.TableLayout) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ExplainNode(net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainNode) FillLayout(org.eclipse.swt.layout.FillLayout) TableColumn(org.eclipse.swt.widgets.TableColumn) IMenuListener(org.eclipse.jface.action.IMenuListener) SQLException(java.sql.SQLException) GridData(org.eclipse.swt.layout.GridData) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager)

Aggregations

SQLException (java.sql.SQLException)2 ExplainNode (net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainNode)2 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 ExplainPlanActionGroup (net.sourceforge.sqlexplorer.oracle.actions.explain.ExplainPlanActionGroup)1 Query (net.sourceforge.sqlexplorer.parsers.Query)1 ResultsTab (net.sourceforge.sqlexplorer.sqleditor.results.ResultsTab)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 ColumnWeightData (org.eclipse.jface.viewers.ColumnWeightData)1 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)1 TableLayout (org.eclipse.jface.viewers.TableLayout)1 TableTreeViewer (org.eclipse.jface.viewers.TableTreeViewer)1 Viewer (org.eclipse.jface.viewers.Viewer)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1