Search in sources :

Example 1 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class SetCommand method registerLastCommandListener.

public void registerLastCommandListener(final CommandDispatcher dispatcher) {
    _specialVariables.add(SPECIAL_LAST_COMMAND);
    dispatcher.addExecutionListener(new ExecutionListener() {

        @Override
        public void beforeExecution(final SQLSession session, final String command) {
        }

        @Override
        public void afterExecution(final SQLSession session, final String command, final int result) {
            setVariable(SPECIAL_LAST_COMMAND, command.trim());
        }
    });
}
Also used : SQLSession(henplus.SQLSession) ExecutionListener(henplus.event.ExecutionListener)

Example 2 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class ConnectCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession currentSession, final String cmd, final String param) {
    SQLSession session = null;
    final StringTokenizer st = new StringTokenizer(param);
    final int argc = st.countTokens();
    if ("sessions".equals(cmd)) {
        showSessions();
        return SUCCESS;
    } else if ("connect".equals(cmd)) {
        if (argc < 1 || argc > 2) {
            return SYNTAX_ERROR;
        }
        String url = (String) st.nextElement();
        String alias = argc == 2 ? st.nextToken() : null;
        if (alias == null) {
            /*
                 * we only got one parameter. So the that single parameter might
                 * have been an alias. let's see..
                 */
            if (_knownUrls.containsKey(url)) {
                final String possibleAlias = url;
                url = _knownUrls.get(url);
                if (!possibleAlias.equals(url)) {
                    alias = possibleAlias;
                }
            }
        }
        try {
            session = new SQLSession(url, null, null);
            _knownUrls.put(url, url);
            if (alias != null) {
                _knownUrls.put(alias, url);
            }
            _currentSessionName = createSessionName(session, alias);
            _sessionManager.addSession(_currentSessionName, session);
            _sessionManager.setCurrentSession(session);
        } catch (final Exception e) {
            HenPlus.msg().println(e.toString());
            return EXEC_FAILED;
        }
    } else if ("switch".equals(cmd)) {
        String sessionName = null;
        if (argc != 1 && _sessionManager.getSessionCount() != 2) {
            return SYNTAX_ERROR;
        }
        if (argc == 0 && _sessionManager.getSessionCount() == 2) {
            final Iterator<String> it = _sessionManager.getSessionNames().iterator();
            while (it.hasNext()) {
                sessionName = it.next();
                if (!sessionName.equals(_currentSessionName)) {
                    break;
                }
            }
        } else {
            sessionName = (String) st.nextElement();
        }
        session = _sessionManager.getSessionByName(sessionName);
        if (session == null) {
            HenPlus.msg().println("'" + sessionName + "': no such session");
            return EXEC_FAILED;
        }
        _currentSessionName = sessionName;
    } else if ("rename-session".equals(cmd)) {
        String sessionName = null;
        if (argc != 1) {
            return SYNTAX_ERROR;
        }
        sessionName = (String) st.nextElement();
        if (sessionName.length() < 1) {
            return SYNTAX_ERROR;
        }
        /*
             * // moved to sessionmanager.renameSession
             * 
             * if (_sessionManager.sessionNameExists(sessionName)) {
             * HenPlus.err().println("A session with that name already exists");
             * return EXEC_FAILED; }
             * 
             * session =
             * _sessionManager.removeSessionWithName(currentSessionName); if
             * (session == null) { return EXEC_FAILED; }
             * _sessionManager.addSession(sessionName, session);
             */
        final int renamed = _sessionManager.renameSession(_currentSessionName, sessionName);
        if (renamed == EXEC_FAILED) {
            return EXEC_FAILED;
        }
        _currentSessionName = sessionName;
        session = _sessionManager.getCurrentSession();
    } else if ("disconnect".equals(cmd)) {
        _currentSessionName = null;
        if (argc != 0) {
            return SYNTAX_ERROR;
        }
        _sessionManager.closeCurrentSession();
        HenPlus.msg().println("session closed.");
        if (_sessionManager.hasSessions()) {
            _currentSessionName = _sessionManager.getFirstSessionName();
            session = _sessionManager.getSessionByName(_currentSessionName);
        }
    }
    if (_currentSessionName != null) {
        _henplus.setPrompt(_currentSessionName + "> ");
    } else {
        _henplus.setDefaultPrompt();
    }
    _henplus.setCurrentSession(session);
    return SUCCESS;
}
Also used : SQLSession(henplus.SQLSession) StringTokenizer(java.util.StringTokenizer) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 3 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class ListUserObjectsCommand method completeAllColumns.

public Iterator<String> completeAllColumns(final String partialColumn) {
    final SQLSession session = _henplus.getCurrentSession();
    if (session == null) {
        return null;
    }
    final NameCompleter completer = getAllColumnsCompleter(session);
    return completer.getAlternatives(partialColumn);
}
Also used : SQLSession(henplus.SQLSession) NameCompleter(henplus.view.util.NameCompleter)

Example 4 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class ListUserObjectsCommand method columnsFor.

/**
     * fixme: add this to the cached values determined by rehash.
     */
public Collection<String> columnsFor(String tabName) {
    final SQLSession session = _henplus.getCurrentSession();
    final Set<String> result = new HashSet<String>();
    // use createStmt
    final Connection conn = session.getConnection();
    ResultSet rset = null;
    String schema = null;
    final int schemaDelim = tabName.indexOf('.');
    if (schemaDelim > 0) {
        schema = tabName.substring(0, schemaDelim);
        tabName = tabName.substring(schemaDelim + 1);
    }
    try {
        final DatabaseMetaData meta = conn.getMetaData();
        rset = meta.getColumns(conn.getCatalog(), schema, tabName, null);
        while (rset.next()) {
            result.add(rset.getString(4));
        }
    } catch (final Exception e) {
    // ignore.
    } finally {
        if (rset != null) {
            try {
                rset.close();
            } catch (final Exception e) {
            }
        }
    }
    return result;
}
Also used : SQLSession(henplus.SQLSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) HashSet(java.util.HashSet)

Example 5 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class ConnectCommand method connect.

/**
     * @param url
     * @param username
     * @param password
     * @throws ClassNotFoundException
     * @throws SQLException
     * @throws IOException
     */
private void connect(final String url, final String username, final String password) throws ClassNotFoundException, SQLException, IOException {
    SQLSession session;
    session = new SQLSession(url, username, password);
    _currentSessionName = createSessionName(session, null);
    _sessionManager.addSession(_currentSessionName, session);
    _knownUrls.put(url, url);
    _henplus.setPrompt(_currentSessionName + "> ");
    _sessionManager.setCurrentSession(session);
}
Also used : SQLSession(henplus.SQLSession)

Aggregations

SQLSession (henplus.SQLSession)8 HashSet (java.util.HashSet)3 SessionManager (henplus.SessionManager)2 ListUserObjectsCommand (henplus.commands.ListUserObjectsCommand)2 NameCompleter (henplus.view.util.NameCompleter)2 StringTokenizer (java.util.StringTokenizer)2 ExecutionListener (henplus.event.ExecutionListener)1 Column (henplus.view.Column)1 TableRenderer (henplus.view.TableRenderer)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1