Search in sources :

Example 1 with ListUserObjectsCommand

use of henplus.commands.ListUserObjectsCommand in project henplus by neurolabs.

the class HenPlus method initializeCommands.

public void initializeCommands(final String[] argv) {
    _henplusProperties = new PropertyRegistry();
    _henplusProperties.registerProperty("comments-remove", _commandSeparator.getRemoveCommentsProperty());
    _sessionManager = SessionManager.getInstance();
    // FIXME: to many cross dependencies of commands now. clean up.
    _settingStore = new SetCommand(this);
    _dispatcher = new CommandDispatcher(_settingStore);
    _objectLister = new ListUserObjectsCommand(this);
    _henplusProperties.registerProperty("echo-commands", new EchoCommandProperty(_dispatcher));
    _dispatcher.register(new HelpCommand());
    /*
         * this one prints as well the initial copyright header.
         */
    _dispatcher.register(new AboutCommand());
    _dispatcher.register(new ExitCommand());
    _dispatcher.register(new EchoCommand());
    final PluginCommand pluginCommand = new PluginCommand(this);
    _dispatcher.register(pluginCommand);
    _dispatcher.register(new DriverCommand(this));
    final AliasCommand aliasCommand = new AliasCommand(this);
    _dispatcher.register(aliasCommand);
    if (_fromTerminal) {
        _dispatcher.register(new KeyBindCommand(this));
    }
    final LoadCommand loadCommand = new LoadCommand();
    _dispatcher.register(loadCommand);
    _dispatcher.register(new ConnectCommand(this, _sessionManager));
    _dispatcher.register(new StatusCommand());
    _dispatcher.register(_objectLister);
    _dispatcher.register(new DescribeCommand(_objectLister));
    _dispatcher.register(new TreeCommand(_objectLister));
    _dispatcher.register(new SQLCommand(_objectLister, _henplusProperties));
    _dispatcher.register(new ImportCommand(_objectLister));
    // _dispatcher.register(new ExportCommand());
    _dispatcher.register(new DumpCommand(_objectLister, loadCommand));
    _dispatcher.register(new ShellCommand());
    _dispatcher.register(new SpoolCommand(this));
    _dispatcher.register(_settingStore);
    PropertyCommand propertyCommand;
    propertyCommand = new PropertyCommand(this, _henplusProperties);
    _dispatcher.register(propertyCommand);
    _dispatcher.register(new SessionPropertyCommand(this));
    _dispatcher.register(new SystemInfoCommand());
    pluginCommand.load();
    aliasCommand.load();
    propertyCommand.load();
    Readline.setCompleter(_dispatcher);
    /* FIXME: do this platform independently */
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            shutdown();
        }
    });
    /*
         * if your compiler/system/whatever does not support the sun.misc.
         * classes, then just disable this call and the SigIntHandler class.
         */
    try {
        SigIntHandler.install();
    } catch (final Throwable t) {
    // ignore.
    }
/*
         * TESTING for ^Z support in the shell. sun.misc.SignalHandler stoptest
         * = new sun.misc.SignalHandler () { public void handle(sun.misc.Signal
         * sig) { System.out.println("caught: " + sig); } }; try {
         * sun.misc.Signal.handle(new sun.misc.Signal("TSTP"), stoptest); }
         * catch (Exception e) { // ignore. }
         * 
         * end testing
         */
}
Also used : ImportCommand(henplus.commands.ImportCommand) DumpCommand(henplus.commands.DumpCommand) ListUserObjectsCommand(henplus.commands.ListUserObjectsCommand) SessionPropertyCommand(henplus.commands.properties.SessionPropertyCommand) PropertyCommand(henplus.commands.properties.PropertyCommand) AliasCommand(henplus.commands.AliasCommand) SQLCommand(henplus.commands.SQLCommand) SpoolCommand(henplus.commands.SpoolCommand) DriverCommand(henplus.commands.DriverCommand) DescribeCommand(henplus.commands.DescribeCommand) SessionPropertyCommand(henplus.commands.properties.SessionPropertyCommand) LoadCommand(henplus.commands.LoadCommand) SystemInfoCommand(henplus.commands.SystemInfoCommand) EchoCommand(henplus.commands.EchoCommand) TreeCommand(henplus.commands.TreeCommand) SetCommand(henplus.commands.SetCommand) PluginCommand(henplus.commands.PluginCommand) ExitCommand(henplus.commands.ExitCommand) HelpCommand(henplus.commands.HelpCommand) StatusCommand(henplus.commands.StatusCommand) ShellCommand(henplus.commands.ShellCommand) ConnectCommand(henplus.commands.ConnectCommand) AboutCommand(henplus.commands.AboutCommand) KeyBindCommand(henplus.commands.KeyBindCommand)

Example 2 with ListUserObjectsCommand

use of henplus.commands.ListUserObjectsCommand 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 3 with ListUserObjectsCommand

use of henplus.commands.ListUserObjectsCommand 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

ListUserObjectsCommand (henplus.commands.ListUserObjectsCommand)3 SQLSession (henplus.SQLSession)2 SessionManager (henplus.SessionManager)2 HashSet (java.util.HashSet)2 AboutCommand (henplus.commands.AboutCommand)1 AliasCommand (henplus.commands.AliasCommand)1 ConnectCommand (henplus.commands.ConnectCommand)1 DescribeCommand (henplus.commands.DescribeCommand)1 DriverCommand (henplus.commands.DriverCommand)1 DumpCommand (henplus.commands.DumpCommand)1 EchoCommand (henplus.commands.EchoCommand)1 ExitCommand (henplus.commands.ExitCommand)1 HelpCommand (henplus.commands.HelpCommand)1 ImportCommand (henplus.commands.ImportCommand)1 KeyBindCommand (henplus.commands.KeyBindCommand)1 LoadCommand (henplus.commands.LoadCommand)1 PluginCommand (henplus.commands.PluginCommand)1 SQLCommand (henplus.commands.SQLCommand)1 SetCommand (henplus.commands.SetCommand)1 ShellCommand (henplus.commands.ShellCommand)1