mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-25 22:01:19 +00:00
refactor combine_transition_tables
This commit is contained in:
parent
5a18acacff
commit
59178d29a8
1 changed files with 13 additions and 23 deletions
|
@ -36,40 +36,30 @@ BEGIN
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- Selects both old and new rows in a trigger and allows using `sum(count_diff)` to get the number to add to a count
|
-- Selects both old and new rows in a trigger. Column 1 is -1 if old and 1 if new, which can be used with `sum` to get
|
||||||
|
-- the number to add to a count. Column 2 is the original row as a composite value.
|
||||||
CREATE FUNCTION r.combine_transition_tables (tg_op text)
|
CREATE FUNCTION r.combine_transition_tables (tg_op text)
|
||||||
RETURNS SETOF record
|
RETURNS SETOF record
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
IF (TG_OP = 'UPDATE') THEN
|
IF (TG_OP IN ('UPDATE', 'DELETE')) THEN
|
||||||
RETURN QUERY
|
RETURN QUERY
|
||||||
SELECT
|
SELECT
|
||||||
-1 AS count_diff,
|
-1,
|
||||||
old_table AS affected_row
|
|
||||||
FROM
|
|
||||||
old_table
|
old_table
|
||||||
UNION ALL
|
|
||||||
SELECT
|
|
||||||
1 AS count_diff,
|
|
||||||
new_table AS affected_row
|
|
||||||
FROM
|
|
||||||
new_table;
|
|
||||||
ELSIF (TG_OP = 'INSERT') THEN
|
|
||||||
RETURN QUERY
|
|
||||||
SELECT
|
|
||||||
1 AS count_diff,
|
|
||||||
*
|
|
||||||
FROM
|
|
||||||
new_table;
|
|
||||||
ELSE
|
|
||||||
RETURN QUERY
|
|
||||||
SELECT
|
|
||||||
-1 AS count_diff,
|
|
||||||
*
|
|
||||||
FROM
|
FROM
|
||||||
old_table;
|
old_table;
|
||||||
END IF;
|
END IF;
|
||||||
|
IF (TG_OP IN ('UPDATE', 'INSERT')) THEN
|
||||||
|
RETURN QUERY
|
||||||
|
SELECT
|
||||||
|
1,
|
||||||
|
new_table
|
||||||
|
FROM
|
||||||
|
new_table;
|
||||||
|
END IF;
|
||||||
|
RETURN;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue