Package
1. Objective
IvorySQL provides compatibility for Oracle packages. A package is an encapsulated collection of related program objects stored together in the database. Program objects are procedures, functions, variables, constants, cursors, and exceptions.
This document aims to provide a comprehensive understanding of the process of implementing custom packages.
2. Function descriptions
IvorySQL provides compatibility for Oracle custom packages, including creation, alteration, and deletion of packages and package bodies. We also add support for package-related commands in the PostgreSQL interactive terminal (psql) with the new \dk command.
2.1. Create the specification of a package
The CREATE OR REPLACE PACKAGE statement is used to create or replace the specification of a package. A package is a collection of related stored procedures, functions, and other program objects, stored as a single unit in the database. The package specification declares these objects, and the package body defines them.
Creating a package specification requires CREATE on the target schema and USAGE on the plisql language. Replacing an existing specification also requires the privileges of the package owner. Superusers can perform these operations.
2.2. Create package body
The CREATE OR REPLACE PACKAGE BODY statement creates or replaces the definitions declared by a package specification. It requires CREATE on the target schema and an existing specification in that schema. Replacing an existing body also requires the privileges of the package owner. Superusers can perform these operations.
When the package specification contains cursors or subprograms, a package body must be present to define them. Otherwise, the package body is optional.
2.3. Alter package
The ALTER PACKAGE statement modifies package properties and requires the privileges of the package owner, or superuser privileges.
2.4. Drop package and package body
The DROP PACKAGE statement deletes a package in the database. This statement will remove both the package body and the package specification.The DROP PACKAGE BODY statement only deletes the package body.
DROP PACKAGE requires the privileges of the package owner or the schema owner, or superuser privileges. DROP PACKAGE BODY requires the privileges of the owning package’s owner, or superuser privileges.
2.5. DISCARD PACKAGE
The DISCARD PACKAGE functionality is implemented for compatibility with PostgreSQL’s DISCARD feature.
2.6. Use \dk[+] in psql for package and package body information
In psql, \dk[+] is used to view the definition information of packages and package bodies.
Command |
Descriptions |
\dk[+] |
List the package information currently visible. |
\dk[+] xxx |
List the package specification and package body content of the xxx package. |
3. Test cases
3.1. Create the specification of a package
ivorysql=# create or replace package pkg is
ivorysql-# var1 integer;
ivorysql-# var2 integer;
ivorysql-# function test_f(id integer) return integer;
ivorysql-# procedure test_p(id integer);
ivorysql-# end;
ivorysql-# /
CREATE PACKAGE
3.2. Create package body
ivorysql=# create or replace package body pkg is
ivorysql-# var3 integer;
ivorysql-# function test_f(id integer) return integer is
ivorysql-# begin
ivorysql-# dbms_output.put_line('pkg test_f');
ivorysql-# return id;
ivorysql-# end;
ivorysql-# procedure test_p(id integer) is
ivorysql-# begin
ivorysql-# dbms_output.put_line('pkg proc');
ivorysql-# end;
ivorysql-# --privite function
ivorysql-# function test_piv1(id integer) return integer is
ivorysql-# begin
ivorysql-# return id;
ivorysql-# end;
ivorysql-# --privite procedure
ivorysql-# procedure test_piv2(id integer) is
ivorysql-# begin
ivorysql-# dbms_output.put_line('privite proc');
ivorysql-# end;
ivorysql-# begin
ivorysql-# var1 := 1;
ivorysql-# var2 := 2;
ivorysql-# var3 := 4;
ivorysql-# end;
ivorysql-# /
CREATE PACKAGE BODY
3.4. Drop package and package body
ivorysql=# Drop package pkg;
DROP PACKAGE
ivorysql=# Drop package body pkg;
DROP PACKAGE BODY
3.6. Use \dk[+] in psql for package and package body information
ivorysql=# \dk
List of packages
Schema | Name | Owner
--------+----------+----------
public | pkg | ivorysql
public | test_pkg | ivorysql
(2 rows)
ivorysql=# \dk pkg
List of packages
Schema | Name | Owner
--------+------+----------
public | pkg | ivorysql
(1 row)
ivorysql=# \dk pkg1
Did not find any package named "pkg1".
ivorysql=# \dk+
List of packages
Schema | Name | Owner | Security | Editionable | Use Collation | Specification | Package Body
--------+----------+----------+----------+-------------+---------------+-----------------------------------------------+-----------------------------------------------------
---------
public | pkg | ivorysql | definer | Editionable | default | var1 integer; +|
| | | | | | var2 integer; +|
| | | | | | function test_f(id integer) return integer; +|
| | | | | | procedure test_p(id integer); +|
| | | | | | end |
public | test_pkg | ivorysql | definer | Editionable | default | var1 integer; +| FUNCTION test_f(id integer) RETURN integer IS
+
| | | | | | FUNCTION test_f(id integer) RETURN integer;+| BEGIN
+
| | | | | | end | dbms_output.put_line('invoke function test_pkg.t
est_f');+
| | | | | | | RETURN 23;
+
| | | | | | | end;
+
| | | | | | | BEGIN
+
| | | | | | | var1 := 23;
+
| | | | | | | end
(2 rows)