PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
name.c File Reference
#include "postgres.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
Include dependency graph for name.c:

Go to the source code of this file.

Functions

Datum namein (PG_FUNCTION_ARGS)
 
Datum nameout (PG_FUNCTION_ARGS)
 
Datum namerecv (PG_FUNCTION_ARGS)
 
Datum namesend (PG_FUNCTION_ARGS)
 
Datum nameeq (PG_FUNCTION_ARGS)
 
Datum namene (PG_FUNCTION_ARGS)
 
Datum namelt (PG_FUNCTION_ARGS)
 
Datum namele (PG_FUNCTION_ARGS)
 
Datum namegt (PG_FUNCTION_ARGS)
 
Datum namege (PG_FUNCTION_ARGS)
 
int namecpy (Name n1, Name n2)
 
int namestrcpy (Name name, const char *str)
 
int namestrcmp (Name name, const char *str)
 
Datum current_user (PG_FUNCTION_ARGS)
 
Datum session_user (PG_FUNCTION_ARGS)
 
Datum current_schema (PG_FUNCTION_ARGS)
 
Datum current_schemas (PG_FUNCTION_ARGS)
 

Function Documentation

Datum current_schema ( PG_FUNCTION_ARGS  )

Definition at line 279 of file name.c.

References CStringGetDatum, DirectFunctionCall1, fetch_search_path(), get_namespace_name(), linitial_oid, list_free(), namein(), NIL, PG_RETURN_DATUM, and PG_RETURN_NULL.

Referenced by ExecEvalSQLValueFunction().

280 {
281  List *search_path = fetch_search_path(false);
282  char *nspname;
283 
284  if (search_path == NIL)
285  PG_RETURN_NULL();
286  nspname = get_namespace_name(linitial_oid(search_path));
287  list_free(search_path);
288  if (!nspname)
289  PG_RETURN_NULL(); /* recently-deleted namespace? */
291 }
#define NIL
Definition: pg_list.h:69
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
#define CStringGetDatum(X)
Definition: postgres.h:584
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define linitial_oid(l)
Definition: pg_list.h:113
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4168
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum current_schemas ( PG_FUNCTION_ARGS  )

Definition at line 294 of file name.c.

References construct_array(), CStringGetDatum, DirectFunctionCall1, fetch_search_path(), get_namespace_name(), i, lfirst_oid, list_free(), list_length(), namein(), NAMEOID, palloc(), PG_GETARG_BOOL, and PG_RETURN_POINTER.

295 {
296  List *search_path = fetch_search_path(PG_GETARG_BOOL(0));
297  ListCell *l;
298  Datum *names;
299  int i;
300  ArrayType *array;
301 
302  names = (Datum *) palloc(list_length(search_path) * sizeof(Datum));
303  i = 0;
304  foreach(l, search_path)
305  {
306  char *nspname;
307 
308  nspname = get_namespace_name(lfirst_oid(l));
309  if (nspname) /* watch out for deleted namespace */
310  {
311  names[i] = DirectFunctionCall1(namein, CStringGetDatum(nspname));
312  i++;
313  }
314  }
315  list_free(search_path);
316 
317  array = construct_array(names, i,
318  NAMEOID,
319  NAMEDATALEN, /* sizeof(Name) */
320  false, /* Name is not by-val */
321  'c'); /* alignment of Name */
322 
323  PG_RETURN_POINTER(array);
324 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define NAMEOID
Definition: pg_type.h:300
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define NAMEDATALEN
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
#define CStringGetDatum(X)
Definition: postgres.h:584
uintptr_t Datum
Definition: postgres.h:372
static int list_length(const List *l)
Definition: pg_list.h:89
void * palloc(Size size)
Definition: mcxt.c:848
void list_free(List *list)
Definition: list.c:1133
int i
Definition: pg_list.h:45
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4168
#define lfirst_oid(lc)
Definition: pg_list.h:108
Datum current_user ( PG_FUNCTION_ARGS  )

Definition at line 263 of file name.c.

References CStringGetDatum, DirectFunctionCall1, GetUserId(), GetUserNameFromId(), namein(), and PG_RETURN_DATUM.

Referenced by ExecEvalSQLValueFunction().

264 {
266 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
Oid GetUserId(void)
Definition: miscinit.c:284
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
#define CStringGetDatum(X)
Definition: postgres.h:584
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:692
int namecpy ( Name  n1,
Name  n2 
)

Definition at line 191 of file name.c.

References NAMEDATALEN, NameStr, and StrNCpy.

Referenced by pg_get_replication_slots().

192 {
193  if (!n1 || !n2)
194  return -1;
195  StrNCpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
196  return 0;
197 }
#define NAMEDATALEN
#define StrNCpy(dst, src, len)
Definition: c.h:836
#define NameStr(name)
Definition: c.h:493
Datum nameeq ( PG_FUNCTION_ARGS  )

Definition at line 134 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

135 {
136  Name arg1 = PG_GETARG_NAME(0);
137  Name arg2 = PG_GETARG_NAME(1);
138 
139  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) == 0);
140 }
#define NAMEDATALEN
Definition: c.h:487
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namege ( PG_FUNCTION_ARGS  )

Definition at line 179 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

180 {
181  Name arg1 = PG_GETARG_NAME(0);
182  Name arg2 = PG_GETARG_NAME(1);
183 
184  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
185 }
#define NAMEDATALEN
Definition: c.h:487
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namegt ( PG_FUNCTION_ARGS  )

Definition at line 170 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

171 {
172  Name arg1 = PG_GETARG_NAME(0);
173  Name arg2 = PG_GETARG_NAME(1);
174 
175  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0);
176 }
#define NAMEDATALEN
Definition: c.h:487
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namein ( PG_FUNCTION_ARGS  )

Definition at line 46 of file name.c.

References NAMEDATALEN, NameStr, palloc0(), PG_GETARG_CSTRING, pg_mbcliplen(), and PG_RETURN_NAME.

Referenced by AlterSubscription(), convert_requires_to_datum(), CreateAccessMethod(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreatePolicy(), CreatePublication(), CreateRole(), CreateSubscription(), CreateTableSpace(), CreateTrigger(), current_schema(), current_schemas(), current_user(), get_available_versions_for_extension(), getdatabaseencoding(), InsertExtensionTuple(), pg_available_extensions(), pg_client_encoding(), pg_convert_from(), pg_convert_to(), PG_encoding_to_char(), pg_get_viewdef_worker(), plpgsql_exec_trigger(), RenameRole(), session_user(), and string_to_datum().

47 {
48  char *s = PG_GETARG_CSTRING(0);
49  Name result;
50  int len;
51 
52  len = strlen(s);
53 
54  /* Truncate oversize input */
55  if (len >= NAMEDATALEN)
56  len = pg_mbcliplen(s, len, NAMEDATALEN - 1);
57 
58  /* We use palloc0 here to ensure result is zero-padded */
59  result = (Name) palloc0(NAMEDATALEN);
60  memcpy(NameStr(*result), s, len);
61 
62  PG_RETURN_NAME(result);
63 }
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:820
Definition: c.h:487
void * palloc0(Size size)
Definition: mcxt.c:877
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
NameData * Name
Definition: c.h:491
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
Datum namele ( PG_FUNCTION_ARGS  )

Definition at line 161 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

162 {
163  Name arg1 = PG_GETARG_NAME(0);
164  Name arg2 = PG_GETARG_NAME(1);
165 
166  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) <= 0);
167 }
#define NAMEDATALEN
Definition: c.h:487
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namelt ( PG_FUNCTION_ARGS  )

Definition at line 152 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

153 {
154  Name arg1 = PG_GETARG_NAME(0);
155  Name arg2 = PG_GETARG_NAME(1);
156 
157  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) < 0);
158 }
#define NAMEDATALEN
Definition: c.h:487
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namene ( PG_FUNCTION_ARGS  )

Definition at line 143 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

144 {
145  Name arg1 = PG_GETARG_NAME(0);
146  Name arg2 = PG_GETARG_NAME(1);
147 
148  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) != 0);
149 }
#define NAMEDATALEN
Definition: c.h:487
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum nameout ( PG_FUNCTION_ARGS  )

Definition at line 69 of file name.c.

References NameStr, PG_GETARG_NAME, PG_RETURN_CSTRING, and pstrdup().

Referenced by make_greater_string(), RelationBuildTriggers(), and set_timetravel().

70 {
71  Name s = PG_GETARG_NAME(0);
72 
74 }
char * pstrdup(const char *in)
Definition: mcxt.c:1076
Definition: c.h:487
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namerecv ( PG_FUNCTION_ARGS  )

Definition at line 80 of file name.c.

References buf, StringInfoData::cursor, ereport, errcode(), errdetail(), errmsg(), ERROR, StringInfoData::len, NAMEDATALEN, palloc0(), pfree(), PG_GETARG_POINTER, PG_RETURN_NAME, and pq_getmsgtext().

81 {
83  Name result;
84  char *str;
85  int nbytes;
86 
87  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
88  if (nbytes >= NAMEDATALEN)
89  ereport(ERROR,
90  (errcode(ERRCODE_NAME_TOO_LONG),
91  errmsg("identifier too long"),
92  errdetail("Identifier must be less than %d characters.",
93  NAMEDATALEN)));
94  result = (NameData *) palloc0(NAMEDATALEN);
95  memcpy(result, str, nbytes);
96  pfree(str);
97  PG_RETURN_NAME(result);
98 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define NAMEDATALEN
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
Definition: c.h:487
static char * buf
Definition: pg_test_fsync.c:67
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
void * palloc0(Size size)
Definition: mcxt.c:877
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
Datum namesend ( PG_FUNCTION_ARGS  )

Definition at line 104 of file name.c.

References buf, NameStr, PG_GETARG_NAME, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendtext().

105 {
106  Name s = PG_GETARG_NAME(0);
108 
109  pq_begintypsend(&buf);
110  pq_sendtext(&buf, NameStr(*s), strlen(NameStr(*s)));
112 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Definition: c.h:487
static char * buf
Definition: pg_test_fsync.c:67
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
int namestrcmp ( Name  name,
const char *  str 
)

Definition at line 247 of file name.c.

References NameStr.

Referenced by attnameAttNum(), CopyGetAttnums(), CreateTrigger(), get_timetravel(), GetAttributeByName(), set_timetravel(), and SPI_fnumber().

248 {
249  if (!name && !str)
250  return 0;
251  if (!name)
252  return -1; /* NULL < anything */
253  if (!str)
254  return 1; /* NULL < anything */
255  return strncmp(NameStr(*name), str, NAMEDATALEN);
256 }
#define NAMEDATALEN
#define NameStr(name)
Definition: c.h:493
Datum session_user ( PG_FUNCTION_ARGS  )

Definition at line 269 of file name.c.

References CStringGetDatum, DirectFunctionCall1, GetSessionUserId(), GetUserNameFromId(), namein(), and PG_RETURN_DATUM.

Referenced by ExecEvalSQLValueFunction().

270 {
272 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
Oid GetSessionUserId(void)
Definition: miscinit.c:318
#define CStringGetDatum(X)
Definition: postgres.h:584
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:692