Исходный код СУБД 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
BEGIN;
 
----------------------------------------------------------------------
-- First, we test simple use cases for INOUT and OUT
----------------------------------------------------------------------
 
-- Test inout_basic_0 for inout values (INOUT)
CREATE OR REPLACE FUNCTION inout_basic_0(INOUT argument_0 INT) AS $$
    if(argument_0 != 0){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    argument_0 = 1;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-basic-0', 'inout_basic_0', inout_basic_0(0) = 1;
 
-- Test inout_basic_1 for inout values (OUT)
CREATE OR REPLACE FUNCTION inout_basic_1(OUT argument_0 INT) AS $$
    argument_0 = 1;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-basic-1', 'inout_basic_1', inout_basic_1() = 1;
 
----------------------------------------------------------------------
-- Second, we test NULL for INOUT and OUT
----------------------------------------------------------------------
 
-- Test that non-null input can give null output on INOUT
CREATE OR REPLACE FUNCTION inout_null_1(INOUT argument_0 INT) AS $$
    if(argument_0 is null ){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    argument_0 = null;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-1', 'inout_null_1', inout_null_1(11) IS NULL;
 
-- Test that null input can give non-null output on INOUT
CREATE OR REPLACE FUNCTION inout_null_2(INOUT argument_0 INT) AS $$
    if(argument_0 != null ){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    argument_0 = 3;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-2', 'inout_null_2', inout_null_2(NULL) = 3;
 
-- Test that OUT can be null
CREATE OR REPLACE FUNCTION inout_null_3(OUT argument_0 INT) AS $$
    argument_0 = null;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-3', 'inout_null_3', inout_null_3() IS NULL;
 
-- Test that non-null input can give null output on INOUT, with multiple args
CREATE OR REPLACE FUNCTION inout_null_4(INOUT argument_0 INT, in argument_1 INT) AS $$
    if(argument_0 is null ){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    if(argument_1 != 3){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    argument_0 = null;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-4', 'inout_null_4', inout_null_4(11, 3) IS NULL;
 
-- Test that null input can give non-null output on INOUT, with multiple args
CREATE OR REPLACE FUNCTION inout_null_5(INOUT argument_0 INT, IN argument_1 INT) AS $$
    if(argument_0 != null ){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    if(argument_1 != 3){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    argument_0 = 3;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-5', 'inout_null_5', inout_null_5(NULL, 3) = 3;
 
-- Test that OUT can be null, or not, with multiple args
CREATE OR REPLACE FUNCTION inout_null_6(IN argument_0 INT, OUT argument_1 INT) AS $$
    if(argument_0 == 1){ argument_1 = null; }
    else if(argument_0 == 2){ argument_1 = 2; }
    else { throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-6', 'inout_null_6', inout_null_6(1) IS NULL;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-null-7', 'inout_null_6', inout_null_6(2) = 2;
 
----------------------------------------------------------------------
-- Third, we test large argument sets, with first arguments being
-- each of IN/INOUT/OUT
----------------------------------------------------------------------
 
-- Test inout_multiarg_1 for inout values (IN, INOUT, IN, OUT, OUT, INOUT, IN, OUT)
CREATE OR REPLACE FUNCTION inout_multiarg_1(IN argument_0 INT, INOUT argument_1 INT, IN argument_2 INT, OUT argument_3 INT, OUT argument_4 INT, INOUT argument_5 INT, IN argument_6 INT, OUT argument_7 INT) AS $$
    if(argument_0 != 0){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    if(argument_1 != 1){ throw new SystemException($"Failed assertion: argument_1 = {argument_1}");}
    argument_1 = 2;
    if(argument_2 != 2){ throw new SystemException($"Failed assertion: argument_2 = {argument_2}");}
    argument_3 = 4;
    argument_4 = 5;
    if(argument_5 != null){ throw new SystemException($"Failed assertion: argument_5 = {argument_5}");}
    argument_5 = 6;
    if(argument_6 != 6){ throw new SystemException($"Failed assertion: argument_6 = {argument_6}");}
    argument_7 = null;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-multiarg-1', 'inout_multiarg_1', inout_multiarg_1(0, 1, 2, NULL, 6) = ROW(2, 4, 5, 6, NULL::INT);
 
-- Test inout_multiarg_2 for inout values (INOUT, OUT, INOUT, INOUT, IN, OUT, IN, OUT)
CREATE OR REPLACE FUNCTION inout_multiarg_2(INOUT argument_0 INT, OUT argument_1 INT, INOUT argument_2 INT, INOUT argument_3 INT, IN argument_4 INT, OUT argument_5 INT, IN argument_6 INT, OUT argument_7 INT) AS $$
    if(argument_0 != null){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
    argument_0 = 1;
    argument_1 = 2;
    if(argument_2 != 2){ throw new SystemException($"Failed assertion: argument_2 = {argument_2}");}
    argument_2 = 3;
    if(argument_3 != 3){ throw new SystemException($"Failed assertion: argument_3 = {argument_3}");}
    argument_3 = null;
    if(argument_4 != 4){ throw new SystemException($"Failed assertion: argument_4 = {argument_4}");}
    argument_5 = 6;
    if(argument_6 != 6){ throw new SystemException($"Failed assertion: argument_6 = {argument_6}");}
    argument_7 = 8;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-multiarg-2', 'inout_multiarg_2', inout_multiarg_2(NULL, 2, 3, 4, 6) = ROW(1, 2, 3, NULL::INT, 6, 8);
 
-- Test inout_multiarg_3 for inout values (OUT, IN, IN, OUT, INOUT, OUT, IN, INOUT)
CREATE OR REPLACE FUNCTION inout_multiarg_3(OUT argument_0 INT, IN argument_1 INT, IN argument_2 INT, OUT argument_3 INT, INOUT argument_4 INT, OUT argument_5 INT, IN argument_6 INT, INOUT argument_7 INT) AS $$
    argument_0 = null;
    if(argument_1 != 1){ throw new SystemException($"Failed assertion: argument_1 = {argument_1}");}
    if(argument_2 != 2){ throw new SystemException($"Failed assertion: argument_2 = {argument_2}");}
    argument_3 = 4;
    if(argument_4 != 4){ throw new SystemException($"Failed assertion: argument_4 = {argument_4}");}
    argument_4 = 5;
    argument_5 = 6;
    if(argument_6 != 6){ throw new SystemException($"Failed assertion: argument_6 = {argument_6}");}
    if(argument_7 != 7){ throw new SystemException($"Failed assertion: argument_7 = {argument_7}");}
    argument_7 = 8;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-multiarg-3', 'inout_multiarg_3', inout_multiarg_3(1, 2, 4, 6, 7) = ROW(NULL::INT, 4, 5, 6, 8);
 
-- Test inout_multiarg_4inout values (IN, INOUT, IN, OUT, OUT, INOUT, INOUT, IN)
-- This one makes sure that `STRICT` works fine.
CREATE OR REPLACE FUNCTION inout_multiarg_4(IN argument_0 INT, INOUT argument_1 INT, IN argument_2 INT, OUT argument_3 INT, OUT argument_4 INT, INOUT argument_5 INT, INOUT argument_6 INT, IN argument_7 INT) AS $$
        if(argument_0 != 0){ throw new SystemException($"Failed assertion: argument_0 = {argument_0}");}
        if(argument_1 != 1){ throw new SystemException($"Failed assertion: argument_1 = {argument_1}");}
        argument_1 = 2;
        if(argument_2 != 2){ throw new SystemException($"Failed assertion: argument_2 = {argument_2}");}
        argument_3 = 4;
        argument_4 = 5;
        if(argument_5 != 5){ throw new SystemException($"Failed assertion: argument_5 = {argument_5}");}
        argument_5 = null;
        if(argument_6 != 6){ throw new SystemException($"Failed assertion: argument_6 = {argument_6}");}
        argument_6 = 7;
        if(argument_7 != 7){ throw new SystemException($"Failed assertion: argument_7 = {argument_7}");}
$$ LANGUAGE plcsharp STRICT;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-multiarg-4', 'inout_multiarg_4', inout_multiarg_4(0, 1, 2, 5, 6, 7) = ROW(2, 4, 5, NULL::INT, 7);
 
----------------------------------------------------------------------
-- Fourth, we test ARRAY (with NULL) for INOUT, OUT
----------------------------------------------------------------------
 
CREATE OR REPLACE FUNCTION inout_array_10(INOUT values_array MACADDR[], OUT nulls INT) AS $$
    Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
    ArrayManipulation.FlatArray(values_array, ref flatten_values);
    nulls = 0;
    for(int i = 0; i < flatten_values.Length; i++)
    {
        if (flatten_values.GetValue(i) == null){
            nulls++;
            continue;
        }
 
        PhysicalAddress orig_value = (PhysicalAddress)flatten_values.GetValue(i);
        byte[] bytes = orig_value.GetAddressBytes();
        bytes[0] += 1;
        PhysicalAddress new_value = new PhysicalAddress(bytes);
 
        flatten_values.SetValue((PhysicalAddress)new_value, i);
    }
    values_array = flatten_values;
$$ LANGUAGE plcsharp STRICT;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-array-10', 'inout_array_10', inout_array_10(
    ARRAY[
        MACADDR '08-00-2b-01-02-03',
        MACADDR '09-00-2b-01-02-03',
        null::macaddr,
        MACADDR 'a8-00-2b-01-02-03',
        null::macaddr
    ]) = ROW(
        ARRAY[
            MACADDR '09-00-2b-01-02-03',
            MACADDR '0a-00-2b-01-02-03',
            null::macaddr,
            MACADDR 'a9-00-2b-01-02-03',
            null::macaddr
        ], 2
    );
 
CREATE OR REPLACE FUNCTION inout_array_11(OUT values_array MACADDR[], IN address MACADDR, IN count INT) AS $$
    Array output = Array.CreateInstance(typeof(object), count);
    for(int i = 0; i < count; i++)
    {
        output.SetValue((PhysicalAddress)address, i);
    }
    values_array = output;
$$ LANGUAGE plcsharp STRICT;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-array-11', 'inout_array_11', inout_array_11(MACADDR '08-00-2b-01-02-03', 3) =
        ARRAY[
            MACADDR '08-00-2b-01-02-03',
            MACADDR '08-00-2b-01-02-03',
            MACADDR '08-00-2b-01-02-03'
        ];
 
----------------------------------------------------------------------
-- Fifth, we test non-trivial StructTypeHandler, here INET
----------------------------------------------------------------------
 
-- Test IN/INOUT/OUT for non-trivial StructTypeHandler, in this case INET
 
CREATE OR REPLACE FUNCTION inout_simple_10(OUT checksum INT, IN address INET) AS $$
    int i;
 
    // get bytes
    byte[] bytes = address.Address.GetAddressBytes();
 
    // compute checksum
    checksum = 0;
    for(i = 0; i<bytes.Length;i++){ checksum += bytes[i]; }
$$ LANGUAGE plcsharp STRICT;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-simple-10', 'inout_simple_10', inout_simple_10(CIDR '192.168/24') = 360;
 
CREATE OR REPLACE FUNCTION inout_simple_20(INOUT address INET, IN pos INT, IN delta INT) AS $$
    int i;
 
    // compute new address
    (IPAddress Address, int Netmask) address2 = address ?? (IPAddress.Parse("1.1.1.1"), 8);
    byte[] bytes = address2.Address.GetAddressBytes();
    bytes[pos]+=(byte)delta;
    address = (new IPAddress(bytes), address2.Netmask);
$$ LANGUAGE plcsharp STRICT;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-simple-20', 'inout_simple_20', inout_simple_20(CIDR '192.168/24', 2, 3) = INET '192.168.3.0/24';
 
CREATE OR REPLACE FUNCTION inout_simple_30(OUT checksum INT, INOUT address INET, IN pos INT, IN delta INT) AS $$
    int i;
 
    // compute new address
    (IPAddress Address, int Netmask) address2 = address ?? (IPAddress.Parse("1.1.1.1"), 8);
    byte[] bytes = address2.Address.GetAddressBytes();
    bytes[pos]+=(byte)delta;
    address = (new IPAddress(bytes), address2.Netmask);
 
    // compute checksum
    checksum = 0;
    for(i = 0; i<bytes.Length;i++){ checksum += bytes[i]; }
$$ LANGUAGE plcsharp STRICT;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-simple-30', 'inout_simple_30', inout_simple_30(CIDR '192.168/24', 2, 3) = ROW(363, INET '192.168.3.0/24');
 
----------------------------------------------------------------------
-- Fifth, we test non-trivial ObjectTypeHandler, here StringHandler
----------------------------------------------------------------------
 
CREATE OR REPLACE FUNCTION inout_object_10(IN a text, INOUT b text) AS $$
    if (a == null) a = "";
    if (b == null) b = "";
    b = a + " " + b;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-10', 'inout_object_10', inout_object_10('red', 'blue') = 'red blue';
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-11', 'inout_object_10', inout_object_10('red', NULL) = 'red ';
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-12', 'inout_object_10', inout_object_10(NULL, 'blue') = ' blue';
 
CREATE OR REPLACE FUNCTION inout_object_20(IN a text, b text, OUT c text) AS $$
    if (a == null) a = "";
    if (b == null) b = "";
    c = a + " " + b;
$$ LANGUAGE plcsharp;
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-20', 'inout_object_20', inout_object_20('red', 'blue') = 'red blue';
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-21', 'inout_object_20', inout_object_20('red', NULL) = 'red ';
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-22', 'inout_object_20', inout_object_20(NULL, 'blue') = ' blue';
 
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'c#-inout-object-23', 'inout_object_20', inout_object_20('🐂', '🥰') = '🐂 🥰'::TEXT;
 
COMMIT;