I have script for Oracle. I don't know, how this part of the script should be moved to PostgreSQL.
I rewrite function generating primary key to PostgreSQL. Can you help me?
Oracle code:
cGet cursor(pTABLE_NAME NVARCHAR2) is
select OBJECT_ID
from SYS_OBJECT
where OBJECT_NAME = lower(pTABLE_NAME);
How to write it on PostgreSQL?
Full code:
CREATE OR REPLACE function mySchema.genPrimKey(pTABLE_NAME NVARCHAR2) return NVARCHAR2 is
xResult NVARCHAR2(10);
xNextVal NUMBER;
xSQL VARCHAR2(1000);
lcRegistr1 NVARCHAR2(1);
...
lnRegistr1 NUMBER;
...
cursor cGet(pTABLE_NAME NVARCHAR2) is
select OBJECT_ID
from SYS_OBJECT
where OBJECT_NAME = lower(pTABLE_NAME);
rGet cGet%ROWTYPE;
TYPE tArray is table of NVARCHAR2(1) index by binary_integer;
xArrayLV tArray;
begin
open cGet(pTABLE_NAME);
fetch cGet into rGet;
close cGet;
if (rGet.Object_Id is null) then
raise eObjNull;
end if;
xSQL:='select SEQ$'||upper(replace(trim(pTABLE_NAME),'_'))||'.nextval from dual';
execute immediate xSQL into xNextVal;
if (xNextVal is null) then
raise eNextVal;
end if;
if (62 * 62 * 62 * 62 * 62) < xNextVal then
raise eExtend;
end if;
lcRegistr1:= '0';
...
lnRegistr1:= 0;
...
xArrayLV( 1):= '0';
xArrayLV( 2):= '1';
...
xArrayLV(61):= 'y';
xArrayLV(62):= 'z';
--look 5 registr
IF (1 * 62 * 62 * 62 * 62) < xNextVal then
FOR i in 1..62 loop
IF (i * 62 * 62 * 62 * 62) < xNextVal then
lnRegistr1:= i * 62 * 62 * 62 * 62;
lcRegistr1:= xArrayLV(i + 1);
end if;
end loop;
END IF;
xNextVal:= xNextVal - lnRegistr1;
....
xResult:= lcRegistr1 || lcRegistr2 || lcRegistr3 || lcRegistr4 || lcRegistr5;
return rGet.Object_Id||'!'||xResult;
exception
...
end genPrimKey;
If you need - I can send script to you by email. It's Oracle code and I should rewrite it to PostgreSQL, but I don't understood this part.
No cursor needed:
if not exists (select * from pg_tables where tablename = lower(ptable_name)) then
raise 'Table % not found', lower(ptable_name);
end if;
A (very) rough translation of the PL/SQL code, would probably be something like this.
CREATE OR REPLACE function mySchema.genPrimKey(pTABLE_NAME text)
returns text
language plpgsql
as
$body$
declare
xResult VARCHAR(10);
xNextVal integer;
lcRegistr1 char1(1);
...
lnRegistr1 NUMBER;
...
xArrayLV char(1)[];
begin
if not exists select * from pg_tables where tablename = lower(ptable_name) then
raise 'Table % not found', ptable_name;
end if;
xNextval := nextval('SEQ$'||upper(replace(trim(pTABLE_NAME),'_')))
if (xNextVal is null) then
raise 'Sequence % not found', 'SEQ$'||upper(replace(trim(pTABLE_NAME),'_';
end if;
if (62 * 62 * 62 * 62 * 62) < xNextVal then
raise 'Value too large';
end if;
lnRegistr1:= 0;
xarraylv := array['0','1','2', .... , 'z'];
--look 5 registr
IF (1 * 62 * 62 * 62 * 62) < xNextVal then
FOR i in 1..62 loop
IF (i * 62 * 62 * 62 * 62) < xNextVal then
lnRegistr1:= i * 62 * 62 * 62 * 62;
lcRegistr1:= xArrayLV[i + 1];
end if;
end loop;
END IF;
xNextVal:= xNextVal - lnRegistr1;
....
xResult:= lcRegistr1 || lcRegistr2 || lcRegistr3 || lcRegistr4 || lcRegistr5;
return rGet.Object_Id||'!'||xResult;
exception
...
end;
$$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.