问题
In order to use H2 database in our Junit tests instead of calling Oracle, I am blocked on creating aliases on H2 to emulate some Oracle compatibility :
I first declared an alias for to_char for date to char conversion : works fine
create alias TO_CHAR as $$ java.lang.String toChar(java.util.Date date, String format) throws Exception{ ... }$$;Then I try to declare an alias for to_char for number to char conversion : now h2 doesn't accept it :
create alias TO_CHAR as $$ java.lang.String toChar(java.lang.Number value){ ... }$$;
It is rejected with the following error message :
org.h2.jdbc.JdbcSQLException: Function alias "TO_CHAR" already exists; SQL statement: create alias TO_CHAR as $$
java.lang.String toChar(java.lang.Number value){ return java.lang.String .valueOf(value); }$$
I also tried to declare the 2 functions in 1 block, like :
create alias TO_CHAR as $$
java.lang.String toChar(int value){ ... }
java.lang.String toChar(java.util.Date date, String format) throws Exception{ ... } $$;
In this case, there are no errors, but only the firest method declared is taken in account.
So, is there any way to declare 2 aliases having the same name but not the same signature ?
回答1:
As for TO_CHAR, H2 support it now, since version 1.3.175 (2013-01-18).
H2 does support function overloading. However, there is a limitation, because declaring such functions as source code is not supported.
You would need to declare the method as follows:
CREATE ALIAS YOUR_METHOD FOR "acme.Function.yourMethod";
This limitation is documented under User Defined Functions as follows:
Method Overloading
Multiple methods may be bound to a SQL function if the class is already compiled and included in the classpath. Each Java method must have a different number of arguments. Method overloading is not supported when declaring functions as source code.
来源:https://stackoverflow.com/questions/21484853/create-alias-with-the-same-name-but-not-the-same-signature