Исходный код СУБД 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
// <copyright file="BitStringHandler.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.Runtime.InteropServices;
using NpgsqlTypes;
 
namespace PlDotNET.Handler
{
    /// <summary>
    /// A type handler for the PostgreSQL var bit string data type.
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/static/datatype-bit.html.
    /// </remarks>
    [OIDHandler(OID.VARBITOID, OID.VARBITARRAYOID)]
    public partial class VarBitStringHandler : ObjectTypeHandler<BitArray>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VarBitStringHandler"/> class.
        /// </summary>
        public VarBitStringHandler()
        {
            this.ElementOID = OID.VARBITOID;
            this.ArrayOID = OID.VARBITARRAYOID;
        }
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_GetDatumVarBitAttributes().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static unsafe partial void pldotnet_GetDatumVarBitAttributes(IntPtr datum, ref int len, ref byte* dat);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_CreateDatumVarBit().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static partial IntPtr pldotnet_CreateDatumVarBit(int len, byte[] dat);
 
        /// <summary>
        /// Creates a BitArray object from a PostgreSQL bit string data type.
        /// </summary>
        public static unsafe BitArray CreateBitArray(IntPtr datum)
        {
            int bitLen = 0;
            byte* bitDat = null;
            pldotnet_GetDatumVarBitAttributes(datum, ref bitLen, ref bitDat);
 
            int byteLen = (bitLen / 8) + ((bitLen % 8) > 0 ? 1 : 0);
            byte[] bytes = new byte[byteLen];
            for (int i = 0; i < byteLen; i++)
            {
                bytes[i] = bitDat[i];
            }
 
            // the reverse BitArray constructed from byte[]
            BitArray auxiliar = new(bytes);
 
            BitArray result = new(bitLen);
            for (int i = 0, cont = 0; i < byteLen; i++)
            {
                for (int j = 7; j >= 0; j--)
                {
                    if (cont == bitLen)
                    {
                        break;
                    }
 
                    result[cont++] = auxiliar[(i * 8) + j];
                }
            }
 
            return result;
        }
 
        /// <summary>
        /// Creates a PostgreSQL bit string data type from a BitArray object.
        /// </summary>
        public static IntPtr CreateDatum(BitArray value)
        {
            int bitLen = value.Length;
            int byteLen = (bitLen / 8) + ((bitLen % 8) > 0 ? 1 : 0);
 
            // the reverse BitArray; it will be used to call "CopyTo"
            BitArray auxiliar = new(byteLen * 8);
            for (int i = 0, cont = 0; i < byteLen; i++)
            {
                for (int j = 7; j >= 0; j--)
                {
                    if (cont == bitLen)
                    {
                        break;
                    }
 
                    auxiliar[(i * 8) + j] = value[cont++];
                }
            }
 
            byte[] bytes = new byte[byteLen];
            auxiliar.CopyTo(bytes, 0);
 
            return pldotnet_CreateDatumVarBit(bitLen, bytes);
        }
 
        /// <inheritdoc />
        public override BitArray InputValue(IntPtr datum)
        {
            return CreateBitArray(datum);
        }
 
        /// <inheritdoc />
        public override IntPtr OutputValue(BitArray value)
        {
            return CreateDatum(value);
        }
    }
 
    /// <summary>
    /// A type handler for the PostgreSQL bit string data type.
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/static/datatype-bit.html.
    /// </remarks>
    [OIDHandler(OID.BITOID, OID.BITARRAYOID)]
    public class BitStringHandler : ObjectTypeHandler<BitArray>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BitStringHandler"/> class.
        /// </summary>
        public BitStringHandler()
        {
            this.ElementOID = OID.BITOID;
            this.ArrayOID = OID.BITARRAYOID;
        }
 
        /// <inheritdoc />
        public override BitArray InputValue(IntPtr datum)
        {
            return VarBitStringHandler.CreateBitArray(datum);
        }
 
        /// <inheritdoc />
        public override IntPtr OutputValue(BitArray value)
        {
            return VarBitStringHandler.CreateDatum(value);
        }
    }
}