提交 4f5f82e7 authored 作者: Thomas Mueller's avatar Thomas Mueller

CSV tool: new feature to disable writing the column header (option writeColumnHeader).

上级 a7b4f8b7
......@@ -1700,11 +1700,12 @@ combined into a space separated key-value pairs, as follows:
""STRINGDECODE('charset=UTF-8 escape=\"" fieldDelimiter=\"" fieldSeparator=, ' ||""
""'lineComment=# lineSeparator=\n null= rowSeparator=')"".
The following options are supported:
""caseSensitiveColumnNames"" (true or false; disabled by default),
""charset"", ""escape"", ""fieldDelimiter"", ""fieldSeparator"",
""lineComment"" (disabled by default),
""lineSeparator"", ""null"", ""rowSeparator"" (not set by default),
""preserveWhitespace"" (true or false; disabled by default),
""caseSensitiveColumnNames"" (true or false; disabled by default).
""writeColumnHeader"" (true or false; enabled 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 (""'\\'"").
......
......@@ -53,6 +53,7 @@ public class Csv implements SimpleRowSource {
private String fieldSeparatorWrite = ",";
private boolean caseSensitiveColumnNames;
private boolean preserveWhitespace;
private boolean writeColumnHeader = true;
// TODO change the docs at setLineCommentCharacter
// TODO also change help.csv
......@@ -84,8 +85,8 @@ public class Csv implements SimpleRowSource {
private int writeResultSet(ResultSet rs) throws SQLException {
try {
ResultSetMetaData meta = rs.getMetaData();
int rows = 0;
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
String[] row = new String[columnCount];
int[] sqlTypes = new int[columnCount];
......@@ -93,7 +94,9 @@ public class Csv implements SimpleRowSource {
row[i] = meta.getColumnLabel(i + 1);
sqlTypes[i] = meta.getColumnType(i + 1);
}
writeRow(row);
if (writeColumnHeader) {
writeRow(row);
}
while (rs.next()) {
for (int i = 0; i < columnCount; i++) {
Object o;
......@@ -836,6 +839,24 @@ public class Csv implements SimpleRowSource {
return preserveWhitespace;
}
/**
* Enable or disable writing the column header.
*
* @param value the new value for the setting
*/
public void setWriteColumnHeader(boolean value) {
this.writeColumnHeader = value;
}
/**
* Whether the column header is written.
*
* @return the current value for the setting
*/
public boolean getWriteColumnHeader() {
return writeColumnHeader;
}
/**
* INTERNAL.
* Parse and set the CSV options.
......@@ -873,10 +894,12 @@ public class Csv implements SimpleRowSource {
charset = value;
} else if (isParam(key, "preserveWhitespace")) {
setPreserveWhitespace(Boolean.parseBoolean(value));
} else if (isParam(key, "writeColumnHeader")) {
setWriteColumnHeader(Boolean.parseBoolean(value));
} else if (isParam(key, "caseSensitiveColumnNames")) {
setCaseSensitiveColumnNames(Boolean.parseBoolean(value));
} else {
throw DbException.get(ErrorCode.UNSUPPORTED_SETTING_1, key);
throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, key);
}
}
return charset;
......
......@@ -52,6 +52,7 @@ public class TestCsv extends TestBase {
}
public void test() throws Exception {
testWriteColumnHeader();
testCaseSensitiveColumnNames();
testWriteResultSetDataType();
testPreserveWhitespace();
......@@ -71,6 +72,19 @@ public class TestCsv extends TestBase {
deleteDb("csv");
}
private void testWriteColumnHeader() throws Exception {
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
stat.execute("call csvwrite('" + getBaseDir() + "/test.tsv', 'select x from dual', 'writeColumnHeader=false')");
String x = IOUtils.readStringAndClose(IOUtils.getReader(FileUtils.newInputStream(getBaseDir() + "/test.tsv")), -1);
assertEquals("\"1\"", x.trim());
stat.execute("call csvwrite('" + getBaseDir() + "/test.tsv', 'select x from dual', 'writeColumnHeader=true')");
x = IOUtils.readStringAndClose(IOUtils.getReader(FileUtils.newInputStream(getBaseDir() + "/test.tsv")), -1);
assertEquals("\"X\"\n\"1\"", x.trim());
conn.close();
}
private void testWriteResultSetDataType() throws Exception {
// Oracle: ResultSet.getString on a date or time column returns a
// strange result (2009-6-30.16.17. 21. 996802000 according to a
......@@ -203,7 +217,7 @@ public class TestCsv extends TestBase {
assertEquals("", charset);
createClassProxy(Csv.class);
assertThrows(ErrorCode.UNSUPPORTED_SETTING_1, csv).setOptions("escape=a error=b");
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, csv).setOptions("escape=a error=b");
assertEquals('a', csv.getEscapeCharacter());
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论