create alias with the same name but not the same signature

坚强是说给别人听的谎言 提交于 2019-12-12 02:37:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!