From: SVN by r. <sv...@ca...> - 2008-02-13 07:23:57
|
Author: roy Date: 2008-02-13 08:23:46 +0100 (Wed, 13 Feb 2008) New Revision: 234 Modified: 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: added abort to command interface code cleanup some initial work started for running a commands in the background (so abort can actually be called :) ) Modified: src/main/java/nl/improved/sqlclient/QueryExecutor.java =================================================================== --- src/main/java/nl/improved/sqlclient/QueryExecutor.java 2008-02-10 15:17:36 UTC (rev 233) +++ src/main/java/nl/improved/sqlclient/QueryExecutor.java 2008-02-13 07:23:46 UTC (rev 234) @@ -26,6 +26,8 @@ import java.util.ArrayList; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.logging.Level; +import java.util.logging.Logger; import nl.improved.sqlclient.util.ResultBuilder; /** @@ -161,6 +163,15 @@ } } + public CharSequence cancel() { + try { + DBConnector.getInstance().getStatement().cancel(); + return "Cancel accepted"; + } catch (SQLException ex) { + return "Cancel Failed: "+ ex.toString(); + } + } + /** * Executes a SQL query. * @param command the SQL query to execute. @@ -189,6 +200,7 @@ while (results.next()) { 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) {} } rowCount++; } Modified: src/main/java/nl/improved/sqlclient/SQLShell.java =================================================================== --- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-10 15:17:36 UTC (rev 233) +++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-13 07:23:46 UTC (rev 234) @@ -38,6 +38,8 @@ */ public class SQLShell extends Window { + private CommandThread commandThread; + /** * The (default) maximum matches to show in a selection dialog. */ @@ -94,11 +96,6 @@ private CommandManager commands = new CommandManager(); /** - * Executor for SQL Statements - */ - private StatementExecutor queryExecutor; - - /** * Constructor. */ public SQLShell() { @@ -193,6 +190,7 @@ * @param inp the character that is being pressed by the user. */ protected void handleInput(InputChar inp) { + debug("Input: " + inp.getCode()); try { if (inp.getCode() != InputChar.KEY_PPAGE && inp.getCode() != InputChar.KEY_NPAGE) { pageUpCount = 0; // some character entered, so reset pageup count @@ -281,6 +279,11 @@ if (commandLines.getCommandString().length() == 0) { //Quit on empty commandline, ignore otherwise executeCommand(new InputCommand("quit")); } + } else if (inp.toString() != null && inp.toString().equals("")) { // ctrl+a + output("Abort requested"); + if (commandThread.isAlive() && commandThread.getCommand().abort()) { + output("Abort done.."); + } } else { if (inp.getCharacter() == '\n') { // execute the command @@ -604,26 +607,47 @@ * @return true if it succeeded. */ protected boolean executeCommand(SQLCommand sqlCommand) { - String command = sqlCommand.getCommandString(); - Command cCommand = commands.findCommand(command); + return executeCommand(sqlCommand, false); + } + + 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()); - try { - output(cCommand.execute(sqlCommand)); - } catch(Exception e) { - error(e); + commandThread = new CommandThread(cCommand) { + public void run() { + try { + output(cCommand.execute(sqlCommand)); + } catch(Exception e) { + error(e); + } + } + }; + if (direct || cCommand instanceof QuitCommand || cCommand instanceof ConnectCommand) { + commandThread.run(); + } else { + //commandThread.start(); // TODO + commandThread.run(); } return true; } if (sqlCommand.endsWith(";")) { // execute sql command output(sqlCommand.getUntrimmedCommandString()); - try { - output(getResult(command)); - } catch(SQLException e) { - error(e); - } catch(IllegalStateException e) { - error(e); + commandThread = new CommandThread(new QueryCommand()) { + public void run() { + output(getCommand().execute(sqlCommand)); + } + }; + if (direct) { + commandThread.run(); + } else { + //commandThread.start(); // TODO + commandThread.run(); } return true; } else if (command.equalsIgnoreCase("printStackTrace")) { @@ -637,20 +661,6 @@ return false; } - /** - * Execute a query and return its result. - * @param command the query to execute - * @return the result of the query - */ - protected CharSequence getResult(CharSequence command) throws SQLException { - if (command.length() > "select".length() && "select".equalsIgnoreCase(command.subSequence(0, "create".length()).toString())) { - return DBConnector.getInstance().getQueryExecutor().executeQuery(command); - } - if (queryExecutor == null) { - queryExecutor = new StatementExecutor(); - } - return queryExecutor.execute(command); - } /** * Paint the screen. @@ -799,6 +809,7 @@ * @param command the command string for setting up a connection * @return a readable result of the execution of this command. */ + @Override public CharSequence execute(SQLCommand cmd) { String command = cmd.getCommandString(); try { @@ -812,6 +823,7 @@ throw new IllegalStateException("Failed to connect: " + e.getMessage(), e); } } + @Override public CharSequence getCommandString() { return "connect"; } @@ -822,10 +834,12 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override public CharSequence getHelp() { StringBuffer buf = new StringBuffer(); Iterator<String> idents = DBConnector.getInstance().getPredefinedConnectionIdentifiers().iterator(); @@ -850,11 +864,17 @@ "Currently configured connection identifiers:\n"+ buf.toString(); } + + @Override + public boolean abort() { + return false;// not implemented + } } /** * Command that enables the user to close a connection. */ private static class DisConnectCommand implements Command { + @Override public CharSequence execute(SQLCommand cmd) { try { DBConnector.getInstance().disconnect(); @@ -863,6 +883,7 @@ throw new IllegalStateException("Failed to disconnect: " + e.getMessage(), e); } } + @Override public CharSequence getCommandString() { return "disconnect"; } @@ -873,18 +894,25 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override public CharSequence getHelp() { return "Close the current conection to the database"; } + @Override + public boolean abort() { + return false;// not implemented + } } /** * Some basic window settings like resize. */ private class WindowCommand implements Command { + @Override public CharSequence execute(SQLCommand cmd) { String command = cmd.getCommandString(); String argument = command.trim().substring("window".length()).trim(); @@ -895,6 +923,7 @@ return "Uknown command '"+ argument+"'"; } + @Override public CharSequence getCommandString() { return "window"; } @@ -905,18 +934,25 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override public CharSequence getHelp() { return "window resize: notify the sql client of a screen resize"; } + @Override + public boolean abort() { + return false;// not implemented + } } /** * Provide a list of commands executed. */ private class HistoryCommand implements Command { + @Override public CharSequence execute(SQLCommand command) { StringBuilder returnValue = new StringBuilder(); Iterator<SQLCommand> iCommands = commandHistory.iterator(); @@ -927,6 +963,7 @@ } return returnValue; } + @Override public CharSequence getCommandString() { return "history"; } @@ -937,13 +974,19 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override public CharSequence getHelp() { return "Show history of executed statements\n" + "By using '/<search>' you can search the command history" ; } + @Override + public boolean abort() { + return false;// not implemented + } } /** @@ -955,13 +998,16 @@ public QuitCommand(String cmd) { this.cmd = cmd; } + @Override public CharSequence execute(SQLCommand command) { hide(); // quit return "Application terminated."; } + @Override public CharSequence getHelp() { return "Quit(exit) the application."; } + @Override public CharSequence getCommandString() { return cmd; } @@ -971,14 +1017,20 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override + public boolean abort() { + return false;// not implemented + } } /** * Provide help to the user. */ private class HelpCommand implements Command { + @Override public CharSequence execute(SQLCommand sqlCommand) { // the execution of help consists of: // 1. is general help.. @@ -1042,6 +1094,7 @@ returnValue.insert(0, helpHeader); return returnValue; } + @Override public CharSequence getCommandString() { return "help"; } @@ -1052,14 +1105,25 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override public CharSequence getHelp() { return "this command. Please use 'help' to get a list of available commands you can use."; } + @Override + public boolean abort() { + return false;// not implemented + } } + /** + * Convert '~/' to the username dir. + * @param fileName the filename to convert + * @return the converted filename + */ private static String toFileName(String fileName) { if (fileName.startsWith("~/")) { return System.getProperty("user.home")+fileName.substring(1); @@ -1073,6 +1137,7 @@ private class SpoolCommand implements Command { private String fileName; + @Override public CharSequence execute(SQLCommand cmd) { String command = cmd.getCommandString(); String nextPart = command.substring("spool".length()).trim(); @@ -1101,6 +1166,7 @@ } } + @Override public CharSequence getCommandString() { return "spool"; } @@ -1111,14 +1177,20 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } + @Override public CharSequence getHelp() { return "filename: Spool all output and queries to the specified file\n"+ "off : Stop spooling data to the file.\n" + "Current status:"+(spoolWriter != null ? "on, writing to '"+fileName+"'" : "off"); } + @Override + public boolean abort() { + return false;// not implemented + } } private class ExecuteBatchCommand implements Command { @@ -1150,6 +1222,7 @@ } } + @Override public CharSequence getCommandString() { return "@"; } @@ -1160,6 +1233,7 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { String fileName = command.getCommandString().substring(1).trim(); // cutoff '@' String dirName; @@ -1184,11 +1258,16 @@ return new TabCompletionInfo(TabCompletionInfo.MatchType.UNKNOWN,Arrays.asList(new File(dirName).list()) , fileName); } + @Override public CharSequence getHelp() { return "Specify filename to execute a 'batch' command.\n"+ "For example '@mystatements.sql' executes all statements part of the mystatements.sql file in the current directory."+ "Note that all statements must be terminated with ';' (sql statements as well as connect statements or spool)"; } + @Override + public boolean abort() { + return false;// not implemented + } } /** @@ -1223,6 +1302,73 @@ } } + private static class CommandThread extends Thread { + private Command cmd; + public CommandThread(Command cmd) { + this.cmd = cmd; + } + + Command getCommand() { + return cmd; + } + } + + private class QueryCommand implements Command { + + /** + * Executor for SQL Statements + */ + private StatementExecutor statementExecutor; + + @Override + public CharSequence execute(SQLCommand cmd) { + try { + String command = cmd.getCommandString(); + if (command.length() > "select".length() && "select".equalsIgnoreCase(command.subSequence(0, "create".length()).toString())) { + return DBConnector.getInstance().getQueryExecutor().executeQuery(command); + } + if (statementExecutor == null) { + statementExecutor = new StatementExecutor(); + } + return statementExecutor.execute(command); + } catch(SQLException e) { + error(e); + return ""; + } catch(IllegalStateException e) { + error(e); + return ""; + } + } + + @Override + public CharSequence getCommandString() { + return ""; + } + + @Override + public TabCompletionInfo getTabCompletionInfo(SQLCommand commandInfo, Point commandPoint) { + // TODO call SQLUTil.. + return null; + } + + @Override + public CharSequence getHelp() { + return ""; + } + + @Override + public boolean abort() { + try { + DBConnector.getInstance().getStatement().cancel(); + return true; + } catch (SQLException ex) { + Logger.getLogger(SQLShell.class.getName()).log(Level.SEVERE, null, ex); + return false; + } + } + + } + public static void main(String[] args) { SQLShell shell = new SQLShell(); shell.show(); Modified: src/main/java/nl/improved/sqlclient/commands/Command.java =================================================================== --- src/main/java/nl/improved/sqlclient/commands/Command.java 2008-02-10 15:17:36 UTC (rev 233) +++ src/main/java/nl/improved/sqlclient/commands/Command.java 2008-02-13 07:23:46 UTC (rev 234) @@ -48,4 +48,10 @@ * @return a explenation string of how to use this command. */ CharSequence getHelp(); + + /** + * Attempt to abort the command and return true if succeeded. + * @return true if abort succeeded + */ + boolean abort(); } Modified: src/main/java/nl/improved/sqlclient/commands/DescCommand.java =================================================================== --- src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2008-02-10 15:17:36 UTC (rev 233) +++ src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2008-02-13 07:23:46 UTC (rev 234) @@ -123,4 +123,9 @@ return "Describes the table structure.\n" + "For example 'desc mytable' shows the properties of the mytable table"; } + + @Override + public boolean abort() { + throw new UnsupportedOperationException("Not supported yet."); + } } Modified: src/main/java/nl/improved/sqlclient/commands/InfoCommand.java =================================================================== --- src/main/java/nl/improved/sqlclient/commands/InfoCommand.java 2008-02-10 15:17:36 UTC (rev 233) +++ src/main/java/nl/improved/sqlclient/commands/InfoCommand.java 2008-02-13 07:23:46 UTC (rev 234) @@ -33,6 +33,7 @@ * @param command the command that is executed * @return the info of the connection */ + @Override public CharSequence execute(SQLCommand cmd) { java.sql.Connection conn = DBConnector.getInstance().getConnection(); StringBuilder returnValue = new StringBuilder(); @@ -64,6 +65,7 @@ * Return the command string. * @return the command string. */ + @Override public CharSequence getCommandString() { return "info"; } @@ -74,6 +76,7 @@ * @param commandPoint the cursor position * @return some tab completion info for the specified command. */ + @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { return null; } @@ -82,7 +85,13 @@ * Return the command help. * @return the command help. */ + @Override public CharSequence getHelp() { return "Shows information about the current connection."; } + + @Override + public boolean abort() { + return false; + } } Modified: src/main/java/nl/improved/sqlclient/commands/ShowCommand.java =================================================================== --- src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2008-02-10 15:17:36 UTC (rev 233) +++ src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2008-02-13 07:23:46 UTC (rev 234) @@ -33,6 +33,7 @@ /** * Execute the describe command. */ + @Override public CharSequence execute(SQLCommand command) { java.sql.Connection conn = DBConnector.getInstance().getConnection(); String cmd = command.getCommandString(); @@ -93,6 +94,7 @@ * Return the command string desc. * @return the command string desc. */ + @Override public CharSequence getCommandString() { return "show"; } @@ -129,8 +131,14 @@ * Return the command help. * @return the command help. */ + @Override public CharSequence getHelp() { return "tables: show all tables available in the currently selected schema\n"+ "tables having columnname: same as 'show tables' but limited to tables with that specific columnname"; } + + @Override + public boolean abort() { + return false; // not implemented + } } |