Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
4d8dd6e7
提交
4d8dd6e7
authored
3月 23, 2017
作者:
Max Englander
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enum-support: add support for math operations
上级
fbce5a3f
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
113 行增加
和
35 行删除
+113
-35
_messages_en.prop
h2/src/docsrc/textbase/_messages_en.prop
+3
-0
ErrorCode.java
h2/src/main/org/h2/api/ErrorCode.java
+52
-6
_messages_en.prop
h2/src/main/org/h2/res/_messages_en.prop
+3
-0
Column.java
h2/src/main/org/h2/table/Column.java
+2
-2
Value.java
h2/src/main/org/h2/value/Value.java
+2
-0
ValueEnum.java
h2/src/main/org/h2/value/ValueEnum.java
+50
-26
TestPreparedStatement.java
h2/src/test/org/h2/test/jdbc/TestPreparedStatement.java
+1
-1
没有找到文件。
h2/src/docsrc/textbase/_messages_en.prop
浏览文件 @
4d8dd6e7
...
...
@@ -10,6 +10,9 @@
22018=Data conversion error converting {0}
22025=Error in LIKE ESCAPE: {0}
22030=Value not permitted for column {0}: {1}
22031=Value not a member of enumerators {0}: {1}
22032=Empty enums are not allowed
22033=Duplicate enumerators are not allowed for enum types: {0}
23502=NULL not allowed for column {0}
23503=Referential integrity constraint violation: {0}
23505=Unique index or primary key violation: {0}
...
...
h2/src/main/org/h2/api/ErrorCode.java
浏览文件 @
4d8dd6e7
...
...
@@ -161,16 +161,62 @@ public class ErrorCode {
/**
* The error with code <code>22030</code> is thrown when
* an attempt is made to insert or update an ENUM value,
* but the target value is not one of the values permitted
* by the column.
* an attempt is made to INSERT or UPDATE an ENUM-typed cell,
* but the value is not one of the values enumerated by the
* type.
*
* This error is best thrown in a context when the column name
* and it's enumerated values are known.
*
* Example:
* <pre>
* CREATE TABLE TEST(ID INT, CASE ENUM('sensitive','insensitive'));
* INSERT INTO TEST VALUES(1, 'Hello');
* CREATE TABLE TEST(CASE ENUM('sensitive','insensitive'));
* INSERT INTO TEST VALUES('snake');
* </pre>
*/
public
static
final
int
ENUM_VALUE_NOT_PERMITTED_1
=
22030
;
/**
* The error with code <code>22031</code> is typically thrown
* when a math operation is attempted on an ENUM-typed cell,
* but the value resulting from the operation is not one of
* values enumerated by the type.
*
* This error is best thrown in a context when the column name
* is not known, but the enumerated values of the type are known.
*
* Example:
* <pre>
* CREATE TABLE TEST(CASE ENUM('sensitive','insensitive'));
* INSERT INTO TEST VALUES('sensitive');
* UPDATE TEST SET CASE = CASE + 100;
* </pre>
*/
public
static
final
int
ENUM_VALUE_NOT_PERMITTED_2
=
22031
;
/**
* The error with code <code>22032</code> is thrown when an
* attempt is made to add or modify an ENUM-typed column so
* that it would not have any enumerated values.
*
* Example:
* <pre>
* CREATE TABLE TEST(CASE ENUM());
* </pre>
*/
public
static
final
int
ENUM_EMPTY
=
22032
;
/**
* The error with code <code>22033</code> is thrown when an
* attempt is made to add or modify an ENUM-typed column so
* that it would have duplicate values.
*
* Example:
* <pre>
* CREATE TABLE TEST(CASE ENUM('sensitive', 'sensitive'));
* </pre>
*/
public
static
final
int
VALUE_NOT_PERMITTED
=
22030
;
public
static
final
int
ENUM_DUPLICATE
=
22033
;
// 23: constraint violation
...
...
h2/src/main/org/h2/res/_messages_en.prop
浏览文件 @
4d8dd6e7
...
...
@@ -10,6 +10,9 @@
22018=Data conversion error converting {0}
22025=Error in LIKE ESCAPE: {0}
22030=Value not permitted for column {0}: {1}
22031=Value not a member of enumerators {0}: {1}
22032=Empty enums are not allowed
22033=Duplicate enumerators are not allowed for enum types: {0}
23502=NULL not allowed for column {0}
23503=Referential integrity constraint violation: {0}
23505=Unique index or primary key violation: {0}
...
...
h2/src/main/org/h2/table/Column.java
浏览文件 @
4d8dd6e7
...
...
@@ -376,8 +376,8 @@ public class Column {
if
(
s
.
length
()
>
127
)
{
s
=
s
.
substring
(
0
,
128
)
+
"..."
;
}
throw
DbException
.
get
(
ErrorCode
.
VALUE_NOT_PERMITTED
,
getCreateSQL
(),
s
+
" ("
+
value
.
get
String
()
+
")"
);
throw
DbException
.
get
(
ErrorCode
.
ENUM_VALUE_NOT_PERMITTED_1
,
getCreateSQL
(),
s
+
" ("
+
value
.
get
Int
()
+
")"
);
}
value
=
ValueEnum
.
get
(
enumerators
,
value
);
...
...
h2/src/main/org/h2/value/Value.java
浏览文件 @
4d8dd6e7
...
...
@@ -622,6 +622,8 @@ public abstract class Value {
return
ValueInt
.
get
(
getBoolean
().
booleanValue
()
?
1
:
0
);
case
BYTE:
return
ValueInt
.
get
(
getByte
());
case
ENUM:
return
ValueInt
.
get
(
getInt
());
case
SHORT:
return
ValueInt
.
get
(
getShort
());
case
LONG:
...
...
h2/src/main/org/h2/value/ValueEnum.java
浏览文件 @
4d8dd6e7
...
...
@@ -29,19 +29,24 @@ public class ValueEnum extends Value {
this
.
ordinal
=
ordinal
;
}
@Override
public
Value
add
(
final
Value
v
)
{
final
Value
iv
=
v
.
convertTo
(
Value
.
INT
);
return
convertTo
(
Value
.
INT
).
add
(
iv
);
}
private
static
final
void
check
(
final
String
[]
enumerators
)
{
switch
(
validate
(
enumerators
))
{
case
VALID:
return
;
case
EMPTY:
throw
DbException
.
get
(
ErrorCode
.
INVALID_VALUE_2
,
"Empty enum is not allowed"
);
throw
DbException
.
get
(
ErrorCode
.
ENUM_EMPTY
);
case
DUPLICATE:
throw
DbException
.
get
(
ErrorCode
.
INVALID_VALUE_2
,
"Enum with duplicate enumerator is not allowed"
);
throw
DbException
.
get
(
ErrorCode
.
ENUM_DUPLICATE
,
toString
(
enumerators
)
);
default
:
throw
DbException
.
get
(
ErrorCode
.
INVALID_VALUE_2
,
"Invalid enumerator list for enum"
);
toString
(
enumerators
)
);
}
}
...
...
@@ -51,12 +56,9 @@ public class ValueEnum extends Value {
switch
(
validate
(
enumerators
,
label
))
{
case
VALID:
return
;
case
EMPTY:
throw
DbException
.
get
(
ErrorCode
.
VALUE_NOT_PERMITTED
,
"Enumerator label may not be empty"
);
default
:
throw
DbException
.
get
(
ErrorCode
.
VALUE_NOT_PERMITTED
,
"Enumerator label is invalid
"
);
throw
DbException
.
get
(
ErrorCode
.
ENUM_VALUE_NOT_PERMITTED_2
,
toString
(
enumerators
),
"'"
+
label
+
"'
"
);
}
}
...
...
@@ -67,8 +69,8 @@ public class ValueEnum extends Value {
case
VALID:
return
;
default
:
throw
DbException
.
get
(
ErrorCode
.
VALUE_NOT_PERMITTED
,
"Provided enumerator label does not match any member of enum"
);
throw
DbException
.
get
(
ErrorCode
.
ENUM_VALUE_NOT_PERMITTED_2
,
toString
(
enumerators
),
Integer
.
toString
(
ordinal
)
);
}
}
...
...
@@ -79,15 +81,21 @@ public class ValueEnum extends Value {
case
VALID:
return
;
default
:
throw
DbException
.
get
(
ErrorCode
.
VALUE_NOT_PERMITTED
,
"Provided value does not match any enumerators "
+
toString
(
enumerators
),
value
.
toString
());
throw
DbException
.
get
(
ErrorCode
.
ENUM_VALUE_NOT_PERMITTED_2
,
toString
(
enumerators
),
value
.
toString
());
}
}
@Override
protected
int
compareSecure
(
final
Value
o
,
final
CompareMode
mode
)
{
final
ValueEnum
v
=
ValueEnum
.
get
(
enumerators
,
o
);
return
MathUtils
.
compareInt
(
ordinal
(),
v
.
ordinal
());
protected
int
compareSecure
(
final
Value
v
,
final
CompareMode
mode
)
{
final
ValueEnum
ev
=
ValueEnum
.
get
(
enumerators
,
v
);
return
MathUtils
.
compareInt
(
ordinal
(),
ev
.
ordinal
());
}
@Override
public
Value
divide
(
final
Value
v
)
{
final
Value
iv
=
v
.
convertTo
(
Value
.
INT
);
return
convertTo
(
Value
.
INT
).
divide
(
iv
);
}
@Override
...
...
@@ -166,6 +174,11 @@ public class ValueEnum extends Value {
return
label
;
}
@Override
public
int
getType
()
{
return
Value
.
ENUM
;
}
@Override
public
int
hashCode
()
{
return
enumerators
.
hashCode
()
+
ordinal
;
...
...
@@ -183,13 +196,20 @@ public class ValueEnum extends Value {
return
validate
(
enumerators
,
value
).
equals
(
Validation
.
VALID
);
}
protected
int
ordinal
()
{
return
ordinal
;
}
@Override
public
int
getType
()
{
return
Value
.
ENUM
;
public
Value
modulus
(
final
Value
v
)
{
final
Value
iv
=
v
.
convertTo
(
Value
.
INT
);
return
convertTo
(
Value
.
INT
).
modulus
(
iv
);
}
protected
int
ordinal
()
{
return
ordinal
;
@Override
public
Value
multiply
(
final
Value
v
)
{
final
Value
iv
=
v
.
convertTo
(
Value
.
INT
);
return
convertTo
(
Value
.
INT
).
multiply
(
iv
);
}
@Override
...
...
@@ -198,6 +218,12 @@ public class ValueEnum extends Value {
prep
.
setInt
(
parameterIndex
,
ordinal
);
}
@Override
public
Value
subtract
(
final
Value
v
)
{
final
Value
iv
=
v
.
convertTo
(
Value
.
INT
);
return
convertTo
(
Value
.
INT
).
subtract
(
iv
);
}
private
static
String
toString
(
final
String
[]
enumerators
)
{
String
result
=
"("
;
for
(
int
i
=
0
;
i
<
enumerators
.
length
;
i
++)
{
...
...
@@ -213,12 +239,10 @@ public class ValueEnum extends Value {
private
static
Validation
validate
(
final
String
[]
enumerators
,
final
String
label
)
{
check
(
enumerators
);
if
(
label
==
null
||
label
.
trim
().
length
()
==
0
)
{
return
Validation
.
EMPTY
;
}
final
String
cleanLabel
=
label
.
trim
().
toLowerCase
();
for
(
int
i
=
0
;
i
<
enumerators
.
length
;
i
++)
{
if
(
l
abel
.
equals
(
enumerators
[
i
]))
{
if
(
cleanL
abel
.
equals
(
enumerators
[
i
]))
{
return
Validation
.
VALID
;
}
}
...
...
@@ -228,7 +252,7 @@ public class ValueEnum extends Value {
private
static
Validation
validate
(
final
String
[]
enumerators
)
{
for
(
int
i
=
0
;
i
<
enumerators
.
length
;
i
++)
{
if
(
enumerators
[
i
]
==
null
||
enumerators
[
i
].
equals
(
""
))
{
if
(
enumerators
[
i
]
==
null
||
enumerators
[
i
].
trim
().
equals
(
""
))
{
return
Validation
.
EMPTY
;
}
...
...
h2/src/test/org/h2/test/jdbc/TestPreparedStatement.java
浏览文件 @
4d8dd6e7
...
...
@@ -453,7 +453,7 @@ public class TestPreparedStatement extends TestBase {
PreparedStatement
prep
=
conn
.
prepareStatement
(
"INSERT INTO test_enum VALUES(?)"
);
prep
.
setObject
(
1
,
badSizes
[
i
]);
assertThrows
(
ErrorCode
.
VALUE_NOT_PERMITTED
,
prep
).
execute
();
assertThrows
(
ErrorCode
.
ENUM_VALUE_NOT_PERMITTED_1
,
prep
).
execute
();
}
String
[]
goodSizes
=
new
String
[]{
"small"
,
"medium"
,
"large"
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论