1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* oid.c
|
2000-12-22 22:36:09 +01:00
|
|
|
* Functions for the built-in type Oid ... also oidvector.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-08-15 00:21:59 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.46 2001/08/14 22:21:58 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-11-21 05:27:39 +01:00
|
|
|
#include "postgres.h"
|
1996-11-06 11:32:10 +01:00
|
|
|
|
2000-01-10 16:41:34 +01:00
|
|
|
#include <ctype.h>
|
2000-11-21 05:27:39 +01:00
|
|
|
#include <errno.h>
|
2000-12-22 22:36:09 +01:00
|
|
|
#include <limits.h>
|
2000-11-21 05:27:39 +01:00
|
|
|
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/builtins.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* USER I/O ROUTINES *
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-12-22 22:36:09 +01:00
|
|
|
static Oid
|
|
|
|
oidin_subr(const char *funcname, const char *s, char **endloc)
|
|
|
|
{
|
|
|
|
unsigned long cvt;
|
|
|
|
char *endptr;
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
cvt = strtoul(s, &endptr, 10);
|
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* strtoul() normally only sets ERANGE. On some systems it also may
|
|
|
|
* set EINVAL, which simply means it couldn't parse the input string.
|
|
|
|
* This is handled by the second "if" consistent across platforms.
|
|
|
|
* Note that for historical reasons we accept an empty string as
|
|
|
|
* meaning 0.
|
2000-12-22 22:36:09 +01:00
|
|
|
*/
|
|
|
|
if (errno && errno != EINVAL)
|
|
|
|
elog(ERROR, "%s: error reading \"%s\": %m",
|
|
|
|
funcname, s);
|
|
|
|
if (endptr == s && *endptr)
|
|
|
|
elog(ERROR, "%s: error in \"%s\": can't parse \"%s\"",
|
|
|
|
funcname, s, endptr);
|
|
|
|
|
|
|
|
if (endloc)
|
|
|
|
{
|
|
|
|
/* caller wants to deal with rest of string */
|
|
|
|
*endloc = endptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* allow only whitespace after number */
|
|
|
|
while (*endptr && isspace((unsigned char) *endptr))
|
|
|
|
endptr++;
|
|
|
|
if (*endptr)
|
|
|
|
elog(ERROR, "%s: error in \"%s\": can't parse \"%s\"",
|
|
|
|
funcname, s, endptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = (Oid) cvt;
|
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Cope with possibility that unsigned long is wider than Oid, in
|
|
|
|
* which case strtoul will not raise an error for some values that are
|
|
|
|
* out of the range of Oid.
|
2000-12-28 02:51:15 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* For backwards compatibility, we want to accept inputs that are given
|
|
|
|
* with a minus sign, so allow the input value if it matches after
|
|
|
|
* either signed or unsigned extension to long.
|
2000-12-22 22:36:09 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* To ensure consistent results on 32-bit and 64-bit platforms, make sure
|
|
|
|
* the error message is the same as if strtoul() had returned ERANGE.
|
2000-12-22 22:36:09 +01:00
|
|
|
*/
|
2000-12-28 02:51:15 +01:00
|
|
|
#if OID_MAX != ULONG_MAX
|
|
|
|
if (cvt != (unsigned long) result &&
|
2001-03-22 05:01:46 +01:00
|
|
|
cvt != (unsigned long) ((int) result))
|
2000-12-22 22:36:09 +01:00
|
|
|
elog(ERROR, "%s: error reading \"%s\": %s",
|
|
|
|
funcname, s, strerror(ERANGE));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
oidin(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *s = PG_GETARG_CSTRING(0);
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
result = oidin_subr("oidin", s, NULL);
|
|
|
|
PG_RETURN_OID(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
oidout(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid o = PG_GETARG_OID(0);
|
|
|
|
char *result = (char *) palloc(12);
|
|
|
|
|
|
|
|
snprintf(result, 12, "%u", o);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-01-10 17:13:23 +01:00
|
|
|
* oidvectorin - converts "num num ..." to internal form
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note:
|
2000-06-05 09:29:25 +02:00
|
|
|
* Fills any unsupplied positions with InvalidOid.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorin(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char *oidString = PG_GETARG_CSTRING(0);
|
1998-09-22 22:28:15 +02:00
|
|
|
Oid *result;
|
2000-01-10 05:36:37 +01:00
|
|
|
int slot;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-01-10 05:36:37 +01:00
|
|
|
result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
|
|
|
|
|
2000-12-22 22:36:09 +01:00
|
|
|
for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-03 21:45:40 +01:00
|
|
|
while (*oidString && isspace((unsigned char) *oidString))
|
2000-01-10 16:41:34 +01:00
|
|
|
oidString++;
|
2000-12-22 22:36:09 +01:00
|
|
|
if (*oidString == '\0')
|
|
|
|
break;
|
|
|
|
result[slot] = oidin_subr("oidvectorin", oidString, &oidString);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-12-03 21:45:40 +01:00
|
|
|
while (*oidString && isspace((unsigned char) *oidString))
|
2000-01-10 16:41:34 +01:00
|
|
|
oidString++;
|
|
|
|
if (*oidString)
|
2000-04-12 19:17:23 +02:00
|
|
|
elog(ERROR, "oidvector value has too many values");
|
2000-01-10 05:36:37 +01:00
|
|
|
while (slot < INDEX_MAX_KEYS)
|
2000-06-05 09:29:25 +02:00
|
|
|
result[slot++] = InvalidOid;
|
2000-01-10 05:36:37 +01:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-10 17:13:23 +01:00
|
|
|
* oidvectorout - converts internal form to "num num ..."
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorout(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
|
2000-04-12 19:17:23 +02:00
|
|
|
int num,
|
|
|
|
maxnum;
|
1998-02-26 05:46:47 +01:00
|
|
|
char *rp;
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-01-10 16:41:34 +01:00
|
|
|
/* find last non-zero value in vector */
|
2000-04-12 19:17:23 +02:00
|
|
|
for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
|
2000-01-10 16:41:34 +01:00
|
|
|
if (oidArray[maxnum] != 0)
|
|
|
|
break;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* assumes sign, 10 digits, ' ' */
|
2000-04-12 19:17:23 +02:00
|
|
|
rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
|
2000-01-10 16:41:34 +01:00
|
|
|
for (num = 0; num <= maxnum; num++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-01-10 16:41:34 +01:00
|
|
|
if (num != 0)
|
|
|
|
*rp++ = ' ';
|
2000-11-21 04:23:21 +01:00
|
|
|
sprintf(rp, "%u", oidArray[num]);
|
1997-09-07 07:04:48 +02:00
|
|
|
while (*++rp != '\0')
|
|
|
|
;
|
|
|
|
}
|
2000-01-10 16:41:34 +01:00
|
|
|
*rp = '\0';
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_CSTRING(result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* PUBLIC ROUTINES *
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oideq(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 == arg2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 != arg2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-11-21 04:23:21 +01:00
|
|
|
Datum
|
|
|
|
oidlt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 < arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
oidle(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 <= arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
oidge(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 >= arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
oidgt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 > arg2);
|
|
|
|
}
|
|
|
|
|
2001-08-15 00:21:59 +02:00
|
|
|
Datum
|
|
|
|
oidlarger(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
oidsmaller(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid arg1 = PG_GETARG_OID(0);
|
|
|
|
Oid arg2 = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
|
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectoreq(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
|
|
|
|
Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
|
2000-06-05 09:29:25 +02:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorne(PG_FUNCTION_ARGS)
|
1998-10-29 19:07:09 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
|
|
|
|
Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
|
2000-06-05 09:29:25 +02:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
|
1998-10-29 19:07:09 +01:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorlt(PG_FUNCTION_ARGS)
|
1998-08-19 04:04:17 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
|
|
|
|
Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
|
|
|
|
2000-01-10 05:36:37 +01:00
|
|
|
for (i = 0; i < INDEX_MAX_KEYS; i++)
|
2000-06-05 09:29:25 +02:00
|
|
|
if (arg1[i] != arg2[i])
|
|
|
|
PG_RETURN_BOOL(arg1[i] < arg2[i]);
|
|
|
|
PG_RETURN_BOOL(false);
|
1998-08-19 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorle(PG_FUNCTION_ARGS)
|
1998-08-19 04:04:17 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
|
|
|
|
Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
|
|
|
|
2000-01-10 05:36:37 +01:00
|
|
|
for (i = 0; i < INDEX_MAX_KEYS; i++)
|
2000-06-05 09:29:25 +02:00
|
|
|
if (arg1[i] != arg2[i])
|
|
|
|
PG_RETURN_BOOL(arg1[i] <= arg2[i]);
|
|
|
|
PG_RETURN_BOOL(true);
|
1998-08-19 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorge(PG_FUNCTION_ARGS)
|
1998-08-19 04:04:17 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
|
|
|
|
Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
|
|
|
|
2000-01-10 05:36:37 +01:00
|
|
|
for (i = 0; i < INDEX_MAX_KEYS; i++)
|
2000-06-05 09:29:25 +02:00
|
|
|
if (arg1[i] != arg2[i])
|
|
|
|
PG_RETURN_BOOL(arg1[i] >= arg2[i]);
|
|
|
|
PG_RETURN_BOOL(true);
|
1998-08-19 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oidvectorgt(PG_FUNCTION_ARGS)
|
1998-08-19 04:04:17 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
|
|
|
|
Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
|
|
|
|
2000-01-10 05:36:37 +01:00
|
|
|
for (i = 0; i < INDEX_MAX_KEYS; i++)
|
2000-06-05 09:29:25 +02:00
|
|
|
if (arg1[i] != arg2[i])
|
|
|
|
PG_RETURN_BOOL(arg1[i] > arg2[i]);
|
|
|
|
PG_RETURN_BOOL(false);
|
1998-08-19 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
oid_text(PG_FUNCTION_ARGS)
|
1997-10-25 07:21:54 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
Oid oid = PG_GETARG_OID(0);
|
1998-02-26 05:46:47 +01:00
|
|
|
text *result;
|
|
|
|
int len;
|
|
|
|
char *str;
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
str = DatumGetCString(DirectFunctionCall1(oidout,
|
|
|
|
ObjectIdGetDatum(oid)));
|
|
|
|
len = strlen(str) + VARHDRSZ;
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
result = (text *) palloc(len);
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = len;
|
2000-06-05 09:29:25 +02:00
|
|
|
memcpy(VARDATA(result), str, (len - VARHDRSZ));
|
1998-01-07 19:47:07 +01:00
|
|
|
pfree(str);
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
text_oid(PG_FUNCTION_ARGS)
|
1997-10-25 07:21:54 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
text *string = PG_GETARG_TEXT_P(0);
|
1998-02-26 05:46:47 +01:00
|
|
|
Oid result;
|
|
|
|
int len;
|
|
|
|
char *str;
|
1997-10-25 07:21:54 +02:00
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
len = (VARSIZE(string) - VARHDRSZ);
|
1997-10-25 07:21:54 +02:00
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
str = palloc(len + 1);
|
2000-06-05 09:29:25 +02:00
|
|
|
memcpy(str, VARDATA(string), len);
|
1998-02-26 05:46:47 +01:00
|
|
|
*(str + len) = '\0';
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-12-22 22:36:09 +01:00
|
|
|
result = oidin_subr("text_oid", str, NULL);
|
|
|
|
|
1998-01-07 19:47:07 +01:00
|
|
|
pfree(str);
|
1997-10-25 07:21:54 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_OID(result);
|
|
|
|
}
|