Java存储过程中ODCITableDescribe的实现

时间:2018-11-08 15:14:47

标签: java sql oracle plsql java-stored-procedures

当前问题是第二部分 of this ODCI related question.

我已经在Oracle SQL中实现了一个收集类型,该类型实际上定义为一种类型和该类型的表。

CREATE TYPE row_type AS OBJECT
(
  C1 VARCHAR2(50),
  C2 VARCHAR2(50),
  C3 VARCHAR2(50)
);
/

CREATE TYPE row_type_set AS TABLE OF row_type;

我还定义了ODCI类型,并将其实现为数据库中的Java存储过程:

SQL:

CREATE OR REPLACE TYPE ODCIImpl AS OBJECT (
  key INTEGER,

  STATIC FUNCTION ODCITableStart(sctx OUT ODCIImpl, cur SYS_REFCURSOR)
    RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'ODCIImpl.ODCITableStart(oracle.sql.STRUCT[], java.sql.ResultSet) return java.math.BigDecimal',

  MEMBER FUNCTION ODCITableFetch(self IN OUT ODCIImpl, nrows IN NUMBER,
                                 outSet OUT row_type_set) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'ODCIImpl.ODCITableFetch(java.math.BigDecimal, oracle.sql.ARRAY[]) return java.math.BigDecimal',

  MEMBER FUNCTION ODCITableClose(self IN ODCIImpl) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'ODCIImpl.ODCITableClose() return java.math.BigDecimal'

);
/

Java存储过程:

import java.io.*;
import java.util.*;
import oracle.sql.*;
import java.sql.*;
import java.math.BigDecimal;
import oracle.CartridgeServices.*;

// stored context type

public class StoredCtx
{
  ResultSet rset;
  public StoredCtx(ResultSet rs) { rset=rs; }
}

// implementation type

public class ODCIImpl implements SQLData 
{
  private BigDecimal key;

  final static BigDecimal SUCCESS = new BigDecimal(0);
  final static BigDecimal ERROR = new BigDecimal(1);
  final static int MAX_COLUMNS = 3;

  // Implement SQLData interface.

  String sql_type;
  public String getSQLTypeName() throws SQLException 
  {
    return sql_type;
  }

  public void readSQL(SQLInput stream, String typeName) throws SQLException 
  {
    sql_type = typeName;
    key = stream.readBigDecimal();
  }

  public void writeSQL(SQLOutput stream) throws SQLException 
  {
    stream.writeBigDecimal(key);
  }

  // type methods implementing ODCITable interface

  static public BigDecimal ODCITableStart(STRUCT[] sctx,ResultSet rset)
    throws SQLException 
  {
    Connection conn = DriverManager.getConnection("jdbc:default:connection:");

    // create a stored context and store the result set in it
    StoredCtx ctx=new StoredCtx(rset);

    // register stored context with cartridge services
    int key;
    try {
      key = ContextManager.setContext(ctx);
    } catch (CountException ce) {
      return ERROR;
    }

    // create a ODCIImpl instance and store the key in it
    Object[] impAttr = new Object[1];
    impAttr[0] = new BigDecimal(key); 
    StructDescriptor sd = new StructDescriptor("ODCIIMPL",conn);
    sctx[0] = new STRUCT(sd,conn,impAttr);

    return SUCCESS;
  }

  public BigDecimal ODCITableFetch(BigDecimal nrows, ARRAY[] outSet)
    throws SQLException 
  {
    Connection conn = DriverManager.getConnection("jdbc:default:connection:");

    // retrieve stored context using the key
    StoredCtx ctx;
    try {
      ctx=(StoredCtx)ContextManager.getContext(key.intValue());
    } catch (InvalidKeyException ik ) {
      return ERROR;
    }

    // get the nrows parameter, but return up to 10 rows
    int nrowsval = nrows.intValue();

    // create a vector for the fetched rows
    Vector v = new Vector(nrowsval);
    int i=0;

    StructDescriptor outDesc = 
      StructDescriptor.createDescriptor("ROW_TYPE", conn);
    Object[] out_attr = new Object[MAX_COLUMNS];

    ResultSetMetaData rsmd = ctx.rset.getMetaData();
    int columnsNumber = rsmd.getColumnCount();

    while(nrowsval>0 && ctx.rset.next()){
        for(int j = 0; j < columnsNumber; j++) {
            if(j == MAX_COLUMNS)
                break;
            out_attr[j] = (Object)ctx.rset.getString(j+1);
        }
      v.add((Object)new STRUCT(outDesc, conn, out_attr));
      i+=1;
      nrowsval-=1;
    }

    // return if no rows found
    if(i==0) return SUCCESS;

    // create the output ARRAY using the vector
    Object out_arr[] = v.toArray();
    ArrayDescriptor ad = new ArrayDescriptor("ROW_TYPE_SET",conn);
    outSet[0] = new ARRAY(ad,conn,out_arr);

    return SUCCESS;
  }

  public BigDecimal ODCITableClose() throws SQLException {

    // retrieve stored context using the key, and remove from ContextManager
    StoredCtx ctx;
    try {
      ctx=(StoredCtx)ContextManager.clearContext(key.intValue());
    } catch (InvalidKeyException ik ) {
      return ERROR;
    }

    // close the result set
    Statement stmt = ctx.rset.getStatement();
    ctx.rset.close();
    if(stmt!=null) stmt.close();

    return SUCCESS;
  }

}

所有这些之后,我实现了一个流水线函数,可以使用游标调用该函数。

CREATE OR REPLACE FUNCTION Exec_Remote_SQL_JSP(p SYS_REFCURSOR) RETURN row_type_set
  PIPELINED USING ODCIImpl;
/

我的问题现在是如何在Java存储过程中实现ODCITableDescribe方法,以便在仿真表中输出任何数据类型?首先,有可能吗?我似乎没有从 here here

的Oracle文档中找到与此相关的任何信息。

如果可能的话,可以解释的是,我们不再需要开头提到的集合类型。仿真表应具有与我们要从中选择信息的表相同的大小和数据类型。

0 个答案:

没有答案