Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
eae51e9e
提交
eae51e9e
authored
8月 14, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement casts between INTERVAL types
上级
c76da8f2
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
465 行增加
和
0 行删除
+465
-0
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+111
-0
Value.java
h2/src/main/org/h2/value/Value.java
+39
-0
interval.sql
h2/src/test/org/h2/test/scripts/datatypes/interval.sql
+315
-0
没有找到文件。
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
eae51e9e
...
...
@@ -6,14 +6,17 @@
*/
package
org
.
h2
.
util
;
import
java.math.BigInteger
;
import
java.sql.Date
;
import
java.sql.Time
;
import
java.sql.Timestamp
;
import
java.util.Calendar
;
import
java.util.GregorianCalendar
;
import
java.util.TimeZone
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.IntervalQualifier
;
import
org.h2.engine.Mode
;
import
org.h2.message.DbException
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueInterval
;
...
...
@@ -1746,4 +1749,112 @@ public class DateTimeUtils {
return
nanosOfDay
-
mod
;
}
/**
* Converts interval value to an absolute value.
*
* @param interval the interval value
* @return absolute value in months for year-month intervals,
* in nanoseconds for day-time intervals
*/
public
static
BigInteger
intervalToAbsolute
(
ValueInterval
interval
)
{
switch
(
interval
.
getQualifier
())
{
case
YEAR:
return
BigInteger
.
valueOf
(
interval
.
getLeading
()).
multiply
(
BigInteger
.
valueOf
(
12
));
case
MONTH:
return
BigInteger
.
valueOf
(
interval
.
getLeading
());
case
DAY:
return
BigInteger
.
valueOf
(
interval
.
getLeading
()).
multiply
(
BigInteger
.
valueOf
(
NANOS_PER_DAY
));
case
HOUR:
return
BigInteger
.
valueOf
(
interval
.
getLeading
()).
multiply
(
BigInteger
.
valueOf
(
3_600_000_000_000L
));
case
MINUTE:
return
BigInteger
.
valueOf
(
interval
.
getLeading
()).
multiply
(
BigInteger
.
valueOf
(
60_000_000_000L
));
case
SECOND:
return
intervalToAbsolute
(
interval
,
1_000_000_000
);
case
YEAR_TO_MONTH:
return
intervalToAbsolute
(
interval
,
12
);
case
DAY_TO_HOUR:
return
intervalToAbsolute
(
interval
,
24
,
3_600_000_000_000L
);
case
DAY_TO_MINUTE:
return
intervalToAbsolute
(
interval
,
24
*
60
,
60_000_000_000L
);
case
DAY_TO_SECOND:
return
intervalToAbsolute
(
interval
,
NANOS_PER_DAY
);
case
HOUR_TO_MINUTE:
return
intervalToAbsolute
(
interval
,
60
,
60_000_000_000L
);
case
HOUR_TO_SECOND:
return
intervalToAbsolute
(
interval
,
3_600_000_000_000L
);
case
MINUTE_TO_SECOND:
return
intervalToAbsolute
(
interval
,
60_000_000_000L
);
default
:
throw
new
IllegalArgumentException
();
}
}
private
static
BigInteger
intervalToAbsolute
(
ValueInterval
interval
,
long
multiplier
,
long
totalMultiplier
)
{
return
intervalToAbsolute
(
interval
,
multiplier
).
multiply
(
BigInteger
.
valueOf
(
totalMultiplier
));
}
private
static
BigInteger
intervalToAbsolute
(
ValueInterval
interval
,
long
multiplier
)
{
long
leading
=
interval
.
getLeading
(),
remaining
=
interval
.
getRemaining
();
if
(
leading
<
0
)
{
remaining
=
-
remaining
;
}
return
BigInteger
.
valueOf
(
leading
).
multiply
(
BigInteger
.
valueOf
(
multiplier
)).
add
(
BigInteger
.
valueOf
(
remaining
));
}
/**
* Converts absolute value to an interval value.
*
* @param qualifier the qualifier of interval
* @param absolute absolute value in months for year-month intervals,
* in nanoseconds for day-time intervals
* @return the interval value
*/
public
static
ValueInterval
intervalFromAbsolute
(
IntervalQualifier
qualifier
,
BigInteger
absolute
)
{
switch
(
qualifier
)
{
case
YEAR:
return
ValueInterval
.
from
(
qualifier
,
absolute
.
divide
(
BigInteger
.
valueOf
(
12
)).
longValue
(),
0
);
case
MONTH:
return
ValueInterval
.
from
(
qualifier
,
leadingExact
(
absolute
),
0
);
case
DAY:
return
ValueInterval
.
from
(
qualifier
,
leadingExact
(
absolute
.
divide
(
BigInteger
.
valueOf
(
NANOS_PER_DAY
))),
0
);
case
HOUR:
return
ValueInterval
.
from
(
qualifier
,
leadingExact
(
absolute
.
divide
(
BigInteger
.
valueOf
(
3_600_000_000_000L
))),
0
);
case
MINUTE:
return
ValueInterval
.
from
(
qualifier
,
leadingExact
(
absolute
.
divide
(
BigInteger
.
valueOf
(
60_000_000_000L
))),
0
);
case
SECOND:
return
intervalFromAbsolute
(
qualifier
,
absolute
,
1_000_000_000
);
case
YEAR_TO_MONTH:
return
intervalFromAbsolute
(
qualifier
,
absolute
,
12
);
case
DAY_TO_HOUR:
return
intervalFromAbsolute
(
qualifier
,
absolute
.
divide
(
BigInteger
.
valueOf
(
3_600_000_000_000L
)),
24
);
case
DAY_TO_MINUTE:
return
intervalFromAbsolute
(
qualifier
,
absolute
.
divide
(
BigInteger
.
valueOf
(
60_000_000_000L
)),
24
*
60
);
case
DAY_TO_SECOND:
return
intervalFromAbsolute
(
qualifier
,
absolute
,
NANOS_PER_DAY
);
case
HOUR_TO_MINUTE:
return
intervalFromAbsolute
(
qualifier
,
absolute
.
divide
(
BigInteger
.
valueOf
(
60_000_000_000L
)),
60
);
case
HOUR_TO_SECOND:
return
intervalFromAbsolute
(
qualifier
,
absolute
,
3_600_000_000_000L
);
case
MINUTE_TO_SECOND:
return
intervalFromAbsolute
(
qualifier
,
absolute
,
60_000_000_000L
);
default
:
throw
new
IllegalArgumentException
();
}
}
private
static
ValueInterval
intervalFromAbsolute
(
IntervalQualifier
qualifier
,
BigInteger
absolute
,
long
divisor
)
{
BigInteger
[]
dr
=
absolute
.
divideAndRemainder
(
BigInteger
.
valueOf
(
divisor
));
return
ValueInterval
.
from
(
qualifier
,
leadingExact
(
dr
[
0
]),
Math
.
abs
(
dr
[
1
].
longValue
()));
}
private
static
long
leadingExact
(
BigInteger
absolute
)
{
if
(
absolute
.
compareTo
(
BigInteger
.
valueOf
(
999_999_999_999_999_999L
))
>
0
||
absolute
.
compareTo
(
BigInteger
.
valueOf
(-
999_999_999_999_999_999L
))
<
0
)
{
throw
DbException
.
get
(
ErrorCode
.
NUMERIC_VALUE_OUT_OF_RANGE_1
,
absolute
.
toString
());
}
return
absolute
.
longValue
();
}
}
h2/src/main/org/h2/value/Value.java
浏览文件 @
eae51e9e
...
...
@@ -20,6 +20,7 @@ import java.sql.Time;
import
java.sql.Timestamp
;
import
java.sql.Types
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.IntervalQualifier
;
import
org.h2.engine.Mode
;
import
org.h2.engine.SysProperties
;
import
org.h2.message.DbException
;
...
...
@@ -1148,6 +1149,44 @@ public abstract class Value {
}
break
;
}
case
Value
.
INTERVAL_YEAR
:
case
Value
.
INTERVAL_MONTH
:
case
Value
.
INTERVAL_YEAR_TO_MONTH
:
switch
(
getType
())
{
case
Value
.
INTERVAL_YEAR
:
case
Value
.
INTERVAL_MONTH
:
case
Value
.
INTERVAL_YEAR_TO_MONTH
:
return
DateTimeUtils
.
intervalFromAbsolute
(
IntervalQualifier
.
valueOf
(
targetType
-
Value
.
INTERVAL_YEAR
),
DateTimeUtils
.
intervalToAbsolute
((
ValueInterval
)
this
));
}
break
;
case
Value
.
INTERVAL_DAY
:
case
Value
.
INTERVAL_HOUR
:
case
Value
.
INTERVAL_MINUTE
:
case
Value
.
INTERVAL_SECOND
:
case
Value
.
INTERVAL_DAY_TO_HOUR
:
case
Value
.
INTERVAL_DAY_TO_MINUTE
:
case
Value
.
INTERVAL_DAY_TO_SECOND
:
case
Value
.
INTERVAL_HOUR_TO_MINUTE
:
case
Value
.
INTERVAL_HOUR_TO_SECOND
:
case
Value
.
INTERVAL_MINUTE_TO_SECOND
:
switch
(
getType
())
{
case
Value
.
INTERVAL_DAY
:
case
Value
.
INTERVAL_HOUR
:
case
Value
.
INTERVAL_MINUTE
:
case
Value
.
INTERVAL_SECOND
:
case
Value
.
INTERVAL_DAY_TO_HOUR
:
case
Value
.
INTERVAL_DAY_TO_MINUTE
:
case
Value
.
INTERVAL_DAY_TO_SECOND
:
case
Value
.
INTERVAL_HOUR_TO_MINUTE
:
case
Value
.
INTERVAL_HOUR_TO_SECOND
:
case
Value
.
INTERVAL_MINUTE_TO_SECOND
:
return
DateTimeUtils
.
intervalFromAbsolute
(
IntervalQualifier
.
valueOf
(
targetType
-
Value
.
INTERVAL_YEAR
),
DateTimeUtils
.
intervalToAbsolute
((
ValueInterval
)
this
));
}
break
;
}
// conversion by parsing the string value
String
s
=
getString
();
...
...
h2/src/test/org/h2/test/scripts/datatypes/interval.sql
浏览文件 @
eae51e9e
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论