001: /*
002: * Copyright 2007 Roy van der Kuil (roy@vanderkuil.nl) and Stefan Rotman (stefan@rotman.net)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.improved.sqlclient.commands;
017:
018: import java.util.List;
019: import nl.improved.sqlclient.Point;
020: import nl.improved.sqlclient.TabCompletionInfo;
021: import nl.improved.sqlclient.SQLCommand;
022: import nl.improved.sqlclient.DBConnector;
023: import java.sql.SQLException;
024: import java.sql.DatabaseMetaData;
025: import java.sql.ResultSet;
026:
027: /**
028: * Command for showing connection info.
029: */
030: public class InfoCommand implements Command {
031: /**
032: * Execute the info command.
033: * @param command the command that is executed
034: * @return the info of the connection
035: */
036: @Override
037: public CharSequence execute(SQLCommand cmd) {
038: java.sql.Connection conn = DBConnector.getInstance()
039: .getConnection();
040: StringBuilder returnValue = new StringBuilder();
041: try {
042: DatabaseMetaData metaData = conn.getMetaData();
043: returnValue.append("URL : " + metaData.getURL()
044: + "\n");
045: returnValue.append("User : " + metaData.getUserName()
046: + "\n");
047: returnValue.append("Driver : "
048: + metaData.getDriverName() + " "
049: + metaData.getDriverVersion() + "\n");
050: returnValue.append("Database : "
051: + metaData.getDatabaseProductName() + " "
052: + metaData.getDatabaseProductVersion() + "\n");
053: returnValue.append("Autocommit: " + conn.getAutoCommit()
054: + "\n");
055: returnValue.append("Schemas : ");
056: ResultSet rs = metaData.getSchemas();
057: if (rs.next()) {
058: returnValue.append(rs.getString("TABLE_SCHEM") + "\n");
059: } else {
060: returnValue.append('\n');
061: }
062: while (rs.next()) {
063: returnValue.append(" : "
064: + rs.getString("TABLE_SCHEM") + "\n");
065: }
066: } catch (SQLException ex) {
067: throw new IllegalStateException(
068: "Failed to find table info: " + ex, ex);
069: }
070: returnValue.append("\n");
071: return returnValue;
072: }
073:
074: /**
075: * Return the command string.
076: * @return the command string.
077: */
078: @Override
079: public CharSequence getCommandString() {
080: return "info";
081: }
082:
083: /**
084: * Returns some tab completion info for the specified command.
085: * @param commandInfo the command lines
086: * @param commandPoint the cursor position
087: * @return some tab completion info for the specified command.
088: */
089: @Override
090: public TabCompletionInfo getTabCompletionInfo(SQLCommand command,
091: Point commandPoint) {
092: return null;
093: }
094:
095: /**
096: * Return the command help.
097: * @return the command help.
098: */
099: @Override
100: public CharSequence getHelp() {
101: return "Shows information about the current connection.";
102: }
103:
104: @Override
105: public boolean abort() {
106: return false;
107: }
108:
109: @Override
110: public boolean backgroundProcessSupported() {
111: return false;
112: }
113: }
|