Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-06-19 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// <copyright file="Program.cs" company="Brick Abode">
//
// PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
//                      procedural languages (PL)
//
//
// Copyright (c) 2023 Brick Abode
//
// This code is subject to the terms of the PostgreSQL License.
// The full text of the license can be found in the LICENSE file
// at the top level of the pldotnet repository.
//
// </copyright>
 
using System;
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using NpgsqlTypes;
 
#pragma warning disable CS8602
#pragma warning disable CS8625
#pragma warning disable CS8605
#pragma warning disable CS8600
 
namespace TestDLLFunctions
{
    public class TestClass
    {
        public static int FlatArray(Array originalArray, ref Array flatArray, int[] auxiliar = null, int contEl = 0, int loc = 1)
        {
            if (contEl >= originalArray.Length)
            {
                return contEl;
            }
            else if (contEl == 0 || auxiliar == null)
            {
                auxiliar = new int[originalArray.Rank];
            }
 
            int ndim = originalArray.Rank;
            int[] dim = new int[ndim];
            for (int i = 0; i < ndim; i++)
            {
                dim[i] = originalArray.GetLength(i);
            }
 
            if (loc == 1)
            {
                for (int i = 0; i < dim[ndim - loc]; i++)
                {
                    flatArray.SetValue(originalArray.GetValue(auxiliar), contEl++);
                    auxiliar[ndim - loc] += 1;
                }
            }
 
            for (int i = 1; i < loc; i++)
            {
                auxiliar[ndim - loc] += 1;
                if (auxiliar[ndim - loc] < dim[ndim - loc])
                {
                    contEl = FlatArray(originalArray, ref flatArray, auxiliar, contEl, i);
                }
            }
 
            auxiliar[ndim - loc] = 0;
            contEl = FlatArray(originalArray, ref flatArray, auxiliar, contEl, ++loc);
            return contEl;
        }
 
        public static BitArray? modifybit(BitArray? a)
        {
            if (a == null)
            {
                return null;
            }
 
            a[0] = !a[0];
            a[^1] = !a[^1];
            return a;
        }
 
        public static BitArray? modifyvarbit(BitArray? a)
        {
            if (a == null)
            {
                return null;
            }
 
            a[0] = !a[0];
            a[^1] = !a[^1];
            return a;
        }
 
        public static bool? booleanand(bool? a, bool? b)
        {
            a ??= false;
            b ??= false;
 
            return a & b;
        }
 
        public static byte[]? byteaconversions(byte[]? a, byte[]? b)
        {
            UTF8Encoding utf8_e = new();
            if (a == null && b == null)
            {
                return null;
            }
 
            if (a == null)
            {
                return b;
            }
 
            if (b == null)
            {
                return a;
            }
 
            string s1 = utf8_e.GetString(a, 0, a.Length);
            string s2 = utf8_e.GetString(b, 0, b.Length);
            string result = s1 + " " + s2;
            return utf8_e.GetBytes(result);
        }
 
        public static DateTime? setnewdate(DateTime? orig_timestamp, DateOnly? new_date)
        {
            if (orig_timestamp == null)
            {
                orig_timestamp = new DateTime(2022, 1, 1, 8, 30, 20);
            }
 
            if (new_date == null)
            {
                new_date = new DateOnly(2023, 12, 25);
            }
 
            int new_day = ((DateOnly)new_date).Day;
            int new_month = ((DateOnly)new_date).Month;
            int new_year = ((DateOnly)new_date).Year;
            DateTime new_timestamp = new(new_year, new_month, new_day, ((DateTime)orig_timestamp).Hour, ((DateTime)orig_timestamp).Minute, ((DateTime)orig_timestamp).Second);
            return new_timestamp;
        }
 
        public static double? sumdoublearray(Array doubles)
        {
            Array flatten_doubles = Array.CreateInstance(typeof(object), doubles.Length);
            TestClass.FlatArray(doubles, ref flatten_doubles);
            double double_sum = 0;
            for (int i = 0; i < flatten_doubles.Length; i++)
            {
                if (flatten_doubles.GetValue(i) == null)
                {
                    continue;
                }
 
                double_sum += (double)flatten_doubles.GetValue(i);
            }
 
            return double_sum;
        }
 
        public static double? sumdouble(double? a, double? b)
        {
            a ??= 0;
            b ??= 0;
            return a + b;
        }
 
        public static short? sum2smallint(short? a, short? b)
        {
            a ??= 0;
            b ??= 0;
            return (short)(a + b); // C# requires short cast
        }
 
        public static int? sum2integer(int? a, int? b)
        {
            a ??= 0;
            b ??= 0;
            return a + b;
        }
 
        public static long? sum2bigint(long? a, long? b)
        {
            a ??= 0;
            b ??= 0;
            return a + b;
        }
 
        public static Array? updatearraypolygonindex(Array values_array, NpgsqlPolygon desired, Array index)
        {
            int[] arrayInteger = index.Cast<int>().ToArray();
            values_array.SetValue(desired, arrayInteger);
            return values_array;
        }
 
        public static Array? updatejsonarrayindex(Array values_array, string desired, Array index)
        {
            int[] arrayInteger = index.Cast<int>().ToArray();
            values_array.SetValue(desired, arrayInteger);
            return values_array;
        }
 
        public static Array? increasemoney(Array values_array)
        {
            Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
            TestClass.FlatArray(values_array, ref flatten_values);
            for (int i = 0; i < flatten_values.Length; i++)
            {
                if (flatten_values.GetValue(i) == null)
                {
                    continue;
                }
 
                decimal orig_value = (decimal)flatten_values.GetValue(i);
                decimal new_value = orig_value + 1;
                flatten_values.SetValue((decimal)new_value, i);
            }
 
            return flatten_values;
        }
 
        public static Array? increasemacaddress8(Array values_array)
        {
            Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
            TestClass.FlatArray(values_array, ref flatten_values);
            for (int i = 0; i < flatten_values.Length; i++)
            {
                if (flatten_values.GetValue(i) == null)
                {
                    continue;
                }
 
                PhysicalAddress orig_value = (PhysicalAddress)flatten_values.GetValue(i);
                byte[] bytes = orig_value.GetAddressBytes();
                bytes[0] += 1;
                PhysicalAddress new_value = new(bytes);
                flatten_values.SetValue((PhysicalAddress)new_value, i);
            }
 
            return flatten_values;
        }
 
        public static Array? increasecidraddress(Array values_array)
        {
            Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
            TestClass.FlatArray(values_array, ref flatten_values);
            for (int i = 0; i < flatten_values.Length; i++)
            {
                if (flatten_values.GetValue(i) == null)
                {
                    continue;
                }
 
                (IPAddress address, int netmask) = ((IPAddress Address, int Netmask))flatten_values.GetValue(i);
                byte[] bytes = address.GetAddressBytes();
                bytes[0] += 1;
                (IPAddress Address, int Netmask) new_value = (new IPAddress(bytes), netmask);
                flatten_values.SetValue(((IPAddress Address, int Netmask))new_value, i);
            }
 
            return flatten_values;
        }
 
        public static Array? increaseint8ranges(Array values_array)
        {
            Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
            TestClass.FlatArray(values_array, ref flatten_values);
            for (int i = 0; i < flatten_values.Length; i++)
            {
                if (flatten_values.GetValue(i) == null)
                {
                    continue;
                }
 
                NpgsqlRange<long> orig_value = (NpgsqlRange<long>)flatten_values.GetValue(i);
                NpgsqlRange<long> new_value = new(orig_value.LowerBound + 1, orig_value.LowerBoundIsInclusive, orig_value.LowerBoundInfinite, orig_value.UpperBound + 1, orig_value.UpperBoundIsInclusive, orig_value.UpperBoundInfinite);
                flatten_values.SetValue((NpgsqlRange<long>)new_value, i);
            }
 
            return flatten_values;
        }
 
        public static Array? increasedateonlyranges(Array values_array)
        {
            Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
            TestClass.FlatArray(values_array, ref flatten_values);
            for (int i = 0; i < flatten_values.Length; i++)
            {
                if (flatten_values.GetValue(i) == null)
                {
                    continue;
                }
 
                NpgsqlRange<DateOnly> orig_value = (NpgsqlRange<DateOnly>)flatten_values.GetValue(i);
 
                NpgsqlRange<DateOnly> new_value = new(orig_value.LowerBound.AddDays(1), orig_value.LowerBoundIsInclusive, orig_value.LowerBoundInfinite, orig_value.UpperBound.AddDays(1), orig_value.UpperBoundIsInclusive, orig_value.UpperBoundInfinite);
                flatten_values.SetValue((NpgsqlRange<DateOnly>)new_value, i);
            }
 
            return flatten_values;
        }
 
        public static string? concatenatevarchars(string? a, string? b, string? c)
        {
            a ??= string.Empty;
            b ??= string.Empty;
            c ??= string.Empty;
            return (a + " " + b + " " + c).ToUpper();
        }
 
        public static Guid? combineuuids(Guid? a, Guid? b)
        {
            if (a == null)
            {
                a = new Guid("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
            }
 
            if (b == null)
            {
                b = new Guid("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
            }
 
            string aStr = a.ToString();
            string bStr = b.ToString();
            var aList = aStr.Split('-');
            var bList = bStr.Split('-');
            string newUuuidStr = aList[0] + aList[1] + aList[2] + bList[3] + bList[4];
            return new Guid(newUuuidStr);
        }
    }
}
 
namespace TestDLLFunctions.OtherTests
{
    public class TestClass
    {
        public static NpgsqlPoint? middlePointStrict(NpgsqlPoint pointa, NpgsqlPoint pointb)
        {
            double x = (pointa.X + pointb.X) * 0.5;
            double y = (pointa.Y + pointb.Y) * 0.5;
            var new_point = new NpgsqlPoint(x, y);
 
            return new_point;
        }
 
        public static NpgsqlPoint? middlePointDefault(NpgsqlPoint? pointa, NpgsqlPoint? pointb)
        {
            if (pointa == null)
            {
                pointa = new NpgsqlPoint(0, 0);
            }
 
            if (pointb == null)
            {
                pointb = new NpgsqlPoint(0, 0);
            }
 
            double x = (((NpgsqlPoint)pointa).X + ((NpgsqlPoint)pointb).X) * 0.5;
            double y = (((NpgsqlPoint)pointa).Y + ((NpgsqlPoint)pointb).Y) * 0.5;
            var new_point = new NpgsqlPoint(x, y);
 
            return new_point;
        }
    }
 
    public class InoutTests
    {
        public static void inoutBasic0(ref int? argument_0)
        {
            if (argument_0 != 0)
            {
                throw new SystemException($"Failed assertion: argument_0 = {argument_0}");
            }
 
            argument_0 = 1;
        }
 
        public static void inoutBasic1(out int? argument_0)
        {
            argument_0 = 1;
        }
 
        public static void inputNull1(ref int? argument_0)
        {
            if (argument_0 is null)
            {
                throw new SystemException($"Failed assertion: argument_0 = {argument_0}");
            }
 
            argument_0 = null;
        }
 
        public static void inputNull2(ref int? argument_0)
        {
            if (argument_0 != null)
            {
                throw new SystemException($"Failed assertion: argument_0 = {argument_0}");
            }
 
            argument_0 = 3;
        }
 
        public static void inputNull3(out int? argument_0)
        {
            argument_0 = null;
        }
 
        public static void inputNull4(ref int? argument_0, int? argument_1)
        {
            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;
        }
 
        public static void inputNull5(ref int? argument_0, int? argument_1)
        {
            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;
        }
 
        public static void inputNull6(int? argument_0, out int? argument_1)
        {
            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}");
            }
        }
 
        public static void inoutMultiarg1(int? argument_0, ref int? argument_1, int? argument_2, out int? argument_3, out int? argument_4, ref int? argument_5, int? argument_6, out int? argument_7)
        {
            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;
        }
 
        public static void inoutMultiarg2(ref int? argument_0, out int? argument_1, ref int? argument_2, ref int? argument_3, int? argument_4, out int? argument_5, int? argument_6, out int? argument_7)
        {
            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;
        }
 
        public static void inoutMultiarg3(out int? argument_0, int? argument_1, int? argument_2, out int? argument_3, ref int? argument_4, out int? argument_5, int? argument_6, ref int? argument_7)
        {
            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;
        }
 
        public static void inoutMultiarg4(int argument_0, ref int? argument_1, int argument_2, out int? argument_3, out int? argument_4, ref int? argument_5, ref int? argument_6, int argument_7)
        {
            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}");
            }
        }
 
        public static void inoutArray10(ref Array? values_array, out int? nulls)
        {
            Array flatten_values = Array.CreateInstance(typeof(object), values_array.Length);
            TestDLLFunctions.TestClass.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(bytes);
                flatten_values.SetValue((PhysicalAddress)new_value, i);
            }
 
            values_array = flatten_values;
        }
 
        public static void inoutArray11(out Array? values_array, PhysicalAddress address, int count)
        {
            Array output = Array.CreateInstance(typeof(object), count);
            for (int i = 0; i < count; i++)
            {
                output.SetValue((PhysicalAddress)address, i);
            }
 
            values_array = output;
        }
 
        public static void inoutSimple10(out int? checksum, (IPAddress Address, int Netmask) address)
        {
            int i;
 
            // get bytes
            byte[] bytes = address.Address.GetAddressBytes();
 
            // compute checksum
            checksum = 0;
            for (i = 0; i < bytes.Length; i++)
            {
                checksum += bytes[i];
            }
        }
 
        public static void inoutSimple20(ref (IPAddress Address, int Netmask)? address, int pos, int delta)
        {
            // compute new address
            (IPAddress address1, int netmask) = address ?? (IPAddress.Parse("1.1.1.1"), 8);
            byte[] bytes = address1.GetAddressBytes();
            bytes[pos] += (byte)delta;
            address = (new IPAddress(bytes), netmask);
        }
 
        public static void inoutSimple30(out int? checksum, ref (IPAddress Address, int Netmask)? address, int pos, int delta)
        {
            int i;
 
            // compute new address
            (IPAddress address1, int netmask) = address ?? (IPAddress.Parse("1.1.1.1"), 8);
            byte[] bytes = address1.GetAddressBytes();
            bytes[pos] += (byte)delta;
            address = (new IPAddress(bytes), netmask);
 
            // compute checksum
            checksum = 0;
            for (i = 0; i < bytes.Length; i++)
            {
                checksum += bytes[i];
            }
        }
 
        public static void inoutObject10(string? a, ref string? b)
        {
            a ??= string.Empty;
            b ??= string.Empty;
            b = a + " " + b;
        }
 
        public static void inoutObject20(string? a, string? b, out string? c)
        {
            a ??= string.Empty;
            b ??= string.Empty;
            c = a + " " + b;
        }
    }
}