Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b38351fe
提交
b38351fe
authored
2月 25, 2016
作者:
mtandy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve compatibility with oracle to_date RRRR
上级
7224c100
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
64 行增加
和
3 行删除
+64
-3
ToDateTokenizer.java
h2/src/main/org/h2/util/ToDateTokenizer.java
+16
-3
TestCompatibilityOracle.java
h2/src/test/org/h2/test/db/TestCompatibilityOracle.java
+27
-0
TestFunctions.java
h2/src/test/org/h2/test/db/TestFunctions.java
+21
-0
没有找到文件。
h2/src/main/org/h2/util/ToDateTokenizer.java
浏览文件 @
b38351fe
...
...
@@ -37,10 +37,14 @@ class ToDateTokenizer {
static
final
Pattern
PATTERN_NUMBER
=
Pattern
.
compile
(
"^([+-]?[0-9]+)"
);
/**
* The pattern for for digits (typically a year).
* The pattern for fo
u
r digits (typically a year).
*/
static
final
Pattern
PATTERN_FOUR_DIGITS
=
Pattern
.
compile
(
"^([+-]?[0-9]{4})"
);
/**
* The pattern 2-4 digits (e.g. for RRRR).
*/
static
final
Pattern
PATTERN_TWO_TO_FOUR_DIGITS
=
Pattern
.
compile
(
"^([+-]?[0-9]{2,4})"
);
/**
* The pattern for three digits.
*/
...
...
@@ -142,6 +146,8 @@ class ToDateTokenizer {
dateNr
=
Integer
.
parseInt
(
inputFragmentStr
);
// Gregorian calendar does not have a year 0.
// 0 = 0001 BC, -1 = 0002 BC, ... so we adjust
if
(
dateNr
==
0
)
throwException
(
params
,
"Year may not be zero"
);
result
.
set
(
Calendar
.
YEAR
,
dateNr
>=
0
?
dateNr
:
dateNr
+
1
);
break
;
case
YYY:
...
...
@@ -155,9 +161,16 @@ class ToDateTokenizer {
break
;
case
RRRR:
inputFragmentStr
=
matchStringOrThrow
(
PATTERN_TWO_DIGITS
,
params
,
formatTokenEnum
);
PATTERN_TWO_
TO_FOUR_
DIGITS
,
params
,
formatTokenEnum
);
dateNr
=
Integer
.
parseInt
(
inputFragmentStr
);
dateNr
+=
dateNr
<
50
?
2000
:
1900
;
if
(
inputFragmentStr
.
length
()
<
4
)
{
if
(
dateNr
<
50
)
dateNr
+=
2000
;
else
if
(
dateNr
<
100
)
dateNr
+=
1900
;
}
if
(
dateNr
==
0
)
throwException
(
params
,
"Year may not be zero"
);
result
.
set
(
Calendar
.
YEAR
,
dateNr
);
break
;
case
RR:
...
...
h2/src/test/org/h2/test/db/TestCompatibilityOracle.java
浏览文件 @
b38351fe
...
...
@@ -11,6 +11,7 @@ import java.sql.ResultSet;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.Types
;
import
java.text.SimpleDateFormat
;
import
java.util.Arrays
;
import
org.h2.test.TestBase
;
import
org.h2.tools.SimpleResultSet
;
...
...
@@ -35,6 +36,7 @@ public class TestCompatibilityOracle extends TestBase {
testTreatEmptyStringsAsNull
();
testDecimalScale
();
testPoundSymbolInColumnName
();
testToDate
();
}
private
void
testTreatEmptyStringsAsNull
()
throws
SQLException
{
...
...
@@ -140,6 +142,31 @@ public class TestCompatibilityOracle extends TestBase {
conn
.
close
();
}
private
void
testToDate
()
throws
SQLException
{
deleteDb
(
"oracle"
);
Connection
conn
=
getConnection
(
"oracle;MODE=Oracle"
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"CREATE TABLE DATETABLE (ID NUMBER PRIMARY KEY, TESTVAL TIMESTAMP)"
);
stat
.
execute
(
"INSERT INTO DATETABLE VALUES (1, to_date('31-DEC-9999 23:59:59','DD-MON-RRRR HH24:MI:SS'))"
);
stat
.
execute
(
"INSERT INTO DATETABLE VALUES (2, to_date('01-JAN-0001 00:00:00','DD-MON-RRRR HH24:MI:SS'))"
);
assertResultDate
(
"9999-12-31T23:59:59"
,
stat
,
"SELECT TESTVAL FROM DATETABLE WHERE ID=1"
);
assertResultDate
(
"0001-01-01T00:00:00"
,
stat
,
"SELECT TESTVAL FROM DATETABLE WHERE ID=2"
);
conn
.
close
();
}
private
void
assertResultDate
(
String
expected
,
Statement
stat
,
String
sql
)
throws
SQLException
{
SimpleDateFormat
iso8601
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss"
);
ResultSet
rs
=
stat
.
executeQuery
(
sql
);
if
(
rs
.
next
())
{
assertEquals
(
expected
,
iso8601
.
format
(
rs
.
getTimestamp
(
1
)));
}
else
{
assertEquals
(
expected
,
null
);
}
}
private
void
assertResult
(
Object
[][]
expectedRowsOfValues
,
Statement
stat
,
String
sql
)
throws
SQLException
{
assertResult
(
newSimpleResultSet
(
expectedRowsOfValues
),
stat
,
sql
);
...
...
h2/src/test/org/h2/test/db/TestFunctions.java
浏览文件 @
b38351fe
...
...
@@ -1288,6 +1288,11 @@ public class TestFunctions extends TestBase implements AggregateFunction {
}
catch
(
Exception
e
)
{
assertEquals
(
DbException
.
class
.
getSimpleName
(),
e
.
getClass
().
getSimpleName
());
}
try
{
ToDateParser
.
toDate
(
"1-DEC-0000"
,
"DD-MON-RRRR"
);
fail
(
"Oracle to_date should reject year 0 (ORA-01841)"
);
}
catch
(
Exception
e
)
{
}
}
private
void
testToDate
()
throws
ParseException
{
...
...
@@ -1400,6 +1405,22 @@ public class TestFunctions extends TestBase implements AggregateFunction {
date
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
parse
(
"2013-01-29"
);
assertEquals
(
date
,
ToDateParser
.
toDate
(
"113029"
,
"J"
));
date
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss"
).
parse
(
"9999-12-31T23:59:59"
);
assertEquals
(
date
,
ToDateParser
.
toDate
(
"31-DEC-9999 23:59:59"
,
"DD-MON-YYYY HH24:MI:SS"
));
assertEquals
(
date
,
ToDateParser
.
toDate
(
"31-DEC-9999 23:59:59"
,
"DD-MON-RRRR HH24:MI:SS"
));
SimpleDateFormat
ymd
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
assertEquals
(
ymd
.
parse
(
"0001-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-0001"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"9999-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-9999"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"2000-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-000"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"1999-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-099"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"0100-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-100"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"2000-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-00"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"2049-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-49"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"1950-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-50"
,
"DD-MON-RRRR"
));
assertEquals
(
ymd
.
parse
(
"1999-03-01"
),
ToDateParser
.
toDate
(
"1-MAR-99"
,
"DD-MON-RRRR"
));
}
private
static
void
setMonth
(
Date
date
,
int
month
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论