You can directly export Oracle packages as Postgres Pro
packages
using ora2pgpro by setting the
TYPE configuration option to
PACKAGE.
ora2pgpro finds the package and
reconstructs the abstract syntax tree (AST) representing the source
code in the sources directory under the current
output directory. Then it generates equivalent code in
PL/pgSQL in the result
directory under the current output directory.
ora2pgpro aims to convert package code
in such a way that the resulting package is as functional as possible
and leaves WARNING messages in code places that
require programmer's attention.
This is an example of an Oracle package.
CREATE OR REPLACE PACKAGE pkgc IS
TYPE r_customer_type IS RECORD(
customer_name varchar2(50),
credit_limit number(10,2)
);
TYPE t_customer_type IS VARRAY(2)
OF r_customer_type;
PROCEDURE VARRAY_TEST AS
t_customers t_customer_type := t_customer_type();
rec r_customer_type;
tmp_string varchar2(2000);
BEGIN
t_customers.EXTEND;
t_customers(t_customers.LAST).customer_name := 'ABC Corp';
t_customers(t_customers.LAST).credit_limit := 10000;
FOR indx in 1 .. t_customers.COUNT LOOP
rec := t_customers(indx);
tmp_string := 'RECORD: ' || rec.customer_name || ', ' || rec.credit_limit;
insert into log_table (id, line) values (1+ indx, tmp_string);
END LOOP;
END;
BEGIN delete from log_table;
END pkgc;
The resulting Postgres Pro package looks as follows.
CREATE SCHEMA PKGC ;
CREATE OR REPLACE FUNCTION PKGC.__INIT__() RETURNS VOID AS $$
#package
BEGIN
delete from pkgc_log;
END;
$$ LANGUAGE plpgsql;
CREATE TYPE PKGC.r_customer_type AS (
customer_name varchar(50),
credit_limit numeric(10,2)
);
CREATE DOMAIN PKGC.t_customer_type PKGC.r_customer_type[];
CREATE OR REPLACE PROCEDURE PKGC.VARRAY_TEST () AS $$
#package
DECLARE
/*WARNING: collection constructors are not supported.*/
t_customers PKGC.t_customer_type /*:=*/ /*t_customer_type() /*varray_constructor_call*/*/;
rec PKGC.r_customer_type;
tmp_string varchar(2000);
BEGIN
t_customers = array_cat(t_customers, array_fill(NULL::PKGC.R_CUSTOMER_TYPE, ARRAY[1]));
t_customers[array_upper(T_CUSTOMERS, 1)].customer_name = 'ABC Corp';
t_customers[array_upper(T_CUSTOMERS, 1)].credit_limit = 10000;
FOR indx in 1 .. array_length(T_CUSTOMERS, 1) LOOP
rec = t_customers[indx];
tmp_string = 'RECORD: ' || rec.customer_name || ', ' || rec.credit_limit;
insert into pkgc_log (id, line) values (1+ indx, tmp_string);
END LOOP;
END; $$ LANGUAGE PLPGSQL;