001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Jeff Mesnil
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.console.text.commands.sqlconsole;
022:
023: import java.io.IOException;
024: import java.sql.DatabaseMetaData;
025: import java.sql.ResultSet;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
030: import org.continuent.sequoia.console.text.ConsoleException;
031: import org.continuent.sequoia.console.text.commands.ConsoleCommand;
032: import org.continuent.sequoia.console.text.formatter.TableFormatter;
033: import org.continuent.sequoia.console.text.module.AbstractConsoleModule;
034: import org.continuent.sequoia.console.text.module.VirtualDatabaseConsole;
035:
036: /**
037: * This class defines a ShowTables
038: *
039: * @author <a href="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
040: */
041: public class ShowTables extends ConsoleCommand {
042:
043: /**
044: * Creates a new <code>Quit.java</code> object
045: *
046: * @param module the command is attached to
047: */
048: public ShowTables(AbstractConsoleModule module) {
049: super (module);
050: }
051:
052: /**
053: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
054: */
055: public void parse(String commandText) throws IOException,
056: ConsoleException {
057: try {
058: DatabaseMetaData dbmd = ((VirtualDatabaseConsole) module)
059: .getConnection().getMetaData();
060: ResultSet tableSet = dbmd.getTables(null, null, null,
061: new String[] { "TABLE", "VIEW" }); //$NON-NLS-1$ //$NON-NLS-2$
062: if (tableSet == null) {
063: console.printInfo(ConsoleTranslate
064: .get("sql.command.show.tables.no.tables")); //$NON-NLS-1$
065: return;
066: }
067: List tableNames = new ArrayList();
068: while (tableSet.next()) {
069: try {
070: tableNames.add(tableSet.getString(tableSet
071: .findColumn("TABLE_NAME"))); //$NON-NLS-1$
072: } catch (Exception e) {
073: // do nothing
074: }
075: }
076: if (tableNames.isEmpty()) {
077: console.printInfo(ConsoleTranslate
078: .get("sql.command.show.tables.no.tables")); //$NON-NLS-1$
079: } else {
080: console.println(TableFormatter.format(
081: new String[] { "tables" },
082: getTableNamesAsCells(tableNames), true));
083: }
084: } catch (Exception e) {
085: console.printError(ConsoleTranslate.get(
086: "sql.command.sqlquery.error", e), //$NON-NLS-1$
087: e);
088: }
089: }
090:
091: private String[][] getTableNamesAsCells(List tableNames) {
092: String[][] cells = new String[tableNames.size()][1];
093: for (int i = 0; i < tableNames.size(); i++) {
094: cells[i][0] = (String) tableNames.get(i);
095: }
096: return cells;
097: }
098:
099: /**
100: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
101: */
102: public String getCommandName() {
103: return "show tables"; //$NON-NLS-1$
104: }
105:
106: /**
107: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
108: */
109: public String getCommandDescription() {
110: return ConsoleTranslate
111: .get("sql.command.show.tables.description"); //$NON-NLS-1$
112: }
113: }
|