编写用于触发器的普通 moddatetime 程序。

huangapple go评论50阅读模式
英文:

Write plain moddatetime procedute to use in triggers

问题

我的云服务提供商不允许我安装`moddatetime`扩展。然而,我的数据库架构依赖于`moddatetime`过程。

我该如何编写一个纯粹的PSQL替代品,代替这个扩展中的`moddatetime(colname)`函数?([这是该扩展的规范][1]

```sql
CREATE FUNCTION moddatetime() RETURNS trigger
  -- ???

<details>
<summary>英文:</summary>

My cloud provider won&#39;t allow me to install the `moddatetime` extension. However, my DB schema relies on the `moddatetime` procedure.

How could I write a plain PSQL drop-in replacement for the `moddatetime(colname)` function that can be used in place of this extension ? ([Here is the specification of this extension][1])

```sql
CREATE FUNCTION moddatetime() RETURNS trigger
  -- ???

答案1

得分: 0

以下函数将充当moddatetime的即插即用替代品。


CREATE OR REPLACE FUNCTION moddatetime() RETURNS trigger LANGUAGE plpgsql AS $moddatetime$
  DECLARE
    colname name;
  BEGIN
    IF (TG_NARGS = 1) THEN
      colname = TG_ARGV[0];
    ELSE
      RAISE EXCEPTION 'moddatetime(colname)需要一个参数';
    END IF;

    RETURN json_populate_record(NEW, json_build_object(colname, NOW()));
  END;
$moddatetime$;

如果你想使用原生的,如果无法安装原生扩展,则退回到这个:


DO $SETUP_MODDATETIME$
BEGIN
  CREATE EXTENSION IF NOT EXISTS "moddatetime";
EXCEPTION WHEN INSUFFICIENT_PRIVILEGE THEN
  -- 无法使用moddatetime扩展,因此创建一个函数
  CREATE OR REPLACE FUNCTION moddatetime() RETURNS trigger LANGUAGE plpgsql AS $moddatetime$
    DECLARE
      colname name;
    BEGIN
      IF (TG_NARGS = 1) THEN
        colname = TG_ARGV[0];
      ELSE
        RAISE EXCEPTION 'moddatetime(colname)需要一个参数';
      END IF;

      RETURN json_populate_record(NEW, json_build_object(colname, NOW()));
    END;
  $moddatetime$;
END $SETUP_MODDATETIME$;

英文:

The following function will act as a drop-in replacement for moddatetime.


CREATE OR REPLACE FUNCTION moddatetime() RETURNS trigger LANGUAGE plpgsql AS $moddatetime$
  DECLARE
    colname name;
  BEGIN
    IF (TG_NARGS = 1) THEN
      colname = TG_ARGV[0];
    ELSE
      RAISE EXCEPTION &#39;moddatetime(colname) requires one argument&#39;;
    END IF;

    RETURN json_populate_record(NEW, json_build_object(colname, NOW()));
  END;
$moddatetime$;

If you want to use the native one and fallback if the native extension cannot be installed, use the following:


DO $SETUP_MODDATETIME$
BEGIN
  CREATE EXTENSION IF NOT EXISTS &quot;moddatetime&quot;;
EXCEPTION WHEN INSUFFICIENT_PRIVILEGE THEN
  -- Unable to use moddatetime extension, so create a function instead
  CREATE OR REPLACE FUNCTION moddatetime() RETURNS trigger LANGUAGE plpgsql AS $moddatetime$
    DECLARE
      colname name;
    BEGIN
      IF (TG_NARGS = 1) THEN
        colname = TG_ARGV[0];
      ELSE
        RAISE EXCEPTION &#39;moddatetime(colname) requires one argument&#39;;
      END IF;

      RETURN json_populate_record(NEW, json_build_object(colname, NOW()));
    END;
  $moddatetime$;
END $SETUP_MODDATETIME$;

huangapple
  • 本文由 发表于 2023年4月19日 22:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定