Search in sources :

Example 1 with Column

use of henplus.view.Column in project henplus by neurolabs.

the class KeyBindCommand method showKeyBindings.

private void showKeyBindings() {
    DRV_META[0].resetWidth();
    DRV_META[1].resetWidth();
    final TableRenderer table = new TableRenderer(DRV_META, HenPlus.out());
    for (Entry<String, String> entry : _bindings.entrySet()) {
        final Column[] row = new Column[2];
        row[0] = new Column(entry.getKey());
        row[1] = new Column(entry.getValue());
        table.addRow(row);
    }
    table.closeTable();
}
Also used : TableRenderer(henplus.view.TableRenderer) Column(henplus.view.Column)

Example 2 with Column

use of henplus.view.Column in project henplus by neurolabs.

the class AbstractPropertyCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession currentSession, final String cmd, final String param) {
    final StringTokenizer st = new StringTokenizer(param);
    final int argc = st.countTokens();
    if (cmd.startsWith("re")) {
        // 'reset-property'
        if (argc == 1) {
            final String name = st.nextToken();
            PropertyHolder holder;
            holder = getRegistry().getPropertyMap().get(name);
            if (holder == null) {
                return EXEC_FAILED;
            }
            final String defaultValue = holder.getDefaultValue();
            try {
                holder.setValue(defaultValue);
            } catch (final Exception e) {
                HenPlus.msg().println("setting to default '" + defaultValue + "' failed.");
                return EXEC_FAILED;
            }
            return SUCCESS;
        }
        return SYNTAX_ERROR;
    } else {
        /*
             * no args. show available properties
             */
        if (argc == 0) {
            PROP_META[0].resetWidth();
            PROP_META[1].resetWidth();
            final TableRenderer table = new TableRenderer(PROP_META, HenPlus.out());
            for (Map.Entry<String, PropertyHolder> entry : getRegistry().getPropertyMap().entrySet()) {
                final Column[] row = new Column[3];
                final PropertyHolder holder = entry.getValue();
                row[0] = new Column(entry.getKey());
                row[1] = new Column(holder.getValue());
                row[2] = new Column(holder.getShortDescription());
                table.addRow(row);
            }
            table.closeTable();
            return SUCCESS;
        } else if (argc == 1) {
            /*
                 * one arg: show help
                 */
            final String name = st.nextToken();
            PropertyHolder holder;
            holder = getRegistry().getPropertyMap().get(name);
            if (holder == null) {
                return EXEC_FAILED;
            }
            printDescription(name, holder);
            return SUCCESS;
        } else if (argc >= 2) {
            /*
                 * more than one arg
                 */
            final String varname = (String) st.nextElement();
            int pos = 0;
            final int paramLength = param.length();
            // skip whitespace after 'set'
            while (pos < paramLength && Character.isWhitespace(param.charAt(pos))) {
                ++pos;
            }
            // skip non-whitespace after 'set ': variable name
            while (pos < paramLength && !Character.isWhitespace(param.charAt(pos))) {
                ++pos;
            }
            // skip whitespace before vlue..
            while (pos < paramLength && Character.isWhitespace(param.charAt(pos))) {
                ++pos;
            }
            String value = param.substring(pos);
            if (value.startsWith("\"") && value.endsWith("\"")) {
                value = value.substring(1, value.length() - 1);
            } else if (value.startsWith("\'") && value.endsWith("\'")) {
                value = value.substring(1, value.length() - 1);
            }
            try {
                getRegistry().setProperty(varname, value);
            } catch (final Exception e) {
                HenPlus.msg().println(e.getMessage());
                return EXEC_FAILED;
            }
            return SUCCESS;
        }
    }
    return SUCCESS;
}
Also used : TableRenderer(henplus.view.TableRenderer) StringTokenizer(java.util.StringTokenizer) PropertyHolder(henplus.property.PropertyHolder) Column(henplus.view.Column) Map(java.util.Map)

Example 3 with Column

use of henplus.view.Column in project henplus by neurolabs.

the class ResultTablePrinter method printResult.

public static int printResult(final TableDiffResult result) {
    /*
         * if all columns belong to the same table name, then don't report it. A
         * different table name may only occur in rare circumstance like object
         * oriented databases.
         */
    // boolean allSameTableName = true;
    /*
         * build up actual describe table.
         */
    final List<Column[]> rows = new ArrayList<Column[]>();
    if (result != null) {
        // first, print removed columns
        final SortedSet<henplus.sqlmodel.Column> removed = result.getRemovedColumns();
        if (removed != null) {
            // ExtendedColumn header = new ExtendedColumn("Removed Columns",
            // 8, ExtendedColumn.ALIGN_CENTER);
            // rows.add(header);
            appendLines(STAT_REMOVED, rows, removed);
        }
        // then, print added columns
        final SortedSet<henplus.sqlmodel.Column> added = result.getAddedColumns();
        if (added != null) {
            appendLines(STAT_ADDED, rows, added);
        }
        // at last, print modified columns
        final LinkedHashMap<henplus.sqlmodel.Column, henplus.sqlmodel.Column> modified = result.getModifiedColumns();
        if (modified != null) {
            appendModified(rows, modified);
        }
    }
    /*
         * we render the table now, since we only know know, whether we will
         * show the first column or not.
         */
    final ExtendedTableRenderer table = new ExtendedTableRenderer(DESC_META, HenPlus.out());
    for (Column[] row : rows) {
        table.addRow(row);
    }
    table.closeTable();
    return Command.SUCCESS;
}
Also used : ExtendedColumn(henplus.view.ExtendedColumn) Column(henplus.view.Column) ArrayList(java.util.ArrayList) ExtendedTableRenderer(henplus.view.ExtendedTableRenderer)

Example 4 with Column

use of henplus.view.Column in project henplus by neurolabs.

the class PluginCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession currentSession, final String cmd, final String param) {
    final StringTokenizer st = new StringTokenizer(param);
    final int argc = st.countTokens();
    if ("list-plugins".equals(cmd)) {
        if (argc != 0) {
            return SYNTAX_ERROR;
        }
        HenPlus.msg().println("loaded plugins are marked with '*'");
        DRV_META[0].resetWidth();
        DRV_META[1].resetWidth();
        final TableRenderer table = new TableRenderer(DRV_META, HenPlus.out());
        for (Entry<String, Command> entry : _plugins.entrySet()) {
            final Column[] row = new Column[2];
            final Command c = entry.getValue();
            final String clsName = entry.getKey();
            row[0] = new Column((c != null ? "* " : "  ") + clsName);
            if (c != null) {
                final StringBuilder cmds = new StringBuilder();
                final String[] cmdList = c.getCommandList();
                for (int i = 0; i < cmdList.length; ++i) {
                    cmds.append(cmdList[i]).append("\n");
                }
                row[1] = new Column(cmds.toString().trim());
            } else {
                row[1] = new Column(null);
            }
            table.addRow(row);
        }
        table.closeTable();
        return SUCCESS;
    } else if ("plug-in".equals(cmd)) {
        if (argc != 1) {
            return SYNTAX_ERROR;
        }
        final String pluginClass = (String) st.nextElement();
        if (_plugins.containsKey(pluginClass)) {
            HenPlus.msg().println("plugin '" + pluginClass + "' already loaded");
            return EXEC_FAILED;
        }
        Command plugin = null;
        try {
            plugin = loadPlugin(pluginClass);
        } catch (final Exception e) {
            HenPlus.msg().println("couldn't load plugin: " + e.getMessage());
            return EXEC_FAILED;
        }
        if (plugin != null) {
            _plugins.put(pluginClass, plugin);
            final String[] cmds = plugin.getCommandList();
            HenPlus.out().print("adding commands: ");
            for (int i = 0; i < cmds.length; ++i) {
                if (i != 0) {
                    HenPlus.out().print(", ");
                }
                HenPlus.out().print(cmds[i]);
            }
            HenPlus.out().println();
        }
    } else if ("plug-out".equals(cmd)) {
        if (argc != 1) {
            return SYNTAX_ERROR;
        }
        final String pluginClass = (String) st.nextElement();
        if (!_plugins.containsKey(pluginClass)) {
            HenPlus.msg().println("unknown plugin '" + pluginClass + "'");
            return EXEC_FAILED;
        } else {
            final Command c = _plugins.remove(pluginClass);
            _henplus.getDispatcher().unregister(c);
        }
    }
    return SUCCESS;
}
Also used : TableRenderer(henplus.view.TableRenderer) StringTokenizer(java.util.StringTokenizer) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command) Column(henplus.view.Column)

Example 5 with Column

use of henplus.view.Column in project henplus by neurolabs.

the class ResultTablePrinter method appendLines.

private static void appendLines(final String symbol, final List<Column[]> rows, final SortedSet<henplus.sqlmodel.Column> rowSet) {
    for (henplus.sqlmodel.Column col : rowSet) {
        final Column[] row = new Column[8];
        row[0] = new Column(symbol);
        row[1] = new Column(col.getPosition());
        row[2] = new Column(col.getName());
        final String type = extractType(col);
        row[3] = new Column(type);
        row[4] = new Column(col.isNullable() ? YES : NO);
        final String defaultVal = col.getDefault();
        // oracle appends newline to default values for some reason.
        row[5] = new Column((defaultVal != null ? defaultVal.trim() : null));
        // String pkdesc = (String)pks.get(colname);
        row[6] = new Column(getPkDesc(col));
        // String fkdesc = (String)fks.get(colname);
        row[7] = new Column(getFkDesc(col));
        rows.add(row);
    }
}
Also used : ExtendedColumn(henplus.view.ExtendedColumn) Column(henplus.view.Column)

Aggregations

Column (henplus.view.Column)14 TableRenderer (henplus.view.TableRenderer)10 StringTokenizer (java.util.StringTokenizer)5 ExtendedColumn (henplus.view.ExtendedColumn)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AbstractCommand (henplus.AbstractCommand)1 Command (henplus.Command)1 SQLSession (henplus.SQLSession)1 PropertyHolder (henplus.property.PropertyHolder)1 ExtendedTableRenderer (henplus.view.ExtendedTableRenderer)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1