Search in sources :

Example 11 with SQLConnection

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

the class ConnectionsView method getSelectedUsers.

/**
 * Returns a list of selected Users; if recurse is true, indirectly selected users are included also (eg a session's
 * user)
 *
 * @param recurse
 * @return Set of Users, never returns null
 */
public Set<User> getSelectedUsers(boolean recurse) {
    IStructuredSelection selection = (IStructuredSelection) _treeViewer.getSelection();
    if (selection == null) {
        return EMPTY_USERS;
    }
    LinkedHashSet<User> result = new LinkedHashSet<User>();
    Iterator iter = selection.iterator();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof User) {
            result.add((User) obj);
        } else if (recurse) {
            if (obj instanceof Alias) {
                Alias alias = (Alias) obj;
                result.addAll(alias.getUsers());
            } else if (obj instanceof SQLConnection) {
                SQLConnection connection = (SQLConnection) obj;
                result.add(connection.getUser());
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) User(net.sourceforge.sqlexplorer.dbproduct.User) Alias(net.sourceforge.sqlexplorer.dbproduct.Alias) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) Iterator(java.util.Iterator) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 12 with SQLConnection

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

the class CloseAllConnectionsAction method run.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.IAction#run()
     */
public void run() {
    boolean confirm = SQLExplorerPlugin.getDefault().getPluginPreferences().getBoolean(IConstants.CONFIRM_BOOL_CLOSE_ALL_CONNECTIONS);
    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_ALL_CONNECTIONS, false);
        if (dialog.getReturnCode() != IDialogConstants.YES_ID)
            return;
    }
    Set<SQLConnection> connections = getView().getSelectedConnections(true);
    for (SQLConnection connection : connections) {
        synchronized (connection) {
            Session session = connection.getSession();
            if (session != null) {
                synchronized (session) {
                    if (!session.isConnectionInUse())
                        session.disposeConnection();
                }
            } else
                connection.getUser().releaseFromPool(connection);
        }
    }
    setEnabled(false);
    getView().refresh();
}
Also used : SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) Session(net.sourceforge.sqlexplorer.dbproduct.Session)

Example 13 with SQLConnection

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

the class AbstractSQLFolderNode method loadChildren.

public final void loadChildren() {
    SQLConnection connection = null;
    ResultSet rs = null;
    Statement stmt = null;
    PreparedStatement pStmt = null;
    int timeOut = SQLExplorerPlugin.getDefault().getPluginPreferences().getInt(IConstants.INTERACTIVE_QUERY_TIMEOUT);
    try {
        connection = 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();
        }
        while (rs.next()) {
            String name = rs.getString(1);
            if (!isExcludedByFilter(name)) {
                ObjectNode node = new ObjectNode(name, getChildType(), this, getImage());
                addChildNode(node);
            }
        }
        rs.close();
    } catch (Exception e) {
        SQLExplorerPlugin.error("Couldn't load children for: " + getName(), e);
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Error closing result set", e);
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Error closing statement", e);
            }
        if (pStmt != null)
            try {
                pStmt.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Error closing statement", e);
            }
        if (connection != null)
            getSession().releaseConnection(connection);
    }
}
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 14 with SQLConnection

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

the class PreviewTab method getDataSet.

public DataSet getDataSet() throws Exception {
    INode node = getNode();
    if (node == null) {
        return null;
    }
    if (node instanceof TableNode) {
        TableNode tableNode = (TableNode) node;
        int maxResults = SQLExplorerPlugin.getDefault().getPluginPreferences().getInt(IConstants.PRE_ROW_COUNT);
        if (maxResults == 0) {
            maxResults = 50;
        }
        SQLConnection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        DataSet dataSet = null;
        try {
            connection = tableNode.getSession().grabConnection();
            statement = connection.createStatement();
            statement.setMaxRows(maxResults);
            // $NON-NLS-1$
            statement.execute("select * from " + tableNode.getQualifiedName());
            resultSet = statement.getResultSet();
            dataSet = new DataSet(resultSet, null);
        } finally {
            if (resultSet != null)
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseRs"), e);
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseStmt"), e);
                }
            if (connection != null)
                getNode().getSession().releaseConnection(connection);
        }
        return dataSet;
    }
    return null;
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) Statement(java.sql.Statement) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) ResultSet(java.sql.ResultSet)

Example 15 with SQLConnection

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

the class SQLEditor method onCloseEditor.

/**
 * Called internally when the user tries to close the editor
 */
private void onCloseEditor() {
    textEditor.getDocumentProvider().disconnect(getEditorInput());
    textEditor.setInput(null);
    clearResults();
    // when close the SQLEditor, close the connection
    if (session != null) {
        List<SQLConnection> connections = session.getUser().getUnusedConnections();
        for (SQLConnection sqlConn : connections) {
            session.getUser().releaseFromPool(sqlConn);
        }
    }
// TDQ-5952~
}
Also used : SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection)

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