1 22 23 package org.continuent.sequoia.console.text.commands.sqlconsole; 24 25 import java.io.IOException ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.ResultSet ; 28 import java.util.ArrayList ; 29 import java.util.List ; 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 43 public class ShowTables extends ConsoleCommand 44 { 45 46 51 public ShowTables(AbstractConsoleModule module) 52 { 53 super(module); 54 } 55 56 59 public void parse(String commandText) throws IOException , ConsoleException 60 { 61 try 62 { 63 DatabaseMetaData dbmd = ((VirtualDatabaseConsole) module).getConnection() 64 .getMetaData(); 65 ResultSet tableSet = dbmd.getTables(null, null, null, new String []{ 66 "TABLE", "VIEW"}); if (tableSet == null) 68 { 69 console.printInfo(ConsoleTranslate 70 .get("sql.command.show.tables.no.tables")); return; 72 } 73 List tableNames = new ArrayList (); 74 while (tableSet.next()) 75 { 76 try 77 { 78 tableNames.add(tableSet.getString(tableSet.findColumn("TABLE_NAME"))); } 80 catch (Exception e) 81 { 82 } 84 } 85 if (tableNames.isEmpty()) 86 { 87 console.printInfo(ConsoleTranslate 88 .get("sql.command.show.tables.no.tables")); } 90 else 91 { 92 console.println(TableFormatter.format(new String []{"tables"}, 93 getTableNamesAsCells(tableNames), true)); 94 } 95 } 96 catch (Exception e) 97 { 98 console.printError(ConsoleTranslate.get("sql.command.sqlquery.error", e), e); 100 } 101 } 102 103 private String [][] getTableNamesAsCells(List tableNames) 104 { 105 String [][] cells = new String [tableNames.size()][1]; 106 for (int i = 0; i < tableNames.size(); i++) 107 { 108 cells[i][0] = (String ) tableNames.get(i); 109 } 110 return cells; 111 } 112 113 116 public String getCommandName() 117 { 118 return "show tables"; } 120 121 124 public String getCommandDescription() 125 { 126 return ConsoleTranslate.get("sql.command.show.tables.description"); } 128 } | Popular Tags |