Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
4f5f82e7
提交
4f5f82e7
authored
3月 12, 2012
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
CSV tool: new feature to disable writing the column header (option writeColumnHeader).
上级
a7b4f8b7
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
43 行增加
和
5 行删除
+43
-5
help.csv
h2/src/docsrc/help/help.csv
+2
-1
Csv.java
h2/src/main/org/h2/tools/Csv.java
+26
-3
TestCsv.java
h2/src/test/org/h2/test/db/TestCsv.java
+15
-1
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
4f5f82e7
...
...
@@ -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; dis
abled by default).
""
writeColumnHeader"" (true or false; en
abled 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 (""'\\'"").
...
...
h2/src/main/org/h2/tools/Csv.java
浏览文件 @
4f5f82e7
...
...
@@ -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
);
}
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
;
...
...
h2/src/test/org/h2/test/db/TestCsv.java
浏览文件 @
4f5f82e7
...
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论