1996-07-09 06:22:35 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* name.c
|
1997-09-07 05:04:48 +00:00
|
|
|
* Functions for the built-in type "name".
|
1996-07-09 06:22:35 +00:00
|
|
|
* name replaces char16 and is carefully implemented so that it
|
|
|
|
* is a string of length NAMEDATALEN. DO NOT use hard-coded constants anywhere
|
|
|
|
* always use NAMEDATALEN as the symbolic constant! - jolly 8/21/95
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
2002-06-20 20:29:54 +00:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 05:58:53 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-08-26 17:54:02 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.40 2002/08/26 17:53:58 tgl Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2000-08-03 16:35:08 +00:00
|
|
|
|
2002-04-26 01:24:08 +00:00
|
|
|
#include "catalog/namespace.h"
|
2002-08-26 17:54:02 +00:00
|
|
|
#include "catalog/pg_type.h"
|
2000-08-03 16:35:08 +00:00
|
|
|
#include "miscadmin.h"
|
2002-04-26 01:24:08 +00:00
|
|
|
#include "utils/array.h"
|
1999-07-16 03:14:30 +00:00
|
|
|
#include "utils/builtins.h"
|
2002-04-26 01:24:08 +00:00
|
|
|
#include "utils/lsyscache.h"
|
2002-06-13 06:19:45 +00:00
|
|
|
#include "mb/pg_wchar.h"
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* USER I/O ROUTINES (none) *
|
1996-07-09 06:22:35 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-09-07 05:04:48 +00:00
|
|
|
* namein - converts "..." to internal representation
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
1997-09-07 05:04:48 +00:00
|
|
|
* Note:
|
|
|
|
* [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls
|
|
|
|
* Now, always NULL terminated
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
namein(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
char *s = PG_GETARG_CSTRING(0);
|
1997-09-08 02:41:22 +00:00
|
|
|
NameData *result;
|
1998-05-29 13:31:52 +00:00
|
|
|
int len;
|
2002-06-13 06:19:45 +00:00
|
|
|
char *ermsg;
|
|
|
|
|
|
|
|
/* veryfy encoding */
|
|
|
|
len = strlen(s);
|
|
|
|
if ((ermsg = pg_verifymbstr(s, len)))
|
|
|
|
elog(ERROR, "%s", ermsg);
|
|
|
|
|
|
|
|
len = pg_mbcliplen(s, len, NAMEDATALEN-1);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
result = (NameData *) palloc(NAMEDATALEN);
|
|
|
|
/* always keep it null-padded */
|
2002-06-13 06:19:45 +00:00
|
|
|
memset(result, 0, NAMEDATALEN);
|
|
|
|
memcpy(NameStr(*result), s, len);
|
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
PG_RETURN_NAME(result);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-03 16:35:08 +00:00
|
|
|
* nameout - converts internal representation to "..."
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
nameout(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name s = PG_GETARG_NAME(0);
|
|
|
|
|
|
|
|
PG_RETURN_CSTRING(pstrdup(NameStr(*s)));
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* PUBLIC ROUTINES *
|
1996-07-09 06:22:35 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
1997-09-07 05:04:48 +00:00
|
|
|
* nameeq - returns 1 iff arguments are equal
|
|
|
|
* namene - returns 1 iff arguments are not equal
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
1997-09-07 05:04:48 +00:00
|
|
|
* BUGS:
|
|
|
|
* Assumes that "xy\0\0a" should be equal to "xy\0b".
|
|
|
|
* If not, can do the comparison backwards for efficiency.
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
1997-09-07 05:04:48 +00:00
|
|
|
* namelt - returns 1 iff a < b
|
|
|
|
* namele - returns 1 iff a <= b
|
|
|
|
* namegt - returns 1 iff a < b
|
|
|
|
* namege - returns 1 iff a <= b
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*/
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
nameeq(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name arg1 = PG_GETARG_NAME(0);
|
|
|
|
Name arg2 = PG_GETARG_NAME(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) == 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
namene(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name arg1 = PG_GETARG_NAME(0);
|
|
|
|
Name arg2 = PG_GETARG_NAME(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) != 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
namelt(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name arg1 = PG_GETARG_NAME(0);
|
|
|
|
Name arg2 = PG_GETARG_NAME(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) < 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
namele(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name arg1 = PG_GETARG_NAME(0);
|
|
|
|
Name arg2 = PG_GETARG_NAME(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) <= 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
namegt(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name arg1 = PG_GETARG_NAME(0);
|
|
|
|
Name arg2 = PG_GETARG_NAME(1);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
Datum
|
|
|
|
namege(PG_FUNCTION_ARGS)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-08-03 16:35:08 +00:00
|
|
|
Name arg1 = PG_GETARG_NAME(0);
|
|
|
|
Name arg2 = PG_GETARG_NAME(1);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-08-03 16:35:08 +00:00
|
|
|
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (see char.c for comparison/operation routines) */
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
int
|
|
|
|
namecpy(Name n1, Name n2)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-07 05:04:48 +00:00
|
|
|
if (!n1 || !n2)
|
1998-09-01 03:29:17 +00:00
|
|
|
return -1;
|
1999-11-07 23:08:36 +00:00
|
|
|
strncpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
|
1998-09-01 03:29:17 +00:00
|
|
|
return 0;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
1997-08-19 21:40:56 +00:00
|
|
|
#ifdef NOT_USED
|
1997-09-07 05:04:48 +00:00
|
|
|
int
|
|
|
|
namecat(Name n1, Name n2)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-04-12 17:17:23 +00:00
|
|
|
return namestrcat(n1, NameStr(*n2)); /* n2 can't be any longer
|
|
|
|
* than n1 */
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1997-08-19 21:40:56 +00:00
|
|
|
#endif
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1998-10-08 18:30:52 +00:00
|
|
|
#ifdef NOT_USED
|
1997-09-07 05:04:48 +00:00
|
|
|
int
|
|
|
|
namecmp(Name n1, Name n2)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1999-11-07 23:08:36 +00:00
|
|
|
return strncmp(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1998-10-08 18:30:52 +00:00
|
|
|
#endif
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
int
|
2000-01-22 14:20:56 +00:00
|
|
|
namestrcpy(Name name, const char *str)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-07 05:04:48 +00:00
|
|
|
if (!name || !str)
|
1998-09-01 03:29:17 +00:00
|
|
|
return -1;
|
1999-11-07 23:08:36 +00:00
|
|
|
StrNCpy(NameStr(*name), str, NAMEDATALEN);
|
1998-09-01 03:29:17 +00:00
|
|
|
return 0;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
1997-08-19 21:40:56 +00:00
|
|
|
#ifdef NOT_USED
|
1997-09-07 05:04:48 +00:00
|
|
|
int
|
2000-01-22 14:20:56 +00:00
|
|
|
namestrcat(Name name, const char *str)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
int i;
|
|
|
|
char *p,
|
|
|
|
*q;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
if (!name || !str)
|
1998-09-01 03:29:17 +00:00
|
|
|
return -1;
|
1999-11-07 23:08:36 +00:00
|
|
|
for (i = 0, p = NameStr(*name); i < NAMEDATALEN && *p; ++i, ++p)
|
1997-09-07 05:04:48 +00:00
|
|
|
;
|
|
|
|
for (q = str; i < NAMEDATALEN; ++i, ++p, ++q)
|
|
|
|
{
|
|
|
|
*p = *q;
|
|
|
|
if (!*q)
|
|
|
|
break;
|
|
|
|
}
|
1998-09-01 03:29:17 +00:00
|
|
|
return 0;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1997-08-19 21:40:56 +00:00
|
|
|
#endif
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
int
|
2000-01-22 14:20:56 +00:00
|
|
|
namestrcmp(Name name, const char *str)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-07 05:04:48 +00:00
|
|
|
if (!name && !str)
|
1998-09-01 03:29:17 +00:00
|
|
|
return 0;
|
1997-09-07 05:04:48 +00:00
|
|
|
if (!name)
|
1998-09-01 04:40:42 +00:00
|
|
|
return -1; /* NULL < anything */
|
1997-09-07 05:04:48 +00:00
|
|
|
if (!str)
|
1998-09-01 03:29:17 +00:00
|
|
|
return 1; /* NULL < anything */
|
1999-11-07 23:08:36 +00:00
|
|
|
return strncmp(NameStr(*name), str, NAMEDATALEN);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-09-19 18:18:04 +00:00
|
|
|
|
2002-04-26 01:24:08 +00:00
|
|
|
/*
|
|
|
|
* SQL-functions CURRENT_USER, SESSION_USER
|
|
|
|
*/
|
2000-09-19 18:18:04 +00:00
|
|
|
Datum
|
|
|
|
current_user(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-06-11 13:40:53 +00:00
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(GetUserId()))));
|
2000-09-19 18:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
session_user(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-06-11 13:40:53 +00:00
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(GetSessionUserId()))));
|
2000-09-19 18:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-26 01:24:08 +00:00
|
|
|
/*
|
|
|
|
* SQL-functions CURRENT_SCHEMA, CURRENT_SCHEMAS
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
current_schema(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-05-17 20:53:33 +00:00
|
|
|
List *search_path = fetch_search_path(false);
|
2002-04-26 01:24:08 +00:00
|
|
|
char *nspname;
|
|
|
|
|
|
|
|
if (search_path == NIL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
nspname = get_namespace_name((Oid) lfirsti(search_path));
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(nspname)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
current_schemas(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-06-15 20:03:51 +00:00
|
|
|
List *search_path = fetch_search_path(PG_GETARG_BOOL(0));
|
2002-04-26 01:24:08 +00:00
|
|
|
int nnames = length(search_path);
|
|
|
|
Datum *names;
|
|
|
|
int i;
|
|
|
|
ArrayType *array;
|
|
|
|
|
|
|
|
/* +1 here is just to avoid palloc(0) error */
|
|
|
|
names = (Datum *) palloc((nnames + 1) * sizeof(Datum));
|
|
|
|
i = 0;
|
|
|
|
while (search_path)
|
|
|
|
{
|
|
|
|
char *nspname;
|
|
|
|
|
|
|
|
nspname = get_namespace_name((Oid) lfirsti(search_path));
|
|
|
|
names[i] = DirectFunctionCall1(namein, CStringGetDatum(nspname));
|
|
|
|
i++;
|
|
|
|
search_path = lnext(search_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
array = construct_array(names, nnames,
|
2002-08-26 17:54:02 +00:00
|
|
|
NAMEOID,
|
2002-04-26 01:24:08 +00:00
|
|
|
NAMEDATALEN, /* sizeof(Name) */
|
2002-08-26 17:54:02 +00:00
|
|
|
false, /* Name is not by-val */
|
2002-04-26 01:24:08 +00:00
|
|
|
'i'); /* alignment of Name */
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* PRIVATE ROUTINES *
|
1996-07-09 06:22:35 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-08-19 21:40:56 +00:00
|
|
|
#ifdef NOT_USED
|
1997-09-07 05:04:48 +00:00
|
|
|
uint32
|
1996-07-09 06:22:35 +00:00
|
|
|
NameComputeLength(Name name)
|
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
char *charP;
|
|
|
|
int length;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1999-11-07 23:08:36 +00:00
|
|
|
for (length = 0, charP = NameStr(*name);
|
1997-09-07 05:04:48 +00:00
|
|
|
length < NAMEDATALEN && *charP != '\0';
|
|
|
|
length++, charP++)
|
|
|
|
;
|
|
|
|
return (uint32) length;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
2001-10-28 06:26:15 +00:00
|
|
|
|
1997-08-19 21:40:56 +00:00
|
|
|
#endif
|