将MySQL触发器重写为Postgres

时间:2015-08-10 12:52:26

标签: mysql sql postgresql stored-procedures

人!

我在MySQL数据库中有一个触发器:

CREATE DEFINER="root"@"127.0.0.1" TRIGGER `tai_actions_for_active_count` AFTER INSERT ON `actions` FOR EACH ROW BEGIN
  DECLARE l_isnn TINYTEXT;
  IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN

    SET l_isnn = IF(NEW.isnn is NULL, '*', NEW.isnn);
    IF NOT NEW.action_type = 'CLOSE' THEN
      INSERT INTO subscriptions.`statistics_active_count`(`service_id`, `operator_id`, `isnn`, `active_count`)
        VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1)
        ON DUPLICATE KEY UPDATE active_count = active_count + 1;
    ELSE
      SET @tai_actions_for_active_count = -1;
      UPDATE subscriptions.`statistics_active_count` SET active_count = @tai_actions_for_active_count := active_count - 1
        WHERE `service_id` = NEW.service_id AND `operator_id` = NEW.operator_id AND `isnn` = l_isnn;
      IF @tai_actions_for_active_count = 0 THEN DELETE FROM subscriptions.`statistics_active_count` WHERE `active_count` = 0; END IF;
    END IF;
  END IF;
END

所以我需要重写它以使其在Postgres数据库中运行。由于ON DUPLICATE KEY UPDATE我正在使用Postgres版本9.5 UPSERT (ON CONFLICT (KEY) DO UPDATE)

所以我很不清楚SQL语言你能告诉我我做错了什么吗?有Postgres PL代码:

  DECLARE
    l_isnn TEXT;
    tai_actions_for_active_count INTEGER;
  BEGIN
    IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
        IF NEW.isnn is NULL THEN
            l_isnn := '*';
        ELSE
            l_isnn := NEW.isnn;
        END IF;
        IF NOT NEW.action_type = 'CLOSE' THEN
            INSERT INTO "subscriptions.statistics_active_count"(service_id, operator_id, isnn, active_count)
                VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1)
                ON CONFLICT(active_count) DO UPDATE SET active_count = active_count + 1;
        ELSE
            tai_actions_for_active_count := -1;
            UPDATE "subscriptions.statistics_active_count" SET active_count = active_count - 1
        -- (tai_actions_for_active_count := active_count - 1)
                WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
            UPDATE "subscriptions.statistics_active_count" SET tai_actions_for_active_count = active_count
                WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
            IF tai_actions_for_active_count = 0 THEN DELETE FROM "subscriptions.statistics_active_count" WHERE active_count = 0; END IF;
        END IF;
    END IF;
    RETURN NULL;
  END;

由于我想测试此触发器,我收到错误 - relation "subscriptions.statistics_active_count" does not exist

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

最后我得到了解决方案。我想:)

BEGIN
   IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
      IF NEW.isnn IS NULL THEN
         l_isnn := '*';
      ELSE
         l_isnn := NEW.isnn;
      END IF;
      IF NOT NEW.action_type = 'CLOSE' THEN
         BEGIN
            INSERT INTO subscriptions.statistics_active_count (service_id, operator_id, isnn, active_count)
            VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1);
         EXCEPTION WHEN unique_violation THEN
            UPDATE subscriptions.statistics_active_count  SET active_count = active_count + 1
            WHERE service_id = NEW.service_id and operator_id=NEW.operator_id;
         END;
      ELSE
         tai_actions_for_active_count := -1;
         WITH upd AS
         (UPDATE subscriptions.statistics_active_count
         SET active_count = active_count - 1
         WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
         RETURNING active_count)
         SELECT *
         FROM upd INTO tai_actions_for_active_count;
         IF tai_actions_for_active_count = 0 THEN
            DELETE FROM public.statistics_active_count
            WHERE active_count = 0;
         END IF;
      END IF;
   END IF;
END;