|
From: SVN by r. <sv...@ca...> - 2008-02-26 20:43:28
|
Author: roy
Date: 2008-02-26 21:43:14 +0100 (Tue, 26 Feb 2008)
New Revision: 239
Modified:
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:
refactor step to configure background process support
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-19 20:02:17 UTC (rev 238)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-26 20:43:14 UTC (rev 239)
@@ -727,44 +727,8 @@
}
private boolean executeCommand(final SQLCommand sqlCommand, boolean direct) {
- if (commandThread != null && commandThread.isAlive()) {
- try { commandThread.join();}catch(Exception e) {}
- }
- final String command = sqlCommand.getCommandString();
- final Command cCommand = commands.findCommand(command);
- if (cCommand != null) {
- output(sqlCommand.getUntrimmedCommandString());
- commandThread = new CommandThread(cCommand) {
- public void execute() {
- try {
- output(cCommand.execute(sqlCommand));
- } catch(Exception e) {
- error(e);
- }
- }
- };
- if (direct || cCommand instanceof QuitCommand || cCommand instanceof ConnectCommand) {
- commandThread.run();
- } else {
- commandThread.start();
- }
- return true;
- }
- if (sqlCommand.endsWith(";")) {
- // execute sql command
- output(sqlCommand.getUntrimmedCommandString());
- commandThread = new CommandThread(new QueryCommand()) {
- public void execute() {
- output(getCommand().execute(sqlCommand));
- }
- };
- if (direct) {
- commandThread.run();
- } else {
- commandThread.start();
- }
- return true;
- } else if (command.equalsIgnoreCase("printStackTrace")) {
+ final String commandString = sqlCommand.getCommandString();
+ if (commandString.equalsIgnoreCase("printStackTrace")) {
if (lastExceptionDetails == null) {
output("No known last exception to print");
} else {
@@ -772,8 +736,41 @@
}
return true;
}
- return false;
+ final Command command = createCommand(commandString);
+ if (command == null) {
+ return false;
+ }
+ // make sure only one command is run at once
+ if (commandThread != null && commandThread.isAlive()) {
+ try {
+ commandThread.join();
+ } catch (InterruptedException ex) {
+ Logger.getLogger(SQLShell.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+ if (direct || !command.backgroundProcessSupported()) {
+ output(command.execute(sqlCommand));
+ } else {
+ commandThread = new CommandThread(command) {
+ @Override
+ void execute() {
+ output(getCommand().execute(sqlCommand));
+ }
+ };
+ commandThread.start();
+ }
+ return true;
}
+ private Command createCommand(String commandString) {
+ Command command = commands.findCommand(commandString);
+ if (command != null) {
+ return command;
+ }
+ if (commandString.endsWith(";")) {
+ return new QueryCommand(); // TODO is this ever reached???
+ }
+ return null;
+ }
/**
@@ -983,6 +980,10 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
/**
* Command that enables the user to close a connection.
@@ -1020,6 +1021,10 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
/**
@@ -1060,6 +1065,10 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
/**
@@ -1101,6 +1110,10 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
/**
@@ -1139,6 +1152,10 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
/**
* Provide help to the user.
@@ -1252,6 +1269,10 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
/**
@@ -1326,26 +1347,40 @@
public boolean abort() {
return false;// not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
private class ExecuteBatchCommand implements Command {
+ private boolean cancelled = false;
+ @Override
public CharSequence execute(SQLCommand sqlCommand) {
+ cancelled = false;
String command = sqlCommand.getCommandString();
// read file from file system and execute
FileInputStream fin = null;
try {
fin = new FileInputStream(toFileName(command.substring(1)));
+ output("Reading file: "+ toFileName(command.substring(1))+"\n");
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
StringBuilder cmd = new StringBuilder();
String line;
- while ( (line = reader.readLine()) != null) {
+ while ( ((line = reader.readLine()) != null)) {
+ if (cancelled) {
+ return "Aborted";
+ }
if (line.startsWith("--")) {
continue;
}
cmd.append(line);
if (line.endsWith(";")) {
// Exec cmd
- executeCommand(new InputCommand(cmd));
+ String commandString = cmd.toString();
+ Command cCommand = createCommand(commandString);
+ output(commandString);
+ output(cCommand.execute(new InputCommand(commandString))); // TODO start in background...
cmd=new StringBuilder();
}
}
@@ -1401,8 +1436,13 @@
}
@Override
public boolean abort() {
- return false;// not implemented
+ cancelled = true;
+ return true;
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return true;
+ }
}
/**
@@ -1510,6 +1550,10 @@
output(DBConnector.getInstance().getQueryExecutor().cancel());
return true;
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return true;
+ }
}
Modified: src/main/java/nl/improved/sqlclient/commands/Command.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/Command.java 2008-02-19 20:02:17 UTC (rev 238)
+++ src/main/java/nl/improved/sqlclient/commands/Command.java 2008-02-26 20:43:14 UTC (rev 239)
@@ -54,4 +54,10 @@
* @return true if abort succeeded
*/
boolean abort();
+
+ /**
+ * Return true if the command (instance) supports to be started in the background.
+ * @return true if the command (instance) supports to be started in the background.
+ */
+ boolean backgroundProcessSupported();
}
Modified: src/main/java/nl/improved/sqlclient/commands/DescCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2008-02-19 20:02:17 UTC (rev 238)
+++ src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2008-02-26 20:43:14 UTC (rev 239)
@@ -128,4 +128,8 @@
public boolean abort() {
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
Modified: src/main/java/nl/improved/sqlclient/commands/InfoCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/InfoCommand.java 2008-02-19 20:02:17 UTC (rev 238)
+++ src/main/java/nl/improved/sqlclient/commands/InfoCommand.java 2008-02-26 20:43:14 UTC (rev 239)
@@ -94,4 +94,8 @@
public boolean abort() {
return false;
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
Modified: src/main/java/nl/improved/sqlclient/commands/ShowCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2008-02-19 20:02:17 UTC (rev 238)
+++ src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2008-02-26 20:43:14 UTC (rev 239)
@@ -141,4 +141,8 @@
public boolean abort() {
return false; // not implemented
}
+ @Override
+ public boolean backgroundProcessSupported() {
+ return false;
+ }
}
|