In Oracle, how do I verify the object type used from an object type hierarchy?

折月煮酒 提交于 2019-12-24 11:16:08

问题


I have a Type hierarchy in an Oracle Schema:

CREATE OR REPLACE TYPE FV AS OBJECT (
   idno           NUMBER)
NOT FINAL;
/

CREATE TYPE FV_Integer UNDER FV (
   features INTEGER_ARRAY)
   NOT FINAL;
/


CREATE TYPE FV_Number UNDER FV (
   features NUMBER_ARRAY)
   NOT FINAL;
/

I want to build a PLSQL function that veryfies which type of the hierarchy is an object: for a function dummy(obj1 FV, obj2 FV)... how can I check what is the object type of the hierarchy the user is using?

For example, I want to print the objects type names (the function is for ilustration, it is not a real pl/sql code):

 dummy(obj1 FV, obj2 FV){
      if obj1%type = FV_INTEGER
          THEN print 'FV_INTEGER'
      endif
      if obj2%type = FV_NUMBER
          THEN print 'FV_NUMBER'
      endif
}

回答1:


You can inspect the type of an object using sys.anydata:

create or replace function which_type
    ( p_fv fv )
    return varchar2
as
begin
    return sys.anydata.gettypename(sys.anydata.convertobject(p_fv));
end which_type;

Test:

create or replace type number_array as table of number;
create or replace type integer_array as table of integer;

create or replace type fv as object (
   idno           number)
not final;
/

create type fv_integer under fv (
   features integer_array)
   not final;
/

create type fv_number under fv (
   features number_array)
   not final;
/

create table fv_test (my_fv fv);

insert into fv_test values (fv(1));
insert into fv_test values (fv_integer(1, integer_array(1)));
insert into fv_test values (fv_number(1, number_array(1)));

select which_type(my_fv) from fv_test;

WHICH_TYPE(MY_FV)
-------------------------
WILLIAM.FV
WILLIAM.FV_INTEGER
WILLIAM.FV_NUMBER

3 rows selected.



回答2:


Use user_types dictionary view to create the function :

SQL> set serveroutput on;
SQL> create or replace function chk_typ_obj( i_type_name user_types.type_name%type )
  2  return pls_integer is
  3    o_val pls_integer;
  4  begin
  5    for c in
  6      (
  7      select decode(t.incomplete,'NO',1,0) icomp
  8        from user_types t
  9       where t.type_name = i_type_name
 10      )
 11      loop
 12        o_val := c.icomp;
 13      end loop;
 14  
 15        return o_val;
 16  end;
 17  /

Function created

SQL> var o_val number;
SQL> exec :o_val := chk_typ_obj('FV');

PL/SQL procedure successfully completed
o_val
---------
1

SQL> exec :o_val := chk_typ_obj('FV_NUMBER');

PL/SQL procedure successfully completed
o_val
---------
0

The declaration is incomplete, if it omits the AS OBJECT in that.

So, if chk_typ_obj

  • returns 1, then it's of TYPE OBJECT

  • returns 0, then it's not of TYPE OBJECT

  • rturns null, then there's no such type called by that name



来源:https://stackoverflow.com/questions/51033733/in-oracle-how-do-i-verify-the-object-type-used-from-an-object-type-hierarchy

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