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