Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
f2759786
提交
f2759786
authored
4月 09, 2014
作者:
noelgrandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Oracle-compatible TRANSLATE function, patch by Eric Chatellier.
上级
93430bf7
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
96 行增加
和
2 行删除
+96
-2
help.csv
h2/src/docsrc/help/help.csv
+8
-0
changelog.html
h2/src/docsrc/html/changelog.html
+1
-0
Function.java
h2/src/main/org/h2/expression/Function.java
+44
-1
TestFunctions.java
h2/src/test/org/h2/test/db/TestFunctions.java
+42
-0
dictionary.txt
h2/src/tools/org/h2/build/doc/dictionary.txt
+1
-1
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
f2759786
...
...
@@ -3361,6 +3361,14 @@ Oracle-compatible TO_CHAR function that can format a timestamp, a number, or tex
CALL TO_CHAR(TIMESTAMP '2010-01-01 00:00:00', 'DD MON, YYYY')
"
"Functions (String)","TRANSLATE","
TRANSLATE(value , searchString, replacementString]])
","
Oracle-compatible TRANSLATE function that replaces a sequence of characters in a string with another set of characters.
","
CALL TRANSLATE('Hello world', 'eo', 'EO')
"
"Functions (Time and Date)","CURRENT_DATE","
{ CURRENT_DATE [ () ] | CURDATE() | SYSDATE | TODAY }
","
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
f2759786
...
...
@@ -46,6 +46,7 @@ Change Log
<h2>
Next Version (unreleased)
</h2>
<ul><li>
Support the data type "DATETIME2" as an alias for "DATETIME", for MS SQL Server compatibility.
</li><li>
Add Oracle-compatible TRANSLATE function, patch by Eric Chatellier.
</li></ul>
<h2>
Version 1.3.176 (2014-04-05)
</h2>
...
...
h2/src/main/org/h2/expression/Function.java
浏览文件 @
f2759786
...
...
@@ -7,6 +7,7 @@
package
org
.
h2
.
expression
;
import
static
org
.
h2
.
util
.
ToChar
.
toChar
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
...
...
@@ -94,7 +95,7 @@ public class Function extends Expression implements FunctionCall {
STRINGDECODE
=
80
,
STRINGTOUTF8
=
81
,
UTF8TOSTRING
=
82
,
XMLATTR
=
83
,
XMLNODE
=
84
,
XMLCOMMENT
=
85
,
XMLCDATA
=
86
,
XMLSTARTDOC
=
87
,
XMLTEXT
=
88
,
REGEXP_REPLACE
=
89
,
RPAD
=
90
,
LPAD
=
91
,
CONCAT_WS
=
92
,
TO_CHAR
=
93
;
LPAD
=
91
,
CONCAT_WS
=
92
,
TO_CHAR
=
93
,
TRANSLATE
=
94
;
public
static
final
int
CURDATE
=
100
,
CURTIME
=
101
,
DATE_ADD
=
102
,
DATE_DIFF
=
103
,
DAY_NAME
=
104
,
DAY_OF_MONTH
=
105
,
...
...
@@ -299,6 +300,7 @@ public class Function extends Expression implements FunctionCall {
addFunction
(
"RPAD"
,
RPAD
,
VAR_ARGS
,
Value
.
STRING
);
addFunction
(
"LPAD"
,
LPAD
,
VAR_ARGS
,
Value
.
STRING
);
addFunction
(
"TO_CHAR"
,
TO_CHAR
,
VAR_ARGS
,
Value
.
STRING
);
addFunction
(
"TRANSLATE"
,
TRANSLATE
,
3
,
Value
.
STRING
);
// date
addFunctionNotDeterministic
(
"CURRENT_DATE"
,
CURRENT_DATE
,
...
...
@@ -1401,6 +1403,14 @@ public class Function extends Expression implements FunctionCall {
database
.
getMode
().
treatEmptyStringsAsNull
);
}
break
;
case
TRANSLATE:
{
String
matching
=
v1
.
getString
();
String
replacement
=
v2
.
getString
();
result
=
ValueString
.
get
(
translate
(
v0
.
getString
(),
matching
,
replacement
),
database
.
getMode
().
treatEmptyStringsAsNull
);
break
;
}
case
H2VERSION:
result
=
ValueString
.
get
(
Constants
.
getVersion
(),
database
.
getMode
().
treatEmptyStringsAsNull
);
...
...
@@ -1910,6 +1920,39 @@ public class Function extends Expression implements FunctionCall {
return
e
;
}
/** This is the org.apache.commons.lang3.StringUtils
* #replaceChars(String, String, String) implementation
*/
private
static
String
translate
(
String
str
,
String
searchChars
,
String
replaceChars
)
{
if
(
str
==
null
||
str
.
length
()
==
0
||
searchChars
==
null
||
searchChars
.
length
()
==
0
)
{
return
str
;
}
if
(
replaceChars
==
null
)
{
replaceChars
=
""
;
// EMPTY
}
boolean
modified
=
false
;
int
replaceCharsLength
=
replaceChars
.
length
();
int
strLength
=
str
.
length
();
StringBuilder
buf
=
new
StringBuilder
(
strLength
);
for
(
int
i
=
0
;
i
<
strLength
;
i
++)
{
char
ch
=
str
.
charAt
(
i
);
int
index
=
searchChars
.
indexOf
(
ch
);
if
(
index
>=
0
)
{
modified
=
true
;
if
(
index
<
replaceCharsLength
)
{
buf
.
append
(
replaceChars
.
charAt
(
index
));
}
}
else
{
buf
.
append
(
ch
);
}
}
if
(
modified
)
{
return
buf
.
toString
();
}
return
str
;
}
private
static
double
roundmagic
(
double
d
)
{
if
((
d
<
0.0000000000001
)
&&
(
d
>
-
0.0000000000001
))
{
return
0.0
;
...
...
h2/src/test/org/h2/test/db/TestFunctions.java
浏览文件 @
f2759786
...
...
@@ -92,6 +92,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testToCharFromDateTime
();
testToCharFromNumber
();
testToCharFromText
();
testTranslate
();
// TODO
// testCachingOfDeterministicFunctionAlias();
...
...
@@ -1162,6 +1163,47 @@ public class TestFunctions extends TestBase implements AggregateFunction {
conn
.
close
();
}
private
void
testTranslate
()
throws
SQLException
{
Connection
conn
=
getConnection
(
"functions"
);
Statement
stat
=
conn
.
createStatement
();
String
createSQL
=
"CREATE TABLE testTranslate(id BIGINT, "
+
"txt1 varchar);"
;
stat
.
execute
(
createSQL
);
stat
.
execute
(
"insert into testTranslate(id, txt1) "
+
"values(1, 'test1')"
);
stat
.
execute
(
"insert into testTranslate(id, txt1) "
+
"values(2, null)"
);
stat
.
execute
(
"insert into testTranslate(id, txt1) "
+
"values(3, '')"
);
stat
.
execute
(
"insert into testTranslate(id, txt1) "
+
"values(4, 'caps')"
);
String
query
=
"SELECT translate(txt1, 'p', 'r') "
+
"FROM testTranslate order by id asc"
;
ResultSet
rs
=
stat
.
executeQuery
(
query
);
rs
.
next
();
String
actual
=
rs
.
getString
(
1
);
assertEquals
(
"test1"
,
actual
);
rs
.
next
();
actual
=
rs
.
getString
(
1
);
assertNull
(
actual
);
rs
.
next
();
actual
=
rs
.
getString
(
1
);
assertEquals
(
""
,
actual
);
rs
.
next
();
actual
=
rs
.
getString
(
1
);
assertEquals
(
"cars"
,
actual
);
rs
.
close
();
rs
=
stat
.
executeQuery
(
"select translate(null,null,null)"
);
rs
.
next
();
assertNull
(
rs
.
getObject
(
1
));
stat
.
execute
(
"drop table testTranslate"
);
conn
.
close
();
}
private
void
testToCharFromDateTime
()
throws
SQLException
{
deleteDb
(
"functions"
);
Connection
conn
=
getConnection
(
"functions"
);
...
...
h2/src/tools/org/h2/build/doc/dictionary.txt
浏览文件 @
f2759786
...
...
@@ -753,6 +753,6 @@ fmrn fmxxx fmday fml syyyy tzd nov iyy iyyy fmc fmb fmxx tzr btc yyfxyy scc syea
overwrote though randomize readability datagram rsync mongodb divides crypto
predicted prediction wojtek hops jurczyk cbtree predict vast assumption upside
adjusted lastly sgtatham cleaning gillet prevented
angus bernd macdonald eckenfels granting moreover exponential transferring
angus bernd
chatellier
macdonald eckenfels granting moreover exponential transferring
dedup megabyte breadth traversal affine tucc jentsch yyyymmdd vertica graf
mutate
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论