【问题标题】:How can i pass array of composite type from nodejs to postgresql function?如何将复合类型的数组从 nodejs 传递给 postgresql 函数?
【发布时间】:2015-12-13 03:01:49
【问题描述】:

我有一个 postgres 函数:

CREATE OR REPLACE FUNCTION add_new_views(VARIADIC all_view event_view[])
  RETURNS void AS
$BODY$
DECLARE
    view event_view;
BEGIN
    FOREACH view IN ARRAY all_view LOOP 
        INSERT INTO t_event_views VALUES (view.event_id, view.device_code);
    END LOOP;
END;
$BODY$

event_view - 这是我的复合类型:

CREATE TYPE event_view (event_id uuid, device_code text);

我可以像这样通过 pgAdmin 使用我的功能:

SELECT add_new_views(VARIADIC ARRAY [
    ('b9d78fc3-b55a-452e-b935-8ce4f1e79284', 'asd')::event_view,
    ('b9d78fc3-b55a-452e-b935-8ce4f1e79284', 'asd')::event_view
]);

但我需要从 nodejs 调用它。要与 postgresql 连接,我使用 node-postgres 'pg' 模块。

我发送这个请求:

query = 'SELECT add_new_views(VARIADIC [$1])';
current_client.query(query, allViews, function(err ,res) {
    if(err) console.log(err);
    console.log(res)
})

allViews - 数组如下所示:

[ [ '(b9d78fc3-b55a-452e-b935-8ce4f1e79284)', '(CHUPERM)' ],
  [ '(b9d78fc3-b55a-452e-b935-8ce4f1e79284)', '(HUE)' ],
  [ '(b9d78fc3-b55a-452e-b935-8ce4f1e79284)', '(HU)' ],
  [ '(00ae9a6c-781f-48d1-ab42-bfa87d09bdbb)', '(HITE)' ],
  [ '(00ae9a6c-781f-48d1-ab42-bfa87d09bdbb)', '(HIJFIRM)' ],
  [ '(00ae9a6c-781f-48d1-ab42-bfa87d09bdbb)', '(DVERMNYEZAPILI)' ],
  [ '(00ae9a6c-781f-48d1-ab42-bfa87d09bdbb)', '(TICHTOMENYANEVIDISH)' ],
  [ '(00ae9a6c-781f-48d1-ab42-bfa87d09bdbb)', '(HITEBEAD)' ] ]

作为回应,我得到:

{ [error: malformed record literal: "(b9d78fc3-b55a-452e-b935-8ce4f1e79284)"]
  name: 'error',
  length: 133,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Too few columns.',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'rowtypes.c',
  line: '183',
  routine: 'record_in' }

如果从元素 allView 数组中删除括号,我会收到错误“等待括号”。 当我尝试传递给 postgres 简单的一维数组时,一切正常(ofc 我更改了 postgre 函数中所需的所有内容)。但是有了这个复合类型数组,我什么也做不了。请帮忙。对不起我的英语不好。

【问题讨论】:

  • 我忘记了,我尝试生成这样的所有视图:[ { event_id: 'b9d78fc3-b55a-452e-b935-8ce4f1e79284', device_code: 'CHUPERM' }, .... ]。作为回应,我得到了错误。
  • 您有没有找到解决您遇到的问题的方法?

标签: arrays node.js postgresql


【解决方案1】:

你确定你需要在每一个语句之间加上括号吗?我在您从 pgadmin 发布的示例中看到 htat,它们没有括号,可能您应该删除它们

[ [ 'b9d78fc3-b55a-452e-b935-8ce4f1e79284', 'CHUPERM' ],
  [ 'b9d78fc3-b55a-452e-b935-8ce4f1e79284', 'HUE' ],
  [ 'b9d78fc3-b55a-452e-b935-8ce4f1e79284', 'HU' ],
  [ '00ae9a6c-781f-48d1-ab42-bfa87d09bdbb', 'HITE' ],
  [ '00ae9a6c-781f-48d1-ab42-bfa87d09bdbb', 'HIJFIRM' ],
  [ '00ae9a6c-781f-48d1-ab42-bfa87d09bdbb', 'DVERMNYEZAPILI' ],
  [ '00ae9a6c-781f-48d1-ab42-bfa87d09bdbb', 'TICHTOMENYANEVIDISH' ],
  [ '00ae9a6c-781f-48d1-ab42-bfa87d09bdbb', 'HITEBEAD' ] ]

【讨论】:

  • 我的意思是() 为什么你把它们放在数组里?而在您在 pgadmin 中执行的查询中,您没有它们
  • 对不起,我不明白。我在问题的结尾写了关于删除括号(括号)的内容。如果我删除它们,我会收到一个错误“等待括号”。
【解决方案2】:

答案可能会迟到,但可以解决这个问题。 node-postgres 需要的正确语法是:

[
   '(b9d78fc3-b55a-452e-b935-8ce4f1e79284, CHUPERM)',
   '(b9d78fc3-b55a-452e-b935-8ce4f1e79284, HUE)',
    ...
]

需要一维数组中行的字符串表示,而不是二维数组。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2019-02-05
    • 2019-09-01
    • 1970-01-01
    • 2019-04-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    相关资源
    最近更新 更多