Search in sources :

Example 6 with SQLConnection

use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.

the class AbstractSQLSourceTab method getSource.

public String getSource() {
    String source = null;
    SQLConnection connection = null;
    ResultSet rs = null;
    Statement stmt = null;
    PreparedStatement pStmt = null;
    int timeOut = SQLExplorerPlugin.getDefault().getPluginPreferences().getInt(IConstants.INTERACTIVE_QUERY_TIMEOUT);
    try {
        connection = getNode().getSession().grabConnection();
        Object[] params = getSQLParameters();
        if (params == null || params.length == 0) {
            // use normal statement
            stmt = connection.createStatement();
            stmt.setQueryTimeout(timeOut);
            rs = stmt.executeQuery(getSQL());
        } else {
            // use prepared statement
            pStmt = connection.prepareStatement(getSQL());
            pStmt.setQueryTimeout(timeOut);
            for (int i = 0; i < params.length; i++) {
                if (params[i] instanceof String) {
                    pStmt.setString(i + 1, (String) params[i]);
                } else if (params[i] instanceof Integer) {
                    pStmt.setInt(i + 1, ((Integer) params[i]).intValue());
                } else if (params[i] instanceof String) {
                    pStmt.setLong(i + 1, ((Long) params[i]).longValue());
                }
            }
            rs = pStmt.executeQuery();
        }
        // $NON-NLS-1$
        source = "";
        while (rs.next()) {
            source = source + rs.getString(1);
        }
        rs.close();
    } catch (Exception e) {
        SQLExplorerPlugin.error(Messages.getString("AbstractSQLSourceTab.cannotLoadSource") + getNode().getName(), e);
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseRs"), e);
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseStmt"), e);
            }
        if (pStmt != null)
            try {
                pStmt.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseStmt"), e);
            }
        if (connection != null)
            getNode().getSession().releaseConnection(connection);
    }
    return source;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 7 with SQLConnection

use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.

the class ConnectionTreeLabelProvider method getText.

/**
 * Return the text to display
 *
 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
 */
public String getText(Object element) {
    if (element instanceof Alias) {
        Alias alias = (Alias) element;
        String label = alias.getName();
        int numSessions = 0;
        for (User user : alias.getUsers()) numSessions += user.getConnections().size();
        if (numSessions == 1)
            return label + " (" + numSessions + " " + Messages.getString("ConnectionsView.ConnectedAlias.single.Postfix") + // $NON-NLS-1$
            ")";
        if (numSessions > 1)
            return label + " (" + numSessions + " " + Messages.getString("ConnectionsView.ConnectedAlias.multiple.Postfix") + // $NON-NLS-1$
            ")";
        return label;
    } else if (element instanceof User) {
        User user = (User) element;
        return user.getUserName();
    } else if (element instanceof SQLConnection) {
        SQLConnection connection = (SQLConnection) element;
        String label;
        if (connection.getDescription() == null) {
            // $NON-NLS-1$
            SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
            // $NON-NLS-2$
            label = Messages.getString("ConnectionsView.ConnectedAlias.ConnectedSince") + ' ' + fmt.format(new Date(connection.getCreatedTime()));
        } else
            // $NON-NLS-2$
            label = Messages.getString("ConnectionsView.ConnectedAlias.Connection") + ' ' + connection.getDescription();
        if (connection.isPooled())
            // $NON-NLS-1$
            label += ' ' + Messages.getString("ConnectionsView.ConnectedAlias.Pooled");
        return label;
    }
    return null;
}
Also used : User(net.sourceforge.sqlexplorer.dbproduct.User) Alias(net.sourceforge.sqlexplorer.dbproduct.Alias) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 8 with SQLConnection

use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.

the class CloseConnectionAction method run.

public void run() {
    boolean confirm = SQLExplorerPlugin.getDefault().getPluginPreferences().getBoolean(IConstants.CONFIRM_BOOL_CLOSE_CONNECTION);
    for (SQLConnection connection : getView().getSelectedConnections(false)) {
        Session session = connection.getSession();
        if (session != null && !session.isConnectionInUse()) {
            if (confirm) {
                MessageDialogWithToggle dialog = MessageDialogWithToggle.openYesNoQuestion(getView().getSite().getShell(), Messages.getString("ConnectionsView.Actions.CloseAll.Confirm.Title"), Messages.getString("ConnectionsView.Actions.CloseAll.Confirm.Message"), Messages.getString("ConnectionsView.Actions.CloseAll.Confirm.Toggle"), false, null, null);
                if (dialog.getToggleState() && dialog.getReturnCode() == IDialogConstants.YES_ID)
                    SQLExplorerPlugin.getDefault().getPluginPreferences().setValue(IConstants.CONFIRM_BOOL_CLOSE_CONNECTION, false);
                if (dialog.getReturnCode() != IDialogConstants.YES_ID)
                    return;
            }
            session.disposeConnection();
        } else if (session == null)
            connection.getUser().releaseFromPool(connection);
    }
    getView().refresh();
}
Also used : SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) Session(net.sourceforge.sqlexplorer.dbproduct.Session)

Example 9 with SQLConnection

use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.

the class SQLEditorCatalogSwitcher method createControl.

protected Control createControl(Composite parent) {
    _catalogCombo = new Combo(parent, SWT.READ_ONLY);
    _catalogCombo.setToolTipText(Messages.getString("SQLEditor.Actions.ChooseCatalog.ToolTip"));
    _catalogCombo.setSize(200, _catalogCombo.getSize().y);
    _catalogCombo.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent arg0) {
            int selIndex = _catalogCombo.getSelectionIndex();
            String newCat = _catalogCombo.getItem(selIndex);
            if (_editor.getSession() != null) {
                try {
                    _editor.getSession().setCatalog(newCat);
                } catch (Exception e1) {
                    SQLExplorerPlugin.error("Error changing catalog", e1);
                }
            }
        }
    });
    _catalogCombo.add("");
    if (_editor.getSession() != null) {
        try {
            String[] catalogs = getMetaDataSession().getCatalogs();
            User user = _editor.getSession().getUser();
            // Get the connection directly from the user because the session may be busy with its one one
            SQLConnection connection = user.getConnection();
            try {
                String currentCatalog = connection.getCatalog();
                for (int i = 0; i < catalogs.length; i++) {
                    _catalogCombo.add(catalogs[i]);
                    if (currentCatalog.equals(catalogs[i])) {
                        _catalogCombo.select(_catalogCombo.getItemCount() - 1);
                    }
                }
            } finally {
                if (connection != null)
                    user.releaseConnection(connection);
            }
        } catch (SQLException e) {
            SQLExplorerPlugin.error(e);
        }
    }
    return _catalogCombo;
}
Also used : User(net.sourceforge.sqlexplorer.dbproduct.User) SQLException(java.sql.SQLException) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Combo(org.eclipse.swt.widgets.Combo) SQLException(java.sql.SQLException)

Example 10 with SQLConnection

use of net.sourceforge.sqlexplorer.dbproduct.SQLConnection in project tdq-studio-se by Talend.

the class ConnectionsView method createPartControl.

/**
 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createPartControl(Composite parent) {
    // $NON-NLS-1$
    PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, SQLExplorerPlugin.PLUGIN_ID + ".AliasView");
    SQLExplorerPlugin.getDefault().getAliasManager().addListener(this);
    // create outline
    _treeViewer = new TreeViewer(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
    getSite().setSelectionProvider(_treeViewer);
    // create action bar
    IToolBarManager toolBarMgr = getViewSite().getActionBars().getToolBarManager();
    // PTODO qzhang delete the Context meun in the Connections View for feature 3519.
    // toolBarMgr.add(new NewAliasAction());
    // toolBarMgr.add(new NewEditorAction());
    // toolBarMgr.add(new NewDatabaseStructureViewAction());
    // toolBarMgr.add(new CloseAllConnectionsAction());
    // toolBarMgr.add(new CloseConnectionAction());
    // use hash lookup to improve performance
    _treeViewer.setUseHashlookup(true);
    // add content and label provider
    _treeViewer.setContentProvider(new ConnectionTreeContentProvider());
    _treeViewer.setLabelProvider(new ConnectionTreeLabelProvider());
    // Add yyi 2010-09-15 14549: hide connections in SQL Explorer when a connection is moved to the trash bin
    _treeViewer.addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            try {
                IFile file = SQLExplorerPlugin.getDefault().getPropertyFile().get(element);
                if (null != file && file.exists()) {
                    // $NON-NLS-1$
                    return !FileUtils.readFileToString(file.getLocation().toFile()).contains("deleted=\"true\"");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
    });
    // ~
    // set input session
    _treeViewer.setInput(SQLExplorerPlugin.getDefault().getAliasManager());
    // doubleclick on alias opens session
    _treeViewer.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            if (selection != null) {
                User user = null;
                Object selected = selection.getFirstElement();
                if (selected instanceof Alias) {
                    Alias alias = (Alias) selection.getFirstElement();
                    user = alias.getDefaultUser();
                } else if (selected instanceof User) {
                    user = (User) selected;
                } else if (selected instanceof SQLConnection) {
                    user = ((SQLConnection) selected).getUser();
                }
                if (user != null) {
                    openNewEditor(user);
                }
            }
        }
    });
    _treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            refreshToolbar();
        }
    });
    // add context menu
    final ConnectionTreeActionGroup actionGroup = new ConnectionTreeActionGroup();
    // $NON-NLS-1$
    MenuManager menuManager = new MenuManager("ConnectionTreeContextMenu");
    menuManager.setRemoveAllWhenShown(true);
    Menu contextMenu = menuManager.createContextMenu(_treeViewer.getTree());
    _treeViewer.getTree().setMenu(contextMenu);
    menuManager.addMenuListener(new IMenuListener() {

        public void menuAboutToShow(IMenuManager manager) {
            actionGroup.fillContextMenu(manager);
        }
    });
    _treeViewer.expandToLevel(2);
    parent.layout();
    SQLExplorerPlugin.getDefault().startDefaultConnections(this);
}
Also used : IFile(org.eclipse.core.resources.IFile) User(net.sourceforge.sqlexplorer.dbproduct.User) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TreeViewer(org.eclipse.jface.viewers.TreeViewer) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Viewer(org.eclipse.jface.viewers.Viewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) PartInitException(org.eclipse.ui.PartInitException) SQLCannotConnectException(net.sourceforge.sqlexplorer.SQLCannotConnectException) IMenuListener(org.eclipse.jface.action.IMenuListener) IToolBarManager(org.eclipse.jface.action.IToolBarManager) Alias(net.sourceforge.sqlexplorer.dbproduct.Alias) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) Menu(org.eclipse.swt.widgets.Menu) IMenuManager(org.eclipse.jface.action.IMenuManager)

Aggregations

SQLConnection (net.sourceforge.sqlexplorer.dbproduct.SQLConnection)17 SQLException (java.sql.SQLException)7 User (net.sourceforge.sqlexplorer.dbproduct.User)7 Alias (net.sourceforge.sqlexplorer.dbproduct.Alias)6 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 PreparedStatement (java.sql.PreparedStatement)3 Iterator (java.util.Iterator)3 LinkedHashSet (java.util.LinkedHashSet)3 Session (net.sourceforge.sqlexplorer.dbproduct.Session)3 INode (net.sourceforge.sqlexplorer.dbstructure.nodes.INode)2 ParserException (net.sourceforge.sqlexplorer.parsers.ParserException)2 QueryParser (net.sourceforge.sqlexplorer.parsers.QueryParser)2 MessageDialogWithToggle (org.eclipse.jface.dialogs.MessageDialogWithToggle)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 DatabaseMetaData (java.sql.DatabaseMetaData)1