Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*-------------------------------------------------------------------------
 *
 * oid.c
 *    Functions for the built-in type Oid ... also oidvector.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    src/backend/utils/adt/oid.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
 
#include <ctype.h>
#include <limits.h>
 
#include "catalog/pg_type.h"
#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
#include "utils/array.h"
#include "utils/builtins.h"
 
 
#define OidVectorSize(n)    (offsetof(oidvector, values) + (n) * sizeof(Oid))
 
 
/*****************************************************************************
 *   USER I/O ROUTINES                                                       *
 *****************************************************************************/
 
Datum
oidin(PG_FUNCTION_ARGS)
{
    char       *s = PG_GETARG_CSTRING(0);
    Oid         result;
 
    result = uint32in_subr(s, NULL, "oid", fcinfo->context);
    PG_RETURN_OID(result);
}
 
Datum
oidout(PG_FUNCTION_ARGS)
{
    Oid         o = PG_GETARG_OID(0);
    char       *result = (char *) palloc(12);
 
    snprintf(result, 12, "%u", o);
    PG_RETURN_CSTRING(result);
}
 
/*
 *      oidrecv         - converts external binary format to oid
 */
Datum
oidrecv(PG_FUNCTION_ARGS)
{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
 
    PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
}
 
/*
 *      oidsend         - converts oid to binary format
 */
Datum
oidsend(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    StringInfoData buf;
 
    pq_begintypsend(&buf);
    pq_sendint32(&buf, arg1);
    PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
 
/*
 * construct oidvector given a raw array of Oids
 *
 * If oids is NULL then caller must fill values[] afterward
 */
oidvector *
buildoidvector(const Oid *oids, int n)
{
    oidvector  *result;
 
    result = (oidvector *) palloc0(OidVectorSize(n));
 
    if (n > 0 && oids)
        memcpy(result->values, oids, n * sizeof(Oid));
 
    /*
     * Attach standard array header.  For historical reasons, we set the index
     * lower bound to 0 not 1.
     */
    SET_VARSIZE(result, OidVectorSize(n));
    result->ndim = 1;
    result->dataoffset = 0;     /* never any nulls */
    result->elemtype = OIDOID;
    result->dim1 = n;
    result->lbound1 = 0;
 
    return result;
}
 
/*
 * validate that an array object meets the restrictions of oidvector
 *
 * We need this because there are pathways by which a general oid[] array can
 * be cast to oidvector, allowing the type's restrictions to be violated.
 * All code that receives an oidvector as a SQL parameter should check this.
 */
void
check_valid_oidvector(const oidvector *oidArray)
{
    /*
     * We insist on ndim == 1 and dataoffset == 0 (that is, no nulls) because
     * otherwise the array's layout will not be what calling code expects.  We
     * needn't be picky about the index lower bound though.  Checking elemtype
     * is just paranoia.
     */
    if (oidArray->ndim != 1 ||
        oidArray->dataoffset != 0 ||
        oidArray->elemtype != OIDOID)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("array is not a valid oidvector")));
}
 
/*
 *      oidvectorin         - converts "num num ..." to internal form
 */
Datum
oidvectorin(PG_FUNCTION_ARGS)
{
    char       *oidString = PG_GETARG_CSTRING(0);
    Node       *escontext = fcinfo->context;
    oidvector  *result;
    int         nalloc;
    int         n;
 
    nalloc = 32;                /* arbitrary initial size guess */
    result = (oidvector *) palloc0(OidVectorSize(nalloc));
 
    for (n = 0;; n++)
    {
        while (*oidString && isspace((unsigned char) *oidString))
            oidString++;
        if (*oidString == '\0')
            break;
 
        if (n >= nalloc)
        {
            nalloc *= 2;
            result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
        }
 
        result->values[n] = uint32in_subr(oidString, &oidString,
                                          "oid", escontext);
        if (SOFT_ERROR_OCCURRED(escontext))
            PG_RETURN_NULL();
    }
 
    SET_VARSIZE(result, OidVectorSize(n));
    result->ndim = 1;
    result->dataoffset = 0;     /* never any nulls */
    result->elemtype = OIDOID;
    result->dim1 = n;
    result->lbound1 = 0;
 
    PG_RETURN_POINTER(result);
}
 
/*
 *      oidvectorout - converts internal form to "num num ..."
 */
Datum
oidvectorout(PG_FUNCTION_ARGS)
{
    oidvector  *oidArray = (oidvector *) PG_GETARG_POINTER(0);
    int         num,
                nnums;
    char       *rp;
    char       *result;
 
    /* validate input before fetching dim1 */
    check_valid_oidvector(oidArray);
    nnums = oidArray->dim1;
 
    /* assumes sign, 10 digits, ' ' */
    rp = result = (char *) palloc(nnums * 12 + 1);
    for (num = 0; num < nnums; num++)
    {
        if (num != 0)
            *rp++ = ' ';
        sprintf(rp, "%u", oidArray->values[num]);
        while (*++rp != '\0')
            ;
    }
    *rp = '\0';
    PG_RETURN_CSTRING(result);
}
 
/*
 *      oidvectorrecv           - converts external binary format to oidvector
 */
Datum
oidvectorrecv(PG_FUNCTION_ARGS)
{
    LOCAL_FCINFO(locfcinfo, 3);
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    oidvector  *result;
 
    /*
     * Normally one would call array_recv() using DirectFunctionCall3, but
     * that does not work since array_recv wants to cache some data using
     * fcinfo->flinfo->fn_extra.  So we need to pass it our own flinfo
     * parameter.
     */
    InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
                             InvalidOid, NULL, NULL);
 
    locfcinfo->args[0].value = PointerGetDatum(buf);
    locfcinfo->args[0].isnull = false;
    locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
    locfcinfo->args[1].isnull = false;
    locfcinfo->args[2].value = Int32GetDatum(-1);
    locfcinfo->args[2].isnull = false;
 
    result = (oidvector *) DatumGetPointer(array_recv(locfcinfo));
 
    Assert(!locfcinfo->isnull);
 
    /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
    if (ARR_NDIM(result) != 1 ||
        ARR_HASNULL(result) ||
        ARR_ELEMTYPE(result) != OIDOID ||
        ARR_LBOUND(result)[0] != 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
                 errmsg("invalid oidvector data")));
 
    PG_RETURN_POINTER(result);
}
 
/*
 *      oidvectorsend           - converts oidvector to binary format
 */
Datum
oidvectorsend(PG_FUNCTION_ARGS)
{
    /* We don't do check_valid_oidvector, since array_send won't care */
    return array_send(fcinfo);
}
 
/*
 *      oidparse                - get OID from ICONST/FCONST node
 */
Oid
oidparse(Node *node)
{
    switch (nodeTag(node))
    {
        case T_Integer:
            return intVal(node);
        case T_Float:
 
            /*
             * Values too large for int4 will be represented as Float
             * constants by the lexer.  Accept these if they are valid OID
             * strings.
             */
            return uint32in_subr(castNode(Float, node)->fval, NULL,
                                 "oid", NULL);
        default:
            elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
    }
    return InvalidOid;          /* keep compiler quiet */
}
 
/* qsort comparison function for Oids */
int
oid_cmp(const void *p1, const void *p2)
{
    Oid         v1 = *((const Oid *) p1);
    Oid         v2 = *((const Oid *) p2);
 
    return pg_cmp_u32(v1, v2);
}
 
 
/*****************************************************************************
 *   PUBLIC ROUTINES                                                         *
 *****************************************************************************/
 
Datum
oideq(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_BOOL(arg1 == arg2);
}
 
Datum
oidne(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_BOOL(arg1 != arg2);
}
 
Datum
oidlt(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_BOOL(arg1 < arg2);
}
 
Datum
oidle(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_BOOL(arg1 <= arg2);
}
 
Datum
oidge(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_BOOL(arg1 >= arg2);
}
 
Datum
oidgt(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_BOOL(arg1 > arg2);
}
 
Datum
oidlarger(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
}
 
Datum
oidsmaller(PG_FUNCTION_ARGS)
{
    Oid         arg1 = PG_GETARG_OID(0);
    Oid         arg2 = PG_GETARG_OID(1);
 
    PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
}
 
Datum
oidvectoreq(PG_FUNCTION_ARGS)
{
    int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
    PG_RETURN_BOOL(cmp == 0);
}
 
Datum
oidvectorne(PG_FUNCTION_ARGS)
{
    int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
    PG_RETURN_BOOL(cmp != 0);
}
 
Datum
oidvectorlt(PG_FUNCTION_ARGS)
{
    int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
    PG_RETURN_BOOL(cmp < 0);
}
 
Datum
oidvectorle(PG_FUNCTION_ARGS)
{
    int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
    PG_RETURN_BOOL(cmp <= 0);
}
 
Datum
oidvectorge(PG_FUNCTION_ARGS)
{
    int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
    PG_RETURN_BOOL(cmp >= 0);
}
 
Datum
oidvectorgt(PG_FUNCTION_ARGS)
{
    int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
    PG_RETURN_BOOL(cmp > 0);
}