001: /*
002: * Copyright 2007 Roy van der Kuil (roy@vanderkuil.nl)
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 nl.improved.sqlclient.SQLCommand;
019: import nl.improved.sqlclient.DBConnector;
020: import nl.improved.sqlclient.Point;
021: import nl.improved.sqlclient.SQLUtil;
022: import nl.improved.sqlclient.TabCompletionInfo;
023: import java.util.Arrays;
024: import java.util.List;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.sql.SQLException;
028: import java.sql.ResultSet;
029:
030: /**
031: * Command to describe table properties.
032: */
033: public class ShowCommand implements Command {
034: /**
035: * Execute the describe command.
036: */
037: @Override
038: public CharSequence execute(SQLCommand command) {
039: java.sql.Connection conn = DBConnector.getInstance()
040: .getConnection();
041: String cmd = command.getCommandString();
042: if (cmd.endsWith(";")) {
043: cmd = cmd.substring(0, cmd.length() - 1);
044: }
045: StringBuilder returnValue = new StringBuilder();
046: String subCommand = cmd.substring(cmd.indexOf(' ')).trim()
047: .toUpperCase();
048: if (subCommand.startsWith("TABLES")) {
049: if (subCommand.indexOf(' ') > 0) {
050: String otherCommand = subCommand.substring(
051: subCommand.indexOf(' ')).trim();
052: String columnName = DBConnector.getInstance()
053: .translateDbVar(
054: otherCommand.substring(
055: otherCommand.indexOf(' '))
056: .trim());
057: if (otherCommand.toUpperCase().matches(
058: "HAVING[\\s]+[A-Z]+.*")) {
059: try {
060: ResultSet rs = conn.getMetaData().getColumns(
061: conn.getCatalog(),
062: DBConnector.getInstance().getSchema(),
063: "%", columnName);
064: List matches = new ArrayList<String>();
065: while (rs.next()) {
066: if (!matches.contains(rs
067: .getString("TABLE_NAME"))) {
068: matches.add(rs.getString("TABLE_NAME"));
069: }
070: }
071: returnValue
072: .append("\nTables with column name: '"
073: + columnName + "'\n");
074: Iterator<String> iMatches = matches.iterator();
075: while (iMatches.hasNext()) {
076: returnValue.append(" ");
077: returnValue.append(iMatches.next());
078: returnValue.append('\n');
079: }
080: returnValue.append("DONE\n");
081: return returnValue.toString();
082: } catch (SQLException e) {
083: throw new IllegalStateException(
084: "Failed to find tablenames", e);
085: }
086: }
087: }
088: try {
089: ResultSet rs = conn.getMetaData().getTables(
090: conn.getCatalog(),
091: DBConnector.getInstance().getSchema(), null,
092: new String[] { "TABLE" });
093: while (rs.next()) {
094: returnValue.append(" ");
095: returnValue.append(rs.getString("TABLE_NAME"));
096: returnValue.append('\n');
097: }
098: returnValue.append('\n');
099: } catch (SQLException ex) {
100: throw new IllegalStateException(
101: "Failed to find tablenames", ex);
102: }
103: } else {
104: returnValue.append("Don't know what to show for: '"
105: + cmd.substring(cmd.lastIndexOf(' ')).trim()
106: + "'\n");
107: }
108: return returnValue;
109: }
110:
111: /**
112: * Return the command string desc.
113: * @return the command string desc.
114: */
115: @Override
116: public CharSequence getCommandString() {
117: return "show";
118: }
119:
120: /**
121: * Returns some tab completion info for the specified command.
122: * @param commandInfo the command lines
123: * @param commandPoint the cursor position
124: * @return some tab completion info for the specified command.
125: */
126: @Override
127: public TabCompletionInfo getTabCompletionInfo(SQLCommand command,
128: Point commandPoint) {
129: String commandString = command.getUntrimmedCommandString();
130: String commandStringUpper = commandString.toUpperCase();
131:
132: if (commandStringUpper
133: .matches("[\\s]*SHOW[\\s]+TABLES[\\s]+HAVING[\\s]+([A-Z]|_|-|[0-9])*")) {
134: String end = commandString.trim().substring(
135: commandString.trim().lastIndexOf(' ') + 1).trim();
136: if (end.equalsIgnoreCase("HAVING")) {
137: return new TabCompletionInfo(
138: TabCompletionInfo.MatchType.COLUMN_NAMES,
139: Arrays.asList(new String[] { "%" }), "");
140: } else {
141: return new TabCompletionInfo(
142: TabCompletionInfo.MatchType.COLUMN_NAMES,
143: Arrays.asList(new String[] { "%" }), end);
144: }
145: }
146:
147: String startOfCommand = SQLUtil.getStartOfCommand(command
148: .getLines(), commandPoint);
149: String end = startOfCommand.substring(startOfCommand
150: .lastIndexOf(' ') + 1);
151: if (commandStringUpper
152: .matches("SHOW[\\s]+(|T|TA|TAB|TABL|TABLE)")) {
153: return new TabCompletionInfo(
154: TabCompletionInfo.MatchType.UNKNOWN, Arrays
155: .asList(new String[] { "TABLES" }), end);
156: }
157: return new TabCompletionInfo(
158: TabCompletionInfo.MatchType.UNKNOWN, Arrays
159: .asList(new String[] { "HAVING" }), end);
160: }
161:
162: /**
163: * Return the command help.
164: * @return the command help.
165: */
166: @Override
167: public CharSequence getHelp() {
168: return "tables: show all tables available in the currently selected schema\n"
169: + "tables having columnname: same as 'show tables' but limited to tables with that specific columnname";
170: }
171:
172: @Override
173: public boolean abort() {
174: return false; // not implemented
175: }
176:
177: @Override
178: public boolean backgroundProcessSupported() {
179: return false;
180: }
181: }
|