PostgreSQL Source Code  git master
quote.c File Reference
#include "postgres.h"
#include "utils/builtins.h"
Include dependency graph for quote.c:

Go to the source code of this file.

Functions

Datum quote_ident (PG_FUNCTION_ARGS)
 
static size_t quote_literal_internal (char *dst, const char *src, size_t len)
 
Datum quote_literal (PG_FUNCTION_ARGS)
 
char * quote_literal_cstr (const char *rawstr)
 
Datum quote_nullable (PG_FUNCTION_ARGS)
 

Function Documentation

◆ quote_ident()

Datum quote_ident ( PG_FUNCTION_ARGS  )

Definition at line 24 of file quote.c.

References cstring_to_text(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, quote_identifier(), generate_unaccent_rules::str, and text_to_cstring().

Referenced by quote_ident_cstr().

25 {
26  text *t = PG_GETARG_TEXT_PP(0);
27  const char *qstr;
28  char *str;
29 
30  str = text_to_cstring(t);
31  qstr = quote_identifier(str);
33 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10628
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
char * text_to_cstring(const text *t)
Definition: varlena.c:204
Definition: c.h:549

◆ quote_literal()

Datum quote_literal ( PG_FUNCTION_ARGS  )

Definition at line 77 of file quote.c.

References palloc(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, quote_literal_internal(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by quote_nullable().

78 {
79  text *t = PG_GETARG_TEXT_PP(0);
80  text *result;
81  char *cp1;
82  char *cp2;
83  int len;
84 
85  len = VARSIZE_ANY_EXHDR(t);
86  /* We make a worst-case result area; wasting a little space is OK */
87  result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
88 
89  cp1 = VARDATA_ANY(t);
90  cp2 = VARDATA(result);
91 
92  SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
93 
94  PG_RETURN_TEXT_P(result);
95 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static size_t quote_literal_internal(char *dst, const char *src, size_t len)
Definition: quote.c:46
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ quote_literal_cstr()

char* quote_literal_cstr ( const char *  rawstr)

Definition at line 102 of file quote.c.

References palloc(), and quote_literal_internal().

Referenced by build_tuplestore_recursively(), fetch_remote_table_info(), fetch_table_list(), get_sql_delete(), get_sql_insert(), get_sql_update(), get_tuple_of_interest(), PLy_quote_literal(), PLy_quote_nullable(), and text_format_string_conversion().

103 {
104  char *result;
105  int len;
106  int newlen;
107 
108  len = strlen(rawstr);
109  /* We make a worst-case result area; wasting a little space is OK */
110  result = palloc(len * 2 + 3 + 1);
111 
112  newlen = quote_literal_internal(result, rawstr, len);
113  result[newlen] = '\0';
114 
115  return result;
116 }
static size_t quote_literal_internal(char *dst, const char *src, size_t len)
Definition: quote.c:46
void * palloc(Size size)
Definition: mcxt.c:949

◆ quote_literal_internal()

static size_t quote_literal_internal ( char *  dst,
const char *  src,
size_t  len 
)
static

Definition at line 46 of file quote.c.

References ESCAPE_STRING_SYNTAX, and SQL_STR_DOUBLE.

Referenced by quote_literal(), and quote_literal_cstr().

47 {
48  const char *s;
49  char *savedst = dst;
50 
51  for (s = src; s < src + len; s++)
52  {
53  if (*s == '\\')
54  {
55  *dst++ = ESCAPE_STRING_SYNTAX;
56  break;
57  }
58  }
59 
60  *dst++ = '\'';
61  while (len-- > 0)
62  {
63  if (SQL_STR_DOUBLE(*src, true))
64  *dst++ = *src;
65  *dst++ = *src++;
66  }
67  *dst++ = '\'';
68 
69  return dst - savedst;
70 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1086
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1083

◆ quote_nullable()

Datum quote_nullable ( PG_FUNCTION_ARGS  )

Definition at line 124 of file quote.c.

References cstring_to_text(), DirectFunctionCall1, PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_DATUM, PG_RETURN_TEXT_P, and quote_literal().

125 {
126  if (PG_ARGISNULL(0))
128  else
130  PG_GETARG_DATUM(0)));
131 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum quote_literal(PG_FUNCTION_ARGS)
Definition: quote.c:77
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define PG_ARGISNULL(n)
Definition: fmgr.h:204