You can subscribe to this list here.
| 2007 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
(8) |
Oct
(34) |
Nov
(7) |
Dec
(2) |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2008 |
Jan
(29) |
Feb
(10) |
Mar
(14) |
Apr
(4) |
May
(2) |
Jun
|
Jul
(14) |
Aug
(25) |
Sep
(6) |
Oct
(18) |
Nov
(4) |
Dec
(14) |
| 2009 |
Jan
(28) |
Feb
(15) |
Mar
(15) |
Apr
(8) |
May
|
Jun
|
Jul
(1) |
Aug
(4) |
Sep
(12) |
Oct
(1) |
Nov
|
Dec
(22) |
| 2010 |
Jan
(14) |
Feb
|
Mar
(2) |
Apr
|
May
(7) |
Jun
|
Jul
(3) |
Aug
(1) |
Sep
|
Oct
|
Nov
|
Dec
|
|
From: SVN by r. <sv...@ca...> - 2008-03-05 07:29:37
|
Author: roy Date: 2008-03-05 08:29:22 +0100 (Wed, 05 Mar 2008) New Revision: 251 Modified: pom.xml Log: version 0.5 Modified: pom.xml =================================================================== --- pom.xml 2008-03-04 10:52:42 UTC (rev 250) +++ pom.xml 2008-03-05 07:29:22 UTC (rev 251) @@ -19,7 +19,7 @@ <groupId>nl.improved</groupId> <artifactId>sqlshell</artifactId> <packaging>jar</packaging> - <version>0.5-SNAPSHOT</version> + <version>0.5</version> <name>SQLShell ~ the improved sqlclient</name> <url>http://sqlshell.sourceforge.org</url> <build> |
|
From: SVN by r. <sv...@ca...> - 2008-03-04 10:52:56
|
Author: roy
Date: 2008-03-04 11:52:42 +0100 (Tue, 04 Mar 2008)
New Revision: 250
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
try to fix more paint issues...
really hard to track though
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-03 10:42:27 UTC (rev 249)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-04 10:52:42 UTC (rev 250)
@@ -561,14 +561,13 @@
error(e);
}
}
- paint(); // refresh screen
}
/**
* Output error exception to the screen.
* @param e the error to print to the screen
*/
- protected void error(Throwable e) {
+ protected synchronized void error(Throwable e) {
output(e.getMessage() == null ? e.toString() : e.getMessage());
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
@@ -1402,6 +1401,7 @@
output(commandString);
output(currentCommand.execute(new InputCommand(commandString)));
cmd=new StringBuilder();
+ new Thread() { public void run() { paint();}}.start();
}
}
} catch(IOException e) {
|
|
From: SVN by r. <sv...@ca...> - 2008-03-03 10:47:21
|
Author: roy
Date: 2008-03-03 11:42:27 +0100 (Mon, 03 Mar 2008)
New Revision: 249
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
small cleanup of code
made exit sqlshell the last keybinding in the help overview
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-03 07:39:20 UTC (rev 248)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-03 10:42:27 UTC (rev 249)
@@ -307,25 +307,25 @@
return "PageDown:\tMove forward in screen history";
}
});
- actionKeys.put("", new KeyAction() { //Ctrl+D
+ actionKeys.put("", new KeyAction() { // ctrl+a
public void execute() {
- if (commandLines.getCommandString().length() == 0) { //Quit on empty commandline, ignore otherwise
- executeCommand(new InputCommand("quit"));
+ output("Abort requested");
+ if (commandThread.isAlive() && commandThread.getCommand().abort()) {
+ output("Abort done..");
}
}
public CharSequence getHelp() {
- return "Control-D:\tExit sqlshell";
+ return "Control-A:\tAbort current command (if it is supported by that command)";
}
});
- actionKeys.put("", new KeyAction() { // ctrl+a
+ actionKeys.put("", new KeyAction() { //Ctrl+D
public void execute() {
- output("Abort requested");
- if (commandThread.isAlive() && commandThread.getCommand().abort()) {
- output("Abort done..");
+ if (commandLines.getCommandString().length() == 0) { //Quit on empty commandline, ignore otherwise
+ executeCommand(new InputCommand("quit"));
}
}
public CharSequence getHelp() {
- return "Control-A:\tAbort current command (if it is supported by that command)";
+ return "Control-D:\tExit sqlshell";
}
});
@@ -762,7 +762,7 @@
Logger.getLogger(SQLShell.class.getName()).log(Level.SEVERE, null, ex);
}
}
- output(commandString+"\n");
+ output(commandString);
if (direct || !command.backgroundProcessSupported()) {
output(command.execute(sqlCommand));
} else {
@@ -1383,7 +1383,7 @@
FileInputStream fin = null;
try {
fin = new FileInputStream(toFileName(command.substring(1)));
- output("Reading file: "+ toFileName(command.substring(1))+"\n");
+ output("Reading file: "+ toFileName(command.substring(1)));
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
StringBuilder cmd = new StringBuilder();
String line;
@@ -1399,7 +1399,7 @@
// Exec cmd
String commandString = cmd.toString();
currentCommand = createCommand(commandString);
- output(commandString+"\n");
+ output(commandString);
output(currentCommand.execute(new InputCommand(commandString)));
cmd=new StringBuilder();
}
|
|
From: SVN by r. <sv...@ca...> - 2008-03-03 07:39:31
|
Author: roy
Date: 2008-03-03 08:39:20 +0100 (Mon, 03 Mar 2008)
New Revision: 248
Modified:
src/main/java/nl/improved/sqlclient/SQLUtil.java
src/test/java/nl/improved/sqlclient/SQLUtilTest.java
Log:
more tab completion issues fixed
Modified: src/main/java/nl/improved/sqlclient/SQLUtil.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLUtil.java 2008-03-02 14:09:51 UTC (rev 247)
+++ src/main/java/nl/improved/sqlclient/SQLUtil.java 2008-03-03 07:39:20 UTC (rev 248)
@@ -45,13 +45,13 @@
/**
* SQL Comparators.
*/
- private static final String COMPARATOR = "(IN|=|<>|<|>)";
+ private static final String COMPARATOR = "(=|<>|<|>)";
/**
* A sql variable description.
*/
private static final String VALUE = "('.*'|[0-9]+|"+VAR+")";
- public static List<String> KEYWORDS = Arrays.asList(new String[]{"SELECT", "UPDATE", "FROM", "WHERE", "VALUES", "SET", "INSERT", "INTO", "DELETE", "IS", "NULL", "IN", "NOT", "GROUP BY", "ORDER BY", "DESC"});
+ public static List<String> KEYWORDS = Arrays.asList(new String[]{"SELECT", "UPDATE", "FROM", "WHERE", "VALUES", "SET", "INSERT", "INTO", "DELETE", "GROUP BY", "ORDER BY", "DESC"});
/**
* Private constructor.
@@ -240,15 +240,11 @@
String upperCommandString = tmpCommand.toUpperCase();
String VAR = "(|"+TABLE+"\\.)(|"+COLUMN+")";
String VALUE = "('.*'|[0-9]+|"+VAR+")";
- String subPart = "(|"+VAR+"(|[\\s]+"+COMPARATOR+"[\\s]+(|"+VAR+")))";
- //String regExp = "(|"+VALUE+"(|[\\s]+"+COMPARATOR+"[\\s]+(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+)))))";
- String regExp = "(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"))))*))))";
+ // VALUE COMPERATOR VALUE
+ // VALUE IN (.*)
+
+ String regExp = "(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"))))*))|[\\s]*IN[\\s]*(.*)[\\s]*))";
if (upperCommandString.matches(regExp)) {
-/* String end = tmpCommand.substring(tmpCommand.lastIndexOf(' ')+1);
- if (end.indexOf(".") > 0) {
- end = end.substring(end.indexOf(".")+1);
- }
-*/
int lastIndex = tmpCommand.lastIndexOf(' ');
lastIndex = Math.max(lastIndex, tmpCommand.lastIndexOf('.'));
lastIndex = Math.max(lastIndex, tmpCommand.lastIndexOf('\t'));
@@ -259,7 +255,7 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, parseTableNames(commandInfo, commandPoint), end);
}
- //System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp);
+ //else System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp);
String end = "";
if (upperCommandString.matches(".*[\\s]+")) {
end = "";
@@ -273,35 +269,6 @@
}
return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
, Arrays.asList(new String[]{"AND", "OR", "IN", "GROUP BY", "ORDER BY"}), end);
- /*String tmpCommand = startOfCommand.substring(startOfCommand.indexOf(lastKeyword)+lastKeyword.length()+1).toUpperCase();
- if (startOfCommand.trim().endsWith(lastKeyword)
- || tmpCommand.matches(".*(AND|OR)(|[\\s]+(|"+VAR+"))")) {
- int beginIndex = startOfCommand.lastIndexOf(lastKeyword) + lastKeyword.length();
- if (startOfCommand.endsWith(" ")) {
- return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
- , parseTableNames(commandInfo, commandPoint));
- } else {
- int lastSpace = startOfCommand.lastIndexOf(' ');
- return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
- , parseTableNames(commandInfo, commandPoint), startOfCommand.substring(lastSpace+1));
- }
- }
- if (tmpCommand.matches(VAR+"[\\s]*(|(|("+COMPARATOR+")[\\s]*"+VALUE+"*))")) {
- String tmp = startOfCommand.substring(Math.max(startOfCommand.lastIndexOf(',')+1
- , startOfCommand.lastIndexOf(' ')+1));
- List<String> tableNames;
- if (tmp.indexOf('.') > 0) {
- tableNames = Arrays.asList(new String[]{tmp.substring(0, tmp.indexOf('.'))});
- tmp = tmp.substring(tmp.indexOf('.')+1);
- } else {
- tableNames = parseTableNames(commandInfo, commandPoint);
- }
- return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
- , tableNames, tmp);
- }
- return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
- , Arrays.asList(new String[]{"AND", "OR", "IN", "GROUP BY", "ORDER BY"}));
- */
} else if (lastKeyword.equalsIgnoreCase("GROUP BY")
|| lastKeyword.equalsIgnoreCase("ORDER BY")) {
String upperStart = startOfCommand.toUpperCase();
Modified: src/test/java/nl/improved/sqlclient/SQLUtilTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/SQLUtilTest.java 2008-03-02 14:09:51 UTC (rev 247)
+++ src/test/java/nl/improved/sqlclient/SQLUtilTest.java 2008-03-03 07:39:20 UTC (rev 248)
@@ -363,6 +363,17 @@
matches = info.getPossibleMatches();
assertTrue(matches.contains("AND"));
assertEquals("a", info.getStart());
+
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE c in('c','d','e') and A.x"});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ assertEquals("WHERE", SQLUtil.getLastKeyWord(sqlCommand.get(0)));
+ info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertTrue(matches.contains("A"));
+ assertEquals("x", info.getStart());
}
public void testGroupBy() {
@@ -386,6 +397,25 @@
assertEquals(2, matches.size());
assertTrue(matches.contains("c1"));
assertTrue(matches.contains("c2"));
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B WHERE a.b=b.b GROUP BY c"});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(2, matches.size());
+ assertTrue(matches.contains("c1"));
+ assertTrue(matches.contains("c2"));
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B WHERE a.b=b.b GROUP BY c1, "});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(2, matches.size());
+ assertTrue(matches.contains("c2"));
}
public void testOrderBy() {
|
|
From: SVN by r. <sv...@ca...> - 2008-03-02 14:10:00
|
Author: roy Date: 2008-03-02 15:09:51 +0100 (Sun, 02 Mar 2008) New Revision: 247 Modified: ChangeLog Log: mention changes Modified: ChangeLog =================================================================== --- ChangeLog 2008-03-02 14:08:49 UTC (rev 246) +++ ChangeLog 2008-03-02 14:09:51 UTC (rev 247) @@ -1,4 +1,16 @@ --.5 +0.5 + * Allow for commands to start in the background, this will allow to continue + reading keyboard input (currently used to abort commands) + * Added help to keyboard mappings (pageup/down/end/home, etc) + * BUGFIX: Added fixes for mysql, mysql uses lowercase tablenames where oracle uses + uppercase. Currenlty SQLShell tries to figure out which one to use + automatically + * timestamp display fixes + * Tabcompletion improvements (fixed deadlock caused by an error in regular + expressions api from java) + * Improved navigation in edit mode (home/end). It should be more intuitive + and usefull now + * Removed border of one character to maximize the usage of the screen 0.4 (2008-01-24) * Improved speed for displaying large( >200<20000 rows) query results |
|
From: SVN by r. <sv...@ca...> - 2008-03-02 14:09:00
|
Author: roy
Date: 2008-03-02 15:08:49 +0100 (Sun, 02 Mar 2008)
New Revision: 246
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
output fixes
removed border of 1 pix around interface
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 13:50:55 UTC (rev 245)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 14:08:49 UTC (rev 246)
@@ -115,7 +115,7 @@
*/
public SQLShell() {
super(Toolkit.getScreenWidth(),Toolkit.getScreenHeight(), true, "SQLShell");
- char[] emptyLineChar = new char[Toolkit.getScreenWidth()-2];
+ char[] emptyLineChar = new char[Toolkit.getScreenWidth()];
Arrays.fill(emptyLineChar, ' ');
emptyLine = new String(emptyLineChar);
@@ -329,7 +329,7 @@
}
});
- MAX_LINE_LENGTH = Toolkit.getScreenWidth()-(2+PROMPT.length()+2+1); // 2 spaces bouds.. prompt + "> "
+ MAX_LINE_LENGTH = Toolkit.getScreenWidth()-(PROMPT.length()+2+1); // prompt + "> "
output("Welcome to the SQLShell client.");
output("Type 'help' to get a list of available commands other then the default sql input.");
@@ -395,7 +395,7 @@
* @param inp the character that is being pressed by the user.
*/
protected void handleInput(InputChar inp) {
- debug("Input: " + inp.getCode());
+ //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
@@ -793,7 +793,6 @@
*/
@Override
protected synchronized void paint() {
- Toolkit.clearScreen(new CharColor(CharColor.WHITE, CharColor.BLACK, CharColor.REVERSE, CharColor.REVERSE));
CharColor color = new CharColor(CharColor.BLACK, CharColor.WHITE, CharColor.BOLD, CharColor.BOLD);
List<CharSequence> tmpList = new ArrayList<CharSequence>();
@@ -810,8 +809,8 @@
}
}
int startLine;
- if (tmpList.size() > Toolkit.getScreenHeight()-4) {
- startLine = tmpList.size() - (Toolkit.getScreenHeight()-4);
+ if (tmpList.size() > Toolkit.getScreenHeight()-1) {
+ startLine = tmpList.size() - (Toolkit.getScreenHeight()-1);
if (pageUpCount > 0) {
startLine -= (pageUpCount * Toolkit.getScreenHeight()/2);
if (startLine < 0) {
@@ -822,11 +821,15 @@
startLine = 0;
}
int lineNr;
- for (lineNr = startLine;lineNr < tmpList.size() && lineNr - startLine < Toolkit.getScreenHeight()-4; lineNr++) {
- CharSequence line = tmpList.get(lineNr);
- Toolkit.printString(line.toString()
- , 1, lineNr+1-startLine, color);
+ for (lineNr = startLine;lineNr < tmpList.size() && lineNr - startLine < Toolkit.getScreenHeight()-1; lineNr++) {
+ CharSequence linePart = tmpList.get(lineNr);
+ String line = linePart + emptyLine.substring(linePart.length());
+ Toolkit.printString(line
+ , 0, lineNr-startLine, color);
}
+ for (int lNr = lineNr; lNr < Toolkit.getScreenHeight(); lNr++) {
+ Toolkit.printString(emptyLine, 0, lNr, color);
+ }
// paint cursor
color = new CharColor(CharColor.BLACK, CharColor.WHITE, CharColor.REVERSE, CharColor.REVERSE);
@@ -837,7 +840,7 @@
cursorChar = tmp.substring(cursorPosition.x, cursorPosition.x+1);
}
}
- Toolkit.printString(cursorChar, PROMPT.length() +"> ".length() + cursorPosition.x+1, lineNr-(commandLines.getLines().size() -cursorPosition.y-1)-startLine, color);
+ Toolkit.printString(cursorChar, PROMPT.length() +"> ".length() + cursorPosition.x, lineNr-(commandLines.getLines().size() -cursorPosition.y)-startLine, color);
}
private void debug(String debug) {
@@ -851,7 +854,7 @@
* @return the text devided into multiple lines to match the screen width
*/
private static List<CharSequence> getLines(CharSequence text) {
- int maxWidth = Toolkit.getScreenWidth()-2;
+ int maxWidth = Toolkit.getScreenWidth();
List<CharSequence> list = new ArrayList<CharSequence>();
StringBuilder buffer = new StringBuilder();
for (int i=0; i<text.length(); i++) {
@@ -885,7 +888,7 @@
maxWidth = Math.max(maxWidth, iValues.next().length());
}
maxWidth+=2;// add extra space
- int nrOfColumns = (Toolkit.getScreenWidth()-2) / maxWidth;
+ int nrOfColumns = (Toolkit.getScreenWidth()) / maxWidth;
StringBuilder returnValue = new StringBuilder();
for (int row = 0; row < values.size(); row+=nrOfColumns) {
for (int col = 0; col < nrOfColumns && row + col < values.size(); col++) {
|
|
From: SVN by r. <sv...@ca...> - 2008-03-02 13:51:05
|
Author: roy
Date: 2008-03-02 14:50:55 +0100 (Sun, 02 Mar 2008)
New Revision: 245
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
added some javadoc
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 13:48:44 UTC (rev 244)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 13:50:55 UTC (rev 245)
@@ -40,7 +40,14 @@
*/
public class SQLShell extends Window {
+ /**
+ * The current command thread executing a SQLShell command.
+ */
private CommandThread commandThread;
+ /**
+ * True when the prompt should be visible.
+ * Normally it is not visible during the execution of a command
+ */
private boolean showPrompt = true;
/**
@@ -98,6 +105,9 @@
*/
private CommandManager commands = new CommandManager();
+ /**
+ * A map of string key representation to a action that should be executed when a specific key is pressed.
+ */
private Map<String, KeyAction> actionKeys = new LinkedHashMap<String, KeyAction>();
/**
|
|
From: SVN by r. <sv...@ca...> - 2008-03-02 13:48:55
|
Author: roy
Date: 2008-03-02 14:48:44 +0100 (Sun, 02 Mar 2008)
New Revision: 244
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
made the order of the help for keymappings a little more readable (grouped)
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 12:09:49 UTC (rev 243)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 13:48:44 UTC (rev 244)
@@ -24,7 +24,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jcurses.widgets.Window;
@@ -98,7 +98,7 @@
*/
private CommandManager commands = new CommandManager();
- private Map<String, KeyAction> actionKeys = new HashMap<String, KeyAction>();
+ private Map<String, KeyAction> actionKeys = new LinkedHashMap<String, KeyAction>();
/**
* Constructor.
@@ -184,6 +184,32 @@
return "Arrow Down:\tBrowse to next command in the history";
}
});
+ actionKeys.put(Integer.toString(InputChar.KEY_END),new KeyAction() {
+ public void execute() {
+ int curLineEnd = commandLines.getLines().get(cursorPosition.y).length();
+ if (cursorPosition.x == curLineEnd) {
+ cursorPosition.y = commandLines.getLines().size()-1;
+ CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
+ cursorPosition.x = lineBuffer.length();
+ } else {
+ cursorPosition.x = curLineEnd;
+ }
+ }
+ public CharSequence getHelp() {
+ return "End:\tMove the cursor to the end of the line, of if already there to the end of the command";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_HOME), new KeyAction() {
+ public void execute() {
+ if (cursorPosition.x == 0) {
+ cursorPosition.y = 0;
+ }
+ cursorPosition.x = 0;
+ }
+ public CharSequence getHelp() {
+ return "Home:\tMove the cursor to the start of the line, of if already there to the start of the command";
+ }
+ });
actionKeys.put(Integer.toString(InputChar.KEY_BACKSPACE), new KeyAction() {
public void execute() {
if (cursorPosition.x == 0) {
@@ -218,54 +244,6 @@
return "Del:\tDelete the charactor at the current cursor position";
}
});
- actionKeys.put(Integer.toString(InputChar.KEY_PPAGE), new KeyAction() {
- public void execute() {
- if ((screenBuffer.size() + commandLines.getLines().size()
- - (Toolkit.getScreenHeight()/2) * pageUpCount) > 0) {
- pageUpCount++;
-
- }
- }
- public CharSequence getHelp() {
- return "PageUp:\tMove back in screen history";
- }
- });
- actionKeys.put(Integer.toString(InputChar.KEY_NPAGE), new KeyAction() {
- public void execute() {
- if (pageUpCount > 0) {
- pageUpCount--;
- }
- }
- public CharSequence getHelp() {
- return "PageDown:\tMove forward in screen history";
- }
- });
- actionKeys.put(Integer.toString(InputChar.KEY_END),new KeyAction() {
- public void execute() {
- int curLineEnd = commandLines.getLines().get(cursorPosition.y).length();
- if (cursorPosition.x == curLineEnd) {
- cursorPosition.y = commandLines.getLines().size()-1;
- CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
- cursorPosition.x = lineBuffer.length();
- } else {
- cursorPosition.x = curLineEnd;
- }
- }
- public CharSequence getHelp() {
- return "End:\tMove the cursor to the end of the line, of if already there at the end of the command";
- }
- });
- actionKeys.put(Integer.toString(InputChar.KEY_HOME), new KeyAction() {
- public void execute() {
- if (cursorPosition.x == 0) {
- cursorPosition.y = 0;
- }
- cursorPosition.x = 0;
- }
- public CharSequence getHelp() {
- return "End:\tMove the cursor to the start of the line, of if already there at the start of the command";
- }
- });
actionKeys.put("", new KeyAction() {
public void execute() {
StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
@@ -297,6 +275,28 @@
return "Control-U:\tRemove all characters before the cursor position";
}
});
+ actionKeys.put(Integer.toString(InputChar.KEY_PPAGE), new KeyAction() {
+ public void execute() {
+ if ((screenBuffer.size() + commandLines.getLines().size()
+ - (Toolkit.getScreenHeight()/2) * pageUpCount) > 0) {
+ pageUpCount++;
+
+ }
+ }
+ public CharSequence getHelp() {
+ return "PageUp:\tMove back in screen history";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_NPAGE), new KeyAction() {
+ public void execute() {
+ if (pageUpCount > 0) {
+ pageUpCount--;
+ }
+ }
+ public CharSequence getHelp() {
+ return "PageDown:\tMove forward in screen history";
+ }
+ });
actionKeys.put("", new KeyAction() { //Ctrl+D
public void execute() {
if (commandLines.getCommandString().length() == 0) { //Quit on empty commandline, ignore otherwise
|
|
From: SVN by r. <sv...@ca...> - 2008-03-02 12:10:01
|
Author: roy
Date: 2008-03-02 13:09:49 +0100 (Sun, 02 Mar 2008)
New Revision: 243
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
made output synchronized as well
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-27 15:29:54 UTC (rev 242)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-03-02 12:09:49 UTC (rev 243)
@@ -540,7 +540,7 @@
* Output data to the screen.
* @param data the data to print to the screen.
*/
- protected void output(CharSequence data) {
+ protected synchronized void output(CharSequence data) {
screenBuffer.addAll(getLines(data));
if (spoolWriter != null) {
try {
|
|
From: SVN by r. <sv...@ca...> - 2008-02-27 15:30:07
|
Author: roy
Date: 2008-02-27 16:29:54 +0100 (Wed, 27 Feb 2008)
New Revision: 242
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
fix queries
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-27 09:49:14 UTC (rev 241)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-27 15:29:54 UTC (rev 242)
@@ -128,6 +128,7 @@
commands.register("EXIT[\\s]*", new QuitCommand("exit"));
//commands.register("\\\\Q[\\s]*", new QuitCommand("\\q"));
commands.register("@.*", new ExecuteBatchCommand());
+ commands.register("(SELECT|UPDATE|ALTER|INSERT|DELETE).*;[\\s]*", new QueryCommand());
// keys
actionKeys.put(Integer.toString(InputChar.KEY_LEFT), new KeyAction() {
@@ -736,8 +737,11 @@
}
return true;
}
- final Command command = createCommand(commandString);
+ Command command = createCommand(commandString); // first try to find a match without ;
if (command == null) {
+ command = createCommand(sqlCommand.getUntrimmedCommandString()); // then with ; for sql statements...
+ }
+ if (command == null) {
return false;
}
// make sure only one command is run at once
@@ -748,6 +752,7 @@
Logger.getLogger(SQLShell.class.getName()).log(Level.SEVERE, null, ex);
}
}
+ output(commandString+"\n");
if (direct || !command.backgroundProcessSupported()) {
output(command.execute(sqlCommand));
} else {
@@ -766,7 +771,7 @@
if (command != null) {
return command;
}
- if (commandString.endsWith(";")) {
+ if (commandString.matches(".*;[\\s]*")) {
return new QueryCommand(); // TODO is this ever reached???
}
return null;
@@ -1381,7 +1386,7 @@
// Exec cmd
String commandString = cmd.toString();
currentCommand = createCommand(commandString);
- output(commandString);
+ output(commandString+"\n");
output(currentCommand.execute(new InputCommand(commandString)));
cmd=new StringBuilder();
}
@@ -1498,7 +1503,6 @@
execute();
} finally {
showPrompt = true;
- try { Thread.sleep(500);} catch(InterruptedException e) {} // hack
paint();
}
}
|
|
From: SVN by r. <sv...@ca...> - 2008-02-27 09:49:24
|
Author: roy
Date: 2008-02-27 10:49:14 +0100 (Wed, 27 Feb 2008)
New Revision: 241
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
output fix for background commands
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-27 09:07:38 UTC (rev 240)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-27 09:49:14 UTC (rev 241)
@@ -777,7 +777,7 @@
* Paint the screen.
*/
@Override
- protected void paint() {
+ protected synchronized void paint() {
Toolkit.clearScreen(new CharColor(CharColor.WHITE, CharColor.BLACK, CharColor.REVERSE, CharColor.REVERSE));
CharColor color = new CharColor(CharColor.BLACK, CharColor.WHITE, CharColor.BOLD, CharColor.BOLD);
@@ -1380,9 +1380,9 @@
if (line.endsWith(";")) {
// Exec cmd
String commandString = cmd.toString();
- Command cCommand = createCommand(commandString);
+ currentCommand = createCommand(commandString);
output(commandString);
- output(cCommand.execute(new InputCommand(commandString))); // TODO start in background...
+ output(currentCommand.execute(new InputCommand(commandString)));
cmd=new StringBuilder();
}
}
|
|
From: SVN by r. <sv...@ca...> - 2008-02-27 09:07:48
|
Author: roy
Date: 2008-02-27 10:07:38 +0100 (Wed, 27 Feb 2008)
New Revision: 240
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
some abort fixes for batchcommand
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-26 20:43:14 UTC (rev 239)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-27 09:07:38 UTC (rev 240)
@@ -1354,10 +1354,12 @@
}
private class ExecuteBatchCommand implements Command {
- private boolean cancelled = false;
+ private boolean cancelled;
+ private Command currentCommand;
@Override
public CharSequence execute(SQLCommand sqlCommand) {
cancelled = false;
+ currentCommand = null;
String command = sqlCommand.getCommandString();
// read file from file system and execute
FileInputStream fin = null;
@@ -1388,6 +1390,9 @@
error(e);
} finally {
if (fin != null) try { fin.close();}catch(Exception e) {/*ignore*/}
+ if (cancelled) {
+ return "Execution of file '" + toFileName(command.substring(1)) +"' aborted....";
+ }
return "File '" + toFileName(command.substring(1)) +"' executed successfully.";
}
}
@@ -1437,8 +1442,12 @@
@Override
public boolean abort() {
cancelled = true;
+ if (currentCommand != null) {
+ currentCommand.abort();
+ }
return true;
}
+
@Override
public boolean backgroundProcessSupported() {
return true;
|
|
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;
+ }
}
|
|
From: SVN by r. <sv...@ca...> - 2008-02-19 20:02:29
|
Author: roy
Date: 2008-02-19 21:02:17 +0100 (Tue, 19 Feb 2008)
New Revision: 238
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
navigation fixes (end/home)
updates to ctrl-u
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-15 14:43:21 UTC (rev 237)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-19 20:02:17 UTC (rev 238)
@@ -241,21 +241,28 @@
});
actionKeys.put(Integer.toString(InputChar.KEY_END),new KeyAction() {
public void execute() {
- cursorPosition.y = commandLines.getLines().size()-1;
- CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
- cursorPosition.x = lineBuffer.length();
+ int curLineEnd = commandLines.getLines().get(cursorPosition.y).length();
+ if (cursorPosition.x == curLineEnd) {
+ cursorPosition.y = commandLines.getLines().size()-1;
+ CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
+ cursorPosition.x = lineBuffer.length();
+ } else {
+ cursorPosition.x = curLineEnd;
+ }
}
public CharSequence getHelp() {
- return "End:\tMove the cursor to the end of the command";
+ return "End:\tMove the cursor to the end of the line, of if already there at the end of the command";
}
});
actionKeys.put(Integer.toString(InputChar.KEY_HOME), new KeyAction() {
public void execute() {
- cursorPosition.y = 0;
+ if (cursorPosition.x == 0) {
+ cursorPosition.y = 0;
+ }
cursorPosition.x = 0;
}
public CharSequence getHelp() {
- return "Home:\tMove the cursor to the start of the command";
+ return "End:\tMove the cursor to the start of the line, of if already there at the start of the command";
}
});
actionKeys.put("", new KeyAction() {
@@ -271,9 +278,19 @@
});
actionKeys.put("", new KeyAction() { // ctrl+u
public void execute() {
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
- lineBuffer.delete(0, cursorPosition.x);
- cursorPosition.x = 0;
+ if (cursorPosition.x > 0) {
+ StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ lineBuffer.delete(0, cursorPosition.x);
+ cursorPosition.x = 0;
+ } else if (cursorPosition.y > 0) {
+ StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ if (lineBuffer.length() == 0) {
+ commandLines.getEditableLines().remove(cursorPosition.y);
+ }
+ cursorPosition.y--;
+ lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ lineBuffer.delete(0, lineBuffer.length());
+ }
}
public CharSequence getHelp() {
return "Control-U:\tRemove all characters before the cursor position";
|
|
From: SVN by r. <sv...@ca...> - 2008-02-15 14:43:31
|
Author: roy
Date: 2008-02-15 15:43:21 +0100 (Fri, 15 Feb 2008)
New Revision: 237
Modified:
src/main/java/nl/improved/sqlclient/QueryExecutor.java
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
improved cancel support
Modified: src/main/java/nl/improved/sqlclient/QueryExecutor.java
===================================================================
--- src/main/java/nl/improved/sqlclient/QueryExecutor.java 2008-02-15 13:29:16 UTC (rev 236)
+++ src/main/java/nl/improved/sqlclient/QueryExecutor.java 2008-02-15 14:43:21 UTC (rev 237)
@@ -53,6 +53,7 @@
private String timeFormat;
private String timestampFormat;
private String dateFormat;
+ private boolean cancelled = false;
/**
* Constructor.
@@ -166,6 +167,7 @@
public CharSequence cancel() {
try {
DBConnector.getInstance().getStatement().cancel();
+ cancelled = true;
return "Cancel accepted";
} catch (SQLException ex) {
return "Cancel Failed: "+ ex.toString();
@@ -179,6 +181,7 @@
* @throws SQLException if the database could not execute the SQL query for some reason.
*/
protected CharSequence executeQuery(CharSequence command) throws SQLException {
+ cancelled = false;
ResultSet results = DBConnector.getInstance().getStatement().executeQuery(command.toString());
//StringBuffer separator = new StringBuffer();
@@ -197,7 +200,7 @@
ResultBuilder displayValue = new ResultBuilder();
displayValue.setHeader(labels);
int rowCount = 0;
- while (results.next()) {
+ 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) {}
@@ -211,6 +214,9 @@
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);
return displayValue.toString();
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-15 13:29:16 UTC (rev 236)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-15 14:43:21 UTC (rev 237)
@@ -1489,13 +1489,9 @@
@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;
- }
+ //DBConnector.getInstance().getStatement().cancel();
+ output(DBConnector.getInstance().getQueryExecutor().cancel());
+ return true;
}
}
|
|
From: SVN by r. <sv...@ca...> - 2008-02-15 13:29:27
|
Author: roy
Date: 2008-02-15 14:29:16 +0100 (Fri, 15 Feb 2008)
New Revision: 236
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
added keyaction (Help for keys) support
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-14 21:05:21 UTC (rev 235)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-15 13:29:16 UTC (rev 236)
@@ -23,6 +23,8 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jcurses.widgets.Window;
@@ -96,6 +98,8 @@
*/
private CommandManager commands = new CommandManager();
+ private Map<String, KeyAction> actionKeys = new HashMap<String, KeyAction>();
+
/**
* Constructor.
*/
@@ -125,6 +129,178 @@
//commands.register("\\\\Q[\\s]*", new QuitCommand("\\q"));
commands.register("@.*", new ExecuteBatchCommand());
+ // keys
+ actionKeys.put(Integer.toString(InputChar.KEY_LEFT), new KeyAction() {
+ public void execute() {
+ if (cursorPosition.x > 0) {
+ cursorPosition.x--;
+ } else if (cursorPosition.y > 0) {
+ cursorPosition.y--;
+ cursorPosition.x = commandLines.getLines().get(cursorPosition.y).length();
+ }
+ }
+ public CharSequence getHelp() {
+ return "Arrow Left:\tMove cursor to the left";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_RIGHT), new KeyAction() {
+ public void execute() {
+ CharSequence tmp = commandLines.getLines().get(cursorPosition.y);
+ if (cursorPosition.x < tmp.length()) {
+ cursorPosition.x++;
+ } else if (cursorPosition.y < commandLines.getLines().size()-1) {
+ cursorPosition.x = 0;
+ cursorPosition.y++;
+ }
+ }
+ public CharSequence getHelp() {
+ return "Arrow Right:\tMove cursor to the right";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_UP), new KeyAction() {
+ public void execute() {
+ if (commandIndex > 0) {
+ commandLines = commandHistory.get(--commandIndex);
+ cursorPosition.y = commandLines.getLines().size()-1;
+ CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
+ cursorPosition.x = lineBuffer.length();
+ }
+ }
+ public CharSequence getHelp() {
+ return "Arrow Up:\tBrowse to previous command in the history";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_DOWN), new KeyAction() {
+ public void execute() {
+ if (commandIndex < commandHistory.size()-1) {
+ commandLines = commandHistory.get(++commandIndex);
+ cursorPosition.y = commandLines.getLines().size()-1;
+ CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
+ cursorPosition.x = lineBuffer.length();
+ }
+ }
+ public CharSequence getHelp() {
+ return "Arrow Down:\tBrowse to next command in the history";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_BACKSPACE), new KeyAction() {
+ public void execute() {
+ if (cursorPosition.x == 0) {
+ if (cursorPosition.y > 0) {
+ StringBuilder line = getEditableCommand().getEditableLines().remove(cursorPosition.y);
+ cursorPosition.y--;
+ StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ cursorPosition.x = lineBuffer.length();
+ lineBuffer.append(line);
+ }
+ } else {
+ StringBuilder tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
+ if (cursorPosition.x > 0) {
+ tmp.deleteCharAt(cursorPosition.x-1);
+ cursorPosition.x--;
+ }
+ }
+ }
+ public CharSequence getHelp() {
+ return "Backspace:\tRemove the character before the cursor position";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_DC), new KeyAction() {
+ public void execute() {
+ StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ if (cursorPosition.x < lineBuffer.length()) {
+ StringBuilder tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
+ tmp.deleteCharAt(cursorPosition.x);
+ }
+ }
+ public CharSequence getHelp() {
+ return "Del:\tDelete the charactor at the current cursor position";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_PPAGE), new KeyAction() {
+ public void execute() {
+ if ((screenBuffer.size() + commandLines.getLines().size()
+ - (Toolkit.getScreenHeight()/2) * pageUpCount) > 0) {
+ pageUpCount++;
+
+ }
+ }
+ public CharSequence getHelp() {
+ return "PageUp:\tMove back in screen history";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_NPAGE), new KeyAction() {
+ public void execute() {
+ if (pageUpCount > 0) {
+ pageUpCount--;
+ }
+ }
+ public CharSequence getHelp() {
+ return "PageDown:\tMove forward in screen history";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_END),new KeyAction() {
+ public void execute() {
+ cursorPosition.y = commandLines.getLines().size()-1;
+ CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
+ cursorPosition.x = lineBuffer.length();
+ }
+ public CharSequence getHelp() {
+ return "End:\tMove the cursor to the end of the command";
+ }
+ });
+ actionKeys.put(Integer.toString(InputChar.KEY_HOME), new KeyAction() {
+ public void execute() {
+ cursorPosition.y = 0;
+ cursorPosition.x = 0;
+ }
+ public CharSequence getHelp() {
+ return "Home:\tMove the cursor to the start of the command";
+ }
+ });
+ actionKeys.put("", new KeyAction() {
+ public void execute() {
+ StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ int previousBreak = SQLUtil.getLastBreakIndex(lineBuffer.substring(0, cursorPosition.x));
+ lineBuffer.delete(previousBreak, cursorPosition.x);
+ cursorPosition.x = previousBreak;
+ }
+ public CharSequence getHelp() {
+ return "Control-W:\tRemove word before cursor position";
+ }
+ });
+ actionKeys.put("", new KeyAction() { // ctrl+u
+ public void execute() {
+ StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ lineBuffer.delete(0, cursorPosition.x);
+ cursorPosition.x = 0;
+ }
+ public CharSequence getHelp() {
+ return "Control-U:\tRemove all characters before the cursor position";
+ }
+ });
+ actionKeys.put("", new KeyAction() { //Ctrl+D
+ public void execute() {
+ if (commandLines.getCommandString().length() == 0) { //Quit on empty commandline, ignore otherwise
+ executeCommand(new InputCommand("quit"));
+ }
+ }
+ public CharSequence getHelp() {
+ return "Control-D:\tExit sqlshell";
+ }
+ });
+ actionKeys.put("", new KeyAction() { // ctrl+a
+ public void execute() {
+ output("Abort requested");
+ if (commandThread.isAlive() && commandThread.getCommand().abort()) {
+ output("Abort done..");
+ }
+ }
+ public CharSequence getHelp() {
+ return "Control-A:\tAbort current command (if it is supported by that command)";
+ }
+ });
+
MAX_LINE_LENGTH = Toolkit.getScreenWidth()-(2+PROMPT.length()+2+1); // 2 spaces bouds.. prompt + "> "
output("Welcome to the SQLShell client.");
@@ -196,197 +372,119 @@
if (inp.getCode() != InputChar.KEY_PPAGE && inp.getCode() != InputChar.KEY_NPAGE) {
pageUpCount = 0; // some character entered, so reset pageup count
}
- if (inp.isSpecialCode() || inp.toString().equals("") || inp.toString().equals("")) {
- if (inp.getCode() == InputChar.KEY_LEFT) {
- if (cursorPosition.x > 0) {
- cursorPosition.x--;
- } else if (cursorPosition.y > 0) {
- cursorPosition.y--;
- cursorPosition.x = commandLines.getLines().get(cursorPosition.y).length();
- }
- } else if (inp.getCode() == InputChar.KEY_RIGHT) {
- CharSequence tmp = commandLines.getLines().get(cursorPosition.y);
- if (cursorPosition.x < tmp.length()) {
- cursorPosition.x++;
- } else if (cursorPosition.y < commandLines.getLines().size()-1) {
- cursorPosition.x = 0;
- cursorPosition.y++;
- }
- } else if (inp.getCode() == InputChar.KEY_UP) {
- if (commandIndex > 0) {
- commandLines = commandHistory.get(--commandIndex);
- cursorPosition.y = commandLines.getLines().size()-1;
- CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
- cursorPosition.x = lineBuffer.length();
- }
- } else if (inp.getCode() == InputChar.KEY_DOWN) {
- if (commandIndex < commandHistory.size()-1) {
- commandLines = commandHistory.get(++commandIndex);
- cursorPosition.y = commandLines.getLines().size()-1;
- CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
- cursorPosition.x = lineBuffer.length();
- }
- } else if (inp.getCode() == InputChar.KEY_BACKSPACE) {
- if (cursorPosition.x == 0) {
- if (cursorPosition.y > 0) {
- StringBuilder line = getEditableCommand().getEditableLines().remove(cursorPosition.y);
- cursorPosition.y--;
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
- cursorPosition.x = lineBuffer.length();
- lineBuffer.append(line);
- }
- } else {
- StringBuilder tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
- if (cursorPosition.x > 0) {
- tmp.deleteCharAt(cursorPosition.x-1);
- cursorPosition.x--;
- }
- }
- } else if (inp.getCode() == InputChar.KEY_DC) {
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
- if (cursorPosition.x < lineBuffer.length()) {
- StringBuilder tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
- tmp.deleteCharAt(cursorPosition.x);
- }
- } else if (inp.getCode() == InputChar.KEY_PPAGE) {
- if ((screenBuffer.size() + commandLines.getLines().size()
- - (Toolkit.getScreenHeight()/2) * pageUpCount) > 0) {
- pageUpCount++;
-
- }
- } else if (inp.getCode() == InputChar.KEY_NPAGE) {
- if (pageUpCount > 0) {
- pageUpCount--;
- }
- } else if (inp.getCode() == InputChar.KEY_END) {
- cursorPosition.y = commandLines.getLines().size()-1;
- CharSequence lineBuffer = commandLines.getLines().get(cursorPosition.y);
- cursorPosition.x = lineBuffer.length();
- } else if (inp.getCode() == InputChar.KEY_HOME) {
- cursorPosition.y = 0;
- cursorPosition.x = 0;
- } else if (inp.toString() != null && inp.toString().equals("")) { // ctrl+w
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
- int previousBreak = SQLUtil.getLastBreakIndex(lineBuffer.substring(0, cursorPosition.x));
- lineBuffer.delete(previousBreak, cursorPosition.x);
- cursorPosition.x = previousBreak;
- } else if (inp.toString() != null && inp.toString().equals("")) { // ctrl+u
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
- lineBuffer.delete(0, cursorPosition.x);
- cursorPosition.x = 0;
- } else
+ if (inp.isSpecialCode()) {
+ KeyAction ke = actionKeys.get(Integer.toString(inp.getCode()));// || inp.toString().equals("") || inp.toString().equals("")) {
+ if (ke != null) {
+ ke.execute();
+ } else {
debug("Unknown character: "+ inp.getCode());
- } else if (inp.toString().equals("")) { //Ctrl+D
- 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
- SQLCommand sqlCommand = getCommand();
- String command = sqlCommand.getCommandString();
- // search command...
- if (command.length() > 0 && command.charAt(0) == '/') {
- String matchPattern=".*"+command.substring(1,command.length())+".*";
- for (int cIndex = commandHistory.size()-1; cIndex >=0; cIndex--) {
- if (cIndex == commandIndex) {
- continue; // skip current command
+ KeyAction ke = actionKeys.get(inp.toString());
+ if (ke != null) {
+ ke.execute();
+ } else {
+ if (inp.getCharacter() == '\n') { // newline... see if the command can be executed
+ // execute the command
+ SQLCommand sqlCommand = getCommand();
+ String command = sqlCommand.getCommandString();
+ // search command...
+ if (command.length() > 0 && command.charAt(0) == '/') { // search in history
+ String matchPattern=".*"+command.substring(1,command.length())+".*";
+ for (int cIndex = commandHistory.size()-1; cIndex >=0; cIndex--) {
+ if (cIndex == commandIndex) {
+ continue; // skip current command
+ }
+ SQLCommand newSqlCommand = commandHistory.get(cIndex);
+ String commandString = newSqlCommand.getCommandString();
+ if (commandString.matches(matchPattern)) {
+ commandHistory.remove(commandIndex);
+ commandIndex = commandHistory.indexOf(newSqlCommand);
+ commandLines = newSqlCommand;
+ cursorPosition.y = 0;
+ cursorPosition.x = 0;
+ paint(); // force repaint
+ return;
+ }
}
- SQLCommand newSqlCommand = commandHistory.get(cIndex);
- String commandString = newSqlCommand.getCommandString();
- if (commandString.matches(matchPattern)) {
- commandHistory.remove(commandIndex);
- commandIndex = commandHistory.indexOf(newSqlCommand);
- commandLines = newSqlCommand;
- cursorPosition.y = 0;
- cursorPosition.x = 0;
- paint(); // force repaint
- return;
+ Toolkit.beep(); // TODO clear search??
+ return;
+ } else if (executeCommand(sqlCommand)) {
+ // clear command history
+ if (commandIndex != commandHistory.size()-1) {
+ SQLCommand tmpLines = commandLines;
+ commandLines = commandHistory.get(commandHistory.size()-1);
+ if (commandLines.getCommandString().equals("")) {
+ commandHistory.add(commandHistory.size()-1, tmpLines);
+ } else {
+ commandHistory.add(tmpLines);
+ commandLines = tmpLines;
+ }
}
+ if (!commandLines.getCommandString().equals("")) {
+ commandLines = new SQLCommand();
+ commandHistory.add(commandLines);
+ newLine();
+ }
+ commandIndex = commandHistory.size()-1;
+ cursorPosition.y = commandLines.getLines().size()-1;
+ cursorPosition.x = commandLines.getLines().get(cursorPosition.y).length();
+ paint(); // force repaint
+ return;
}
- Toolkit.beep();
- return;
}
- if (executeCommand(sqlCommand)) {
- // clear command history
- if (commandIndex != commandHistory.size()-1) {
- SQLCommand tmpLines = commandLines;
- commandLines = commandHistory.get(commandHistory.size()-1);
- if (commandLines.getCommandString().equals("")) {
- commandHistory.add(commandHistory.size()-1, tmpLines);
- } else {
- commandHistory.add(tmpLines);
- commandLines = tmpLines;
- }
+ CharSequence newText;
+ if (inp.getCharacter() == '\t') {
+ try {
+ newText = getTabCompletion(commandLines, cursorPosition);
+ } catch(IllegalStateException e) {
+ output(getCommand().getCommandString()); // add command as well...
+ error(e);
+ return;
}
- if (!commandLines.getCommandString().equals("")) {
- commandLines = new SQLCommand();
- commandHistory.add(commandLines);
- newLine();
- }
- commandIndex = commandHistory.size()-1;
- cursorPosition.y = commandLines.getLines().size()-1;
- cursorPosition.x = commandLines.getLines().get(cursorPosition.y).length();
- paint(); // force repaint
- return;
+ } else {
+ newText = Character.toString(inp.getCharacter());
}
- }
- CharSequence newText;
- if (inp.getCharacter() == '\t') {
- try {
- newText = getTabCompletion(commandLines, cursorPosition);
- } catch(IllegalStateException e) {
- output(getCommand().getCommandString()); // add command as well...
- error(e);
- return;
- }
- } else {
- newText = Character.toString(inp.getCharacter());
- }
- if (newText.equals("\n")) {
- newLine(); // TODO Fix
- } else {
- List<StringBuilder> editableLines = getEditableCommand().getEditableLines();
- StringBuilder currentLine = editableLines.get(cursorPosition.y);
- currentLine.insert(cursorPosition.x, newText);
- cursorPosition.x += newText.length();
- // check if the new line is becoming too long
- if (currentLine.length() > MAX_LINE_LENGTH) {
- // TODO search for lastspace that is not between '' ??
- int lastSpace = currentLine.lastIndexOf(" ");
- // check if there are enough 'next' lines
- // if not.. add one
- if (editableLines.size()-1 == cursorPosition.y) {
- StringBuilder newLine = new StringBuilder();
- editableLines.add(newLine);
+ if (newText.equals("\n")) {
+ newLine(); // TODO Fix return in middle of an other line
+ } else {
+ List<StringBuilder> editableLines = getEditableCommand().getEditableLines();
+ StringBuilder currentLine = editableLines.get(cursorPosition.y);
+ currentLine.insert(cursorPosition.x, newText);
+ cursorPosition.x += newText.length();
+ // check if the new line is becoming too long
+ if (currentLine.length() > MAX_LINE_LENGTH) {
+ // TODO search for lastspace that is not between '' ??
+ int lastSpace = currentLine.lastIndexOf(" ");
+ // check if there are enough 'next' lines
+ // if not.. add one
+ if (editableLines.size()-1 == cursorPosition.y) {
+ StringBuilder newLine = new StringBuilder();
+ editableLines.add(newLine);
+ }
+ // check if the nextline has enough room for the new word
+ // if not.. add a new line
+ if (editableLines.get(cursorPosition.y+1).length()
+ + (currentLine.length()-lastSpace+1) > MAX_LINE_LENGTH) {
+ StringBuilder newLine = new StringBuilder();
+ editableLines.add(cursorPosition.y+1, newLine);
+ }
+ // fetch the next line
+ StringBuilder nextLine = editableLines.get(cursorPosition.y+1);
+ // if the nextline already has some text.. add a space in front of it
+ if (nextLine.length() > 0) {
+ nextLine.insert(0, ' ');
+ }
+ // insert the new text at the beginning
+ nextLine.insert(0, currentLine.subSequence(lastSpace+1, currentLine.length()));
+ currentLine.delete(lastSpace, currentLine.length());
+ // check if the cursor postition > the new line length
+ // calculate new x and go to nextline
+ if (cursorPosition.x >= lastSpace) {
+ cursorPosition.x = cursorPosition.x - (lastSpace+1);
+ cursorPosition.y++;
+ }
}
- // check if the nextline has enough room for the new word
- // if not.. add a new line
- if (editableLines.get(cursorPosition.y+1).length()
- + (currentLine.length()-lastSpace+1) > MAX_LINE_LENGTH) {
- StringBuilder newLine = new StringBuilder();
- editableLines.add(cursorPosition.y+1, newLine);
- }
- // fetch the next line
- StringBuilder nextLine = editableLines.get(cursorPosition.y+1);
- // if the nextline already has some text.. add a space in front of it
- if (nextLine.length() > 0) {
- nextLine.insert(0, ' ');
- }
- // insert the new text at the beginning
- nextLine.insert(0, currentLine.subSequence(lastSpace+1, currentLine.length()));
- currentLine.delete(lastSpace, currentLine.length());
- // check if the cursor postition > the new line length
- // calculate new x and go to nextline
- if (cursorPosition.x >= lastSpace) {
- cursorPosition.x = cursorPosition.x - (lastSpace+1);
- cursorPosition.y++;
- }
}
}
}
@@ -1076,20 +1174,41 @@
return "Unkown command '"+ cmdString+"'";
}
// default print all commands
+ // TODO iterate
+ StringBuilder returnValue = new StringBuilder();
+ returnValue.append("Available key mappings:\n");
+ int max = 0;
+ Iterator<KeyAction> iKeyActions = actionKeys.values().iterator();
+ while (iKeyActions.hasNext()) {
+ max = Math.max(max, iKeyActions.next().getHelp().toString().indexOf('\t'));
+ }
+ iKeyActions = actionKeys.values().iterator();
+ while (iKeyActions.hasNext()) {
+ returnValue.append(" ");
+ //returnValue.append(iKeyActions.next().getHelp());
+ String help = iKeyActions.next().getHelp().toString();
+ int index = help.indexOf('\t');
+ returnValue.append(help.substring(0, index));
+ for (int i = index; i < max; i++) {
+ returnValue.append(' ');
+ }
+ returnValue.append(help.substring(index));
+ returnValue.append('\n');
+ }
+ returnValue.append("\n\nAvailable commands:\n");
Iterator<Command> iCommands = commands.getCommands().iterator();
while (iCommands.hasNext()) {
Command cmd = iCommands.next();
availableCommands.add(cmd.getCommandString());
}
- StringBuilder returnValue = toColumns(availableCommands);
+ returnValue.append(toColumns(availableCommands));
String helpHeader = "\nHelp for SQLShell client "+SQLProperties.getProperty(SQLProperties.PropertyName.VERSION, "SVN Snapshot")+"\n"+
"Here you find a list of available commands. "+
"To get more information about a specific command enter:\n"+
" help command (for example 'help help')\n\n"+
"If the list is not sufficient enough you could try searching help using:\n"+
" help -k searchstring (for example help -k column)\n"+
- "This results in a list of commands matching the searchstring\n\n"+
- "Available commands:\n";
+ "This results in a list of commands matching the searchstring\n\n";
returnValue.insert(0, helpHeader);
return returnValue;
}
@@ -1381,6 +1500,11 @@
}
+ private interface KeyAction {
+ void execute();
+ CharSequence getHelp();
+ }
+
public static void main(String[] args) {
SQLShell shell = new SQLShell();
shell.show();
|
|
From: SVN by r. <sv...@ca...> - 2008-02-14 21:05:32
|
Author: roy
Date: 2008-02-14 22:05:21 +0100 (Thu, 14 Feb 2008)
New Revision: 235
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
started background thread..
currently it is a little 'hackish'
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-13 07:23:46 UTC (rev 234)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-02-14 21:05:21 UTC (rev 235)
@@ -39,6 +39,7 @@
public class SQLShell extends Window {
private CommandThread commandThread;
+ private boolean showPrompt = true;
/**
* The (default) maximum matches to show in a selection dialog.
@@ -619,7 +620,7 @@
if (cCommand != null) {
output(sqlCommand.getUntrimmedCommandString());
commandThread = new CommandThread(cCommand) {
- public void run() {
+ public void execute() {
try {
output(cCommand.execute(sqlCommand));
} catch(Exception e) {
@@ -630,8 +631,7 @@
if (direct || cCommand instanceof QuitCommand || cCommand instanceof ConnectCommand) {
commandThread.run();
} else {
- //commandThread.start(); // TODO
- commandThread.run();
+ commandThread.start();
}
return true;
}
@@ -639,15 +639,14 @@
// execute sql command
output(sqlCommand.getUntrimmedCommandString());
commandThread = new CommandThread(new QueryCommand()) {
- public void run() {
+ public void execute() {
output(getCommand().execute(sqlCommand));
}
};
if (direct) {
commandThread.run();
} else {
- //commandThread.start(); // TODO
- commandThread.run();
+ commandThread.start();
}
return true;
} else if (command.equalsIgnoreCase("printStackTrace")) {
@@ -676,7 +675,7 @@
//add prompt
List<? extends CharSequence> currentLines = commandLines.getLines();
for (int i = 0; i < currentLines.size(); i++) {
- if (i == 0) {
+ if (i == 0 && showPrompt) {
tmpList.add(PROMPT+"> "+currentLines.get(i));
} else {
String nrI = Integer.toString(i+1);
@@ -1302,12 +1301,25 @@
}
}
- private static class CommandThread extends Thread {
+ private abstract class CommandThread extends Thread {
private Command cmd;
public CommandThread(Command cmd) {
this.cmd = cmd;
}
+ public final void run() {
+ showPrompt = false;
+ try {
+ execute();
+ } finally {
+ showPrompt = true;
+ try { Thread.sleep(500);} catch(InterruptedException e) {} // hack
+ paint();
+ }
+ }
+
+ abstract void execute();
+
Command getCommand() {
return cmd;
}
|
|
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
+ }
}
|
|
From: SVN by r. <sv...@ca...> - 2008-02-10 16:33:56
|
Author: roy
Date: 2008-02-10 16:17:36 +0100 (Sun, 10 Feb 2008)
New Revision: 233
Modified:
src/main/java/nl/improved/sqlclient/SQLUtil.java
src/test/java/nl/improved/sqlclient/SQLUtilTest.java
Log:
fixed deadlock because of a bug in the regular expression code of java
Modified: src/main/java/nl/improved/sqlclient/SQLUtil.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLUtil.java 2008-01-30 11:49:04 UTC (rev 232)
+++ src/main/java/nl/improved/sqlclient/SQLUtil.java 2008-02-10 15:17:36 UTC (rev 233)
@@ -215,7 +215,9 @@
String WHERE = "W|WH|WHE|WHER|WHERE";
String GROUP_BY = "G|GR|GRO|GROU|GROUP|GROUP |GROUP B|GROUP BY";
String ORDER_BY = "O|OR|ORD|ORDE|ORDER|ORDER |ORDER B|ORDER BY";
- if (tmpCommand.matches("[\\s]*"+VAR+"+([\\s]*,[\\s]*"+VAR+"+)*[\\s]+(|"+WHERE+"|"+GROUP_BY+"|"+ORDER_BY+")")) {
+ //String regexpSQL = "[\\s]*"+VAR+"+([\\s]*,[\\s]*"+VAR+"+)*[\\s]+(|"+WHERE+"|"+GROUP_BY+"|"+ORDER_BY+")";
+ String regexpSQL = ".*"+VAR+"[\\s]+(|"+WHERE+"|"+GROUP_BY+"|"+ORDER_BY+")";
+ if (tmpCommand.matches(regexpSQL)) {
String end = startOfCommand.substring(startOfCommand.lastIndexOf(' ')+1);
return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
, Arrays.asList(new String[]{"WHERE", "GROUP BY", "ORDER BY"}), end);
@@ -257,7 +259,7 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, parseTableNames(commandInfo, commandPoint), end);
}
- System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp);
+ //System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp);
String end = "";
if (upperCommandString.matches(".*[\\s]+")) {
end = "";
Modified: src/test/java/nl/improved/sqlclient/SQLUtilTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/SQLUtilTest.java 2008-01-30 11:49:04 UTC (rev 232)
+++ src/test/java/nl/improved/sqlclient/SQLUtilTest.java 2008-02-10 15:17:36 UTC (rev 233)
@@ -160,6 +160,24 @@
assertEquals(0, matches.size());
assertEquals("B", info.getStart());
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B,C"});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(0, matches.size());
+ assertEquals("C", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"select * from testdata, TESTDATA, TESTDATA, t"});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(0, matches.size());
+ assertEquals("t", info.getStart());
+
sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A WHERE 1=1"});
cursorPos = new Point("SELECT * FROM A".length(),0);
info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
@@ -243,7 +261,7 @@
matches = info.getPossibleMatches();
//assertEquals(1, matches.size());
assertTrue(matches.contains("A"));
- System.out.println("I: " + info.getStart());
+ //System.out.println("I: " + info.getStart());
assertEquals("I", info.getStart());
sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A."});
@@ -254,7 +272,7 @@
matches = info.getPossibleMatches();
//assertEquals(1, matches.size());
assertTrue(matches.contains("A"));
- System.out.println("I:'" + info.getStart() +"'");
+ //System.out.println("I:'" + info.getStart() +"'");
assertEquals("", info.getStart());
// with other conditions
|
|
From: SVN by r. <sv...@ca...> - 2008-01-30 12:05:52
|
Author: roy
Date: 2008-01-30 12:49:04 +0100 (Wed, 30 Jan 2008)
New Revision: 232
Modified:
src/main/java/nl/improved/sqlclient/SQLUtil.java
src/test/java/nl/improved/sqlclient/SQLUtilTest.java
Log:
more tab completion fixes
Modified: src/main/java/nl/improved/sqlclient/SQLUtil.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLUtil.java 2008-01-27 12:43:39 UTC (rev 231)
+++ src/main/java/nl/improved/sqlclient/SQLUtil.java 2008-01-30 11:49:04 UTC (rev 232)
@@ -236,6 +236,8 @@
// 'SELECT x FROM A,B WHERE A.x='x' AND/OR '
String tmpCommand = startOfCommand.substring(startOfCommand.indexOf(lastKeyword)+lastKeyword.length()+1);
String upperCommandString = tmpCommand.toUpperCase();
+ String VAR = "(|"+TABLE+"\\.)(|"+COLUMN+")";
+ String VALUE = "('.*'|[0-9]+|"+VAR+")";
String subPart = "(|"+VAR+"(|[\\s]+"+COMPARATOR+"[\\s]+(|"+VAR+")))";
//String regExp = "(|"+VALUE+"(|[\\s]+"+COMPARATOR+"[\\s]+(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+)))))";
String regExp = "(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"))))*))))";
@@ -255,7 +257,7 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, parseTableNames(commandInfo, commandPoint), end);
}
- //System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp);
+ System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp);
String end = "";
if (upperCommandString.matches(".*[\\s]+")) {
end = "";
Modified: src/test/java/nl/improved/sqlclient/SQLUtilTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/SQLUtilTest.java 2008-01-27 12:43:39 UTC (rev 231)
+++ src/test/java/nl/improved/sqlclient/SQLUtilTest.java 2008-01-30 11:49:04 UTC (rev 232)
@@ -246,6 +246,17 @@
System.out.println("I: " + info.getStart());
assertEquals("I", info.getStart());
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A."});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLUtil.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ //assertEquals(1, matches.size());
+ assertTrue(matches.contains("A"));
+ System.out.println("I:'" + info.getStart() +"'");
+ assertEquals("", info.getStart());
+
// with other conditions
sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = 'x' AND "});
cursorPos = new Point(sqlCommand.get(0).length(),0);
|
|
From: SVN by r. <sv...@ca...> - 2008-01-27 13:01:52
|
Author: roy
Date: 2008-01-27 13:43:39 +0100 (Sun, 27 Jan 2008)
New Revision: 231
Modified:
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
fix find column matchin tab completion for mysql (lowercase match databases)
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-01-27 12:39:29 UTC (rev 230)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-01-27 12:43:39 UTC (rev 231)
@@ -476,7 +476,7 @@
List<String> returnValues = new ArrayList<String>();
Iterator<String> iTableNames = tableNames.iterator();
while (iTableNames.hasNext()) {
- String tableName = iTableNames.next().trim().toUpperCase();
+ String tableName = DBConnector.getInstance().translateDbVar(iTableNames.next().trim());
try {
ResultSet rs = getConnection().getMetaData().getColumns(getConnection().getCatalog(), DBConnector.getInstance().getSchema(), tableName, "%");
while (rs.next()) {
|
|
From: SVN by r. <sv...@ca...> - 2008-01-27 12:57:39
|
Author: roy
Date: 2008-01-27 13:39:29 +0100 (Sun, 27 Jan 2008)
New Revision: 230
Modified:
src/main/java/nl/improved/sqlclient/
src/main/java/nl/improved/sqlclient/DBConnector.java
src/main/java/nl/improved/sqlclient/QueryExecutor.java
src/main/java/nl/improved/sqlclient/SQLShell.java
Log:
fixes for displaying timestamp/date format on oracle and mysql
Property changes on: src/main/java/nl/improved/sqlclient
___________________________________________________________________
Name: svn:ignore
- .SQLPlusPlus.java.swp
.SQLPlus.java.swp
.DBConnector.java.swp
.SQLUtil.java.swp
.Point.java.swp
.SQLLineWrapper.java.swp
.SQLOutput.java.swp
+ .SQLPlusPlus.java.swp
.SQLPlus.java.swp
.DBConnector.java.swp
.SQLUtil.java.swp
.Point.java.swp
.SQLLineWrapper.java.swp
.SQLOutput.java.swp
.QueryExecutor.java.swp
Modified: src/main/java/nl/improved/sqlclient/DBConnector.java
===================================================================
--- src/main/java/nl/improved/sqlclient/DBConnector.java 2008-01-27 10:41:23 UTC (rev 229)
+++ src/main/java/nl/improved/sqlclient/DBConnector.java 2008-01-27 12:39:29 UTC (rev 230)
@@ -16,10 +16,14 @@
package nl.improved.sqlclient;
import java.sql.Connection;
+import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.sql.Types;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Set;
import java.util.Map;
@@ -36,7 +40,15 @@
import jcurses.widgets.WidgetsConstants;
public final class DBConnector {
+
+ /**
+ * The default formatting pattern for Date or Date-like columns.
+ */
+ private static final String DEFAULT_TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
+ private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
+ private static final String DEFAULT_TIME_PATTERN = "HH:mm:ss";
+
private static DBConnector instance = null;
private boolean tableNamesUppercase = false;
@@ -45,7 +57,10 @@
private Connection activeConnection;
private Statement statement;
+ private boolean dateIsTimeStamp;
+ private QueryExecutor queryExecutor;
+
private DBConnector() {
predefinedConnections = new Hashtable<String, ConnectionSettings>();
Properties dbProperties = new Properties();
@@ -91,6 +106,10 @@
return name.toLowerCase();
}
+ public boolean treatDateAsTimestamp() {
+ return dateIsTimeStamp;
+ }
+
/**
* Return the used schema.
* NOTE: hack because Oracle uses as a schema the current username
@@ -176,6 +195,17 @@
return connect(getPredefinedConnectionSettings(ident), username, password);
}
+ public QueryExecutor getQueryExecutor() {
+ if (queryExecutor == null) {
+ if (dateIsTimeStamp) {
+ queryExecutor = new QueryExecutor(DEFAULT_TIMESTAMP_PATTERN, DEFAULT_TIME_PATTERN, DEFAULT_TIMESTAMP_PATTERN);
+ } else {
+ queryExecutor = new QueryExecutor(DEFAULT_DATE_PATTERN, DEFAULT_TIME_PATTERN, DEFAULT_TIMESTAMP_PATTERN);
+ }
+ }
+ return queryExecutor;
+ }
+
private ConnectionSettings getPredefinedConnectionSettings(String identifier) {
if (predefinedConnections.containsKey(identifier)) {
return predefinedConnections.get(identifier);
@@ -256,10 +286,10 @@
activeConnection = DriverManager.getConnection(settings.getConnectionURL(), username, password);
activeConnection.setAutoCommit(autoCommit);
- // INITIALIZE to uppercase type
+ // INITIALIZE database settings
try {
- ResultSet rs = activeConnection.getMetaData()
- .getTables(activeConnection.getCatalog(), getSchema()
+ DatabaseMetaData metaData = activeConnection.getMetaData();
+ ResultSet rs = metaData.getTables(activeConnection.getCatalog(), getSchema()
, null, new String[]{"TABLE"});
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
@@ -270,6 +300,20 @@
}
}
}
+ rs = metaData.getTypeInfo();
+ dateIsTimeStamp = true;
+ String timestampType = null, dateType =null;
+ while (rs.next() && (timestampType == null && dateType == null)) {
+ if (Types.TIMESTAMP == rs.getInt("DATA_TYPE")) {
+ timestampType = rs.getString("LOCAL_TYPE_NAME");
+ } if (Types.DATE == rs.getInt("DATA_TYPE")) {
+ dateType = rs.getString("LOCAL_TYPE_NAME");
+ }
+ }
+ if (timestampType != null && dateType != null) {
+ dateIsTimeStamp = dateType.equals(timestampType);
+ }
+
} catch(Exception e) {
/* ignore */
}
@@ -286,6 +330,7 @@
activeConnection.close();
activeConnection = null;
}
+ queryExecutor = null;
}
public Connection getConnection() {
Modified: src/main/java/nl/improved/sqlclient/QueryExecutor.java
===================================================================
--- src/main/java/nl/improved/sqlclient/QueryExecutor.java 2008-01-27 10:41:23 UTC (rev 229)
+++ src/main/java/nl/improved/sqlclient/QueryExecutor.java 2008-01-27 12:39:29 UTC (rev 230)
@@ -15,9 +15,12 @@
*/
package nl.improved.sqlclient;
+import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;
import java.util.ArrayList;
@@ -41,19 +44,24 @@
public class QueryExecutor {
/**
- * The default formatting pattern for Date or Date-like columns.
- */
- private static final String DEFAULTDATEPATTERN = "yyyy-MM-dd HH:mm:ss";
- /**
* Used to format dates with unspecified patterns.
*/
- private DateFormat defaultDateFormat;
+ private DateFormat defaultDateFormat, defaultTimeFormat, defaultTimestampFormat;
+ private String timeFormat;
+ private String timestampFormat;
+ private String dateFormat;
+
/**
* Constructor.
*/
- public QueryExecutor() {
- defaultDateFormat = new SimpleDateFormat(DEFAULTDATEPATTERN);
+ QueryExecutor(String dateFormat, String timeFormat, String timestampFormat) {
+ this.dateFormat = dateFormat;
+ this.timeFormat = timeFormat;
+ this.timestampFormat = timestampFormat;
+ defaultDateFormat = new SimpleDateFormat(dateFormat);
+ defaultTimeFormat = new SimpleDateFormat(timeFormat);
+ defaultTimestampFormat = new SimpleDateFormat(timestampFormat);
}
/**
@@ -79,22 +87,6 @@
}
/**
- * Check if a column is a Date or Date-like.
- * @param metadata the metadata describing the resultset
- * @param column the column to check
- * @return true if the column is date-like, false otherwise.
- */
- private boolean isDate(ResultSetMetaData metadata, int column) throws SQLException {
- switch (metadata.getColumnType(column)) {
- case Types.DATE:
- case Types.TIMESTAMP:
- case Types.TIME:
- return true;
- }
- return false;
- }
-
- /**
* 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).
@@ -104,8 +96,13 @@
* @return the width in characters that should be used to display the column.
*/
private int getColumnWidth(ResultSetMetaData metadata, int column) throws SQLException {
- if (isDate(metadata, column)) {
- return DEFAULTDATEPATTERN.length();
+ 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));
@@ -122,11 +119,43 @@
private CharSequence getDisplayValue(ResultSet rset, int column) throws SQLException {
ResultSetMetaData metadata = rset.getMetaData();
+ switch (metadata.getColumnType(column)) {
+ case Types.DATE: {
+ if (dateFormat.equals(timestampFormat)) {// for databases that see date as a timestamp
+ Timestamp date = rset.getTimestamp(column);
+ if (date == null) {
+ return "NULL";
+ }
+ return defaultTimestampFormat.format(date);
+ }
+ Date date = rset.getDate(column);
+ if (date == null) {
+ return "NULL";
+ }
+ return defaultDateFormat.format(date);
+ }
+ case Types.TIMESTAMP: {
+ try {
+ Timestamp date = rset.getTimestamp(column);
+ if (date == null) {
+ return "NULL";
+ }
+ return defaultTimestampFormat.format(rset.getTimestamp(column));
+ } catch(SQLException e) {
+ return "NULL";
+ }
+ }
+ case Types.TIME: {
+ Time date = rset.getTime(column);
+ if (date == null) {
+ return "NULL";
+ }
+ return defaultTimeFormat.format(date);
+ }
+ }
Object colValue = rset.getObject(column);
if (colValue == null) {
return "NULL";
- } else if (isDate(metadata, column)) {
- return defaultDateFormat.format(rset.getTimestamp(column));
} else {
return colValue.toString();
}
Modified: src/main/java/nl/improved/sqlclient/SQLShell.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLShell.java 2008-01-27 10:41:23 UTC (rev 229)
+++ src/main/java/nl/improved/sqlclient/SQLShell.java 2008-01-27 12:39:29 UTC (rev 230)
@@ -94,10 +94,6 @@
private CommandManager commands = new CommandManager();
/**
- * Executor for SQL Queries
- */
- private QueryExecutor querySelector;
- /**
* Executor for SQL Statements
*/
private StatementExecutor queryExecutor;
@@ -648,10 +644,7 @@
*/
protected CharSequence getResult(CharSequence command) throws SQLException {
if (command.length() > "select".length() && "select".equalsIgnoreCase(command.subSequence(0, "create".length()).toString())) {
- if (querySelector == null) {
- querySelector = new QueryExecutor();
- }
- return querySelector.executeQuery(command);
+ return DBConnector.getInstance().getQueryExecutor().executeQuery(command);
}
if (queryExecutor == null) {
queryExecutor = new StatementExecutor();
|
|
From: SVN by r. <sv...@ca...> - 2008-01-27 10:53:02
|
Author: roy
Date: 2008-01-27 11:41:23 +0100 (Sun, 27 Jan 2008)
New Revision: 229
Modified:
src/main/java/nl/improved/sqlclient/DBConnector.java
src/main/java/nl/improved/sqlclient/commands/DescCommand.java
src/main/java/nl/improved/sqlclient/commands/ShowCommand.java
Log:
fix case sensitivity that differs per databse (oracle wants uppercase, mysql wants lowercase.. sigh)
Modified: src/main/java/nl/improved/sqlclient/DBConnector.java
===================================================================
--- src/main/java/nl/improved/sqlclient/DBConnector.java 2008-01-24 21:02:26 UTC (rev 228)
+++ src/main/java/nl/improved/sqlclient/DBConnector.java 2008-01-27 10:41:23 UTC (rev 229)
@@ -17,6 +17,7 @@
import java.sql.Connection;
import java.sql.DriverManager;
+import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
@@ -37,6 +38,7 @@
public final class DBConnector {
private static DBConnector instance = null;
+ private boolean tableNamesUppercase = false;
private Map<String, ConnectionSettings> predefinedConnections;
private String defaultConnector;
@@ -79,6 +81,16 @@
return connect(defaultConnector, null, null);
}
+ public String translateDbVar(String name) {
+ if (name == null) {
+ return null;
+ }
+ if (tableNamesUppercase) {
+ return name.toUpperCase();
+ }
+ return name.toLowerCase();
+ }
+
/**
* Return the used schema.
* NOTE: hack because Oracle uses as a schema the current username
@@ -244,6 +256,24 @@
activeConnection = DriverManager.getConnection(settings.getConnectionURL(), username, password);
activeConnection.setAutoCommit(autoCommit);
+ // INITIALIZE to uppercase type
+ try {
+ ResultSet rs = activeConnection.getMetaData()
+ .getTables(activeConnection.getCatalog(), getSchema()
+ , null, new String[]{"TABLE"});
+ while (rs.next()) {
+ String tableName = rs.getString("TABLE_NAME");
+ for (int i = 0; i < tableName.length(); i++) {
+ if (Character.isLetter(tableName.charAt(i))) {
+ this.tableNamesUppercase = Character.isUpperCase(tableName.charAt(i));
+ break;
+ }
+ }
+ }
+ } catch(Exception e) {
+ /* ignore */
+ }
+ // END INITIALIZE
return activeConnection;
}
Modified: src/main/java/nl/improved/sqlclient/commands/DescCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2008-01-24 21:02:26 UTC (rev 228)
+++ src/main/java/nl/improved/sqlclient/commands/DescCommand.java 2008-01-27 10:41:23 UTC (rev 229)
@@ -40,7 +40,7 @@
if (cmd.endsWith(";")) {
cmd = cmd.substring(0, cmd.length()-1);
}
- String tableName = cmd.substring(cmd.lastIndexOf(' ')).trim().toUpperCase();
+ String tableName = DBConnector.getInstance().translateDbVar(cmd.substring(cmd.lastIndexOf(' ')).trim());
ResultBuilder result = new ResultBuilder();
result.setHorizontalSeparatorEnabled(false);
result.setVerticalSeparator(' ');
@@ -101,11 +101,13 @@
* Return the command string desc.
* @return the command string desc.
*/
+ @Override
public CharSequence getCommandString() {
return "desc";
}
+ @Override
public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) {
List commandInfo = command.getLines();
Modified: src/main/java/nl/improved/sqlclient/commands/ShowCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2008-01-24 21:02:26 UTC (rev 228)
+++ src/main/java/nl/improved/sqlclient/commands/ShowCommand.java 2008-01-27 10:41:23 UTC (rev 229)
@@ -44,7 +44,7 @@
if (subCommand.startsWith("TABLES")) {
if (subCommand.indexOf(' ') > 0) {
String otherCommand = subCommand.substring(subCommand.indexOf(' ')).trim();
- String columnName = otherCommand.substring(otherCommand.indexOf(' ')).trim().toUpperCase();
+ String columnName = DBConnector.getInstance().translateDbVar(otherCommand.substring(otherCommand.indexOf(' ')).trim());
if (otherCommand.toUpperCase().matches("HAVING[\\s]+[A-Z]+.*")) {
try {
ResultSet rs = conn.getMetaData().getColumns(conn.getCatalog()
@@ -103,6 +103,7 @@
* @param commandPoint the cursor position
* @return some tab completion info for the specified command.
*/
+ @Override
public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) {
String commandString = command.getUntrimmedCommandString();
String commandStringUpper = commandString.toUpperCase();
|
|
From: SVN by r. <sv...@ca...> - 2008-01-24 21:51:26
|
Author: roy Date: 2008-01-24 22:02:26 +0100 (Thu, 24 Jan 2008) New Revision: 228 Modified: ChangeLog Log: 0.4 released Modified: ChangeLog =================================================================== --- ChangeLog 2008-01-24 21:01:47 UTC (rev 227) +++ ChangeLog 2008-01-24 21:02:26 UTC (rev 228) @@ -1,4 +1,6 @@ -0.4 +-.5 + +0.4 (2008-01-24) * Improved speed for displaying large( >200<20000 rows) query results * Fix display of time in date column (at least for oracle) * Improve tab comnpletion |
|
From: SVN by r. <sv...@ca...> - 2008-01-24 21:51:06
|
Author: roy Date: 2008-01-24 22:01:47 +0100 (Thu, 24 Jan 2008) New Revision: 227 Modified: README Log: added new ideas Modified: README =================================================================== --- README 2008-01-24 21:00:21 UTC (rev 226) +++ README 2008-01-24 21:01:47 UTC (rev 227) @@ -26,6 +26,10 @@ [See ChangeLog] FUTURE +- dump history to file +- show status (query executing, waiting for input... etc) +- sort commands in help +- add key bindings in help - add 'show tables like' support - login schreen improvements (select all text on focus) - Improve desc table (to show references, etc) @@ -41,4 +45,4 @@ - Large query results don't show (> 300.000 rows?) - 'where x' doesn't autocomplete table names - make ctrl+j (for example) auto insert join -- make ctrl+c abort query +- make ctrl+a abort query (ctr c quits application... don't know how to catch that) |