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
|
|
|
*
|
2001-01-24 19:43:33 +00:00
|
|
|
* Portions Copyright (c) 1996-2001, 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
|
2001-10-28 06:26:15 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.33 2001/10/28 06:25:52 momjian Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2000-08-03 16:35:08 +00:00
|
|
|
|
|
|
|
#include "miscadmin.h"
|
1999-07-16 03:14:30 +00:00
|
|
|
#include "utils/builtins.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;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
result = (NameData *) palloc(NAMEDATALEN);
|
|
|
|
/* always keep it null-padded */
|
1999-11-07 23:08:36 +00:00
|
|
|
StrNCpy(NameStr(*result), s, NAMEDATALEN);
|
|
|
|
len = strlen(NameStr(*result));
|
1998-05-29 13:31:52 +00:00
|
|
|
while (len < NAMEDATALEN)
|
|
|
|
{
|
1999-11-07 23:08:36 +00:00
|
|
|
*(NameStr(*result) + len) = '\0';
|
1998-05-29 13:31:52 +00:00
|
|
|
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
|
|
|
|
|
|
|
/* SQL-functions CURRENT_USER and SESSION_USER */
|
|
|
|
Datum
|
|
|
|
current_user(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserName(GetUserId()))));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
session_user(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserName(GetSessionUserId()))));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|