H2 database and functions in separate schemas
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 P开发者_JAVA百科ACKAGE 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.
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:
create schema PERMISSION_TOOLS;
CREATE ALIAS PERMISSION_TOOLS.GET_ACCESS_LEVEL as $$
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
$$;
select permission_tools.get_access_level(1) from dual;
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.
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 like call test.PERMISSION_TOOLS.GET_ACCESS_LEVEL
See my answer in How to define oracle package procedure in h2 for testing
精彩评论