Исходный код СУБД 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
/*-------------------------------------------------------------------------
 *
 * quote.c
 *    Functions for quoting identifiers and literals
 *
 * Portions Copyright (c) 2000-2026, PostgreSQL Global Development Group
 *
 *
 * IDENTIFICATION
 *    src/backend/utils/adt/quote.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
 
#include "utils/builtins.h"
#include "varatt.h"
 
 
/*
 * quote_ident -
 *    returns a properly quoted identifier
 */
Datum
quote_ident(PG_FUNCTION_ARGS)
{
    text       *t = PG_GETARG_TEXT_PP(0);
    const char *qstr;
    char       *str;
 
    str = text_to_cstring(t);
    qstr = quote_identifier(str);
    PG_RETURN_TEXT_P(cstring_to_text(qstr));
}
 
/*
 * quote_literal_internal -
 *    helper function for quote_literal and quote_literal_cstr
 *
 * NOTE: This must produce output that will work in old servers with
 * standard_conforming_strings = off.  It's used for example by
 * dblink, which may send the result to another server.
 */
static size_t
quote_literal_internal(char *dst, const char *src, size_t len)
{
    const char *s;
    char       *savedst = dst;
 
    for (s = src; s < src + len; s++)
    {
        if (*s == '\\')
        {
            *dst++ = ESCAPE_STRING_SYNTAX;
            break;
        }
    }
 
    *dst++ = '\'';
    while (len-- > 0)
    {
        if (SQL_STR_DOUBLE(*src, true))
            *dst++ = *src;
        *dst++ = *src++;
    }
    *dst++ = '\'';
 
    return dst - savedst;
}
 
/*
 * quote_literal -
 *    returns a properly quoted literal
 */
Datum
quote_literal(PG_FUNCTION_ARGS)
{
    text       *t = PG_GETARG_TEXT_PP(0);
    text       *result;
    char       *cp1;
    char       *cp2;
    int         len;
 
    len = VARSIZE_ANY_EXHDR(t);
    /* We make a worst-case result area; wasting a little space is OK */
    result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
 
    cp1 = VARDATA_ANY(t);
    cp2 = VARDATA(result);
 
    SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
 
    PG_RETURN_TEXT_P(result);
}
 
/*
 * quote_literal_cstr -
 *    returns a properly quoted literal
 */
char *
quote_literal_cstr(const char *rawstr)
{
    char       *result;
    int         len;
    int         newlen;
 
    len = strlen(rawstr);
    /* We make a worst-case result area; wasting a little space is OK */
    result = palloc(
                    (len * 2)   /* doubling for every character if each one is
                                 * a quote */
                    + 3         /* two outer quotes + possibly 'E' if needed */
                    + 1         /* null terminator */
        );
 
    newlen = quote_literal_internal(result, rawstr, len);
    result[newlen] = '\0';
 
    return result;
}
 
/*
 * quote_nullable -
 *    Returns a properly quoted literal, with null values returned
 *    as the text string 'NULL'.
 */
Datum
quote_nullable(PG_FUNCTION_ARGS)
{
    if (PG_ARGISNULL(0))
        PG_RETURN_TEXT_P(cstring_to_text("NULL"));
    else
        PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
                                            PG_GETARG_DATUM(0)));
}