views:

51

answers:

1

Hi

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.

+4  A: 

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.

Thomas Mueller
Actually i've been testing this solution, however, with inappropriate version. Thanks for noticing, will try this right away.
Marcin Cylke