H2 数据库和函数位于不同的模式中
我正在尝试创建一个测试数据库(使用 H2 数据库)。我在生产中使用 Oracle,在 h2 中拥有 Oracle 兼容模式似乎很好。
时遇到了问题
create or replace PACKAGE permission_tools IS
FUNCTION get_role_access_level(
p_role_id IN NUMBER,
p_permiss IN VARCHAR2)
RETURN NUMBER;
END permission_tools;
项
select permission_tools.get_access_level(?, ?) from dual;
然而,我在将 Oracle Construction: 转换为 H2 等效 。我一直在尝试类似的方法:
CREATE SCHEMA PERMISSION_TOOLS;
CREATE ALIAS PERMISSION_TOOLS.GET_ACCESS_LEVEL as $$
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
$$;
但这给了我编译错误:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "
CREATE ALIAS PERMISSION_TOOLS.[*]GET_ACCESS_LEVEL AS
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
"; expected "FOR"; SQL statement:
CREATE ALIAS PERMISSION_TOOLS.GET_ACCESS_LEVEL as $$
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
$$ [42001-131]
这并没有给出任何关于这里发生的情况的线索。
任何帮助表示赞赏。
I'm trying to create a test database (with H2 database). I'm using Oracle in production, and it seems nice to have oracle compatibility mode in h2.
However I've got a problem with translating oracle construction:
create or replace PACKAGE permission_tools IS
FUNCTION get_role_access_level(
p_role_id IN NUMBER,
p_permiss IN VARCHAR2)
RETURN NUMBER;
END permission_tools;
which I'm calling with:
select permission_tools.get_access_level(?, ?) from dual;
into H2 equivalent. I've been trying something like:
CREATE SCHEMA PERMISSION_TOOLS;
CREATE ALIAS PERMISSION_TOOLS.GET_ACCESS_LEVEL as $
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
$;
But this gives me compilation error:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "
CREATE ALIAS PERMISSION_TOOLS.[*]GET_ACCESS_LEVEL AS
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
"; expected "FOR"; SQL statement:
CREATE ALIAS PERMISSION_TOOLS.GET_ACCESS_LEVEL as $
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
$ [42001-131]
This does not give any clue as to was is going on here.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
H2 不支持包。您可以做的是使用不同的名称创建函数,例如:PERMISSION_TOOLS_GET_ACCESS_LEVEL。缺点是您还需要更改查询。或者,您创建一个模式 PERMISSION_TOOLS 和其中的方法:
请注意,这在 H2 版本 1.2.131 中尚不起作用(根据您收到的错误消息代码,这是您正在使用的版本)。原因是“模式中的函数”最近刚刚实现(版本 1.2.135)。实际上我建议升级到1.2.138版本,因为早期版本修复了与此功能相关的错误。创建特殊模式的方法的缺点是:如果您确实在 PUBLIC 之外的模式中创建此类函数,则无法使用旧版本的 H2 打开数据库。
H2 doesn't support packages. What you could do is create the function using a different name, for example: PERMISSION_TOOLS_GET_ACCESS_LEVEL. The disadvantage is that you need to change the query as well. Or, you create a schema PERMISSION_TOOLS and the method there:
Please not this will not yet work in H2 version 1.2.131 (which is the version you are using according to the error message code you got). The reason is that 'functions in schemas' was just recently implemented (in version 1.2.135). Actually I suggest to upgrade to version 1.2.138, because there was a bug fixed related to this feature in earlier versions. The disadvantage of creating the method is a special schema is: if you do create such functions in schemas other than PUBLIC, then the database can not be opened with older versions of H2.
回答@thomas-mueller,如果你不关心程序的作用。 H2 使用此模式来调用存储过程
database.schema.procedure_name
因此,如果您正在测试,请为测试数据库指定一个名称,例如
test
以及调用存储过程的方式代码就像call test.PERMISSION_TOOLS.GET_ACCESS_LEVEL
请参阅我的答案 如何在 h2 中定义 oracle 包过程用于测试
To answer @thomas-mueller, if you don't care what the procedure does. H2 uses this pattern to call the stored procedure
database.schema.procedure_name
So if your are testing give the test database a name say
test
and the way you will call the stored procedure in code will be likecall test.PERMISSION_TOOLS.GET_ACCESS_LEVEL
See my answer in How to define oracle package procedure in h2 for testing