1 24 25 package org.objectweb.cjdbc.console.text.commands.sqlconsole; 26 27 import java.io.IOException ; 28 import java.sql.DatabaseMetaData ; 29 import java.sql.ResultSet ; 30 import java.util.ArrayList ; 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 45 public class ShowTables extends ConsoleCommand 46 { 47 48 53 public ShowTables(AbstractConsoleModule module) 54 { 55 super(module); 56 } 57 58 61 public void parse(String commandText) throws IOException , ConsoleException 62 { 63 try 64 { 65 DatabaseMetaData dbmd = ((VirtualDatabaseConsole)module).getConnection().getMetaData(); 66 ResultSet tableSet = dbmd.getTables(null, null, null, new String [] {"TABLE", "VIEW"}); 67 if (tableSet == null) 68 { 69 console.printInfo(ConsoleTranslate.get("sql.command.show.tables.no.tables")); 70 return; 71 } 72 ArrayList tableNames = new ArrayList (); 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 [] {"tables"}, getTableNamesAsCells(tableNames), true), ColorPrinter.STATUS); 79 } 80 catch (Exception e) 81 { 82 console.printError(ConsoleTranslate.get("sql.command.sqlquery.error", e), 83 e); 84 } 85 } 86 87 private String [][] getTableNamesAsCells(ArrayList tableNames) 88 { 89 String [][] cells = new String [tableNames.size()][1]; 90 for (int i = 0; i < tableNames.size(); i++) 91 { 92 cells[i][0] = (String )tableNames.get(i); 93 } 94 return cells; 95 } 96 97 100 public String getCommandName() 101 { 102 return "show tables"; 103 } 104 105 108 public String getCommandDescription() 109 { 110 return ConsoleTranslate.get("sql.command.show.tables.description"); 111 } 112 } | Popular Tags |