2

我正在尝试从该函数的源代码为我的函数创建一个 CRUD 矩阵,因此我创建了一个将读取源代码的过程。

create or replace procedure test_ 
IS
  CURSOR c_text is 
    SELECT USER_SOURCE.TEXT 
      FROM USER_SOURCE 
     WHERE USER_SOURCE.name='TEST_FUNCTION' 
       AND USER_SOURCE.type='FUNCTION';
     order by line;

  v_single_text varchar2(4000);
  v_tmp_text varchar2(10000) := ' ';
begin
  open c_text;
  loop
   fetch c_text into v_single_text;
   exit when c_text%notfound;
   v_tmp_text := v_tmp_text|| chr(10) || rtrim(v_single_text);
   dbms_output.put_line(v_single_text);
  end loop;
  close c_text;
end test_;

这对我很有用,我得到了我想要的功能的源代码。这是一个非常简单的函数,我用它来学习 PL/SQL。该过程的输出如下所示。

function test_funkction Return varchar2
IS
  kpp_value varchar2(20); 
begin
  select KPP 
    into kpp_value 
    from CUSTOMER 
   where CUSTOMER_ID = 200713;

  dbms_output.put_line (kpp_value);
  Return kpp_value;
end test_function;

现在,如何解析我在输出中得到的字符串以获得所需的结果,我的结果应该是这样的

==TABLE_NAME==========OPERATIONS==

  CUSTOMER              - R - -

==================================

现在我设法做到了。

但它只适用于我的简单功能,现在我想制作一个适用于任何功能的程序。源代码如下。

create or replace procedure test_


IS


v_string_fnc varchar2(10000) := UPPER('function test_function



Return varchar2

IS

kpp_value varchar2(20);



  begin



  select KPP into kpp_value from CUSTOMER where CUSTOMER_ID = 200713;



  dbms_output.put_line (kpp_value);



 Return kpp_value;



 end test_function;');


 v_check PLS_INTEGER;


 CURSOR c_text is
 SELECT USER_SOURCE.TEXT
 FROM USER_SOURCE
 WHERE USER_SOURCE.name = 'TEST_FUNCTION'
 AND USER_SOURCE.type = 'FUNCTION'
 order by line;


 v_single_text varchar2(4000);
 v_tmp_text    varchar2(10000) := ' ';


 /*v_string      varchar2(10000);*/


 insert_flag char := '-';
 read_flag   char := '-';
 update_flag char := '-';
 delete_flag char := '-';
 empty_space char(34) := '                             ';
 underline   char(42) := '==========================================';


 /*v_txt         varchar2(10000) := ' ';*/


 result_table varchar2(1000) := '/';


 begin


 open c_text;


 loop
 fetch c_text
 into v_single_text;
 exit when c_text%notfound;
 v_tmp_text := v_tmp_text || chr(10) || rtrim(v_single_text);

/* print source code*/
/*dbms_output.put_line(v_single_text);*/

 end loop;


 close c_text;


 /*DELETE SEARCH*/


 v_check := instr(v_string_fnc, 'DELETE ');


 if v_check < 1 then
 dbms_output.put_line('THERE IS NO DELETE COMMAND');
 else
 dbms_output.put_line('THERE IS A DELETE COMMAND');
 delete_flag  := 'D';
 v_check      := instr(v_string_fnc, 'FROM ');
 v_check      := v_check + 5;
 result_table := substr(v_string_fnc, v_check);
 result_table := substr(result_table, 0, instr(result_table, ' '));
 dbms_output.put_line('TABLE AFFECTED BY DELETE: ' || result_table);
 end if;


 /*SELECT SEARCH*/


 v_check := instr(v_string_fnc, 'SELECT ');
 if v_check < 1 then
 dbms_output.put_line('THERE IS NO READ COMMAND');
 else
 dbms_output.put_line('THERE IS A READ COMMAND');
 read_flag    := 'R';
 v_check      := instr(v_string_fnc, 'FROM ');
 v_check      := v_check + 5;
 result_table := substr(v_string_fnc, v_check);
 result_table := substr(result_table, 0, instr(result_table, ' '));
 dbms_output.put_line('TABLE AFFECTED BY READ: ' || result_table);

 end if;


 /*UPDATE SEARCH*/
 v_check := instr(v_string_fnc, 'UPDATE ');
 if v_check < 1 then
 dbms_output.put_line('THERE IS NO UPDATE COMMAND');
 else
 dbms_output.put_line('THERE IS A UPDATE COMMAND');
 update_flag  := 'U';
 v_check      := instr(v_string_fnc, 'FROM ');
 v_check      := v_check + 5;
 result_table := substr(v_string_fnc, v_check);
 result_table := substr(result_table, 0, instr(result_table, ' '));
 dbms_output.put_line('TABLE AFFECTED BY UPDATE: ' || result_table);

 end if;


 /*INSERT SEARCH*/
 v_check := instr(v_string_fnc, 'INSERT ');
 if v_check < 1 then
 dbms_output.put_line('THERE IS NO CREATE COMMAND');
 else
 dbms_output.put_line('THERE IS A CREATE COMMAND');
 insert_flag  := 'C';
 v_check      := instr(v_string_fnc, 'FROM ');
 v_check      := v_check + 5;
 result_table := substr(v_string_fnc, v_check);
 result_table := substr(result_table, 0, instr(result_table, ' '));
 dbms_output.put_line('TABLE AFFECTED BY CREATE: ' || result_table);
 end if;
 dbms_output.put_line(' ');
 dbms_output.put_line('==========' || 'TABLE_NAME' || '==========' ||
                   'OPERATIONS' || '==');
 dbms_output.put_line(empty_space || insert_flag || read_flag ||
                   update_flag || delete_flag);
 dbms_output.put_line(underline);


 end test_;

通过该过程,我可以提取和输出我的代码,dbms 需要进行一些清理,但它会给出我需要的结果。现在有几个问题,如何将我的函数的源代码放入未预定义的变量中,这里是 v_string_fnc 但它需要预定义才能工作。以及如何将某个操作与表链接,在我的示例中很简单,一个 SELECT 和关键字 FROM 给我一个表名。挣扎仍在继续

4

1 回答 1

1

它完成的更大部分,只是在此之后的调整。

v_check := instr2(v_string_fnc, 'DROP ');

if v_check > 0 then

  delete_flag  := 'D';
  v_check      := instr2(v_string_fnc, 'TABLE ', v_check);
  v_check      := v_check + 6;
  result_table := substr(v_string_fnc, v_check);
  rest_string  := result_table;
  result_table := substr(result_table, 0, instr(result_table, ' '));
  result_table := rtrim(result_table);
  result_table := rtrim(result_table, ';');
  merge into result_set
  using dual
  on (tables_used = result_table)
  when matched then
    update set drop_operation = delete_flag
  when not matched then
    insert
      (tables_used, drop_operation)
    values
      (result_table, delete_flag);

  while v_check > 0 loop

    v_check := instr2(rest_string, 'DROP ');

    if v_check > 0 then
      delete_flag  := 'D';
      v_check      := instr2(rest_string, 'TABLE ', v_check);
      v_check      := v_check + 6;
      result_table := substr(rest_string, v_check);
      rest_string  := result_table;
      result_table := substr(result_table, 0, instr(result_table, ' '));
      result_table := rtrim(result_table);
      result_table := rtrim(result_table, ';');
      merge into result_set
      using dual
      on (tables_used = result_table)
      when matched then
        update set drop_operation = delete_flag
      when not matched then
        insert
          (tables_used, drop_operation)
        values
          (result_table, delete_flag);
    end if;
  end loop;
end if;
于 2013-09-30T09:53:59.980 回答