Исходный код СУБД 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
/*
 * 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.
 *
 * pldotnet_main.h
 *
 */
 
#ifndef PLDOTNET_MAIN_H_
#define PLDOTNET_MAIN_H_
 
#include <stddef.h>
#include <stdio.h>
#include <postgres.h>
#include <access/htup_details.h>
#include <catalog/pg_proc.h>
#include <catalog/pg_type.h>
#include <commands/trigger.h>
#include <executor/spi.h>  // For SPI_getrelname and SPI_getnspname
#include <funcapi.h>
#include <glib.h>
#include <utils/array.h>
#include <utils/builtins.h>
#include <utils/fmgrprotos.h>
#include <utils/syscache.h>
#include <utils/typcache.h>
#include <utils/memutils.h>
#include <utils/lsyscache.h>  // type_is_rowtype()
#include <utils/rel.h>
#include <assert.h>
 
#include "pldotnet_hostfxr.h"
#include "pldotnet_spi.h"
 
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)
 
extern PGDLLIMPORT bool check_function_bodies;
 
extern GHashTable *procedures;
 
extern char *root_path;
extern char *dnldir;
 
/**
 * @brief Writes formatted output to a string using snprintf and checks if the
 * output exceeds the size of the buffer.
 *
 * If the output exceeds the size of the buffer, this macro calls elog with an
 * error message indicating that the string was too long for the buffer.
 *
 * @param dst Pointer to the destination buffer where the formatted output will
 * be written.
 * @param size Size of the destination buffer.
 * @param fmt Format string that specifies how the formatted output should be
 * written.
 * @param ... List of arguments for the format specifiers in the format string.
 */
#define SNPRINTF(dst, size, fmt, ...)                                      \
    if (snprintf(dst, size, fmt, __VA_ARGS__) >= size) {                   \
        elog(ERROR, "[pldotnet] (%s:%d) String too long for buffer: " fmt, \
             __FILE__, __LINE__, __VA_ARGS__);                             \
    }
 
typedef enum pldotnet_Language { csharp, fsharp } pldotnet_Language;
 
typedef enum {
    CALL_NORMAL = 1,       // Normal, non-SRF function
    CALL_SRF_FIRST = 2,    // First call to an SRF; create and cache
    CALL_SRF_NEXT = 3,     // Next call to an SRF
    CALL_SRF_CLEANUP = 4,  // SRF is done; you may remove it from the cache
    CALL_TRIGGER = 5       // Called as a trigger
} CallType;
 
typedef enum {
    RETURN_ERROR = 0,           // We encountered an error
    RETURN_NORMAL = 1,          // Normal return to CALL_NORMAL
    RETURN_SRF_NEXT = 2,        // SRF return to CALL_SRF_NEXT
    RETURN_SRF_DONE = 3,        // We have no more values
    RETURN_TRIGGER_SKIP = 4,    // Abort the trigger event
    RETURN_TRIGGER_MODIFY = 5,  // The row has been modified
} ReturnMode;
 
typedef struct MemoryContextWrapper {
    MemoryContext prev;
    MemoryContext curr;
} MemoryContextWrapper;
 
typedef struct pldotnet_PathConfig {
    char prefix[MAXPGPATH];
    char config_path[MAXPGPATH];
    char library_path[MAXPGPATH];
} pldotnet_PathConfig;
 
typedef struct pldotnet_Result {
    size_t length;
    bool is_null;  // for top-level NULL on RECORD
    Datum *values;
    bool *nulls;
    Oid *oids;
    bool *updated;
} pldotnet_Result;
 
typedef struct pldotnet_UserFunctionDeclaration {
    const char *language;
    const char *func_name;
    Oid func_ret_type;
    char *func_param_names;
    Oid *func_param_types;
    char *func_param_modes;
    int num_args;
    int num_input_args;
    int num_output_values;  // 0 for a normal function (1 return), <n> for a
                            // record (INOUT/OUT)
    const char *func_body;
    Oid func_oid;
    bool support_null_input;
    bool retset;
    bool is_trigger;
} pldotnet_UserFunctionDeclaration;
 
typedef struct cb_data {  // old-school C inheritance here
    MemoryContextCallback cb_record;
    uint32_t functionId;
    uint64_t call_id;
} cb_data;
 
/**
 * Set a result in pldotnet_Result.  Returns 0 on success.
 */
extern PGDLLEXPORT int pldotnet_SetResult(pldotnet_Result *output, int offset,
                                          Datum value, bool is_null, Oid oid);
 
/**
 * Get a result from pldotnet_Result.
 */
extern PGDLLEXPORT int pldotnet_GetResult(pldotnet_Result *output, int offset,
                                          Datum *value, bool *is_null,
                                          Oid *oid);
 
/**
 * @brief Get a PostgreSQL setting value.
 * @param settingName The setting name.
 * @return The value for the given setting name.
 */
extern PGDLLEXPORT const char* pldotnet_GetPostgresSetting(const char *settingName);
 
/**
 * @brief The call_handler will be called to execute the procedural
 * language's functions.  The call handler receives a pointer to a
 * FunctionCallInfoData struct containing argument values and information
 * about the called function, and it is expected to return a Datum result.
 *
 * @param PG_FUNCTION_ARGS The standard parameter list for fmgr-compatible
 * functions.
 *
 * @return The datum that can be stored in a PostgreSQL table.
 */
extern Datum plcsharp_call_handler(PG_FUNCTION_ARGS);
 
/**
 * @brief The inline_handler will be called to execute an anonymous code
 * block (DO command) in this language.
 *
 * @param PG_FUNCTION_ARGS The standard parameter list for
 * fmgr-compatible functions.
 *
 * @return The datum that can be stored in a PostgreSQL table.
 */
extern Datum plcsharp_inline_handler(PG_FUNCTION_ARGS);
 
/**
 * @brief The validator function will inspect the function body for syntactical
 * correctness, but it can also look at other properties of the function,
 * for example if the language cannot handle certain argument types. To
 * signal an error, the validator function should use the ereport()
 * function. The return value of the function is ignored.
 *
 * @param PG_FUNCTION_ARGS The standard parameter list for fmgr-compatible
 * functions.
 *
 * @return The datum that can be stored in a PostgreSQL table.
 */
extern Datum plcsharp_validator(PG_FUNCTION_ARGS);
 
/**
 * @brief The call_handler will be called to execute the procedural
 * language's functions. The call handler receives a pointer to a
 * FunctionCallInfoData struct containing argument values and information
 * about the called function, and it is expected to return a Datum result.
 *
 * @param PG_FUNCTION_ARGS The standard parameter list for fmgr-compatible
 * functions.
 *
 * @return The datum that can be stored in a PostgreSQL table.
 */
extern Datum plfsharp_call_handler(PG_FUNCTION_ARGS);
 
/**
 * @brief The inline_handler will be called to execute an anonymous code
 * block (DO command) in this language.
 *
 * @param PG_FUNCTION_ARGS The standard parameter list for
 * fmgr-compatible functions.
 *
 * @return The datum that can be stored in a PostgreSQL table.
 */
extern Datum plfsharp_inline_handler(PG_FUNCTION_ARGS);
 
/**
 * @brief The validator function will inspect the function body for syntactical
 * correctness, but it can also look at other properties of the function,
 * for example if the language cannot handle certain argument types. To
 * signal an error, the validator function should use the ereport()
 * function. The return value of the function is ignored.
 *
 * @param PG_FUNCTION_ARGS The standard parameter list for fmgr-compatible
 * functions.
 *
 * @return The datum that can be stored in a PostgreSQL table.
 */
extern Datum plfsharp_validator(PG_FUNCTION_ARGS);
 
/**
 * @brief Build the config paths related to .NET.
 *
 */
extern bool pldotnet_BuildPaths(void);
 
/**
 * @brief Sets the assembly_loader object.
 *
 * @return true if the assembly_loader was already defined or was found
 * correctly.
 * @return false if the assembly_loader was not found.
 */
extern bool pldotnet_SetNetLoader(void);
 
/**
 * @brief Sets the .NET methods for the C function pointers.
 *
 * @return true if all the .NET functions were found.
 * @return false if no .NET functions were found.
 */
extern bool pldotnet_SetDotNetMethods(void);
 
/**
 * @brief Calls the "elog" function to report a message of PostgreSQL.
 *
 * @param level The message level. For example, INFO, ERROR, WARNING, etc...
 * @param message The message that will be reported.
 */
extern PGDLLEXPORT void pldotnet_Elog(int level, char *message);
 
/**
 * @brief Returns the PostgreSQL version.
 *
 * @return char* the PostgreSQL version like "14.7"
 */
extern PGDLLEXPORT char *pldotnet_GetPostgreSqlVersion(void);
 
/**
 * @brief Creates a new memory context.
 *
 * @param config
 */
void pldotnet_StartNewMemoryContext(MemoryContextWrapper *config);
 
/**
 * @brief Reverts the created memory context.
 *
 * @param config
 */
void pldotnet_ResetMemoryContext(MemoryContextWrapper *config);
 
/**
 * @brief Returns current size of the pldotnet_Result
 *
 * @param result Pointer to the pldotnet_Result structure to be checked.
 * @return Number of fields in the structure
 */
extern PGDLLEXPORT int pldotnet_GetResultLength(pldotnet_Result *result);
 
/**
 * @brief Resizes the result structure.
 *
 * This function is used to resize the result structure to accommodate a
 * specified length.
 *
 * @param r The result structure to be resized.
 * @param length The new length of the result structure.
 */
extern PGDLLEXPORT void pldotnet_ResizeResult(struct pldotnet_Result *r,
                                              size_t length);
 
#endif  // PLDOTNET_MAIN_H_