创建具有相同名称但不具有相同签名的别名 [英] create alias with the same name but not the same signature

查看:197
本文介绍了创建具有相同名称但不具有相同签名的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Junit测试中使用H2数据库而不是调用Oracle,我被禁止在H2上创建别名以模拟某些Oracle兼容性:

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 :

  • 我首先声明了to_char的别名,以便将日期转换为char:可以正常工作

  • 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{ ... }$$;

然后我尝试为to_char声明一个别名,以将数字转换为char:现在h2不接受它: create alias TO_CHAR as $$ java.lang.String toChar(java.lang.Number value){ ... }$$;

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:函数别名"TO_CHAR"已经存在; SQL语句: 创建别名TO_CHAR为$$

org.h2.jdbc.JdbcSQLException: Function alias "TO_CHAR" already exists; SQL statement: create alias TO_CHAR as $$

java.lang.String toChar(java.lang.Number值){ 返回java.lang.String .valueOf(value); } $$

java.lang.String toChar(java.lang.Number value){ return java.lang.String .valueOf(value); }$$

我还试图在1个块中声明2个函数,例如:

I also tried to declare the 2 functions in 1 block, like :

将别名TO_CHAR创建为$$

create alias TO_CHAR as $$

java.lang.String toChar(int value){ ... }

java.lang.String toChar(int value){ ... }

java.lang.String toChar(java.util.Date date,String format)抛出 例外{ ... } $$;

java.lang.String toChar(java.util.Date date, String format) throws Exception{ ... } $$;

在这种情况下,没有错误,但仅考虑了声明的firest方法.

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 ?

推荐答案

对于 TO_CHAR, H2现在支持它,因为版本1.3.175(2013-01-18).

As for TO_CHAR, H2 support it now, since version 1.3.175 (2013-01-18).

H2确实支持函数重载.但是,存在局限性,因为不支持将此类功能声明为源代码.

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:

如果已编译该类并将其包含在类路径中,则可以将多个方法绑定到SQL函数.每个Java方法必须具有不同数量的参数.将函数声明为源代码时,不支持方法重载.

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.

这篇关于创建具有相同名称但不具有相同签名的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆