KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > sqlconsole > ShowTables


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Jeff Mesnil
20  * Contributor(s): ______________________.
21  */

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

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

51   public ShowTables(AbstractConsoleModule module)
52   {
53     super(module);
54   }
55
56   /**
57    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
58    */

59   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
60   {
61     try
62     {
63       DatabaseMetaData JavaDoc dbmd = ((VirtualDatabaseConsole) module).getConnection()
64           .getMetaData();
65       ResultSet JavaDoc tableSet = dbmd.getTables(null, null, null, new String JavaDoc[]{
66           "TABLE", "VIEW"}); //$NON-NLS-1$ //$NON-NLS-2$
67
if (tableSet == null)
68       {
69         console.printInfo(ConsoleTranslate
70             .get("sql.command.show.tables.no.tables")); //$NON-NLS-1$
71
return;
72       }
73       List JavaDoc tableNames = new ArrayList JavaDoc();
74       while (tableSet.next())
75       {
76         try
77         {
78           tableNames.add(tableSet.getString(tableSet.findColumn("TABLE_NAME"))); //$NON-NLS-1$
79
}
80         catch (Exception JavaDoc e)
81         {
82           // do nothing
83
}
84       }
85       if (tableNames.isEmpty())
86       {
87         console.printInfo(ConsoleTranslate
88             .get("sql.command.show.tables.no.tables")); //$NON-NLS-1$
89
}
90       else
91       {
92         console.println(TableFormatter.format(new String JavaDoc[]{"tables"},
93             getTableNamesAsCells(tableNames), true));
94       }
95     }
96     catch (Exception JavaDoc e)
97     {
98       console.printError(ConsoleTranslate.get("sql.command.sqlquery.error", e), //$NON-NLS-1$
99
e);
100     }
101   }
102
103   private String JavaDoc[][] getTableNamesAsCells(List JavaDoc tableNames)
104   {
105     String JavaDoc[][] cells = new String JavaDoc[tableNames.size()][1];
106     for (int i = 0; i < tableNames.size(); i++)
107     {
108       cells[i][0] = (String JavaDoc) tableNames.get(i);
109     }
110     return cells;
111   }
112
113   /**
114    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
115    */

116   public String JavaDoc getCommandName()
117   {
118     return "show tables"; //$NON-NLS-1$
119
}
120
121   /**
122    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
123    */

124   public String JavaDoc getCommandDescription()
125   {
126     return ConsoleTranslate.get("sql.command.show.tables.description"); //$NON-NLS-1$
127
}
128 }
Popular Tags