Исходный код СУБД 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
// <copyright file="StringHandler.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.Buffers;
using System.Runtime.InteropServices;
using System.Text;
using NpgsqlTypes;
 
namespace PlDotNET.Handler
{
    /// <summary>
    /// A type handler for PostgreSQL character data types (text, char, varchar, xml).
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/datatype-character.html.
    /// </remarks>
    public partial class StringHandler : ObjectTypeHandler<string>
    {
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_GetDatumTextAttributes().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static unsafe partial void pldotnet_GetDatumTextAttributes(IntPtr datum, ref int len, ref byte* buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_CreateDatumText().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static partial IntPtr pldotnet_CreateDatumText(int len, byte[] buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_GetDatumCharAttributes().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static unsafe partial void pldotnet_GetDatumCharAttributes(IntPtr datum, ref int len, ref byte* buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_CreateDatumChar().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static partial IntPtr pldotnet_CreateDatumChar(int len, byte[] buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_GetDatumVarCharAttributes().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static unsafe partial void pldotnet_GetDatumVarCharAttributes(IntPtr datum, ref int len, ref byte* buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_CreateDatumVarChar().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static partial IntPtr pldotnet_CreateDatumVarChar(int len, byte[] buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_GetDatumXmlAttributes().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static unsafe partial void pldotnet_GetDatumXmlAttributes(IntPtr datum, ref int len, ref byte* buf);
 
        /// <summary>
        /// C function declared in pldotnet_conversions.h.
        /// See ::pldotnet_CreateDatumXml().
        /// </summary>
        [LibraryImport("@PKG_LIBDIR/pldotnet.so")]
        public static partial IntPtr pldotnet_CreateDatumXml(int len, byte[] buf);
 
        /// <inheritdoc />
        public override unsafe string InputValue(IntPtr datum)
        {
            int strlen = 0;
            byte* str_p = null;
            switch ((int)this.ElementOID)
            {
                case (int)OID.TEXTOID:
                    pldotnet_GetDatumTextAttributes(datum, ref strlen, ref str_p);
                    break;
                case (int)OID.BPCHAROID:
                    pldotnet_GetDatumCharAttributes(datum, ref strlen, ref str_p);
                    break;
                case (int)OID.VARCHAROID:
                    pldotnet_GetDatumVarCharAttributes(datum, ref strlen, ref str_p);
                    break;
                case (int)OID.XMLOID:
                    pldotnet_GetDatumXmlAttributes(datum, ref strlen, ref str_p);
                    break;
                default:
                    throw new NotImplementedException($"StringConstructors doesn't support {(OID)this.ElementOID}");
            }
 
            return Encoding.UTF8.GetString(str_p, strlen);
        }
 
        /// <inheritdoc />
        public override IntPtr OutputValue(string value)
        {
            byte[] encodedBytes = Encoding.UTF8.GetBytes(value);
            int len = encodedBytes.Length;
            return (int)this.ElementOID switch
            {
                (int)OID.TEXTOID => pldotnet_CreateDatumText(len, encodedBytes),
                (int)OID.BPCHAROID => pldotnet_CreateDatumChar(len, encodedBytes),
                (int)OID.VARCHAROID => pldotnet_CreateDatumVarChar(len, encodedBytes),
                (int)OID.XMLOID => pldotnet_CreateDatumXml(len, encodedBytes),
                _ => throw new NotImplementedException($"StringConstructors doesn't support {(OID)this.ElementOID}"),
            };
        }
    }
 
    /// <summary>
    /// A type handler for PostgreSQL character text data types.
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/datatype-character.html.
    /// </remarks>
    [OIDHandler(OID.TEXTOID, OID.TEXTARRAYOID)]
    public class TextHandler : StringHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TextHandler"/> class.
        /// </summary>
        public TextHandler()
        {
            this.ElementOID = OID.TEXTOID;
            this.ArrayOID = OID.TEXTARRAYOID;
        }
    }
 
    /// <summary>
    /// A type handler for PostgreSQL character char data types.
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/datatype-character.html.
    /// </remarks>
    [OIDHandler(OID.BPCHAROID, OID.BPCHARARRAYOID)]
    public class CharHandler : StringHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CharHandler"/> class.
        /// </summary>
        public CharHandler()
        {
            this.ElementOID = OID.BPCHAROID;
            this.ArrayOID = OID.BPCHARARRAYOID;
        }
    }
 
    /// <summary>
    /// A type handler for PostgreSQL character varchar data types.
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/datatype-character.html.
    /// </remarks>
    [OIDHandler(OID.VARCHAROID, OID.VARCHARARRAYOID)]
    public class CharVaryingHandler : StringHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CharVaryingHandler"/> class.
        /// </summary>
        public CharVaryingHandler()
        {
            this.ElementOID = OID.VARCHAROID;
            this.ArrayOID = OID.VARCHARARRAYOID;
        }
    }
 
    /// <summary>
    /// A type handler for PostgreSQL character xml data types.
    /// </summary>
    /// <remarks>
    /// See https://www.postgresql.org/docs/current/datatype-xml.htm.
    /// </remarks>
    [OIDHandler(OID.XMLOID, OID.XMLARRAYOID)]
    public class XmlHandler : StringHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlHandler"/> class.
        /// </summary>
        public XmlHandler()
        {
            this.ElementOID = OID.XMLOID;
            this.ArrayOID = OID.XMLARRAYOID;
        }
    }
}