暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle Java中的用户定义聚合函数

askTom 2017-09-04
211

问题描述

我想实现一个SQL用户定义的聚合函数语言Java.我开始用一种 “hello world” 功能,这是微不足道的代码在PL/SQL-请参阅https://livesql.oracle.com/apex/livesql/s/finvtl1sfarg4x2ywvvdiwrjh

在下一步中,我使用Java类定义了一个类型来实现ODCIAggregate函数,如下所示:

CREATE OR REPLACE 
TYPE j_hw_type AS OBJECT
  EXTERNAL NAME 'JHelloWorld' LANGUAGE JAVA 
  USING SQLData(  

   STATIC FUNCTION
        ODCIAggregateInitialize(sctx IN OUT j_hw_type )
        RETURN NUMBER
        EXTERNAL NAME 'ODCIAggregateInitialize(JHelloWorld) return int',

   MEMBER FUNCTION
        ODCIAggregateIterate(self IN OUT j_hw_type ,
                             VALUE IN VARCHAR2 )
        RETURN NUMBER
        EXTERNAL NAME 'ODCIAggregateIterate(String) return int',
        
   MEMBER FUNCTION
        ODCIAggregateTerminate(self IN j_hw_type,
                               returnValue OUT  VARCHAR2,
                               flags IN NUMBER)
        RETURN NUMBER
        EXTERNAL NAME 'ODCIAggregateTerminate(String, int) return int',
        
   MEMBER FUNCTION
        ODCIAggregateMerge(self IN OUT j_hw_type,
                           ctx2 IN StringAggType)
        RETURN NUMBER
        EXTERNAL NAME 'ODCIAggregateMerge(JHelloWorld) return int'
);
/
复制


但是编译失败了

PLS-00235: the external type is not appropriate for the parameter
复制


我认为这是将IN OUT参数传递给Java方法的问题。
在这一点上,我被卡住了,我没有找到一些有用的引用或Java实现的用户定义的聚合函数的例子。
任何帮助都将受到高度赞赏。

专家解答

您觉得需要使用Java的任何特殊原因?你确定你不能用PL/SQL做到这一点?;)

你的Java代码的来源到底是什么?

可以将out参数与Java包装器一起使用。但是:

In Java and other object-oriented languages, a method cannot assign values to objects passed as arguments. When calling a method from SQL or PL/SQL, to change the value of an argument, you must declare it as an OUT or IN OUT parameter in the call specification. The corresponding Java parameter must be an array with only one element.

You can replace the element value with another Java object of the appropriate type, or you can modify the value, if the Java type permits. Either way, the new value propagates back to the caller. For example, you map a call specification OUT parameter of the NUMBER type to a Java parameter declared as float[] p, and then assign a new value to p[0].


http://docs.oracle.com/database/122/JJDEV/defining-call-specifications.htm#JJDEV13260

您可以在 (寻找raiseSal) 上看到一个示例:

http://docs.oracle.com/database/122/JJDEV/writing-object-type-call-specifications.htm#JJDEV13272
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论