Исходный код СУБД 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
/*
 * 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.c
 *
 */
 
#include "pldotnet_spi.h"
 
bool is_spi_open = false;
 
const char *pldotnet_ErrorSeverity(int elevel) {
    const char *prefix;
 
    switch (elevel) {
        case DEBUG1:
        case DEBUG2:
        case DEBUG3:
        case DEBUG4:
        case DEBUG5:
            prefix = gettext_noop("DEBUG");
            break;
        case LOG:
        case LOG_SERVER_ONLY:
            prefix = gettext_noop("LOG");
            break;
        case INFO:
            prefix = gettext_noop("INFO");
            break;
        case NOTICE:
            prefix = gettext_noop("NOTICE");
            break;
        case WARNING:
#if PG_VERSION_NUM >= 140000
        case WARNING_CLIENT_ONLY:
#endif
            prefix = gettext_noop("WARNING");
            break;
        case ERROR:
            prefix = gettext_noop("ERROR");
            break;
        case FATAL:
            prefix = gettext_noop("FATAL");
            break;
        case PANIC:
            prefix = gettext_noop("PANIC");
            break;
        default:
            prefix = "???";
            break;
    }
    return prefix;
}
 
SPITupleTable *pldotnet_SPIExecute(char *cmd, bool read_only, int64_t limit,
                                   ErrorData **errorData) {
    MemoryContextWrapper memory_context;
    int rv = 0;
 
    PG_TRY();
    {
        pldotnet_StartNewMemoryContext(&memory_context);
 
        rv = SPI_execute(cmd, read_only, limit);
 
        pldotnet_ResetMemoryContext(&memory_context);
    }
    PG_CATCH();
    {
        elog(WARNING,
             "Exception: pldotnet_SPIExecute. SPI_result = %d. SPI_execute "
             "returned %d.",
             SPI_result, rv);
        pldotnet_ResetMemoryContext(&memory_context);
        *errorData = CopyErrorData();
        FlushErrorState();
    }
    PG_END_TRY();
 
    return SPI_tuptable;
}
 
SPITupleTable *pldotnet_SPIExecutePlan(SPIPlanPtr plan, Datum *paramValues,
                                       const char *nullmap, bool read_only,
                                       int64_t limit, ErrorData **errorData) {
    MemoryContextWrapper memory_context;
    int rv = 0;
 
    PG_TRY();
    {
        pldotnet_StartNewMemoryContext(&memory_context);
 
        rv = SPI_execute_plan(plan, paramValues, nullmap, read_only, limit);
 
        pldotnet_ResetMemoryContext(&memory_context);
    }
    PG_CATCH();
    {
        elog(WARNING,
             "Exception: pldotnet_SPIExecutePlan. SPI_result = %d. SPI_execute "
             "returned %d.",
             SPI_result, rv);
        pldotnet_ResetMemoryContext(&memory_context);
        *errorData = CopyErrorData();
        FlushErrorState();
    }
    PG_END_TRY();
 
    return SPI_tuptable;
    return SPI_tuptable;
}
 
void pldotnet_SPICommit(ErrorData **errorData) {
    MemoryContextWrapper memory_context;
 
    PG_TRY();
    {
        pldotnet_StartNewMemoryContext(&memory_context);
        SPI_commit();
        pldotnet_ResetMemoryContext(&memory_context);
    }
    PG_CATCH();
    {
        elog(WARNING, "Exception: pldotnet_SPICommit");
        pldotnet_ResetMemoryContext(&memory_context);
        *errorData = CopyErrorData();
        FlushErrorState();
    }
    PG_END_TRY();
}
 
void pldotnet_SPIRollback(ErrorData **errorData) {
    MemoryContextWrapper memory_context;
 
    PG_TRY();
    {
        pldotnet_StartNewMemoryContext(&memory_context);
        SPI_rollback();
        pldotnet_ResetMemoryContext(&memory_context);
    }
    PG_CATCH();
    {
        elog(WARNING, "Exception: pldotnet_SPIRollback");
        pldotnet_ResetMemoryContext(&memory_context);
        *errorData = CopyErrorData();
        FlushErrorState();
    }
    PG_END_TRY();
}
 
int pldotnet_GetTableColumnNumber(SPITupleTable *tupleTable) {
    return tupleTable != NULL ? (int)tupleTable->tupdesc->natts : 0;
}
 
int pldotnet_GetTableTypeID(SPITupleTable *tupleTable) {
    return tupleTable != NULL ? (int)tupleTable->tupdesc->tdtypeid : 0;
}
 
void pldotnet_GetColProps(int *columnTypes, char **columnNames,
                          int *columnTypmods, int *columnLens,
                          SPITupleTable *tupleTable) {
    int ncols = (int)tupleTable->tupdesc->natts;
    Form_pg_attribute attr;
 
    // More attributes can be identified here:
    // https://doxygen.postgresql.org/tupdesc_8h.html
 
    for (int i = 0; i < ncols; i++) {
        attr = TupleDescAttr(tupleTable->tupdesc, i);
        columnTypes[i] = attr->atttypid;
        columnNames[i] = NameStr(attr->attname);
        columnTypmods[i] = (int)(attr->atttypmod);
        columnLens[i] = (int)(attr->attlen);
    }
}
 
void pldotnet_GetRow(int row, Datum *datums, bool *isNull,
                     SPITupleTable *tupleTable) {
    int ncols = (int)tupleTable->tupdesc->natts;
    Form_pg_attribute attr;
 
    for (int i = 0; i < ncols; i++) {
        attr = TupleDescAttr(tupleTable->tupdesc, i);
        datums[i] = heap_getattr(tupleTable->vals[row], attr->attnum,
                                 tupleTable->tupdesc, &isNull[i]);
    }
}
 
void pldotnet_SPIPrepare(SPIPlanPtr *cmdPointer, char *command, int nargs,
                         Oid *argTypes, ErrorData **errorData) {
    MemoryContextWrapper memory_context;
 
    PG_TRY();
    {
        pldotnet_StartNewMemoryContext(&memory_context);
        *cmdPointer = SPI_prepare(command, nargs, argTypes);
        pldotnet_ResetMemoryContext(&memory_context);
    }
    PG_CATCH();
    {
        elog(WARNING, "Exception: pldotnet_SPIPrepare. SPI_result = %d.",
             SPI_result);
        pldotnet_ResetMemoryContext(&memory_context);
        *errorData = CopyErrorData();
        FlushErrorState();
    }
    PG_END_TRY();
}
 
void pldotnet_FreeErrorData(ErrorData *errorData) { FreeErrorData(errorData); }
 
uint64 pldotnet_GetProcessedRowsNumber(void) { return SPI_processed; }