Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-04-30 9f2ef60949ced4fab4d077a7e1c6d913f3620c9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
----------------------------------------
-- Table creation
DROP TABLE IF EXISTS trigger_test_table;
CREATE TABLE trigger_test_table (
    id int,
    message text
);
 
----------------------------------------
-- Helpful note:
--
-- Use this to delete all triggers:
--
-- echo "SELECT
--     event_object_table as table,
--     trigger_name,
--     event_manipulation as event,
--     action_timing as timing,
--     action_statement
-- FROM
--     information_schema.triggers;
-- "|psql|grep EXEC| awk '{print "drop trigger " $3 " on " $1 ";";}'
----------------------------------------
 
 
----------------------------------------
-- Function creation
 
CREATE OR REPLACE FUNCTION trigger_test_function_modify_fsharp() RETURNS TRIGGER AS $$
        if tg.Arguments.[1] <> "1" then
            raise (SystemException($"Assertion failed: wrong trigger argument, '{tg.Arguments.[1]}' != '1'"))
        if (unbox<int> tg.NewRow.[0]) <> 2 then
            raise (SystemException($"Assertion failed: wrong row value, {tg.NewRow.[1]} != 2"))
 
        tg.NewRow.[1] <- "MODIFIED Text with F#!!!"
        ReturnMode.TriggerModify
$$ LANGUAGE plfsharp;
 
CREATE OR REPLACE FUNCTION trigger_test_skip_fsharp() RETURNS TRIGGER AS $$
        if tg.Arguments.[1] <> "2" then
            raise (SystemException($"Assertion failed: wrong trigger argument, '{tg.Arguments.[1]}' != '2'"))
 
        if (unbox<int> tg.NewRow.[0]) = 5 then
            ReturnMode.TriggerSkip
        else
            ReturnMode.Normal;
$$ LANGUAGE plfsharp;
 
CREATE OR REPLACE FUNCTION trigger_test_tg_vals_fsharp() RETURNS TRIGGER AS $$
        if unbox<int> tg.NewRow.[0] = 6 then
            if tg.TriggerName = "test_trigger_bir_3" &&
                tg.TriggerWhen = "BEFORE" &&
                tg.TriggerLevel = "ROW" &&
                tg.TriggerEvent = "INSERT" &&
                tg.RelationId > 0 &&
                tg.TableName = "trigger_test_table" &&
                tg.TableSchema = "public" &&
                tg.NewRow.Length = 2 &&
                unbox<int> tg.NewRow.[0] = 6 &&
                tg.Arguments.[0] = "BEFORE/INSERT/ROW" &&
                tg.Arguments.[1] = "3" then
                    tg.NewRow.[1] <- "TG value assertions passed"
                    ReturnMode.TriggerModify
            else
                Elog.Error($"failed test, tg values didn't check out: {tg}")
                ReturnMode.Normal
        else
            Elog.Error($"failed test, tg values didn't check out: {tg}")
            ReturnMode.Normal;
$$ LANGUAGE plfsharp;
 
-- This test isn't automatically effective, but it can be manually checked.
CREATE OR REPLACE FUNCTION trigger_test_exception_fsharp() RETURNS TRIGGER AS $$
        raise (SystemException(("This is a test of exception handling")))
        ReturnMode.Normal; // unreached
$$ LANGUAGE plfsharp;
 
CREATE OR REPLACE FUNCTION trigger_test_update_type_fsharp() RETURNS TRIGGER AS $$
        tg.NewRow.[1] = 1;
        ReturnMode.TriggerModify;
$$ LANGUAGE plfsharp;
 
 
----------------------------------------
-- Trigger creation
 
-- encoding for test case:
-- B/A = BEFORE/AFTER
-- I/U/D/T = INSERT/UPDATE/DELETE/TRUNCATE
-- R/S = ROW/STATEMENT
 
CREATE OR REPLACE TRIGGER test_trigger_BIR_1
BEFORE INSERT ON trigger_test_table
FOR EACH ROW
WHEN (new.id = 2)
EXECUTE FUNCTION trigger_test_function_modify_fsharp('BEFORE/INSERT/ROW', 1);
 
CREATE OR REPLACE TRIGGER test_trigger_BIR_2
BEFORE INSERT ON trigger_test_table
FOR EACH ROW
WHEN (new.id = 5)
EXECUTE FUNCTION trigger_test_skip_fsharp('BEFORE/INSERT/ROW', 2);
 
CREATE OR REPLACE TRIGGER test_trigger_BIR_3
BEFORE INSERT ON trigger_test_table
FOR EACH ROW
WHEN (new.id = 6)
EXECUTE FUNCTION trigger_test_tg_vals_fsharp('BEFORE/INSERT/ROW', 3);
 
CREATE OR REPLACE TRIGGER test_trigger_BIR_4
BEFORE INSERT ON trigger_test_table
FOR EACH ROW
WHEN (new.id = 7)
EXECUTE FUNCTION trigger_test_update_type_fsharp('BEFORE/INSERT/ROW', 4);
 
CREATE OR REPLACE TRIGGER test_trigger_AUS_4
AFTER UPDATE ON trigger_test_table
FOR EACH STATEMENT
EXECUTE FUNCTION trigger_test_exception_fsharp ('AFTER/UPDATE/STATEMENT', 4);
 
----------------------------------------
-- Data manipulation
 
INSERT INTO trigger_test_table VALUES (1, 'Inserted Text');
INSERT INTO trigger_test_table VALUES (2, 'Inserted Second Text');
INSERT INTO trigger_test_table VALUES (3, 'Inserted Third Text (for updating)');
INSERT INTO trigger_test_table VALUES (4, 'Inserted Fourth Text (for deletion)');
INSERT INTO trigger_test_table VALUES (5, 'Inserted Fifth Text (for simple skip)');
INSERT INTO trigger_test_table VALUES (6, 'Inserted Sixth Text (for values check modify)');
INSERT INTO trigger_test_table VALUES (7, 'This should be a string');
UPDATE trigger_test_table SET message ='Updated Text' WHERE id=1;
UPDATE trigger_test_table SET id=13, message ='Updated Third Text' WHERE id=3;
DELETE FROM trigger_test_table WHERE id=4;
 
-- Test for zero rows affected, but per-statement trigger fires
UPDATE trigger_test_table SET id=2, message='Updated Text' WHERE id=1234;
 
 
-- Test 1: Check if 'id' values are only 1, 2, 6, 7, or 13
-- This ensures that the delete worked, and we have no extra values
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'validRows',
       NOT EXISTS (SELECT 1 FROM trigger_test_table WHERE id NOT IN (1, 2, 6, 7, 13));
 
-- Test 2: Check 'b' value for 'a' = 1
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'rowUpdated',
       (SELECT message = 'Updated Text' FROM trigger_test_table WHERE id = 1);
 
-- Test 3: Check 'b' value for 'a' = 2
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'rowModifiedByTrigger',
       (SELECT message = 'MODIFIED Text with F#!!!' FROM trigger_test_table WHERE id = 2);
 
-- Test 4: Check 'b' value for 'a' = 13
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'rowUpdated-2',
       (SELECT message = 'Updated Third Text' FROM trigger_test_table WHERE id = 13);
 
-- Test 5: Check that skip works
-- (already checked in Test 1, but this is specific)
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'skipWorks',
       NOT EXISTS (SELECT 1 FROM trigger_test_table WHERE id = 5);
 
-- Test 6: Check that tg values are correct
-- (already checked in Test 1, but this is specific)
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'tgValuesCorrect',
       EXISTS (SELECT 1 FROM trigger_test_table WHERE id = 6 and message = 'TG value assertions passed');
 
-- Test 7: Ensure that type-mismatched updates are ignored
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-trigger', 'tgTypeMismatchHandling',
       EXISTS (SELECT 1 FROM trigger_test_table WHERE id = 7 and message = 'This should be a string');