пользовательский тип в postgres как оракул - PullRequest
0 голосов
/ 20 ноября 2018

Может ли кто-нибудь помочь мне реализовать определенные пользователем типы в postgres?

как я могу воспроизвести ту же функциональность, что и в следующих типах оракулов в postgres:

CREATE TYPE gr_data AS (
col1 numeric, col2 varchar(30)
);

CREATE OR REPLACE TYPE GT_DATA      IS TABLE OF GR_DATA;
CREATE OR REPLACE TYPE GT_TEXT      IS TABLE OF VARCHAR2(255) INDEX BY BINARY_INTEGER ;  
CREATE OR REPLACE TYPE GN_CPT       IS TABLE OF NUMBER        INDEX BY VARCHAR2(30);

Код Postgres такой же, как нижевведите GT_CPT (NomTable) и GT_DATA ().Кто-нибудь может объяснить, почему тип GT_DATA () используется здесь как функция?

    create or replace function test() returns void as $body$
declare
NomTable text:= 'tab1';
tes text;
--Result text;-- not required
  lt_Result GT_DATA := GT_DATA();
begin
GT_CPT(NomTable) := tes;
--Result   GT_DATA := GT_DATA(); -- modified
     lt_Result.EXTEND;
     lt_Result(lt_Result.LAST) := lr_Result;
end $body$
;

1 Ответ

0 голосов
/ 20 ноября 2018

is table of обычно делается с использованием массивов в Postgres.

Эквивалентом INDEX BY VARCHAR2(30) является, вероятно, значение JSONB (пара ключ / значение) или значение hstore .В обоих случаях вы теряете безопасность типов, которую может предложить is table of number index by varchar.

Мне очень неясно, чего вы пытаетесь достичь (особенно как вы хотите использовать table of number, но, возможно, следующееВы начнете:

create type gr_data as 
(
   col1 numeric, col2 varchar(30)
);

create or replace function test() 
   returns void as $body$
declare
  NomTable text:= 'tab1';
  tes text := 'some value';
  result gr_data[];
  gt_cpt jsonb := '{}'; 
begin
  -- this assigns 'some value' to the key 'tab1' in the jsonb key/value pair
  -- similar to GT_CPT(NomTable) := tes;
  gt_cpt := jsonb_set(gt_cpt, array[nomtable], tes);

  -- this assigns an empty array to the variable result
  -- if I remember correctly that's the same as Oracle's type constructor GT_DATA()
  result := array[];
end 
$body$
language plpgsql;
...