提交 d69ecdc1 authored 作者: Thomas Mueller's avatar Thomas Mueller

Csv.write now formats date, time, and timestamp values using java.sql.Date /…

Csv.write now formats date, time, and timestamp values using java.sql.Date / Time / Timestamp.toString(). Previously, ResultSet.getString() was used, which didn't work well for Oracle.
上级 9a5ed683
......@@ -89,13 +89,29 @@ public class Csv implements SimpleRowSource {
int rows = 0;
int columnCount = meta.getColumnCount();
String[] row = new String[columnCount];
int[] sqlTypes = new int[columnCount];
for (int i = 0; i < columnCount; i++) {
row[i] = meta.getColumnLabel(i + 1);
sqlTypes[i] = meta.getColumnType(i + 1);
}
writeRow(row);
while (rs.next()) {
for (int i = 0; i < columnCount; i++) {
row[i] = rs.getString(i + 1);
Object o;
switch (sqlTypes[i]) {
case Types.DATE:
o = rs.getDate(i + 1);
break;
case Types.TIME:
o = rs.getTime(i + 1);
break;
case Types.TIMESTAMP:
o = rs.getTimestamp(i + 1);
break;
default:
o = rs.getString(i + 1);
}
row[i] = o == null ? null : o.toString();
}
writeRow(row);
rows++;
......
......@@ -13,6 +13,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
......@@ -51,6 +52,7 @@ public class TestCsv extends TestBase {
}
public void test() throws Exception {
testWriteResultSetDataType();
testPreserveWhitespace();
testChangeData();
testOptions();
......@@ -68,6 +70,24 @@ public class TestCsv extends TestBase {
deleteDb("csv");
}
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
// customer)
StringWriter writer = new StringWriter();
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select timestamp '-100-01-01 12:00:00.0' ts, null n");
Csv csv = Csv.getInstance();
csv.setFieldDelimiter((char) 0);
csv.setLineSeparator(";");
csv.write(writer, rs);
conn.close();
// getTimestamp().getString() needs to be used (not for H2, but for
// Oracle)
assertEquals("TS,N;0101-01-01 12:00:00.0,;", writer.toString());
}
private void testPreserveWhitespace() throws Exception {
OutputStream out = FileUtils.newOutputStream(getBaseDir() + "/test.tsv", false);
out.write("a,b\n 1 , 2 \n".getBytes());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论