|
From: SVN by r. <sv...@ca...> - 2008-08-19 08:01:46
|
Author: roy
Date: 2008-08-19 10:01:34 +0200 (Tue, 19 Aug 2008)
New Revision: 285
Modified:
src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java
src/main/java/nl/improved/sqlclient/SQLCommand.java
Log:
use stringbuffer instead of stringbuilder since commands are used in a multithreaded environment
Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java
===================================================================
--- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-08-06 20:32:52 UTC (rev 284)
+++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-08-19 08:01:34 UTC (rev 285)
@@ -55,6 +55,9 @@
import javax.xml.transform.stream.StreamResult;
import nl.improved.sqlclient.DBConnector.ConnectionSettings;
import nl.improved.sqlclient.commands.*;
+import nl.improved.sqlclient.history.HistoryPersister;
+import nl.improved.sqlclient.history.exception.CouldNotLoadHistoryException;
+import nl.improved.sqlclient.history.exception.CouldNotSaveHistoryException;
import nl.improved.sqlclient.util.LimitedArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -70,6 +73,7 @@
*/
public abstract class AbstractSQLShellWindow {
+ private String ident;
/**
* The current command thread executing a SQLShell command.
*/
@@ -262,7 +266,7 @@
joinLine();
}
} else {
- StringBuilder tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
+ StringBuffer tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
if (cursorPosition.x > 0) {
tmp.deleteCharAt(cursorPosition.x-1);
cursorPosition.x--;
@@ -278,9 +282,9 @@
@Override
public void execute() {
Point cursorPosition = screen.getCursorPosition();
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ StringBuffer lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
if (cursorPosition.x < lineBuffer.length()) {
- StringBuilder tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
+ StringBuffer tmp = getEditableCommand().getEditableLines().get(cursorPosition.y);
tmp.deleteCharAt(cursorPosition.x);
}
}
@@ -326,7 +330,7 @@
}
return;
}
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ StringBuffer lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
int previousBreak = SQLUtil.getLastBreakIndex(lineBuffer.substring(0, cursorPosition.x-1));
if (lineBuffer.charAt(previousBreak) == ' ' || lineBuffer.charAt(previousBreak) == '\t') {
previousBreak++;
@@ -344,11 +348,11 @@
public void execute() {
Point cursorPosition = screen.getCursorPosition();
if (cursorPosition.x > 0) {
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ StringBuffer 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);
+ StringBuffer lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
if (lineBuffer.length() == 0) {
commandLines.getEditableLines().remove(cursorPosition.y);
}
@@ -446,8 +450,28 @@
t.start();
}
+ /**
+ * Returns the current command history.
+ * @return
+ */
+ public List<SQLCommand> getCommandHistory() {
+ return commandHistory;
+ }
+ public int getCommandIndex() {
+ return commandIndex;
+ }
+
/**
+ * Change the current command index.
+ * @param i
+ */
+ public void setCommandIndex(int i) {
+ this.commandIndex = i;
+ }
+
+
+ /**
* Return the visible screen width.
* @return the visible screen width.
*/
@@ -458,6 +482,7 @@
*/
public abstract int getScreenHeight();
+
/**
* Repaint the screen.
*/
@@ -530,10 +555,10 @@
/**
* Add a new line to the command lines buffer.
*/
- private StringBuilder newLine() {
+ private StringBuffer newLine() {
Point cursorPosition = screen.getCursorPosition();
cursorPosition.x = 0;
- StringBuilder newLine = new StringBuilder();
+ StringBuffer newLine = new StringBuffer();
getEditableCommand().getEditableLines().add(newLine);
cursorPosition.y = commandLines.getLines().size()-1;
return newLine;
@@ -597,6 +622,7 @@
commandHistory.add(tmpLines);
commandLines = tmpLines;
}
+ commandHistory.remove(commandIndex);
}
if (!commandLines.getCommandString().equals("")) {
commandLines = new SQLCommand();
@@ -628,8 +654,8 @@
newLine(); // TODO Fix return in middle of an other line
} else {
Point cursorPosition = screen.getCursorPosition();
- List<StringBuilder> editableLines = getEditableCommand().getEditableLines();
- StringBuilder currentLine = editableLines.get(cursorPosition.y);
+ List<StringBuffer> editableLines = getEditableCommand().getEditableLines();
+ StringBuffer currentLine = editableLines.get(cursorPosition.y);
if (cursorPosition.x > currentLine.length()) {
for (int i = currentLine.length(); i < cursorPosition.x; i++) {
currentLine.append(' ');
@@ -649,18 +675,18 @@
// check if there are enough 'next' lines
// if not.. add one
if (editableLines.size()-1 == cursorPosition.y) {
- StringBuilder newLine = new StringBuilder();
+ StringBuffer newLine = new StringBuffer();
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) > screen.MAX_LINE_LENGTH) {
- StringBuilder newLine = new StringBuilder();
+ StringBuffer newLine = new StringBuffer();
editableLines.add(cursorPosition.y+1, newLine);
}
// fetch the next line
- StringBuilder nextLine = editableLines.get(cursorPosition.y+1);
+ StringBuffer nextLine = editableLines.get(cursorPosition.y+1);
// if the nextline already has some text.. add a space in front of it
// if there is not already a 'breaking character' there
if (nextLine.length() > 0 && ! SQLUtil.isBreakCharacter(nextLine.charAt(0))) {
@@ -706,7 +732,7 @@
commandHistory.add(commandLines);
}
for (int i = 0; i < tmp.size(); i++) {
- commandLines.getEditableLines().add(new StringBuilder(tmp.get(i)));
+ commandLines.getEditableLines().add(new StringBuffer(tmp.get(i)));
}
commandIndex = commandHistory.size()-1;
}
@@ -754,7 +780,8 @@
e.printStackTrace(new PrintWriter(sw));
sw.flush();
lastExceptionDetails = sw.toString();
- //output(sw.toString());
+ //output(lastExceptionDetails);
+ //repaint();
}
/**
@@ -838,7 +865,7 @@
match = DBConnector.getInstance().translateDbVar(match);
if (sub.length() > 0 && !match.startsWith(sub)) { // case insensitive change
Point cursorPosition = screen.getCursorPosition();
- List<StringBuilder> lines = getEditableCommand().getEditableLines();
+ List<StringBuffer> lines = getEditableCommand().getEditableLines();
if (lines.get(cursorPosition.y).length() >=sub.length()) {
cursorPosition.x-=sub.length();
lines.get(cursorPosition.y).delete(cursorPosition.x, cursorPosition.x + sub.length());
@@ -956,13 +983,13 @@
private List<CharSequence> getLines(CharSequence text) {
int maxWidth = getScreenWidth();
List<CharSequence> list = new ArrayList<CharSequence>();
- StringBuilder buffer = new StringBuilder();
+ StringBuffer buffer = new StringBuffer();
for (int i=0; i<text.length(); i++) {
char c = text.charAt(i);
if (c=='\n' || buffer.length() == maxWidth) {
String line = buffer.toString();
list.add(line);
- buffer = new StringBuilder();
+ buffer = new StringBuffer();
if (c != '\n') {
buffer.append(c);
}
@@ -981,7 +1008,7 @@
/**
* Convert a list to a presentable text devided into multiple columns.
*/
- private StringBuilder toColumns(List<? extends CharSequence> values) {
+ private StringBuffer toColumns(List<? extends CharSequence> values) {
int maxWidth = 0;
Iterator<? extends CharSequence> iValues = values.iterator();
while (iValues.hasNext()) {
@@ -989,7 +1016,7 @@
}
maxWidth+=2;// add extra space
int nrOfColumns = (getScreenWidth()) / maxWidth;
- StringBuilder returnValue = new StringBuilder();
+ StringBuffer returnValue = new StringBuffer();
for (int row = 0; row < values.size(); row+=nrOfColumns) {
for (int col = 0; col < nrOfColumns && row + col < values.size(); col++) {
returnValue.append(values.get(row+col) + screen.getEmptyLine().substring(0, maxWidth - values.get(row+col).length()));
@@ -1016,14 +1043,21 @@
if (cmdString.length() > 0 && cmdString.charAt(cmdString.length()-1) == ';') {
cmdString = cmdString.substring(0, cmdString.length()-1);
}
- if (connect(cmdString) != null) {
- return "Connected.\n\n";
- } else {
+ if (connect(cmdString) == null) {
return "Connect failed. Unknown reason\n\n";
}
} catch(SQLException e) {
throw new IllegalStateException("Failed to connect: " + e.getMessage(), e);
}
+ try {
+ int tot = HistoryPersister.loadHistory(AbstractSQLShellWindow.this, ident,
+ getScreen().MAX_LINE_LENGTH, getScreen().MAX_LINE_LENGTH);
+ return "Connected and history loaded (" + tot + " command(s)).\n\n";
+ } catch (CouldNotLoadHistoryException e) {
+ // this.shell.setCommandIndex(0);
+ return "Connected (no history loaded).\n\n";
+ }
+
}
@Override
public CharSequence getCommandString() {
@@ -1056,11 +1090,14 @@
Iterator<String> idents = DBConnector.getInstance().getPredefinedConnectionIdentifiers().iterator();
while (idents.hasNext()) {
buf.append(' ');
- String ident = idents.next();
- buf.append(ident);
- if (ident.equals(DBConnector.getInstance().getDefaultIdentifier())) {
+ String availableIdent = idents.next();
+ buf.append(availableIdent);
+ if (availableIdent.equals(DBConnector.getInstance().getDefaultIdentifier())) {
buf.append(" *");
}
+ if (availableIdent.equals(ident)) {
+ buf.append(" (connected)");
+ }
if (idents.hasNext()) {
buf.append('\n');
}
@@ -1131,7 +1168,7 @@
public Connection connect(String connectString) throws SQLException {
//Shortcut - no need to try and parse everything if we already know we're empty
DBConnector dbConnector = DBConnector.getInstance();
- String ident = dbConnector.getDefaultIdentifier();
+ ident = dbConnector.getDefaultIdentifier();
String username = null;
String password = null;
if (connectString != null && connectString.trim().length() > 0) {
@@ -1170,12 +1207,23 @@
/**
* Command that enables the user to close a connection.
*/
- private static class DisConnectCommand implements Command {
+ private class DisConnectCommand implements Command {
+
@Override
public CharSequence execute(SQLCommand cmd) {
try {
DBConnector.getInstance().disconnect();
- return "Disconnected.\n\n";
+ if (ident != null) {
+ try {
+ HistoryPersister.saveHistory(AbstractSQLShellWindow.this, ident, true);
+ ident = null;
+ return "Disconnected (history saved).\n\n";
+ } catch (CouldNotSaveHistoryException e) {
+ return "Disconnected, could not save history: " + e.getMessage()
+ + "\n\n";
+ }
+ }
+ return "Disconnected";
} catch(SQLException e) {
throw new IllegalStateException("Failed to disconnect: " + e.getMessage(), e);
}
@@ -1215,11 +1263,19 @@
private class HistoryCommand implements Command {
@Override
public CharSequence execute(SQLCommand command) {
- StringBuilder returnValue = new StringBuilder();
+ StringBuffer returnValue = new StringBuffer();
Iterator<SQLCommand> iCommands = commandHistory.iterator();
+ returnValue.append("\n**** History Overview ****\n");
+ int index = 0;
while (iCommands.hasNext()) {
SQLCommand cmd = iCommands.next();
- returnValue.append(cmd.getCommandString());
+ String cmdString = cmd.getCommandString();
+ if (cmdString.trim().length() == 0) {
+ continue;
+ }
+ returnValue.append(++index);
+ returnValue.append("> ");
+ returnValue.append(cmdString);
returnValue.append('\n');
}
return returnValue;
@@ -1266,6 +1322,7 @@
@Override
public CharSequence execute(SQLCommand command) {
run = false;
+ new DisConnectCommand().execute(null);
close();
return "Application terminated.";
}
@@ -1315,7 +1372,7 @@
if (cmdString.length() > 0) {
if (cmdString.startsWith("-k")) {
String keyword = cmdString.subSequence(cmdString.indexOf(" "), cmdString.length()).toString().trim();
- StringBuilder returnValue = new StringBuilder();
+ StringBuffer returnValue = new StringBuffer();
Iterator<Command> iCommands = commands.getCommands().iterator();
while (iCommands.hasNext()) {
Command cmd= iCommands.next();
@@ -1348,7 +1405,7 @@
}
// default print all commands
// TODO iterate
- StringBuilder returnValue = new StringBuilder();
+ StringBuffer returnValue = new StringBuffer();
returnValue.append("Available key mappings:\n");
int max = 0;
List<KeyAction> keys = new ArrayList<KeyAction>(actionKeys.values());
@@ -1768,7 +1825,7 @@
fin = new FileInputStream(toFileName(command.substring(1)));
output("Reading file: "+ toFileName(command.substring(1)));
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
- StringBuilder cmd = new StringBuilder();
+ StringBuffer cmd = new StringBuffer();
String line;
while ( ((line = reader.readLine()) != null)) {
if (cancelled) {
@@ -1784,7 +1841,7 @@
currentCommand = createCommand(commandString);
output(commandString);
output(currentCommand.execute(new InputCommand(commandString)));
- cmd=new StringBuilder();
+ cmd=new StringBuffer();
new Thread() {
@Override
public void run() {
@@ -1869,13 +1926,13 @@
* will eventually execute the Connect command.
*/
private static class InputCommand extends SQLCommand {
- private StringBuilder command;
+ private StringBuffer command;
public InputCommand(String command) {
- this.command = new StringBuilder(command);
+ this.command = new StringBuffer(command);
}
- public InputCommand(StringBuilder command) {
+ public InputCommand(StringBuffer command) {
this.command = command;
}
@@ -1884,12 +1941,12 @@
return command.toString();
}
@Override
- public List<StringBuilder> getEditableLines() {
- return Arrays.asList(new StringBuilder[]{command});
+ public List<StringBuffer> getEditableLines() {
+ return Arrays.asList(new StringBuffer[]{command});
}
@Override
public List<? extends CharSequence> getLines() {
- return Arrays.asList(new StringBuilder[]{command});
+ return Arrays.asList(new StringBuffer[]{command});
}
}
@@ -1976,9 +2033,9 @@
private void joinLine() {
Point cursorPosition = screen.getCursorPosition();
- StringBuilder line = getEditableCommand().getEditableLines().remove(cursorPosition.y);
+ StringBuffer line = getEditableCommand().getEditableLines().remove(cursorPosition.y);
cursorPosition.y--;
- StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
+ StringBuffer lineBuffer = commandLines.getEditableLines().get(cursorPosition.y);
cursorPosition.x = lineBuffer.length();
lineBuffer.append(line);
}
Modified: src/main/java/nl/improved/sqlclient/SQLCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/SQLCommand.java 2008-08-06 20:32:52 UTC (rev 284)
+++ src/main/java/nl/improved/sqlclient/SQLCommand.java 2008-08-19 08:01:34 UTC (rev 285)
@@ -6,16 +6,16 @@
public class SQLCommand {
- private List<StringBuilder> commandLines;
+ private List<StringBuffer> commandLines;
/**
* Constructor.
*/
public SQLCommand() {
- commandLines = new ArrayList<StringBuilder>();
+ commandLines = new ArrayList<StringBuffer>();
}
- public List<StringBuilder> getEditableLines() {
+ public List<StringBuffer> getEditableLines() {
return commandLines;
}
public List<? extends CharSequence> getLines() {
@@ -28,7 +28,7 @@
* @return the part of the command that is before the cursor position.
*/
public CharSequence getSubCommand(Point cursorPosition) {
- StringBuilder commandBuffer = new StringBuilder();
+ StringBuffer commandBuffer = new StringBuffer();
for (int i = 0; i <= cursorPosition.y; i++) {
if (i == cursorPosition.y) {
commandBuffer.append(commandLines.get(i).substring(0, cursorPosition.x));
@@ -47,7 +47,7 @@
}
public String getUntrimmedCommandString() {
- StringBuilder returnValue = new StringBuilder();
+ StringBuffer returnValue = new StringBuffer();
Iterator<? extends CharSequence> parts = getLines().iterator();
while (parts.hasNext()) {
if (returnValue.length() > 0 && returnValue.charAt(returnValue.length()-1) != '\n') {
|