Исходный код СУБД 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
/*
 * PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
 *             procedural languages (PL)
 *
 *
 * Copyright 2023 Brick Abode
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * pldotnet_spi.h
 *
 */
 
#ifndef PLDOTNET_SPI_H_
#define PLDOTNET_SPI_H_
 
#include <stddef.h>
#include <postgres.h>
#include <executor/spi.h>
#include "pldotnet_main.h"
 
#define nullptr ((void *)0)
extern bool is_spi_open;
 
/**
 * @brief Executes an SQL command using the Server Programming Interface (SPI).
 *
 * This function executes the given SQL command using the SPI interface. The
 * execution mode can be set to read-only or read-write mode using the
 * `read_only` parameter. A limit can also be set using the `limit` parameter to
 * restrict the number of rows returned.
 *
 * @param cmd The SQL command to execute.
 * @param read_only A flag indicating whether the execution mode is read-only
 * (`true`) or read-write (`false`).
 * @param limit The maximum number of rows to return.
 * @param errorData The ErrorData struct with the error that occurred
 *
 * @return the SPITupleTable of the performed query.
 */
extern PGDLLEXPORT SPITupleTable *pldotnet_SPIExecute(char *cmd,
                                   bool read_only,
                                   int64_t limit,
                                   ErrorData **errorData);
 
/**
 * @brief Executes an SQL command using the Server Programming Interface (SPI).
 *
 * This function executes the given SQL command using the SPI interface after
 * preparing the server. The execution mode can be set to read-only or
 * read-write mode using the `read_only` parameter. A limit can also be set
 * using the `limit` parameter to restrict the number of rows returned.
 *
 * @param plan The SPIPlanPtr pointer returned by SPI_prepare.
 * @param paramValues The array with the datums parameters.
 * @param nullmap The array which maps the null values.
 * @param read_only A flag indicating whether the execution mode is read-only
 * (`true`) or read-write (`false`).
 * @param limit The maximum number of rows to return.
 * @param errorData The ErrorData struct with the error that occurred
 *
 * @return the SPITupleTable of the performed query.
 */
extern PGDLLEXPORT SPITupleTable *pldotnet_SPIExecutePlan(SPIPlanPtr plan,
                                       Datum *paramValues,
                                       const char *nullmap,
                                       bool read_only,
                                       int64_t limit,
                                       ErrorData **errorData);
 
/**
 * @brief Commits the current transaction using SPI_commit().
 *
 * @param errorData The ErrorData struct with the error that occurred
 *
 */
extern PGDLLEXPORT void pldotnet_SPICommit(ErrorData **errorData);
 
/**
 * @brief Rolls back the current transaction using SPI_rollback().
 *
 * @param errorData The ErrorData struct with the error that occurred
 *
 */
extern PGDLLEXPORT void pldotnet_SPIRollback(ErrorData **errorData);
 
/**
 * @brief Gets the number of columns if the provided SPITupleTable
 * stores a returned table. Otherwise, it returns 0.
 *
 * @param tupleTable A pointer to a SPITupleTable struct. It'll be a null
 * pointer if it is not related to a returned table.
 *
 * @return the number of columns in the table stored in the provided
 * SPITupleTable.
 */
extern PGDLLEXPORT int pldotnet_GetTableColumnNumber(SPITupleTable *tupleTable);
 
/**
 * @brief Gets the tuple description type OID if the provided SPITupleTable
 * stores a returned table. Otherwise, it returns 0.
 *
 * @param tupleTable A pointer to a SPITupleTable struct. It'll be a null
 * pointer if it is not related to a returned table.
 *
 * @return the tuple description type OID of the table stored in the provided
 * SPITupleTable.
 */
extern PGDLLEXPORT int pldotnet_GetTableTypeID(SPITupleTable *tupleTable);
 
/**
 * @brief Get the properties of the columns in the current query result set.
 *
 * This function retrieves the data types and names of the columns in the
 * current query result set. It stores the data types in `columnTypes`
 * and the names in `columnNames`.
 *
 * @param columnTypes Output parameter for the data types of the columns.
 * @param columnNames Output parameter for the names of the columns.
 * @param columnTypmods Output parameter for the type modifier of the columns.
 * @param columnLens Output parameter for the len/size of the columns.
 * @param tupleTable A pointer to a SPITupleTable struct. It'll be a null
 * pointer if it is not related to a returned table.
 */
extern PGDLLEXPORT void pldotnet_GetColProps(int *columnTypes,
                          char **columnNames,
                          int *columnTypmods,
                          int *columnLens,
                          SPITupleTable *tupleTable);
 
/**
 * @brief Extracts the data from the SPITupleTable into an array of Datums
 * and one of booleans indicating whether each value is NULL or not.
 *
 * @param row The index of the current row.
 * @param datums Pointer to an array of Datums. On return, this array will
 * contain the extracted data.
 * @param isNull Pointer to an array of booleans indicating whether each
 * value in `datums` is NULL or not. On return, this array will be filled
 * with the corresponding NULL information.
 * @param tupleTable A pointer to a SPITupleTable struct. It'll be a null
 * pointer if it is not related to a returned table.
 */
extern PGDLLEXPORT void pldotnet_GetRow(int row,
                                        Datum *datums,
                                        bool *isNull,
                                        SPITupleTable *tupleTable);
 
/**
 * @brief Prepares a statement for execution by the SPI manager.
 *
 * @param cmdPointer A pointer to an SPI plan to be filled in by this
 * function.
 * @param command The command string to prepare.
 * @param nargs The number of arguments in the argument type array.
 * @param argTypes An array of argument type OIDs.
 * @param errorData The ErrorData struct with the error that occurred
 */
extern PGDLLEXPORT void pldotnet_SPIPrepare(SPIPlanPtr *cmdPointer,
                                            char *command,
                                            int nargs,
                                            Oid *argTypes,
                                            ErrorData **errorData);
 
/**
 * @brief Get the string representing elevel
 * @param elevel The error level returned from ErrorData
 * This is the same as psql error_severity, but the function was added to be
 * compatible with pre-15 psql
 * @return the error type as a char pointer.
 */
extern PGDLLEXPORT const char *pldotnet_ErrorSeverity(int elevel);
 
/**
 * @brief Free the errorData Pointer after using it on C#
 * @param errorData The ErrorData struct with the error to be freed
 */
extern PGDLLEXPORT void pldotnet_FreeErrorData(ErrorData *errorData);
 
/**
 * @brief Return the value of the global variable SPI_processed.
 * @return the number of processed rows at the last query executed.
 */
extern PGDLLEXPORT uint64 pldotnet_GetProcessedRowsNumber(void);
 
#endif  // PLDOTNET_SPI_H_