Исходный код СУБД 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
using System;
using System.Collections.Generic;
using Xunit;
 
public abstract class BaseTriggerTestFunctionModifyTests : PlDotNetTest
{
    protected abstract string FunctionBody { get; }
    protected abstract LanguageType Language { get; }
 
    public BaseTriggerTestFunctionModifyTests()
    {
        FunctionInfo = new SqlFunctionInfo
        {
            Name = "trigger_test_function_modify",
            Arguments = new List<FunctionArgument>(),
            ReturnType = "TRIGGER",
            Body = FunctionBody,
            Language = Language,
            IsStrict = false
        };
    }
 
    public static object[][] TestCases()
    {
        return new[]
        {
            new object[]
            {
                "c#-trigger",
                "rowModifiedByTrigger",
                "message = 'MODIFIED Text!!!'",
                "WHERE id = 2"
            }
        };
    }
 
    [Theory]
    [MemberData(nameof(TestCases))]
    public void TestTriggerTestFunctionModify(
        string featureName,
        string testName,
        string customAssertion,
        string querySuffix
    )
    {
        var createFunctionSql = GetFunctionDefinition(FunctionInfo);
        ExecuteSql(createFunctionSql);
 
        var triggerSql = @"
DROP TRIGGER IF EXISTS test_trigger_BIR_1 ON trigger_test_table;
CREATE TRIGGER test_trigger_BIR_1
  BEFORE INSERT ON trigger_test_table
  FOR EACH ROW
  WHEN (NEW.id = 2)
  EXECUTE FUNCTION trigger_test_function_modify('BEFORE/INSERT/ROW','1');
";
        ExecuteSql(triggerSql);
 
        var cte = @"
WITH cte AS (
    INSERT INTO trigger_test_table (id, message)
    VALUES (2, 'Inserted Second Text')
    RETURNING *
)
";
 
        RunTestWithSuffix(
            featureName:     featureName,
            testName:        testName,
            cteStatement:    cte,
            customAssertion: customAssertion,
            querySuffix:     querySuffix,
            forceCte:        true
        );
    }
}
 
[Trait("Language", "CSharp")]
[Trait("Category", "Trigger")]
public class TriggerTestFunctionModifyTestsCSharp : BaseTriggerTestFunctionModifyTests
{
    protected override string FunctionBody => @"
    if (tg.Arguments[1] != ""1""){
        throw new SystemException($""Assertion failed: wrong trigger argument, '{tg.Arguments[1]}' != '1'"");
    }
 
    if ((int)tg.NewRow[0] != 2) {
        throw new SystemException($""Assertion failed: wrong row value, {tg.NewRow[1]} != 2"");
    }
 
    tg.NewRow[1] = ""MODIFIED Text!!!"";
    return ReturnMode.TriggerModify;
    ";
    protected override LanguageType Language => LanguageType.PlcSharp;
}