Исходный код СУБД 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
 
----------------------------------------
-- New Function Creation
CREATE OR REPLACE FUNCTION table_type_test()
RETURNS TABLE(id integer, name text) AS
$$
    yield return (1, "Alice");
    yield return (2, "Bob");
$$
LANGUAGE plcsharp;
 
-- Test Invocation
WITH data AS (SELECT * FROM table_type_test())
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'type-integrity',
        BOOL_AND(pg_typeof(id) = 'integer'::regtype AND pg_typeof(name) = 'text'::regtype) FROM data;
 
----------------------------------------
-- Looping test
 
CREATE OR REPLACE FUNCTION table_arg_test(lim int4)
RETURNS TABLE(id integer, name text) AS
$$
    return lim.HasValue
        ? Enumerable.Range(0, lim.Value).Select(i => ((int?)i, $"The number is {i}"))
        : Enumerable.Empty<(int? id, string? name)>();
$$
LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'data-integrity-1', SUM(id) = 45 
    FROM table_arg_test(10);
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'data-integrity-2', SUM(id) IS NULL
    FROM table_arg_test(NULL::int);
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'data-integrity-3', COUNT(*) = 0
    FROM table_arg_test(NULL::int);
 
----------------------------------------
-- array test
 
CREATE OR REPLACE FUNCTION table_array_test(lim int4)
RETURNS TABLE(id integer[], name text) AS
$$
            return lim.HasValue
                ? Enumerable.Range(0, lim.Value).Select(i => ((Array)new int?[] { i, i*i*i, null }, $"The number is {i}"))
                : Enumerable.Empty<(Array? id, string? name)>();
$$
LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'array-output-1', COUNT(*) = 5
    FROM table_array_test(5);
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'array-output-2', SUM(val) = 110
FROM table_array_test(5), UNNEST(id) AS val;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'array-output-3', SUM(val) = 110
FROM table_array_test(5), UNNEST(id) AS val
WHERE ARRAY_POSITION(id, NULL) = 3;
 
-- this is a disgusting way to confirm that none of the arrays
-- has NULL in the second column.
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'array-output-4', COALESCE(SUM(val) = 110, true)
FROM table_array_test(5), UNNEST(id) AS val
WHERE ARRAY_POSITION(id, NULL) = 2;
 
----------------------------------------
-- empty table
 
CREATE OR REPLACE FUNCTION table_empty_test(lim int4)
RETURNS TABLE(id integer[], name text) AS
$$
    yield break; // can't have an empty body, but this will do
$$
LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-table-function', 'empty-result-1', COUNT(*) = 0
FROM table_empty_test(5);