简体   繁体   中英

Rewrite MySQL Trigger to Postgres

guys!

I have a trigger in MySQL database:

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

So I need to rewrite it to make it works in Postgres database. As there's ON DUPLICATE KEY UPDATE I'm using Postgres version 9.5 with UPSERT (ON CONFLICT (KEY) DO UPDATE) .

So I poorly know SQL language can you tell me what I'm doing wrong? There's the Postgres PL code:

  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;

As I want to test this trigger I'm getting an error -- relation "subscriptions.statistics_active_count" does not exist

Can you help me with that code?

Finally I've got the solution. I guess :)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM