Logo Search packages:      
Sourcecode: henplus version File versions  Download package

int henplus::commands::ConnectCommand::execute ( SQLSession  currentSession,
String  cmd,
String  param 
) [inline]

execute the command given.

Implements henplus::Command.

Definition at line 241 of file ConnectCommand.java.

References henplus::view::TableRenderer::addRow(), henplus::view::TableRenderer::closeTable(), currentSessionName, henplus::Command::EXEC_FAILED, henplus::SQLSession::getURL(), henplus::SQLSession::getUsername(), henplus::view::ColumnMetaData::resetWidth(), henplus::Command::SUCCESS, and henplus::Command::SYNTAX_ERROR.

                                                                            {
      SQLSession session = null;

      StringTokenizer st = new StringTokenizer(param);
      int argc = st.countTokens();
      
      if ("sessions".equals(cmd)) {
          HenPlus.msg().println("current session is marked with '*'");
          SESS_META[0].resetWidth();
          SESS_META[1].resetWidth();
          SESS_META[2].resetWidth();
          TableRenderer table = new TableRenderer(SESS_META, HenPlus.out());
          Map.Entry entry = null;
          Iterator it = _sessionManager.getSessionNames().iterator();
          while (it.hasNext()) {
            String sessName = (String)it.next();
            session = _sessionManager.getSessionByName(sessName);
            String prepend = sessName.equals(currentSessionName) 
                ? " * "
                : "   ";
            Column[] row = new Column[3];
            row[0] = new Column(prepend + sessName);
            row[1] = new Column(session.getUsername());
            row[2] = new Column(session.getURL());
            table.addRow(row);
          }
          table.closeTable();
          return SUCCESS;
      }

      else if ("connect".equals(cmd)) {
          if (argc < 1 || argc > 2) {
            return SYNTAX_ERROR;
          }
          String url = (String) st.nextElement();
          currentSessionName = (argc==2) ? (String) st.nextElement() : null;
          try {
            session = new SQLSession(url, null, null);
            knownUrls.add(url);
            currentSessionName = createSessionName(session, 
                                           currentSessionName);
            _sessionManager.addSession(currentSessionName, session);
        _sessionManager.setCurrentSession(session);
          }
          catch (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) {
            Iterator i = _sessionManager.getSessionNames().iterator();
            while (i.hasNext()) {
                sessionName = (String) i.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);
         */
         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;
    }


Generated by  Doxygen 1.6.0   Back to index