本文介绍了在PostgreSQL函数中使用参数运行系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这个问题是否具体,但是我无法创建一个运行Linux shell命令的Postgres函数,但有一个细节:在插入后,它是触发器中的函数,我需要使用一些 NEW 列。

I'm not sure if I was specific in the question, but I'm having trouble creating a Postgres function that runs a Linux shell command, with one detail: it's a function in a Trigger after insert and I need to use some NEW columns.

在MySQL中,使用插件 MySQL UDF非常简单,触发就可以了像这样:

While in MySQL, using the plugin "MySQL UDF" it was pretty simple, trigger worked like this:

BEGIN
 DECLARE result int(10);
 SET result = sys_exec('/usr/bin/php /var/www/html/.../regras.php NEW.uniqueid NEW.linkedid NEW.eventtype');
END

但是在PostgreSQL上我尝试了PL / sh语言,这使得可以运行任何shell脚本,所以我写了以下函数:

But on PostgreSQL I tried the language PL/sh, wich enables running any shell script, so I wrote the following function:

   CREATE FUNCTION tarifador_func2() RETURNS TRIGGER
    LANGUAGE plsh
    AS $$
    #!/bin/sh
    /usr/bin/php /var/www/html/...regras.php NEW.uniqueid NEW.linkedid NEW.eventtype
   $$;

它确实以正确的方式执行.php文件,问题是语言无法识别 NEW 变量作为PHP的参数,因此在 args [] 中得到的是 NEW.uniqueid NEW.linkedid NEW.eventtype

It does execute the .php file in proper way, the problem is the language does not recognize the NEW variables I'm giving as arguments to the PHP, so in the args[] what I got is "NEW.uniqueid", "NEW.linkedid" and "NEW.eventtype".

因此,谁知道我该如何正确地在PL / sh中使用 NEW 参数?
另一个可能的解决方案可能是在创建触发器时通过参数手动设置我需要的三个值,但是不允许在参数中使用 NEW 。 / p>

So, anyone knows how can I properly use the NEW argument in PL/sh?Another possible solution might be to manually set the three values I need via the arguments on crating the trigger, but it's not allowed to use NEW in the arguments.

推荐答案

您可以在plsh触发器中访问一些值。

You can access some values in plsh triggers.


  • 更新仅提供旧的

  • 插入仅提供新的(duh)

  • 删除我没有test

因此,您可以使用$ 1,$ 2

So you get those values using arguments, like $1, $2

您的函数看起来像这样:

You function would look kinda like this:

CREATE FUNCTION tarifador_func2() RETURNS TRIGGER
LANGUAGE plsh
AS $$
#!/bin/sh
/usr/bin/php /var/www/html/...regras.php $3 $6 $1

$$;

请注意我没有使用 $ 1 $ 2 $ 3 ,这是因为 plsh 扩展名将 ALL 列转储为参数,以便在表中声明它们。因此,您可能会执行类似 INSERT INTO table1(column3)VALUES(6); 的操作,它将位于 $ 3 plsh ,假设这是表中的第三列。

Notice that I didn't use $1 $2 $3, that is because plsh extension dumps ALL columns into arguments in order they are declared in your table. So you might do something like INSERT INTO table1 (column3) VALUES (6); and it will be under $3 in plsh, assuming this is third column in table.

作为补充,触发器的元数据可以通过env获得vars。

As a side note, metadata of trigger is available thru env vars.

这篇关于在PostgreSQL函数中使用参数运行系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 04:44