Исходный код СУБД 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
/*-------------------------------------------------------------------------
 *
 * raw.c
 *  Oracle-compatible RAW type support.
 *
 *-------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "fmgr.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/varlena.h"
 
extern Datum byteain(PG_FUNCTION_ARGS);
extern Datum byteaout(PG_FUNCTION_ARGS);
extern Datum bytearecv(PG_FUNCTION_ARGS);
extern Datum byteasend(PG_FUNCTION_ARGS);
 
PG_FUNCTION_INFO_V1(raw_input);
PG_FUNCTION_INFO_V1(raw_out);
PG_FUNCTION_INFO_V1(raw_recv);
PG_FUNCTION_INFO_V1(raw_send);
PG_FUNCTION_INFO_V1(raw_typmodin);
PG_FUNCTION_INFO_V1(raw_typmodout);
PG_FUNCTION_INFO_V1(raw);
 
static void
raw_check_length(const struct varlena *value, int32 typmod)
{
    if (typmod >= 0 && VARSIZE_ANY_EXHDR(value) > typmod)
        ereport(ERROR,
                (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
                 errmsg("value too long for type RAW(%d)", typmod)));
}
 
Datum
raw_input(PG_FUNCTION_ARGS)
{
    Datum       result;
    int32       typmod = PG_GETARG_INT32(2);
 
    result = DirectFunctionCall1(byteain, PG_GETARG_DATUM(0));
    raw_check_length((struct varlena *) DatumGetPointer(result), typmod);
    PG_RETURN_DATUM(result);
}
 
Datum
raw_out(PG_FUNCTION_ARGS)
{
    return byteaout(fcinfo);
}
 
Datum
raw_recv(PG_FUNCTION_ARGS)
{
    Datum       result;
    int32       typmod = PG_GETARG_INT32(2);
 
    result = DirectFunctionCall1(bytearecv, PG_GETARG_DATUM(0));
    raw_check_length((struct varlena *) DatumGetPointer(result), typmod);
    PG_RETURN_DATUM(result);
}
 
Datum
raw_send(PG_FUNCTION_ARGS)
{
    return byteasend(fcinfo);
}
 
Datum
raw_typmodin(PG_FUNCTION_ARGS)
{
    ArrayType  *ta = PG_GETARG_ARRAYTYPE_P(0);
    int32      *tl;
    int         n;
    int32       typmod;
 
    tl = ArrayGetIntegerTypmods(ta, &n);
    if (n != 1)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("RAW type modifier must be a single integer")));
 
    typmod = *tl;
 
    if (typmod < 1 || typmod > 2000)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("RAW length must be between 1 and 2000")));
 
    PG_RETURN_INT32(typmod);
}
 
Datum
raw_typmodout(PG_FUNCTION_ARGS)
{
    int32       typmod = PG_GETARG_INT32(0);
 
    if (typmod < 0)
        PG_RETURN_CSTRING(pstrdup(""));
    PG_RETURN_CSTRING(psprintf("(%d)", typmod));
}
 
Datum
raw(PG_FUNCTION_ARGS)
{
    struct varlena *value = PG_GETARG_VARLENA_PP(0);
    int32       typmod = PG_GETARG_INT32(1);
 
    raw_check_length(value, typmod);
    PG_RETURN_POINTER(value);
}