PostgreSQL Source Code git master
Loading...
Searching...
No Matches
quote.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * quote.c
4 * Functions for quoting identifiers and literals
5 *
6 * Portions Copyright (c) 2000-2026, PostgreSQL Global Development Group
7 *
8 *
9 * IDENTIFICATION
10 * src/backend/utils/adt/quote.c
11 *
12 *-------------------------------------------------------------------------
13 */
14#include "postgres.h"
15
16#include "utils/builtins.h"
17#include "varatt.h"
18
19
20/*
21 * quote_ident -
22 * returns a properly quoted identifier
23 */
35
36/*
37 * quote_literal_internal -
38 * helper function for quote_literal and quote_literal_cstr
39 *
40 * NOTE: This must produce output that will work in old servers with
41 * standard_conforming_strings = off. It's used for example by
42 * dblink, which may send the result to another server.
43 */
44static size_t
45quote_literal_internal(char *dst, const char *src, size_t len)
46{
47 const char *s;
48 char *savedst = dst;
49
50 for (s = src; s < src + len; s++)
51 {
52 if (*s == '\\')
53 {
55 break;
56 }
57 }
58
59 *dst++ = '\'';
60 while (len-- > 0)
61 {
62 if (SQL_STR_DOUBLE(*src, true))
63 *dst++ = *src;
64 *dst++ = *src++;
65 }
66 *dst++ = '\'';
67
68 return dst - savedst;
69}
70
71/*
72 * quote_literal -
73 * returns a properly quoted literal
74 */
77{
78 text *t = PG_GETARG_TEXT_PP(0);
79 text *result;
80 char *cp1;
81 char *cp2;
82 int len;
83
85 /* We make a worst-case result area; wasting a little space is OK */
86 result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
87
88 cp1 = VARDATA_ANY(t);
89 cp2 = VARDATA(result);
90
92
93 PG_RETURN_TEXT_P(result);
94}
95
96/*
97 * quote_literal_cstr -
98 * returns a properly quoted literal
99 */
100char *
102{
103 char *result;
104 int len;
105 int newlen;
106
107 len = strlen(rawstr);
108 /* We make a worst-case result area; wasting a little space is OK */
109 result = palloc(
110 (len * 2) /* doubling for every character if each one is
111 * a quote */
112 + 3 /* two outer quotes + possibly 'E' if needed */
113 + 1 /* null terminator */
114 );
115
117 result[newlen] = '\0';
118
119 return result;
120}
121
122/*
123 * quote_nullable -
124 * Returns a properly quoted literal, with null values returned
125 * as the text string 'NULL'.
126 */
127Datum
#define VARHDRSZ
Definition c.h:711
#define ESCAPE_STRING_SYNTAX
Definition c.h:1161
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition c.h:1158
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
const char * str
void * palloc(Size size)
Definition mcxt.c:1387
const void size_t len
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
static size_t quote_literal_internal(char *dst, const char *src, size_t len)
Definition quote.c:45
char * quote_literal_cstr(const char *rawstr)
Definition quote.c:101
Datum quote_ident(PG_FUNCTION_ARGS)
Definition quote.c:25
Datum quote_nullable(PG_FUNCTION_ARGS)
Definition quote.c:128
Datum quote_literal(PG_FUNCTION_ARGS)
Definition quote.c:76
const char * quote_identifier(const char *ident)
Definition c.h:706
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
text * cstring_to_text(const char *s)
Definition varlena.c:181
char * text_to_cstring(const text *t)
Definition varlena.c:214