Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
50d287a2
Unverified
提交
50d287a2
authored
7 年前
作者:
Noel Grandin
提交者:
GitHub
7 年前
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #706 from ilm-informatique/signal
SIGNAL function
上级
7365ba5c
fa5784cd
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
54 行增加
和
1 行删除
+54
-1
help.csv
h2/src/docsrc/help/help.csv
+8
-0
Function.java
h2/src/main/org/h2/expression/Function.java
+12
-1
DbException.java
h2/src/main/org/h2/message/DbException.java
+12
-0
TestFunctions.java
h2/src/test/org/h2/test/db/TestFunctions.java
+22
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
50d287a2
...
...
@@ -4013,6 +4013,14 @@ This function may be expensive since it has to load every page in the table.
CALL DISK_SPACE_USED('my_table');
"
"Functions (System)","SIGNAL","
SIGNAL(sqlStateString, messageString)
","
Throw an SQLException with the passed SQLState and reason.
","
CALL SIGNAL('23505', 'Duplicate user ID: ' || user_id);
"
"Functions (System)","FILE_READ","
FILE_READ(fileNameString [,encodingString])
","
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/expression/Function.java
浏览文件 @
50d287a2
...
...
@@ -22,6 +22,7 @@ import java.util.Locale;
import
java.util.TimeZone
;
import
java.util.regex.Pattern
;
import
java.util.regex.PatternSyntaxException
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.Command
;
import
org.h2.command.Parser
;
...
...
@@ -109,7 +110,9 @@ public class Function extends Expression implements FunctionCall {
public
static
final
int
DATABASE
=
150
,
USER
=
151
,
CURRENT_USER
=
152
,
IDENTITY
=
153
,
SCOPE_IDENTITY
=
154
,
AUTOCOMMIT
=
155
,
READONLY
=
156
,
DATABASE_PATH
=
157
,
LOCK_TIMEOUT
=
158
,
DISK_SPACE_USED
=
159
;
DISK_SPACE_USED
=
159
,
SIGNAL
=
160
;
private
static
final
Pattern
SIGNAL_PATTERN
=
Pattern
.
compile
(
"[0-9A-Z]{5}"
);
public
static
final
int
IFNULL
=
200
,
CASEWHEN
=
201
,
CONVERT
=
202
,
CAST
=
203
,
COALESCE
=
204
,
NULLIF
=
205
,
CASE
=
206
,
...
...
@@ -480,6 +483,7 @@ public class Function extends Expression implements FunctionCall {
VAR_ARGS
,
Value
.
NULL
);
addFunctionNotDeterministic
(
"DISK_SPACE_USED"
,
DISK_SPACE_USED
,
1
,
Value
.
LONG
);
addFunctionWithNull
(
"SIGNAL"
,
SIGNAL
,
2
,
Value
.
NULL
);
addFunction
(
"H2VERSION"
,
H2VERSION
,
0
,
Value
.
STRING
);
// TableFunction
...
...
@@ -1705,6 +1709,13 @@ public class Function extends Expression implements FunctionCall {
result
=
session
.
getVariable
(
args
[
0
].
getSchemaName
()
+
"."
+
args
[
0
].
getTableName
()
+
"."
+
args
[
0
].
getColumnName
());
break
;
case
SIGNAL:
{
String
sqlState
=
v0
.
getString
();
if
(
sqlState
.
startsWith
(
"00"
)
||
!
SIGNAL_PATTERN
.
matcher
(
sqlState
).
matches
())
throw
DbException
.
getInvalidValueException
(
"SQLSTATE"
,
sqlState
);
String
msgText
=
v1
.
getString
();
throw
DbException
.
fromUser
(
sqlState
,
msgText
);
}
default
:
throw
DbException
.
throwInternalError
(
"type="
+
info
.
type
);
}
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/message/DbException.java
浏览文件 @
50d287a2
...
...
@@ -179,6 +179,18 @@ public class DbException extends RuntimeException {
return
new
DbException
(
getJdbcSQLException
(
errorCode
,
null
,
params
));
}
/**
* Create a database exception for an arbitrary SQLState.
*
* @param sqlstate the state to use
* @param message the message to use
* @return the exception
*/
public
static
DbException
fromUser
(
String
sqlstate
,
String
message
)
{
// do not translate as sqlstate is arbitrary : avoid "message not found"
return
new
DbException
(
new
JdbcSQLException
(
message
,
null
,
sqlstate
,
0
,
null
,
null
));
}
/**
* Create a syntax error exception.
*
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/db/TestFunctions.java
浏览文件 @
50d287a2
...
...
@@ -38,6 +38,7 @@ import java.util.Locale;
import
java.util.Properties
;
import
java.util.TimeZone
;
import
java.util.UUID
;
import
org.h2.api.Aggregate
;
import
org.h2.api.AggregateFunction
;
import
org.h2.api.ErrorCode
;
...
...
@@ -119,6 +120,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testThatCurrentTimestampUpdatesOutsideATransaction
();
testAnnotationProcessorsOutput
();
testRound
();
testSignal
();
deleteDb
(
"functions"
);
}
...
...
@@ -2037,6 +2039,26 @@ public class TestFunctions extends TestBase implements AggregateFunction {
conn
.
close
();
}
private
void
testSignal
()
throws
SQLException
{
deleteDb
(
"functions"
);
Connection
conn
=
getConnection
(
"functions"
);
Statement
stat
=
conn
.
createStatement
();
assertThrows
(
ErrorCode
.
INVALID_VALUE_2
,
stat
).
execute
(
"select signal('00145', 'success class is invalid')"
);
assertThrows
(
ErrorCode
.
INVALID_VALUE_2
,
stat
).
execute
(
"select signal('foo', 'SQLSTATE has 5 chars')"
);
assertThrows
(
ErrorCode
.
INVALID_VALUE_2
,
stat
).
execute
(
"select signal('Ab123', 'SQLSTATE has only digits or upper-case letters')"
);
try
{
stat
.
execute
(
"select signal('AB123', 'some custom error')"
);
fail
(
"Should have thrown"
);
}
catch
(
SQLException
e
)
{
assertEquals
(
"AB123"
,
e
.
getSQLState
());
assertContains
(
e
.
getMessage
(),
"some custom error"
);
}
conn
.
close
();
}
private
void
testThatCurrentTimestampIsSane
()
throws
SQLException
,
ParseException
{
deleteDb
(
"functions"
);
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论