《Oracle PL/SQL开发指南》学习笔记32——源码调试——包(第二部分,包规范)

 

1. 查询用户定义的对象的源码

SQL> /* Formatted on 2018/12/9 22:46:49 (QP5 v5.256.13226.35538) */
SQL> SELECT line, text
  2    FROM user_source
  3   WHERE UPPER (name) = UPPER ('&input_name');
输入 input_name 的值:  TEST
原值    3:  WHERE UPPER (name) = UPPER ('&input_name')
新值    3:  WHERE UPPER (name) = UPPER ('TEST')

LINE TEXT
---- ------------------------------
   1 PROCEDURE test (abc NUMBER)
   2 IS
   3 BEGIN
   4    DBMS_OUTPUT.put_line (abc);


   5 END test;

2. 包规范原型

CREATE [OR REPLACE] PACKAGE package_name
[ EDITIONABLE | NONEDITIONABLE ]
[ AUTHID {DEFINER | CURRENT_USER} ]
[ ACCESSIBLE BY
( FUNCTION some_schema.function_name
[, PROCEDURE some_schema.procedure_name
[, PACKAGE some_schema.package_name
[, TYPE some_schema.object_type_name]]] )] IS
[PRAGMA SERIALLY_REUSABLE;]
[variable_name [CONSTANT] scalar_data_type [:= value];]
[collection_name [CONSTANT] collection_data_type [:= constructor];]
[object_name [CONSTANT] object_data_type [:= constructor];]
[TYPE record_structure IS RECORD
( field_name data_type
[, field_name data_type
[, ... ]]);]
[CURSOR cursor_name
[(parameter_name data_type
[,parameter_name data_type
[, ... ]])] IS
select_statement;]
[TYPE ref_cursor IS REF CURSOR [RETURN { catalog_row | record_structure }];]
[user_exception_name EXCEPTION;
[PRAGMA EXCEPTION_INIT(user_exception_name,-20001);]]
[FUNCTION function_name
[( parameter [IN][OUT] [NOCOPY] sql_data_type | plsql_data_type
[, parameter [IN][OUT] [NOCOPY] sql_data_type | plsql_data_type
[, ... ]])]
RETURN { sql_data_type | plsql_data_type }
[ DETERMINISTIC | PARALLEL_ENABLED ]
[ PIPELINED ]
[ RESULT_CACHE [ RELIES_ON (table_name) ]];]
[ PRAGMA RESTRICT_REFERENCES ({ DEFAULT | function_name }
, option [, option [, ... ]]); ]
[PROCEDURE procedure_name
[( parameter1 [IN][OUT] [NOCOPY] sql_data_type | plsql_data_type
[, parameter2 [IN][OUT] [NOCOPY] sql_data_type | plsql_data_type
[, parameter(n+1) [IN][OUT] [NOCOPY] sql_data_type | plsql_data_type])];]
END package_name;
/

3. 白名单

SQL> ed
已写入 file afiedt.buf

  1  CREATE OR REPLACE PACKAGE small_one
  2     ACCESSIBLE BY (FUNCTION gateway,
  3                    PROCEDURE backdoor,
  4                    PACKAGE api,
  5                    TYPE hobbit)
  6  IS
  7     FUNCTION add (lv_a NUMBER, lv_b NUMBER)
  8        RETURN NUMBER;
  9* END small_one;
SQL> /

程序包已创建。

SQL> ed
已写入 file afiedt.buf

  1  /* Formatted on 2018/12/9 23:06:06 (QP5 v5.256.13226.35538) */
  2  CREATE OR REPLACE PACKAGE BODY small_one
  3  IS
  4     FUNCTION add (lv_a NUMBER, lv_b NUMBER)
  5        RETURN NUMBER
  6     IS
  7     BEGIN
  8        RETURN lv_a + lv_b;
  9     END add;
 10* END small_one;
SQL> /

程序包体已创建。

SQL> /* Formatted on 2018/12/9 23:07:20 (QP5 v5.256.13226.35538) */
SQL> CREATE OR REPLACE FUNCTION gateway (pv_a NUMBER, pv_b NUMBER)
  2     RETURN NUMBER
  3  IS
  4  BEGIN
  5     RETURN small_one.add (pv_a, pv_b);
  6  END;
  7  /

函数已创建。

SQL> declare
  2  result number;
  3  begin
  4  result := small_one.add(1,2);
  5  end;
  6  /
result := small_one.add(1,2);
          *
第 4 行出现错误:
ORA-06550: 第 4 行, 第 11 列:
PLS-00904: 没有足够的权限访问对象 SMALL_ONE
ORA-06550: 第 4 行, 第 1 列:
PL/SQL: Statement ignored


SQL> begin
  2  dbms_output.put_line(gateway(1,2));
  3  end;
  4  /
3

PL/SQL 过程已成功完成。

 

你可能感兴趣的:(数据库(DB))