提交 38d97e2f authored 作者: Thomas Mueller's avatar Thomas Mueller

CSVREAD now supports the option 'preserveWhitespace'.

上级 e7f55b1c
......@@ -1681,8 +1681,9 @@ combined into a space separated key-value pairs, as follows:
""'lineComment=# lineSeparator=\n null= rowSeparator=')"".
The following options are supported:
""charset"", ""escape"", ""fieldDelimiter"", ""fieldSeparator"",
""lineComment"" (""#"" for H2 version 1.2, disabled for H2 version 1.3),
""lineSeparator"", ""null"", ""rowSeparator"" (not set by default).
""lineComment"" (disabled by default),
""lineSeparator"", ""null"", ""rowSeparator"" (not set by default),
""preserveWhitespace"" (true or false; disabled by default).
For a newline or other special character, use STRINGDECODE as in the example above.
A space needs to be escaped with a backslash (""'\ '"").
A backslash needs to be escaped with another backslash (""'\\'"").
......
......@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>Recursive queries with many rows could throw an IndexOutOfBoundsException.
<ul><li>CSVREAD now supports the option 'preserveWhitespace'.
</li><li>Recursive queries with many rows could throw an IndexOutOfBoundsException.
</li><li>The auto-server mode can't be combined with an in-memory database.
This invalid combination wasn't detected so far.
Now trying to open a database in this way fails.
......
......@@ -50,6 +50,7 @@ public class Csv implements SimpleRowSource {
private char fieldDelimiter = '\"';
private char fieldSeparatorRead = ',';
private String fieldSeparatorWrite = ",";
private boolean preserveWhitespace;
// TODO change the docs at setLineCommentCharacter
// TODO also change help.csv
......@@ -514,9 +515,12 @@ public class Csv implements SimpleRowSource {
}
}
String s = new String(inputBuffer, inputBufferStart, inputBufferPos - inputBufferStart - 1);
if (!preserveWhitespace) {
s = s.trim();
}
inputBufferStart = -1;
// check un-delimited value for nullString
return readNull(s.trim());
return readNull(s);
}
}
}
......@@ -779,6 +783,24 @@ public class Csv implements SimpleRowSource {
return nullString;
}
/**
* Enable or disable preserving whitespace in unquoted text.
*
* @param value the new value for the setting
*/
public void setPreserveWhitespace(boolean value) {
this.preserveWhitespace = value;
}
/**
* Whether whitespace in unquoted text is preserved.
*
* @return the current value for the setting
*/
public boolean getPreserveWhitespace() {
return preserveWhitespace;
}
/**
* INTERNAL.
* Parse and set the CSV options.
......@@ -788,7 +810,6 @@ public class Csv implements SimpleRowSource {
*/
public String setOptions(String options) {
String charset = null;
// options = StringUtils.javaDecode(options);
String[] keyValuePairs = StringUtils.arraySplit(options, ' ', false);
for (String pair : keyValuePairs) {
if (pair.length() == 0) {
......@@ -815,6 +836,8 @@ public class Csv implements SimpleRowSource {
setRowSeparatorWrite(value);
} else if (isParam(key, "charset", "characterSet")) {
charset = value;
} else if (isParam(key, "preserveWhitespace")) {
setPreserveWhitespace(Boolean.parseBoolean(value));
} else {
throw DbException.get(ErrorCode.UNSUPPORTED_SETTING_1, key);
}
......
......@@ -54,6 +54,7 @@ public class TestCsv extends TestBase {
}
public void test() throws Exception {
testPreserveWhitespace();
testChangeData();
testOptions();
testPseudoBom();
......@@ -70,6 +71,24 @@ public class TestCsv extends TestBase {
deleteDb("csv");
}
private void testPreserveWhitespace() throws Exception {
OutputStream out = IOUtils.openFileOutputStream(getBaseDir()+"/test.tsv", false);
out.write("a,b\n 1 , 2 \n".getBytes());
out.close();
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
ResultSet rs;
rs = stat.executeQuery("select * from csvread('"+getBaseDir()+"/test.tsv')");
rs.next();
assertEquals("1", rs.getString(1));
assertEquals("2", rs.getString(2));
rs = stat.executeQuery("select * from csvread('"+getBaseDir()+"/test.tsv', null, 'preserveWhitespace=true')");
rs.next();
assertEquals(" 1 ", rs.getString(1));
assertEquals(" 2 ", rs.getString(2));
conn.close();
}
private void testChangeData() throws Exception {
OutputStream out = IOUtils.openFileOutputStream(getBaseDir()+"/test.tsv", false);
out.write("a,b,c,d,e,f,g\n1".getBytes());
......@@ -102,6 +121,7 @@ public class TestCsv extends TestBase {
assertEquals(',', csv.getFieldSeparatorRead());
assertEquals(",", csv.getFieldSeparatorWrite());
assertEquals(Constants.VERSION_MINOR == 3 ? 0 : '#', csv.getLineCommentCharacter());
assertEquals(false, csv.getPreserveWhitespace());
String charset;
......@@ -116,7 +136,7 @@ public class TestCsv extends TestBase {
charset = csv.setOptions("escape=1x fieldDelimiter=2x fieldSeparator=3x " +
"lineComment=4x lineSeparator=5x " +
"null=6x rowSeparator=7x charset=8x");
"null=6x rowSeparator=7x charset=8x preserveWhitespace=true");
assertEquals('1', csv.getEscapeCharacter());
assertEquals('2', csv.getFieldDelimiter());
assertEquals('3', csv.getFieldSeparatorRead());
......@@ -126,6 +146,7 @@ public class TestCsv extends TestBase {
assertEquals("6x", csv.getNullString());
assertEquals("7x", csv.getRowSeparatorWrite());
assertEquals("8x", charset);
assertTrue(csv.getPreserveWhitespace());
charset = csv.setOptions("escape= fieldDelimiter= fieldSeparator= " +
"lineComment= lineSeparator=\r\n " +
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论