Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
5d7daa14
提交
5d7daa14
authored
9 年前
作者:
nicolas-f
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add function FILE_WRITE with unit test
上级
507d02d0
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
1 行删除
+68
-1
help.csv
h2/src/docsrc/help/help.csv
+10
-0
Function.java
h2/src/main/org/h2/expression/Function.java
+23
-1
TestFunctions.java
h2/src/test/org/h2/test/db/TestFunctions.java
+35
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
5d7daa14
...
...
@@ -3821,6 +3821,16 @@ SELECT LENGTH(FILE_READ('~/.h2.server.properties')) LEN;
SELECT FILE_READ('http://localhost:8182/stylesheet.css', NULL) CSS;
"
"Functions (System)","FILE_WRITE","
FILE_WRITE(Blob , fileNameString)
","
Write the supplied parameter into a file. Return the number of bytes written.
Write access to folder, and admin rights are required to execute this command.
","
SELECT FILE_WRITE('Hello world', '/tmp/hello.txt')) LEN;
"
"Functions (System)","GREATEST","
GREATEST(aValue, bValue [,...])
","
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/expression/Function.java
浏览文件 @
5d7daa14
...
...
@@ -7,6 +7,7 @@ package org.h2.expression;
import
static
org
.
h2
.
util
.
ToChar
.
toChar
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
...
...
@@ -47,6 +48,7 @@ import org.h2.tools.CompressTool;
import
org.h2.tools.Csv
;
import
org.h2.util.AutoCloseInputStream
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.IOUtils
;
import
org.h2.util.JdbcUtils
;
import
org.h2.util.MathUtils
;
import
org.h2.util.New
;
...
...
@@ -116,7 +118,7 @@ public class Function extends Expression implements FunctionCall {
ARRAY_LENGTH
=
217
,
LINK_SCHEMA
=
218
,
GREATEST
=
219
,
LEAST
=
220
,
CANCEL_SESSION
=
221
,
SET
=
222
,
TABLE
=
223
,
TABLE_DISTINCT
=
224
,
FILE_READ
=
225
,
TRANSACTION_ID
=
226
,
TRUNCATE_VALUE
=
227
,
NVL2
=
228
,
DECODE
=
229
,
ARRAY_CONTAINS
=
230
;
NVL2
=
228
,
DECODE
=
229
,
ARRAY_CONTAINS
=
230
,
FILE_WRITE
=
232
;
/**
* Used in MySQL-style INSERT ... ON DUPLICATE KEY UPDATE ... VALUES
...
...
@@ -453,6 +455,8 @@ public class Function extends Expression implements FunctionCall {
2
,
Value
.
NULL
,
false
,
false
,
true
);
addFunction
(
"FILE_READ"
,
FILE_READ
,
VAR_ARGS
,
Value
.
NULL
,
false
,
false
,
true
);
addFunction
(
"FILE_WRITE"
,
FILE_WRITE
,
2
,
Value
.
LONG
,
false
,
false
,
true
);
addFunctionNotDeterministic
(
"TRANSACTION_ID"
,
TRANSACTION_ID
,
0
,
Value
.
STRING
);
addFunctionWithNull
(
"DECODE"
,
DECODE
,
...
...
@@ -1597,6 +1601,24 @@ public class Function extends Expression implements FunctionCall {
}
break
;
}
case
FILE_WRITE:
{
session
.
getUser
().
checkAdmin
();
result
=
ValueNull
.
INSTANCE
;
String
fileName
=
v1
.
getString
();
try
{
FileOutputStream
fileOutputStream
=
new
FileOutputStream
(
fileName
);
InputStream
in
=
v0
.
getInputStream
();
try
{
result
=
ValueLong
.
get
(
IOUtils
.
copyAndClose
(
in
,
fileOutputStream
));
}
finally
{
in
.
close
();
}
}
catch
(
IOException
e
)
{
throw
DbException
.
convertIOException
(
e
,
fileName
);
}
break
;
}
case
TRUNCATE_VALUE:
{
result
=
v0
.
convertPrecision
(
v1
.
getLong
(),
v2
.
getBoolean
());
break
;
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/db/TestFunctions.java
浏览文件 @
5d7daa14
...
...
@@ -6,6 +6,9 @@
package
org
.
h2
.
test
.
db
;
import
java.io.BufferedInputStream
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
...
...
@@ -43,6 +46,7 @@ import org.h2.test.TestBase;
import
org.h2.tools.SimpleResultSet
;
import
org.h2.util.IOUtils
;
import
org.h2.util.New
;
import
org.h2.util.StringUtils
;
import
org.h2.value.Value
;
/**
...
...
@@ -96,6 +100,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testToCharFromText
();
testTranslate
();
testGenerateSeries
();
testFileWrite
();
deleteDb
(
"functions"
);
}
...
...
@@ -622,6 +627,36 @@ public class TestFunctions extends TestBase implements AggregateFunction {
FileUtils
.
delete
(
fileName
);
}
private
void
testFileWrite
()
throws
Exception
{
Connection
conn
=
getConnection
(
"functions"
);
Statement
stat
=
conn
.
createStatement
();
// Copy data into clob table
stat
.
execute
(
"DROP TABLE TEST IF EXISTS"
);
PreparedStatement
pst
=
conn
.
prepareStatement
(
"CREATE TABLE TEST(data clob) AS SELECT ? "
+
"data"
);
Properties
prop
=
System
.
getProperties
();
ByteArrayOutputStream
os
=
new
ByteArrayOutputStream
(
prop
.
size
());
prop
.
store
(
os
,
""
);
pst
.
setBinaryStream
(
1
,
new
ByteArrayInputStream
(
os
.
toByteArray
()));
pst
.
execute
();
os
.
close
();
String
fileName
=
new
File
(
getBaseDir
(),
"test.txt"
).
getPath
();
FileUtils
.
delete
(
fileName
);
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT FILE_WRITE(data, "
+
StringUtils
.
quoteStringSQL
(
fileName
)
+
") len from test"
);
assertTrue
(
rs
.
next
());
assertEquals
(
os
.
size
(),
rs
.
getInt
(
1
));
InputStreamReader
r
=
new
InputStreamReader
(
FileUtils
.
newInputStream
(
fileName
));
// Compare expected content with written file content
String
ps2
=
IOUtils
.
readStringAndClose
(
r
,
-
1
);
assertEquals
(
os
.
toString
(),
ps2
);
conn
.
close
();
FileUtils
.
delete
(
fileName
);
}
/**
* This median implementation keeps all objects in memory.
*/
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论