使用行类型或记录文本调用Postgresql过程

使用行类型或记录文本调用Postgresql过程,postgresql,stored-procedures,plpgsql,postgresql-12,Postgresql,Stored Procedures,Plpgsql,Postgresql 12,我正在构建一个食谱应用程序。我这样做只是为了练习在Postgresql中工作: postgres=# select version(); version ----------------------------------------------------------------------

我正在构建一个食谱应用程序。我这样做只是为了练习在Postgresql中工作:

postgres=# select version();
                                                 version                                                      
------------------------------------------------------------------------------------------------------------------
PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
(1 row)

我有几个表格用于存储基于配方的信息;其中一个与成分列表有关:

CREATE TABLE ingredient (
    id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name TEXT NOT NULL,
    quantity NUMERIC NOT NULL,
    unit TEXT,
    display_order INT NOT NULL
);
我正在编写一个名为
save\u recipe
的函数,到目前为止它就是这个样子

CREATE OR REPLACE PROCEDURE save_recipe
  (
  first_name COOK.FIRST_NAME%TYPE,
  last_name COOK.LAST_NAME%TYPE,
  email COOK.EMAIL%TYPE,
  recipe_name RECIPE.NAME%TYPE,
  recipe_cook_time RECIPE.COOK_TIME%TYPE,
  recipe_preface RECIPE.PREFACE%TYPE,
  instructions RECIPE.INSTRUCTIONS%TYPE,
  ingred INGREDIENT
  )
AS $$
DECLARE
  cook_id COOK.ID%TYPE;
  recipe_id RECIPE.ID%TYPE;
BEGIN

  INSERT INTO cook (first_name, last_name, email)
    VALUES (first_name, last_name, email)
    RETURNING id INTO cook_id;

  INSERT INTO recipe (name, cook_id, created_at, cook_time, instructions, preface)
    VALUES (recipe_name, cook_id, now(), recipe_cook_time, instructions, recipe_preface)
    RETURNING id INTO recipe_id;

  INSERT INTO ingredient (name, quantity, unit, display_order)
    VALUES (ingred.name, ingred.quantity, ingred.unit, ingred.display_order);

  COMMIT;

  RAISE NOTICE 'Cook ID : %', cook_id;
  RAISE NOTICE 'Recipe ID : %', recipe_id;

END;
$$
LANGUAGE plpgsql;
但我在创建成分文字时遇到了问题(如果这是正确的话)。这是我目前所能做的最好的事情:

CALL save_recipe(
  first_name => 'Joe',
  last_name => 'Fresh',
  email => 'joe@loblaws.com',
  recipe_name => 'Cherry Pie',
  recipe_cook_time => '1 hour',
  recipe_preface =>'I love cherry pie.',
  instructions => ARRAY['Make.', 'Bake.', 'Eat.'],
  ingred => (0, 'Cherry', 20, 'small, pitted', 1)
);
我希望
ingred
成为一个数组,但我更关心的是,我需要填充
component.id
,即使我在插入过程中忽略它(因为我想使用Postgres提供的生成id)。是否有一种结构/类型可以使用,而不需要像这样指定虚拟ID(最终可以是
数组
类型)


提前感谢。

将参数声明为该类型的。使用
unnest()
将数组的元素获取为行(该函数为可读性而简化):

示例用法:

CALL save_recipe(
    first_name => 'Joe',
    last_name => 'Fresh',
    email => 'joe@loblaws.com',
    variadic ingred => array[
        '(0, Apple, 2, small, 1)', 
        '(0, Pear, 5, large, 2)',
        '(0, Cherry, 20, "small, pitted", 1)'
    ]::ingredient[]
);

select *
from ingredient;

 id |  name   | quantity |      unit      | display_order
----+---------+----------+----------------+---------------
  1 |  Apple  |        2 |  small         |             1
  2 |  Pear   |        5 |  large         |             2
  3 |  Cherry |       20 |  small, pitted |             1
(3 rows)
请注意,如果没有命名参数,过程调用可能会简单得多:

CALL save_recipe(
    'Joe', 'Fresh', 'joe@loblaws.com',
    '(0, Apple, 2, small, 1)', 
    '(0, Pear, 5, large, 2)',
    '(0, Cherry, 20, "small, pitted", 1)'
);

我以前读过这篇文章,但我希望有更好的文章。这可能是我的错。我从我实际使用的代码中做了一个太简单的例子,我不确定
VARIADIC
是否适用于我的情况(基于我刚才读到的内容)。我会更新这个问题。非常感谢您再次访问答案。我真的很感激。它工作得很好。我在成分中插入语句时出错,但后来我意识到我对原始语句的更改不够。它需要是
插入到…选择…从…
而不是
插入到…值…从…
。再次感谢你。
CALL save_recipe(
    'Joe', 'Fresh', 'joe@loblaws.com',
    '(0, Apple, 2, small, 1)', 
    '(0, Pear, 5, large, 2)',
    '(0, Cherry, 20, "small, pitted", 1)'
);