提交 508a3397 authored 作者: Thomas Mueller's avatar Thomas Mueller

CSV tool: new feature to preserve the case sensitivity of column names (option…

CSV tool: new feature to preserve the case sensitivity of column names (option caseSensitiveColumnNames).
上级 c87069fc
......@@ -1702,7 +1702,8 @@ The following options are supported:
""charset"", ""escape"", ""fieldDelimiter"", ""fieldSeparator"",
""lineComment"" (disabled by default),
""lineSeparator"", ""null"", ""rowSeparator"" (not set by default),
""preserveWhitespace"" (true or false; disabled by default).
""preserveWhitespace"" (true or false; disabled by default),
""caseSensitiveColumnNames"" (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 (""'\ '""), and
a backslash needs to be escaped with another backslash (""'\\'"").
......
......@@ -18,7 +18,9 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>PostgreSQL compatibility: LOG(x) is base 10 in the PostgreSQL mode.
<ul><li>CSV tool: new feature to preserve the case sensitivity of column names
(option caseSensitiveColumnNames).
</li><li>PostgreSQL compatibility: LOG(x) is base 10 in the PostgreSQL mode.
</li></ul>
<h2>Version 1.3.164 (2012-02-03)</h2>
......
......@@ -51,6 +51,7 @@ public class Csv implements SimpleRowSource {
private char fieldDelimiter = '\"';
private char fieldSeparatorRead = ',';
private String fieldSeparatorWrite = ",";
private boolean caseSensitiveColumnNames;
private boolean preserveWhitespace;
// TODO change the docs at setLineCommentCharacter
......@@ -370,7 +371,7 @@ public class Csv implements SimpleRowSource {
} else {
if (v.length() == 0) {
v = "COLUMN" + list.size();
} else if (isSimpleColumnName(v)) {
} else if (!caseSensitiveColumnNames && isSimpleColumnName(v)) {
v = v.toUpperCase();
}
list.add(v);
......@@ -647,6 +648,25 @@ public class Csv implements SimpleRowSource {
return fieldSeparatorWrite;
}
/**
* Override the case sensitive column names setting. The default is false.
* If enabled, the case of all column names is always preserved.
*
* @param caseSensitiveColumnNames whether column names are case sensitive
*/
public void setCaseSensitiveColumnNames(boolean caseSensitiveColumnNames) {
this.caseSensitiveColumnNames = caseSensitiveColumnNames;
}
/**
* Get the current case sensitive column names setting.
*
* @return whether column names are case sensitive
*/
public boolean getCaseSensitiveColumnNames() {
return caseSensitiveColumnNames;
}
/**
* Override the field separator for reading. The default is ','.
*
......@@ -853,6 +873,8 @@ public class Csv implements SimpleRowSource {
charset = value;
} else if (isParam(key, "preserveWhitespace")) {
setPreserveWhitespace(Boolean.parseBoolean(value));
} else if (isParam(key, "caseSensitiveColumnNames")) {
setCaseSensitiveColumnNames(Boolean.parseBoolean(value));
} else {
throw DbException.get(ErrorCode.UNSUPPORTED_SETTING_1, key);
}
......
......@@ -52,6 +52,7 @@ public class TestCsv extends TestBase {
}
public void test() throws Exception {
testCaseSensitiveColumnNames();
testWriteResultSetDataType();
testPreserveWhitespace();
testChangeData();
......@@ -88,6 +89,27 @@ public class TestCsv extends TestBase {
assertEquals("TS,N;0101-01-01 12:00:00.0,;", writer.toString());
}
private void testCaseSensitiveColumnNames() throws Exception {
OutputStream out = FileUtils.newOutputStream(getBaseDir() + "/test.tsv", false);
out.write("lower,Mixed,UPPER\n 1 , 2, 3 \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("LOWER", rs.getMetaData().getColumnName(1));
assertEquals("MIXED", rs.getMetaData().getColumnName(2));
assertEquals("UPPER", rs.getMetaData().getColumnName(3));
rs = stat.executeQuery("select * from csvread('" + getBaseDir()
+ "/test.tsv', null, 'caseSensitiveColumnNames=true')");
rs.next();
assertEquals("lower", rs.getMetaData().getColumnName(1));
assertEquals("Mixed", rs.getMetaData().getColumnName(2));
assertEquals("UPPER", rs.getMetaData().getColumnName(3));
conn.close();
}
private void testPreserveWhitespace() throws Exception {
OutputStream out = FileUtils.newOutputStream(getBaseDir() + "/test.tsv", false);
out.write("a,b\n 1 , 2 \n".getBytes());
......@@ -151,9 +173,11 @@ public class TestCsv extends TestBase {
assertEquals("\n", csv.getFieldSeparatorWrite());
assertEquals('"', csv.getLineCommentCharacter());
assertEquals(" \\ ", csv.getLineSeparator());
assertFalse(csv.getPreserveWhitespace());
assertFalse(csv.getCaseSensitiveColumnNames());
charset = csv.setOptions("escape=1x fieldDelimiter=2x fieldSeparator=3x " + "lineComment=4x lineSeparator=5x "
+ "null=6x rowSeparator=7x charset=8x preserveWhitespace=true");
+ "null=6x rowSeparator=7x charset=8x preserveWhitespace=true caseSensitiveColumnNames=true");
assertEquals('1', csv.getEscapeCharacter());
assertEquals('2', csv.getFieldDelimiter());
assertEquals('3', csv.getFieldSeparatorRead());
......@@ -164,6 +188,7 @@ public class TestCsv extends TestBase {
assertEquals("7x", csv.getRowSeparatorWrite());
assertEquals("8x", charset);
assertTrue(csv.getPreserveWhitespace());
assertTrue(csv.getCaseSensitiveColumnNames());
charset = csv.setOptions("escape= fieldDelimiter= fieldSeparator= " + "lineComment= lineSeparator=\r\n "
+ "null=\0 rowSeparator= charset=");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论