|
From: SVN by r. <sv...@ca...> - 2009-12-17 15:47:21
|
Author: roy
Date: 2009-12-17 16:47:07 +0100 (Thu, 17 Dec 2009)
New Revision: 444
Modified:
src/main/java/nl/improved/sqlclient/commands/DumpCommand.java
Log:
added abort method
added encoding fixes
Modified: src/main/java/nl/improved/sqlclient/commands/DumpCommand.java
===================================================================
--- src/main/java/nl/improved/sqlclient/commands/DumpCommand.java 2009-12-17 15:45:34 UTC (rev 443)
+++ src/main/java/nl/improved/sqlclient/commands/DumpCommand.java 2009-12-17 15:47:07 UTC (rev 444)
@@ -17,8 +17,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
@@ -35,6 +33,8 @@
public class DumpCommand extends ReadDumpCommand {
private String fileName;
+ private int warnings = 0;
+ private boolean abort;
public DumpCommand(AbstractSQLShellWindow window) {
super(window);
@@ -77,6 +77,8 @@
@Override
public CommandResult execute(SQLCommand cmd) {
+ abort = false;
+ warnings = 0;
String[] result = getParsedStrings(cmd);
String dumpFileName = result[0];
String tableName = result[1];
@@ -102,7 +104,7 @@
// SAX2.0 ContentHandler.
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
- serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
+ serializer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
//serializer.setOutputProperty(OutputKeys.INDENT,"no");
hd.setResult(streamResult);
hd.startDocument();
@@ -114,7 +116,7 @@
stmt = c.createStatement();
rs = stmt.executeQuery(query);
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
- while (rs.next()) {
+ while (rs.next() && !abort) {
atts.clear();
hd.startElement("", "", "row", atts);
ResultSetMetaData metaData = rs.getMetaData();
@@ -159,7 +161,11 @@
hd.startElement("", "", "col", atts);
String value = rs.getString(col);
if (value != null) {
- hd.characters(value.toCharArray(), 0, value.length());
+ //hd.startCDATA();;
+ //hd.characters(value.toCharArray(), 0, value.length());
+ //hd.endCDATA();
+ char[] chars = trimNonReadableChars(value);
+ hd.characters(chars, 0, chars.length);
}
}
hd.endElement("", "", "col");
@@ -172,7 +178,11 @@
}
hd.endElement("", "", "dump");
hd.endDocument();
-
+ if (abort) {
+ return new SimpleCommandResult(true, "Dump to " + fileName + " aborted. (" + rowCount + " rows written, "+warnings+" warnings)");
+ } else {
+ return new SimpleCommandResult(true, "Dump to " + fileName + " done. (" + rowCount + " rows written, "+warnings+" warnings)");
+ }
} catch (SAXException ex) {
throw new IllegalStateException("Failed to create dump (" + fileName + "): " + ex.toString(), ex);
} catch (TransformerConfigurationException e) {
@@ -192,7 +202,6 @@
out.close();
}
}
- return new SimpleCommandResult(true, "Dump to " + fileName + " done. (" + rowCount + " rows written)");
}
@Override
@@ -225,11 +234,36 @@
@Override
public boolean abort() {
+ this.abort = true;
return false;
}
@Override
public boolean backgroundProcessSupported() {
- return false;
+ return true;
}
+
+ private char[] trimNonReadableChars(String chars) {
+ char[] ch = new char[chars.length()];
+ int index = 0;
+ for (int i = 0; i < chars.length(); i++) {
+ ch[index] = chars.charAt(i);
+ if (ch[index] != 19 && ch[index] != 24 && ch[index] != 25 && ch[index] != 26
+ && ch[index] != 27) {
+ } else if (ch[index] == 28 || ch[index] == 29) {
+ ch[index]='\'';
+ index++;
+ } else {
+ index++;
+ }
+ }
+ if (index == chars.length()) {
+ return ch;
+ }
+ warnings += (chars.length() - index);
+ char[] result = new char[index];
+ System.arraycopy(ch, 0, result, 0, index);
+ return result;
+
+ }
}
|