KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > commands > sqlconsole > ShowTables


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Jeff Mesnil
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.text.commands.sqlconsole;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.DatabaseMetaData JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
33 import org.objectweb.cjdbc.console.text.ColorPrinter;
34 import org.objectweb.cjdbc.console.text.ConsoleException;
35 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand;
36 import org.objectweb.cjdbc.console.text.formatter.TableFormatter;
37 import org.objectweb.cjdbc.console.text.module.AbstractConsoleModule;
38 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseConsole;
39
40 /**
41  * This class defines a ShowTables
42  *
43  * @author <a HREF="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
44  */

45 public class ShowTables extends ConsoleCommand
46 {
47
48   /**
49    * Creates a new <code>Quit.java</code> object
50    *
51    * @param module the command is attached to
52    */

53   public ShowTables(AbstractConsoleModule module)
54   {
55     super(module);
56   }
57
58   /**
59    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#parse(java.lang.String)
60    */

61   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
62   {
63     try
64     {
65       DatabaseMetaData JavaDoc dbmd = ((VirtualDatabaseConsole)module).getConnection().getMetaData();
66       ResultSet JavaDoc tableSet = dbmd.getTables(null, null, null, new String JavaDoc[] {"TABLE", "VIEW"});
67       if (tableSet == null)
68       {
69         console.printInfo(ConsoleTranslate.get("sql.command.show.tables.no.tables"));
70         return;
71       }
72       ArrayList JavaDoc tableNames = new ArrayList JavaDoc();
73       while (!tableSet.isLast())
74       {
75         tableSet.next();
76         tableNames.add(tableSet.getString(tableSet.findColumn("TABLE_NAME")));
77       }
78       console.println(TableFormatter.format(new String JavaDoc[] {"tables"}, getTableNamesAsCells(tableNames), true), ColorPrinter.STATUS);
79     }
80     catch (Exception JavaDoc e)
81     {
82       console.printError(ConsoleTranslate.get("sql.command.sqlquery.error", e),
83           e);
84     }
85   }
86
87   private String JavaDoc[][] getTableNamesAsCells(ArrayList JavaDoc tableNames)
88   {
89     String JavaDoc[][] cells = new String JavaDoc[tableNames.size()][1];
90     for (int i = 0; i < tableNames.size(); i++)
91     {
92       cells[i][0] = (String JavaDoc)tableNames.get(i);
93     }
94     return cells;
95   }
96
97   /**
98    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandName()
99    */

100   public String JavaDoc getCommandName()
101   {
102     return "show tables";
103   }
104
105   /**
106    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandDescription()
107    */

108   public String JavaDoc getCommandDescription()
109   {
110     return ConsoleTranslate.get("sql.command.show.tables.description");
111   }
112 }
Popular Tags