Search in sources :

Example 6 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class ConnectCommand method showSessions.

private void showSessions() {
    HenPlus.msg().println("current session is marked with '*'");
    for (int i = 0; i < SESS_META.length; ++i) {
        SESS_META[i].resetWidth();
    }
    final TableRenderer table = new TableRenderer(SESS_META, HenPlus.out());
    for (String sessName : _sessionManager.getSessionNames()) {
        final SQLSession session = _sessionManager.getSessionByName(sessName);
        final String prepend = sessName.equals(_currentSessionName) ? " * " : "   ";
        final Column[] row = new Column[5];
        row[0] = new Column(prepend + sessName);
        row[1] = new Column(session.getUsername());
        row[2] = new Column(session.getURL());
        row[3] = new Column(TimeRenderer.renderTime(session.getUptime()));
        row[4] = new Column(session.getStatementCount());
        table.addRow(row);
    }
    table.closeTable();
}
Also used : TableRenderer(henplus.view.TableRenderer) SQLSession(henplus.SQLSession) Column(henplus.view.Column)

Example 7 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class TableDiffCommand method complete.

/*
     * (non-Javadoc)
     * 
     * @see henplus.Command#complete(henplus.CommandDispatcher,
     * java.lang.String, java.lang.String)
     */
@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
    final StringTokenizer st = new StringTokenizer(partialCommand);
    // skip cmd.
    st.nextToken();
    int argIndex = st.countTokens();
    /*
         * the following input is given: "command token1 [TAB_PRESSED]" in this
         * case the partialCommand is "command token1", the last word has a
         * length 0!
         * 
         * another input: "command toke[TAB_PRESSED]" then the partialCommand is
         * "command toke", the last word is "toke".
         */
    if (lastWord.length() > 0) {
        argIndex--;
    }
    // check completion for --singledb
    if (argIndex == 0 && lastWord.startsWith("-")) {
        return new Iterator<String>() {

            private boolean _next = true;

            @Override
            public boolean hasNext() {
                return _next;
            }

            @Override
            public String next() {
                _next = false;
                return OPTION_SINGLE_DB;
            }

            @Override
            public void remove() {
            /* do nothing */
            }
        };
    } else if (partialCommand.indexOf(OPTION_SINGLE_DB) != -1 && argIndex > 0) {
        final SessionManager sessionManager = HenPlus.getInstance().getSessionManager();
        final SQLSession session = sessionManager.getCurrentSession();
        final HashSet<String> alreadyGiven = new HashSet<String>();
        while (st.hasMoreElements()) {
            alreadyGiven.add(st.nextToken());
        }
        final ListUserObjectsCommand objectList = HenPlus.getInstance().getObjectLister();
        final Iterator<String> iter = objectList.completeTableName(session, lastWord);
        return new Iterator<String>() {

            String table = null;

            @Override
            public boolean hasNext() {
                while (iter.hasNext()) {
                    table = iter.next();
                    if (alreadyGiven.contains(table) && !lastWord.equals(table)) {
                        continue;
                    }
                    return true;
                }
                return false;
            }

            @Override
            public String next() {
                return table;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("no!");
            }
        };
    } else if (partialCommand.indexOf(OPTION_SINGLE_DB) == -1 && argIndex == 0) {
        // !singledb && process the first session
        return HenPlus.getInstance().getSessionManager().completeSessionName(lastWord);
    } else if (partialCommand.indexOf(OPTION_SINGLE_DB) == -1 && argIndex == 1) {
        // !singledb && process the second session
        final String firstSession = st.nextToken();
        return getSecondSessionCompleter(lastWord, firstSession);
    } else if (argIndex > 1) {
        // process tables
        final SessionManager sessionManager = HenPlus.getInstance().getSessionManager();
        final SQLSession first = sessionManager.getSessionByName(st.nextToken());
        final SQLSession second = sessionManager.getSessionByName(st.nextToken());
        final HashSet<String> alreadyGiven = new HashSet<String>();
        while (st.hasMoreElements()) {
            alreadyGiven.add(st.nextToken());
        }
        final ListUserObjectsCommand objectList = HenPlus.getInstance().getObjectLister();
        final Iterator<String> firstIter = objectList.completeTableName(first, lastWord);
        final Iterator<String> secondIter = objectList.completeTableName(second, lastWord);
        final Iterator<String> intersectionIter = getIntersection(firstIter, secondIter);
        return new Iterator<String>() {

            String table = null;

            @Override
            public boolean hasNext() {
                while (intersectionIter.hasNext()) {
                    table = intersectionIter.next();
                    if (alreadyGiven.contains(table) && !lastWord.equals(table)) {
                        continue;
                    }
                    return true;
                }
                return false;
            }

            @Override
            public String next() {
                return table;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("no!");
            }
        };
    }
    return null;
}
Also used : SQLSession(henplus.SQLSession) StringTokenizer(java.util.StringTokenizer) SessionManager(henplus.SessionManager) Iterator(java.util.Iterator) ListUserObjectsCommand(henplus.commands.ListUserObjectsCommand) HashSet(java.util.HashSet)

Example 8 with SQLSession

use of henplus.SQLSession in project henplus by neurolabs.

the class TableDiffCommand method executeDoubleDb.

private int executeDoubleDb(final StringTokenizer st, final boolean colNameIgnoreCase) {
    if (st.countTokens() < 3) {
        return SYNTAX_ERROR;
    }
    final SessionManager sessionManager = HenPlus.getInstance().getSessionManager();
    if (sessionManager.getSessionCount() < 2) {
        Logger.error("You need two valid sessions for this command.");
        return SYNTAX_ERROR;
    }
    final SQLSession first = sessionManager.getSessionByName(st.nextToken());
    final SQLSession second = sessionManager.getSessionByName(st.nextToken());
    if (first == null || second == null) {
        Logger.error("You need two valid sessions for this command.");
        return EXEC_FAILED;
    } else if (first == second) {
        Logger.error("You should specify two different sessions for this command.");
        return EXEC_FAILED;
    } else if (!st.hasMoreTokens()) {
        Logger.error("You should specify at least one table.");
        return EXEC_FAILED;
    }
    try {
        final long start = System.currentTimeMillis();
        int count = 0;
        final ListUserObjectsCommand objectLister = HenPlus.getInstance().getObjectLister();
        final SortedSet<String> tablesOne = objectLister.getTableNamesForSession(first);
        final SortedSet<String> tablesTwo = objectLister.getTableNamesForSession(second);
        // which tables got already
        final Set<String> alreadyDiffed = new HashSet<String>();
        // diffed?
        /*
             * which tables are found in the first session via wildcards but are
             * not contained in the second session?
             */
        final ArrayList<String> missedFromWildcards = new ArrayList<String>();
        while (st.hasMoreTokens()) {
            final String nextToken = st.nextToken();
            if ("*".equals(nextToken) || nextToken.indexOf('*') > -1) {
                Iterator<String> iter = null;
                if ("*".equals(nextToken)) {
                    iter = objectLister.getTableNamesIteratorForSession(first);
                } else if (nextToken.indexOf('*') > -1) {
                    final String tablePrefix = nextToken.substring(0, nextToken.length() - 1);
                    final NameCompleter compl = new NameCompleter(tablesOne);
                    iter = compl.getAlternatives(tablePrefix);
                }
                while (iter.hasNext()) {
                    final String objTableName = iter.next();
                    count = diffConditionally(objTableName, colNameIgnoreCase, first, second, tablesTwo, alreadyDiffed, missedFromWildcards, count);
                }
            } else if (!alreadyDiffed.contains(nextToken)) {
                diffTable(first, second, nextToken, colNameIgnoreCase);
                alreadyDiffed.add(nextToken);
                count++;
            }
        }
        final StringBuilder msg = new StringBuilder();
        msg.append("Diffing ").append(count).append(count == 1 ? " table took " : " tables took ").append(System.currentTimeMillis() - start).append(" ms.");
        // the user know this.
        if (missedFromWildcards.size() > 0) {
            msg.append("\nTables which matched a given wildcard in your first\n" + "session but were not found in your second session:\n");
            for (String missed : missedFromWildcards) {
                msg.append(missed).append(", ");
            }
            // remove the last two chars
            msg.delete(msg.length() - 2, msg.length());
        }
        Logger.info(msg.toString());
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return SUCCESS;
}
Also used : SessionManager(henplus.SessionManager) NameCompleter(henplus.view.util.NameCompleter) ArrayList(java.util.ArrayList) ListUserObjectsCommand(henplus.commands.ListUserObjectsCommand) SQLSession(henplus.SQLSession) HashSet(java.util.HashSet)

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