|
From: SVN by r. <sv...@ca...> - 2009-01-16 19:46:53
|
Author: roy
Date: 2009-01-16 20:46:42 +0100 (Fri, 16 Jan 2009)
New Revision: 348
Added:
src/main/java/nl/improved/sqlclient/commands/CommandResult.java
src/main/java/nl/improved/sqlclient/commands/SimpleCommandResult.java
Modified:
src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java
src/main/java/nl/improved/sqlclient/QueryExecutor.java
src/main/java/nl/improved/sqlclient/SQLShell.java
src/main/java/nl/improved/sqlclient/commands/Command.java
src/main/java/nl/improved/sqlclient/commands/DescCommand.java
src/main/java/nl/improved/sqlclient/commands/InfoCommand.java
src/main/java/nl/improved/sqlclient/commands/ShowCommand.java
Log:
lot of refactoring in command classes
results can now be paged (initial version)
Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java
===================================================================
--- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -438,7 +438,14 @@
repaint();
if (/*direct ||*/ !cmd.cmd.backgroundProcessSupported()) {
try {
- output(cmd.cmd.execute(cmd.sql));
+ CommandResult result = cmd.cmd.execute(cmd.sql);
+ Iterator<CharSequence> iResult = result.getResult();
+ while (iResult.hasNext()) {
+ output(iResult.next());
+ }
+ if (!result.executedSuccessfully()) {
+ output("Execution Failed...\n");
+ }
} catch(Exception e) {
error(e);
}
@@ -447,7 +454,14 @@
commandThread = new CommandThread(cmd.cmd) {
@Override
void execute() {
- output(getCommand().execute(cmd.sql));
+ CommandResult result = getCommand().execute(cmd.sql);
+ Iterator<CharSequence> iResult = result.getResult();
+ while (iResult.hasNext()) {
+ output(iResult.next());
+ }
+ if (!result.executedSuccessfully()) {
+ output("Execution Failed...\n");
+ }
repaint();
}
};
@@ -1019,7 +1033,14 @@
}
output(sqlCommand.getLines());
//repaint();
- output(command.execute(sqlCommand));
+ CommandResult commandResult = command.execute(sqlCommand);
+ Iterator<CharSequence> result = commandResult.getResult();
+ while (result.hasNext()) {
+ output(result.next());
+ }
+ if (!commandResult.executedSuccessfully()) {
+ output("Execution Failed...\n");
+ }
repaint();
return true;
}
@@ -1100,7 +1121,7 @@
* @return a readable result of the execution of this command.
*/
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
String command = cmd.getCommandString();
try {
String cmdString = command.substring("connect".length()).trim();
@@ -1108,7 +1129,7 @@
cmdString = cmdString.substring(0, cmdString.length()-1);
}
if (connect(cmdString) == null) {
- return "Connect failed. Unknown reason\n\n";
+ return new SimpleCommandResult(false, "Connect failed. Unknown reason\n\n");
}
} catch(SQLException e) {
throw new IllegalStateException("Failed to connect: " + e.getMessage(), e);
@@ -1116,12 +1137,11 @@
try {
int tot = HistoryPersister.loadHistory(AbstractSQLShellWindow.this, ident,
getScreen().MAX_LINE_LENGTH, getScreen().MAX_LINE_LENGTH);
- return "Connected and history loaded (" + tot + " command(s)).\n\n";
+ return new SimpleCommandResult(true, "Connected and history loaded (" + tot + " command(s)).\n\n");
} catch (CouldNotLoadHistoryException e) {
// this.shell.setCommandIndex(0);
- return "Connected (no history loaded).\n\n";
+ return new SimpleCommandResult(true, "Connected (no history loaded).\n\n");
}
-
}
@Override
public CharSequence getCommandString() {
@@ -1274,20 +1294,20 @@
private class DisConnectCommand implements Command {
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
try {
DBConnector.getInstance().disconnect();
if (ident != null) {
try {
HistoryPersister.saveHistory(AbstractSQLShellWindow.this, ident, true);
ident = null;
- return "Disconnected (history saved).\n\n";
+ return new SimpleCommandResult(true, "Disconnected (history saved).\n\n");
} catch (CouldNotSaveHistoryException e) {
- return "Disconnected, could not save history: " + e.getMessage()
- + "\n\n";
+ return new SimpleCommandResult(false, "Disconnected, could not save history: " + e.getMessage()
+ + "\n\n");
}
}
- return "Disconnected";
+ return new SimpleCommandResult(true, "Disconnected");
} catch(SQLException e) {
throw new IllegalStateException("Failed to disconnect: " + e.getMessage(), e);
}
@@ -1326,7 +1346,7 @@
*/
private class HistoryCommand implements Command {
@Override
- public CharSequence execute(SQLCommand command) {
+ public CommandResult execute(SQLCommand command) {
StringBuffer returnValue = new StringBuffer();
Iterator<SQLCommand> iCommands = commandHistory.iterator();
returnValue.append("\n**** History Overview ****\n");
@@ -1342,8 +1362,9 @@
returnValue.append(cmdString);
returnValue.append('\n');
}
- return returnValue;
+ return new SimpleCommandResult(true, returnValue);
}
+
@Override
public CharSequence getCommandString() {
return "history";
@@ -1384,11 +1405,11 @@
this.cmd = cmd;
}
@Override
- public CharSequence execute(SQLCommand command) {
+ public CommandResult execute(SQLCommand command) {
run = false;
new DisConnectCommand().execute(null);
close();
- return "Application terminated.";
+ return new SimpleCommandResult(true, "Application terminated.");
}
@Override
public CharSequence getHelp() {
@@ -1422,7 +1443,7 @@
*/
private class HelpCommand implements Command {
@Override
- public CharSequence execute(SQLCommand sqlCommand) {
+ public CommandResult execute(SQLCommand sqlCommand) {
// the execution of help consists of:
// 1. is general help..
// 2. is detailed help about a specific command
@@ -1451,21 +1472,21 @@
}
}
if (returnValue.length() == 0) {
- return "Don't know what you mean by '"+ keyword+"'";
+ return new SimpleCommandResult(false, "Don't know what you mean by '"+ keyword+"'");
}
- return returnValue;
+ return new SimpleCommandResult(true, returnValue);
} else {
Iterator<Command> iCommands = commands.getCommands().iterator();
while (iCommands.hasNext()) {
Command cmd= iCommands.next();
if (cmd.getCommandString().equals(cmdString)) {
- return cmd.getCommandString()+": "
+ return new SimpleCommandResult(true, cmd.getCommandString()+": "
+ cmd.getHelp().toString().replaceAll("\n"
- , "\n"+screen.getEmptyLine().substring(0, cmd.getCommandString().length()+3));
+ , "\n"+screen.getEmptyLine().substring(0, cmd.getCommandString().length()+3)));
}
}
}
- return "Unkown command '"+ cmdString+"'";
+ return new SimpleCommandResult(true, "Unkown command '"+ cmdString+"'");
}
// default print all commands
// TODO iterate
@@ -1506,7 +1527,7 @@
" help -k searchstring (for example help -k column)\n"+
"This results in a list of commands matching the searchstring\n\n";
returnValue.insert(0, helpHeader);
- return returnValue;
+ return new SimpleCommandResult(true, returnValue);
}
@Override
public CharSequence getCommandString() {
@@ -1639,7 +1660,7 @@
private String fileName;
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
String command = cmd.getCommandString();
String nextPart = command.substring("spool".length()).trim();
if (nextPart.equalsIgnoreCase("off")) {
@@ -1648,9 +1669,9 @@
spoolWriter.close();
} catch(Exception e) {/*ignore*/}
spoolWriter = null;
- return "Spool closed.";
+ return new SimpleCommandResult(true, "Spool closed.");
} else {
- return "No spool to close.";
+ return new SimpleCommandResult(true, "No spool to close.");
}
} else {
StringBuffer returnValue = new StringBuffer(100);
@@ -1675,7 +1696,7 @@
throw new IllegalStateException("Failed to create spool ("+fileName+"): " + e.toString(), e);
}
returnValue.append("Spool to "+fileName+" created.");
- return returnValue.toString();
+ return new SimpleCommandResult(true, returnValue.toString());
}
}
@@ -1739,7 +1760,7 @@
private String fileName;
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
String command = cmd.getCommandString();
String nextPart = command.substring("dump".length()).trim();
String dumpFileName;
@@ -1843,7 +1864,7 @@
out.close();
}
}
- return "Dump to "+fileName+" done. ("+ rowCount+" rows written)";
+ return new SimpleCommandResult(true, "Dump to "+fileName+" done. ("+ rowCount+" rows written)");
}
@Override
@@ -1889,7 +1910,7 @@
private String fileName;
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
String command = cmd.getCommandString();
String nextPart = command.substring("read".length()).trim();
String dumpFileName;
@@ -1993,7 +2014,7 @@
} catch (Exception e) {
throw new IllegalStateException("Failed to read dump ("+fileName+"): " + e.toString(), e);
}
- return "Read from "+fileName+" done. (" + rowCount+" rows imported)";
+ return new SimpleCommandResult(true, "Read from "+fileName+" done. (" + rowCount+" rows imported)");
}
@Override
@@ -2038,7 +2059,7 @@
}
@Override
- public CharSequence execute(SQLCommand sqlCommand) {
+ public CommandResult execute(SQLCommand sqlCommand) {
cancelled = false;
currentCommand = null;
String command = sqlCommand.getCommandString();
@@ -2050,20 +2071,32 @@
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
StringBuffer cmd = new StringBuffer();
String line;
+ int errorCount = 0;
while ( ((line = reader.readLine()) != null)) {
if (cancelled) {
- return "Aborted";
+ return new SimpleCommandResult(true, "Aborted");
}
if (line.startsWith("--")) {
continue;
}
cmd.append(line);
- if (line.endsWith(";")) {
- // Exec cmd
+ //if (line.endsWith(";")) {
String commandString = cmd.toString();
currentCommand = sqlShell.createCommand(commandString);
+ if (currentCommand == null) {
+ continue;
+ }
+ // Exec cmd
sqlShell.output(commandString);
- sqlShell.output(currentCommand.execute(new InputCommand(commandString)));
+ CommandResult result = currentCommand.execute(new InputCommand(commandString));
+ Iterator<CharSequence> output = result.getResult();
+ while (output.hasNext()) {
+ sqlShell.output(output.next());
+ }
+ if (!result.executedSuccessfully()) {
+ errorCount++;
+ sqlShell.output("Execution Failed...\n");
+ }
cmd=new StringBuffer();
new Thread() {
@Override
@@ -2071,19 +2104,19 @@
sqlShell.repaint();
}
}.start();
- }
+ //}
}
if (cancelled) {
- return "Execution of file '" + toFileName(command.substring(1)) +"' aborted....";
+ return new SimpleCommandResult(true, "Execution of file '" + toFileName(command.substring(1)) +"' aborted....");
}
if (cmd.toString().trim().length() > 0) {
- return "File '"+ toFileName(command.substring(1)) +"' missing a ';' at the end of the last command";
+ return new SimpleCommandResult(false, "File '"+ toFileName(command.substring(1)) +"' missing a ';' at the end of the last command");
} else {
- return "File '" + toFileName(command.substring(1)) +"' executed successfully.";
+ return new SimpleCommandResult(true, "File '" + toFileName(command.substring(1)) +"' executed " + (errorCount > 0 ? "with "+ errorCount+" errors." : "successfully."));
}
} catch(IOException e) {
sqlShell.error(e);
- return "File '" + toFileName(command.substring(1)) +"' ended with errors.";
+ return new SimpleCommandResult(true, "File '" + toFileName(command.substring(1)) +"' ended with errors.");
} finally {
if (fin != null) try { fin.close();}catch(Exception e) {/*ignore*/}
}
@@ -2131,13 +2164,13 @@
* Simple command to save the current visible screen history (commands and it"s output).
*/
private class SaveCommand implements Command {
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
String command = cmd.getCommandString().substring("save".length()).trim();
if (command.startsWith("history")) {
String fileName = toFileName(command.substring("history".length()).trim());
File f = new File(fileName);
if (f.exists() && !f.canWrite()) {
- return "Unable to overwrite existing file : " + f.getAbsolutePath();
+ return new SimpleCommandResult(false, "Unable to overwrite existing file : " + f.getAbsolutePath());
}
FileWriter writer = null;
try {
@@ -2146,10 +2179,10 @@
writer.write(c.toString());
writer.write('\n');
}
- return "History successfully written to : "+ f.getAbsolutePath();
+ return new SimpleCommandResult(true, "History successfully written to : "+ f.getAbsolutePath());
} catch (IOException ex) {
Logger.getLogger(AbstractSQLShellWindow.class.getName()).log(Level.SEVERE, null, ex);
- return "Unable to write to file: "+ f.getAbsolutePath() +"("+ ex+")";
+ return new SimpleCommandResult(false, "Unable to write to file: "+ f.getAbsolutePath() +"("+ ex+")");
} finally {
if (writer != null) {
try {
@@ -2281,22 +2314,33 @@
* @return the result of the sql query.
*/
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
try {
- String command = cmd.getCommandString();
+ final String command = cmd.getCommandString();
if (command.length() > "select".length() && "select".equalsIgnoreCase(command.subSequence(0, "create".length()).toString())) {
- return DBConnector.getInstance().getQueryExecutor().executeQuery(command);
+ return new CommandResult() {
+ public boolean executedSuccessfully() {
+ return true;
+ }
+ public Iterator<CharSequence> getResult() {
+ try {
+ return DBConnector.getInstance().getQueryExecutor().executeQuery(command);
+ } catch (SQLException ex) {
+ return null;
+ }
+ }
+ };
}
if (statementExecutor == null) {
statementExecutor = new StatementExecutor();
}
- return statementExecutor.execute(command);
+ return new SimpleCommandResult(true, statementExecutor.execute(command));
} catch(SQLException e) {
error(e);
- return "";
+ return new SimpleCommandResult(false, "");
} catch(IllegalStateException e) {
error(e);
- return "";
+ return new SimpleCommandResult(false, "");
}
}
Modified: src/main/java/nl/improved/sqlclient/QueryExecutor.java
===================================================================
--- src/main/java/nl/improved/sqlclient/QueryExecutor.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/QueryExecutor.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
+import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.improved.sqlclient.util.ResultBuilder;
@@ -90,28 +91,6 @@
}
/**
- * Returns the width at wich a column should be displayed.
- * Usually the ResultSetMetaData will be responsible for this width, but a few exceptions
- * are made (this would typicall be the case for dates).
- * A minimum of 4 is used, so that NULL values won't break the layout.
- * @param metadata the metadata describing the resultset
- * @param column the column to check
- * @return the width in characters that should be used to display the column.
- */
- private int getColumnWidth(ResultSetMetaData metadata, int column) throws SQLException {
- switch (metadata.getColumnType(column)) {
- case Types.DATE:
- return dateFormat.length();
- case Types.TIMESTAMP:
- return timestampFormat.length();
- case Types.TIME:
- return timeFormat.length();
- }
- // Let's assume for now that most columns CAN actually contain NULL values, and therefore we want every column to have a minimum width of 4
- return Math.max(4, metadata.getColumnDisplaySize(column));
- }
-
- /**
* Returns the value to display.
* This deals with alignment for numeric columns, formatting for dates and special
* treatment for NULL values.
@@ -180,7 +159,7 @@
* @return the formatted result.
* @throws SQLException if the database could not execute the SQL query for some reason.
*/
- protected CharSequence executeQuery(CharSequence command) throws SQLException {
+ protected Iterator<CharSequence> executeQuery(CharSequence command) throws SQLException {
cancelled = false;
ResultSet results = DBConnector.getInstance().getStatement().executeQuery(command.toString());
@@ -197,28 +176,74 @@
labels.add(metadata.getColumnLabel(col));
}
- ResultBuilder displayValue = new ResultBuilder();
- displayValue.setHeader(labels);
- int rowCount = 0;
- while (results.next() && !cancelled) {
- for (int col = 1; col <= metadata.getColumnCount(); col++ ) {
- displayValue.set(col-1, rowCount, getDisplayValue(results, col), isNumeric(metadata, col) ? ResultBuilder.Alignment.RIGHT : ResultBuilder.Alignment.LEFT);
- //try {Thread.sleep(10);} catch(Exception e2) {}
+ return new QueryExecutorIterator(results, labels, metadata);
+ }
+
+ private class QueryExecutorIterator implements Iterator<CharSequence> {
+
+ private ResultSet results;
+ private List<String> labels;
+ private int rowCount = 0;
+ private int columnCount;
+ private long start = System.currentTimeMillis();
+ private ResultSetMetaData metadata;
+ private boolean next = true;
+
+ public QueryExecutorIterator(ResultSet results, List<String> labels, ResultSetMetaData metadata) throws SQLException {
+ this.results = results;
+ this.labels = labels;
+ this.metadata = metadata;
+ columnCount = metadata.getColumnCount();
+ }
+
+
+
+ public boolean hasNext() {
+ return next;
+ }
+
+ public CharSequence next() {
+ try {
+ next = false;
+ ResultBuilder displayValue = new ResultBuilder();
+ displayValue.setHeader(labels);
+ int max = 1000;
+ while (results.next() && !cancelled) {
+ for (int col = 1; col <= columnCount; col++ ) {
+ displayValue.set(col-1, rowCount, getDisplayValue(results, col), isNumeric(metadata, col) ? ResultBuilder.Alignment.RIGHT : ResultBuilder.Alignment.LEFT);
+ //try {Thread.sleep(10);} catch(Exception e2) {}
+ }
+ rowCount++;
+ if (rowCount % max == 0) {
+ next = true;
+ break;
+ }
+ }
+ if (!next) {
+ StringBuilder footer = new StringBuilder();
+ footer.append(rowCount);
+ footer.append(" row");
+ if (rowCount != 1) {
+ footer.append("s");
+ }
+ footer.append(" selected.\n");
+ if (cancelled) {
+ footer.append("Aborted....\n");
+ }
+ footer.append("Query took: "+ (System.currentTimeMillis() - start) +" millis\n\n");
+ displayValue.setFooter(footer);
+ } else {
+ displayValue.setFooter("More...\n\n");
+ }
+ return displayValue.toString();
+ } catch(SQLException e) {
+ return null;
}
- rowCount++;
}
- StringBuilder footer = new StringBuilder();
- footer.append(rowCount);
- footer.append(" row");
- if (rowCount != 1) {
- footer.append("s");
+
+ public void remove() {
+ throw new UnsupportedOperationException("Not supported yet.");
}
- footer.append(" selected.\n");
- if (cancelled) {
- footer.append("Aborted....\n");
- }
- footer.append("Query took: "+ (System.currentTimeMillis() - start) +" millis\n\n");
- displayValue.setFooter(footer);
- return displayValue.toString();
+
}
}
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -90,12 +90,12 @@
}
};
Command cmd = new nl.improved.sqlclient.AbstractSQLShellWindow.ExecuteBatchCommand(sqlshellWindow);
- CharSequence output = cmd.execute(new SQLCommand("@"+ argsMap.get("-i")));
+ cmd.execute(new SQLCommand("@"+ argsMap.get("-i")));
if (!argsMap.containsKey("-o")) {
for (CharSequence s : sqlshellWindow.getScreen().getScreenBuffer()) {
System.out.println(s);
}
- System.out.println(output);
+ //System.out.println(output);
} else {
File f = new File(argsMap.get("-o"));
FileOutputStream fout = new FileOutputStream(f);
@@ -103,8 +103,8 @@
fout.write(s.toString().getBytes());
fout.write('\n');
}
- fout.write(output.toString().getBytes());
- fout.write('\n');
+ //fout.write(output.toString().getBytes());
+ //fout.write('\n');
}
} else {
//sqlshellWindow = new SQLShellWindow();
Modified: src/main/java/nl/improved/sqlclient/commands/Command.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/Command.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/commands/Command.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -27,7 +27,7 @@
* @param command the command to execute
* @return a readable result so the user knows what happened
*/
- CharSequence execute(SQLCommand cmd);
+ CommandResult execute(SQLCommand cmd);
/**
* Return the command key (like quit, help, connect) that should show up in the help list.
* @return the command key that should show up in the help list.
Added: src/main/java/nl/improved/sqlclient/commands/CommandResult.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/CommandResult.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/commands/CommandResult.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -0,0 +1,18 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package nl.improved.sqlclient.commands;
+
+import java.util.Iterator;
+
+/**
+ *
+ * @author roy
+ */
+public interface CommandResult {
+
+ boolean executedSuccessfully();
+ Iterator<CharSequence> getResult();
+}
Modified: src/main/java/nl/improved/sqlclient/commands/DescCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -35,7 +35,7 @@
* Execute the describe command.
*/
@Override
- public CharSequence execute(SQLCommand command) {
+ public CommandResult execute(SQLCommand command) {
java.sql.Connection conn = DBConnector.getInstance().getConnection();
String cmd = command.getCommandString();
if (cmd.endsWith(";")) {
@@ -69,7 +69,7 @@
.getTables(conn.getCatalog(), DBConnector.getInstance().getSchema()
, tableName, new String[]{"TABLE"});
if (!rs.next()) {
- return "Failed to find table '"+tableName+"'";
+ return new SimpleCommandResult(false, "Failed to find table '"+tableName+"'");
}
} else {
rs = conn.getMetaData().getPrimaryKeys(conn.getCatalog(), DBConnector.getInstance().getSchema(), tableName);
@@ -95,7 +95,7 @@
} catch (SQLException ex) {
throw new IllegalStateException("Failed to find columnnames for table: "+ tableName, ex);
}
- return result.toString();
+ return new SimpleCommandResult(true, result.toString());
}
/**
Modified: src/main/java/nl/improved/sqlclient/commands/InfoCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/InfoCommand.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/commands/InfoCommand.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -34,13 +34,13 @@
* @return the info of the connection
*/
@Override
- public CharSequence execute(SQLCommand cmd) {
+ public CommandResult execute(SQLCommand cmd) {
java.sql.Connection conn;
try {
conn = DBConnector.getInstance().getConnection();
} catch(IllegalStateException e) {
- return "This command shows information about the current connection.\nCurrently SQLShell is not connected to a database server.\n"+
- "Please use the connect command to create a connection";
+ return new SimpleCommandResult(false, "This command shows information about the current connection.\nCurrently SQLShell is not connected to a database server.\n"+
+ "Please use the connect command to create a connection");
}
StringBuffer returnValue = new StringBuffer();
try {
@@ -64,7 +64,7 @@
throw new IllegalStateException("Failed to find table info: "+ ex, ex);
}
returnValue.append("\n");
- return returnValue;
+ return new SimpleCommandResult(true, returnValue);
}
/**
Modified: src/main/java/nl/improved/sqlclient/commands/ShowCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -35,7 +35,7 @@
* Execute the describe command.
*/
@Override
- public CharSequence execute(SQLCommand command) {
+ public CommandResult execute(SQLCommand command) {
java.sql.Connection conn = DBConnector.getInstance().getConnection();
String cmd = command.getCommandString();
if (cmd.endsWith(";")) {
@@ -82,7 +82,7 @@
returnValue.append('\n');
}
returnValue.append("DONE\n");
- return returnValue.toString();
+ return new SimpleCommandResult(true, returnValue.toString());
} catch(SQLException e) {
throw new IllegalStateException("Failed to find tablenames" , e);
}
@@ -104,7 +104,7 @@
} else {
returnValue.append("Don't know what to show for: '"+ cmd.substring(cmd.lastIndexOf(' ')).trim()+"'\n");
}
- return returnValue;
+ return new SimpleCommandResult(true, returnValue);
}
/**
Added: src/main/java/nl/improved/sqlclient/commands/SimpleCommandResult.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/SimpleCommandResult.java 2009-01-15 20:09:47 UTC (rev 347)
+++ src/main/java/nl/improved/sqlclient/commands/SimpleCommandResult.java 2009-01-16 19:46:42 UTC (rev 348)
@@ -0,0 +1,33 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package nl.improved.sqlclient.commands;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ *
+ * @author roy
+ */
+public class SimpleCommandResult implements CommandResult {
+ private boolean success;
+ private CharSequence result;
+
+ public SimpleCommandResult(boolean success, CharSequence result) {
+ this.success = success;
+ this.result = result;
+ }
+
+ public boolean executedSuccessfully() {
+ return success;
+ }
+
+ public Iterator<CharSequence> getResult() {
+ List<CharSequence> list = Arrays.asList(new CharSequence[]{result});
+ return list.iterator();
+ }
+}
|