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 java.util.ArrayList;
020: import nl.improved.sqlclient.Point;
021: import nl.improved.sqlclient.SQLUtil;
022: import nl.improved.sqlclient.TabCompletionInfo;
023: import nl.improved.sqlclient.SQLCommand;
024: import nl.improved.sqlclient.DBConnector;
025: import nl.improved.sqlclient.util.ResultBuilder;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.DatabaseMetaData;
029:
030: /**
031: * Command to describe table properties.
032: */
033: public class DescCommand implements Command {
034: /**
035: * Execute the describe command.
036: */
037: public CharSequence execute(SQLCommand command) {
038: java.sql.Connection conn = DBConnector.getInstance()
039: .getConnection();
040: String cmd = command.getCommandString();
041: if (cmd.endsWith(";")) {
042: cmd = cmd.substring(0, cmd.length() - 1);
043: }
044: String tableName = DBConnector.getInstance().translateDbVar(
045: cmd.substring(cmd.lastIndexOf(' ')).trim());
046: ResultBuilder result = new ResultBuilder();
047: result.setHorizontalSeparatorEnabled(false);
048: result.setVerticalSeparator(' ');
049: boolean foundMatch = false;
050: try {
051: ResultSet rs = conn.getMetaData().getColumns(
052: conn.getCatalog(),
053: DBConnector.getInstance().getSchema(), tableName,
054: "%");
055: int row = 0;
056: while (rs.next()) {
057: foundMatch = true;
058: result.set(0, row, rs.getString("COLUMN_NAME"));
059: int nullable = rs.getInt("NULLABLE");
060: if (nullable == DatabaseMetaData.columnNoNulls) {
061: result.set(1, row, "NOT NULL");
062: }
063: String columnSize = rs.getString("COLUMN_SIZE");
064: if (columnSize != null) {
065: result.set(2, row, rs.getString("TYPE_NAME") + "("
066: + columnSize + ")");
067: } else {
068: result.set(2, row, rs.getString("TYPE_NAME"));
069: }
070: row++;
071: }
072: if (!foundMatch) {
073: rs = conn.getMetaData().getTables(conn.getCatalog(),
074: DBConnector.getInstance().getSchema(),
075: tableName, new String[] { "TABLE" });
076: if (!rs.next()) {
077: return "Failed to find table '" + tableName + "'";
078: }
079: } else {
080: rs = conn.getMetaData().getPrimaryKeys(
081: conn.getCatalog(),
082: DBConnector.getInstance().getSchema(),
083: tableName);
084: StringBuffer footer = new StringBuffer();
085: String indent = " ";
086: while (rs.next()) {
087: if (footer.length() > 0) {
088: footer.append(",\n");
089: } else {
090: footer.append("PRIMARY KEY");
091: String pkName = rs.getString("PK_NAME");
092: if (pkName != null && pkName.length() > 0) {
093: footer.append(' ');
094: footer.append(pkName);
095: }
096: footer.append(":\n");
097: }
098: footer.append(indent);
099: footer.append(rs.getString("COLUMN_NAME"));
100: }
101: result.setFooter(footer);
102: }
103: } catch (SQLException ex) {
104: throw new IllegalStateException(
105: "Failed to find columnnames for table: "
106: + tableName, ex);
107: }
108: return result.toString();
109: }
110:
111: /**
112: * Return the command string desc.
113: * @return the command string desc.
114: */
115: @Override
116: public CharSequence getCommandString() {
117: return "desc";
118: }
119:
120: @Override
121: public TabCompletionInfo getTabCompletionInfo(SQLCommand command,
122: Point commandPoint) {
123: List commandInfo = command.getLines();
124:
125: String startOfCommand = SQLUtil.getStartOfCommand(commandInfo,
126: commandPoint);
127: String end = startOfCommand.substring(startOfCommand
128: .lastIndexOf(' ') + 1);
129: return new TabCompletionInfo(
130: TabCompletionInfo.MatchType.TABLE_NAMES,
131: new ArrayList<String>(), end);
132: }
133:
134: /**
135: * Return the command help.
136: * @return the command help.
137: */
138: public CharSequence getHelp() {
139: return "Describes the table structure.\n"
140: + "For example 'desc mytable' shows the properties of the mytable table";
141: }
142:
143: @Override
144: public boolean abort() {
145: throw new UnsupportedOperationException("Not supported yet.");
146: }
147:
148: @Override
149: public boolean backgroundProcessSupported() {
150: return false;
151: }
152: }
|