2015-09-04 2 views
2

Привет Я создаю процедуру для вставки метаданных. Я создал типы, и я включил 1 тип в другой тип и в процедуре, которую я выполняю, чтобы получить значение. Поскольку я новичок в PostgreSQL Может ли кто-нибудь помочь мне в том, как вызвать процедуру. Входной параметр типапользовательский тип в качестве входных параметров в функции PostgreSQL

Create Type Form_details as(
        formName character varying(100), 
        submittedBy numeric, 
         createdDate date,  
        updatedBy numeric, 
        updatedDate date, 
        comments character varying(500), 
        Sections Section[] 

) 

create type Section as (
          sectionName character varying(100), 
          sectionLabel character varying(100),       
          sectionOrder numeric       



        ) 

и процедура я написал это

CREATE OR REPLACE FUNCTION form_insertion(formdetails form_details[]) 
    RETURNS character varying AS 
$BODY$ 

DECLARE 
     form_details_seq integer; 
     section_seq integer; 
     formName character varying(100); 
     submittedBy numeric; 
     createdDate date;  
     comments character varying(500); 
       formStatusId numeric; 

     sectionOrder numeric; 
     sectionName character varying(100); 
     sectionLabel character varying(100); 
     attributeId numeric; 

     I integer; 
     J integer; 
begin 
FOR I IN 1..formdetails.COUNT 

LOOP 

formName    :=formdetails[I].formName; 
formStatusId   :=formdetails[I].formStatusId; 
comments    :=formdetails[I].comments; 
    RAISE NOTICE '%', formName; 

    FOR J IN 1..formdetails.Section.COUNT 
    LOOP 

    sectionName    :=formdetails[I].Section[J].sectionName; 
RAISE NOTICE '%', sectionName; 

    END LOOP ; 


END LOOP; 

Return formName,sectionName; 
end; 
$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100; 

Это не полная процедура. Но я пытаюсь проверить это. Не могли бы вы сообщить мне, подходит ли мой подход и как я могу проверить его со стороны БД. Как я пройду этот параметр. Кстати, тип, который я создал, - это объект Java. эта процедура будет вызывать из конца Java. Любая помощь будет принята с благодарностью.

ответ

1

Чтобы вызвать функцию из SQL-запроса, вы должны указать параметры для своего пользовательского типа, как в следующем примере.

select form_insertion(array[ 
    cast(row('Form 1', 1, current_date, 1, current_date, 'This is form 1', 
     array[ 
      cast(row('section-1', 'Section One', 1) as section), 
      cast(row('section-2', 'Section Two', 2) as section), 
      cast(row('section-3', 'Section Three', 3) as section) 
     ] 
    ) as form_details), 
    cast(row('Form 2', 2, current_date, 1, current_date, 'This is form 2', 
     array[ 
      cast(row('section-2', 'Section Two', 2) as section), 
      cast(row('section-3', 'Section Three', 3) as section) 
     ] 
    ) as form_details), 
    cast(row('Form 3', 1, current_date, 1, current_date, 'This is form 3', 
     array[ 
      cast(row('section-1', 'Section One', 1) as section), 
      cast(row('section-3', 'Section Three', 3) as section) 
     ] 
    ) as form_details) 
]) 

Обратите внимание, что массивы PostgreSQL не имеют свойства .COUNT. Вы можете перебирать массив по диапазону индекса с array_upper функции:

for i IN 1..array_upper(formdetails, 1) 
LOOP 
    -- your code here 
END LOOP; 

С PostgreSQL 9.1, вы можете использовать оператор FOREACH Переберите массива:

create or replace function form_insertion(formdetails form_details[]) 
    returns varchar as $$ 
declare 
    detail form_details; 
    sec section; 
begin 
    foreach detail in array formdetails 
    LOOP 
     RAISE NOTICE '%', detail.formName; 

     foreach sec in array detail.sections 
     LOOP 
     raise NOTICE '%', sec.sectionName; 
     END LOOP; 
    END LOOP; 
    return ''; 
end;$$ 
    language plpgsql; 
Смежные вопросы