]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/adt/oid.c
Update copyright for 2014
[postgresql] / src / backend / utils / adt / oid.c
index 2119936d39fccb5cf077d7fc4303a4d363dccac4..8945ef43f0148bf80ade05a1c56996bdf003e13c 100644 (file)
@@ -3,43 +3,53 @@
  * oid.c
  *       Functions for the built-in type Oid ... also oidvector.
  *
- * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/oid.c,v 1.55 2004/03/04 21:47:18 neilc Exp $
+ *       src/backend/utils/adt/oid.c
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
 #include <ctype.h>
-#include <errno.h>
 #include <limits.h>
 
+#include "catalog/pg_type.h"
 #include "libpq/pqformat.h"
+#include "utils/array.h"
 #include "utils/builtins.h"
 
 
+#define OidVectorSize(n)       (offsetof(oidvector, values) + (n) * sizeof(Oid))
+
+
 /*****************************************************************************
  *      USER I/O ROUTINES                                                                                                               *
  *****************************************************************************/
 
 static Oid
-oidin_subr(const char *funcname, const char *s, char **endloc)
+oidin_subr(const char *s, char **endloc)
 {
        unsigned long cvt;
        char       *endptr;
        Oid                     result;
 
+       if (*s == '\0')
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                                errmsg("invalid input syntax for type oid: \"%s\"",
+                                               s)));
+
        errno = 0;
        cvt = strtoul(s, &endptr, 10);
 
        /*
-        * 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.
+        * 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.
         */
        if (errno && errno != ERANGE && errno != EINVAL)
                ereport(ERROR,
@@ -47,20 +57,7 @@ oidin_subr(const char *funcname, const char *s, char **endloc)
                                 errmsg("invalid input syntax for type oid: \"%s\"",
                                                s)));
 
-       /*
-        * In releases prior to 7.5, we accepted an empty string as valid
-        * input (yielding an OID of 0). In 7.5, we accept empty strings,
-        * but emit a warning noting that the feature is deprecated. In
-        * 7.6+, the warning should be replaced by an error.
-        */
-       if (*s == '\0')
-               ereport(WARNING,
-                               (errcode(ERRCODE_WARNING_DEPRECATED_FEATURE),
-                                errmsg("deprecated input syntax for type oid: \"\""),
-                                errdetail("This input will be rejected in "
-                                                  "a future release of PostgreSQL.")));
-
-       if (endptr == s && *s)
+       if (endptr == s && *s != '\0')
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                 errmsg("invalid input syntax for type oid: \"%s\"",
@@ -91,13 +88,13 @@ oidin_subr(const char *funcname, const char *s, char **endloc)
        result = (Oid) cvt;
 
        /*
-        * 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.
+        * 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.
         *
         * 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.
+        * with a minus sign, so allow the input value if it matches after either
+        * signed or unsigned extension to long.
         *
         * 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.
@@ -119,7 +116,7 @@ oidin(PG_FUNCTION_ARGS)
        char       *s = PG_GETARG_CSTRING(0);
        Oid                     result;
 
-       result = oidin_subr("oidin", s, NULL);
+       result = oidin_subr(s, NULL);
        PG_RETURN_OID(result);
 }
 
@@ -158,27 +155,54 @@ oidsend(PG_FUNCTION_ARGS)
        PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
+/*
+ * construct oidvector given a raw array of Oids
+ *
+ * If oids is NULL then caller must fill values[] afterward
+ */
+oidvector *
+buildoidvector(const Oid *oids, int n)
+{
+       oidvector  *result;
+
+       result = (oidvector *) palloc0(OidVectorSize(n));
+
+       if (n > 0 && oids)
+               memcpy(result->values, oids, n * sizeof(Oid));
+
+       /*
+        * Attach standard array header.  For historical reasons, we set the index
+        * lower bound to 0 not 1.
+        */
+       SET_VARSIZE(result, OidVectorSize(n));
+       result->ndim = 1;
+       result->dataoffset = 0;         /* never any nulls */
+       result->elemtype = OIDOID;
+       result->dim1 = n;
+       result->lbound1 = 0;
+
+       return result;
+}
 
 /*
  *             oidvectorin                     - converts "num num ..." to internal form
- *
- *             Note:
- *                             Fills any unsupplied positions with InvalidOid.
  */
 Datum
 oidvectorin(PG_FUNCTION_ARGS)
 {
        char       *oidString = PG_GETARG_CSTRING(0);
-       Oid                *result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
-       int                     slot;
+       oidvector  *result;
+       int                     n;
 
-       for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
+       result = (oidvector *) palloc0(OidVectorSize(FUNC_MAX_ARGS));
+
+       for (n = 0; n < FUNC_MAX_ARGS; n++)
        {
                while (*oidString && isspace((unsigned char) *oidString))
                        oidString++;
                if (*oidString == '\0')
                        break;
-               result[slot] = oidin_subr("oidvectorin", oidString, &oidString);
+               result->values[n] = oidin_subr(oidString, &oidString);
        }
        while (*oidString && isspace((unsigned char) *oidString))
                oidString++;
@@ -186,8 +210,13 @@ oidvectorin(PG_FUNCTION_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 errmsg("oidvector has too many elements")));
-       while (slot < INDEX_MAX_KEYS)
-               result[slot++] = InvalidOid;
+
+       SET_VARSIZE(result, OidVectorSize(n));
+       result->ndim = 1;
+       result->dataoffset = 0;         /* never any nulls */
+       result->elemtype = OIDOID;
+       result->dim1 = n;
+       result->lbound1 = 0;
 
        PG_RETURN_POINTER(result);
 }
@@ -198,24 +227,19 @@ oidvectorin(PG_FUNCTION_ARGS)
 Datum
 oidvectorout(PG_FUNCTION_ARGS)
 {
-       Oid                *oidArray = (Oid *) PG_GETARG_POINTER(0);
+       oidvector  *oidArray = (oidvector *) PG_GETARG_POINTER(0);
        int                     num,
-                               maxnum;
+                               nnums = oidArray->dim1;
        char       *rp;
        char       *result;
 
-       /* find last non-zero value in vector */
-       for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
-               if (oidArray[maxnum] != 0)
-                       break;
-
        /* assumes sign, 10 digits, ' ' */
-       rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
-       for (num = 0; num <= maxnum; num++)
+       rp = result = (char *) palloc(nnums * 12 + 1);
+       for (num = 0; num < nnums; num++)
        {
                if (num != 0)
                        *rp++ = ' ';
-               sprintf(rp, "%u", oidArray[num]);
+               sprintf(rp, "%u", oidArray->values[num]);
                while (*++rp != '\0')
                        ;
        }
@@ -230,11 +254,44 @@ Datum
 oidvectorrecv(PG_FUNCTION_ARGS)
 {
        StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
-       Oid                *result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
-       int                     slot;
+       FunctionCallInfoData locfcinfo;
+       oidvector  *result;
+
+       /*
+        * Normally one would call array_recv() using DirectFunctionCall3, but
+        * that does not work since array_recv wants to cache some data using
+        * fcinfo->flinfo->fn_extra.  So we need to pass it our own flinfo
+        * parameter.
+        */
+       InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 3,
+                                                        InvalidOid, NULL, NULL);
+
+       locfcinfo.arg[0] = PointerGetDatum(buf);
+       locfcinfo.arg[1] = ObjectIdGetDatum(OIDOID);
+       locfcinfo.arg[2] = Int32GetDatum(-1);
+       locfcinfo.argnull[0] = false;
+       locfcinfo.argnull[1] = false;
+       locfcinfo.argnull[2] = false;
+
+       result = (oidvector *) DatumGetPointer(array_recv(&locfcinfo));
+
+       Assert(!locfcinfo.isnull);
+
+       /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
+       if (ARR_NDIM(result) != 1 ||
+               ARR_HASNULL(result) ||
+               ARR_ELEMTYPE(result) != OIDOID ||
+               ARR_LBOUND(result)[0] != 0)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
+                                errmsg("invalid oidvector data")));
+
+       /* check length for consistency with oidvectorin() */
+       if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("oidvector has too many elements")));
 
-       for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
-               result[slot] = (Oid) pq_getmsgint(buf, sizeof(Oid));
        PG_RETURN_POINTER(result);
 }
 
@@ -244,14 +301,31 @@ oidvectorrecv(PG_FUNCTION_ARGS)
 Datum
 oidvectorsend(PG_FUNCTION_ARGS)
 {
-       Oid                *oidArray = (Oid *) PG_GETARG_POINTER(0);
-       StringInfoData buf;
-       int                     slot;
+       return array_send(fcinfo);
+}
 
-       pq_begintypsend(&buf);
-       for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
-               pq_sendint(&buf, oidArray[slot], sizeof(Oid));
-       PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+/*
+ *             oidparse                                - get OID from IConst/FConst node
+ */
+Oid
+oidparse(Node *node)
+{
+       switch (nodeTag(node))
+       {
+               case T_Integer:
+                       return intVal(node);
+               case T_Float:
+
+                       /*
+                        * Values too large for int4 will be represented as Float
+                        * constants by the lexer.      Accept these if they are valid OID
+                        * strings.
+                        */
+                       return oidin_subr(strVal(node), NULL);
+               default:
+                       elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
+       }
+       return InvalidOid;                      /* keep compiler quiet */
 }
 
 
@@ -334,111 +408,47 @@ oidsmaller(PG_FUNCTION_ARGS)
 Datum
 oidvectoreq(PG_FUNCTION_ARGS)
 {
-       Oid                *arg1 = (Oid *) PG_GETARG_POINTER(0);
-       Oid                *arg2 = (Oid *) PG_GETARG_POINTER(1);
+       int32           cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
-       PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
+       PG_RETURN_BOOL(cmp == 0);
 }
 
 Datum
 oidvectorne(PG_FUNCTION_ARGS)
 {
-       Oid                *arg1 = (Oid *) PG_GETARG_POINTER(0);
-       Oid                *arg2 = (Oid *) PG_GETARG_POINTER(1);
+       int32           cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
-       PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
+       PG_RETURN_BOOL(cmp != 0);
 }
 
 Datum
 oidvectorlt(PG_FUNCTION_ARGS)
 {
-       Oid                *arg1 = (Oid *) PG_GETARG_POINTER(0);
-       Oid                *arg2 = (Oid *) PG_GETARG_POINTER(1);
-       int                     i;
-
-       for (i = 0; i < INDEX_MAX_KEYS; i++)
-               if (arg1[i] != arg2[i])
-                       PG_RETURN_BOOL(arg1[i] < arg2[i]);
-       PG_RETURN_BOOL(false);
-}
+       int32           cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
-Datum
-oidvectorle(PG_FUNCTION_ARGS)
-{
-       Oid                *arg1 = (Oid *) PG_GETARG_POINTER(0);
-       Oid                *arg2 = (Oid *) PG_GETARG_POINTER(1);
-       int                     i;
-
-       for (i = 0; i < INDEX_MAX_KEYS; i++)
-               if (arg1[i] != arg2[i])
-                       PG_RETURN_BOOL(arg1[i] <= arg2[i]);
-       PG_RETURN_BOOL(true);
+       PG_RETURN_BOOL(cmp < 0);
 }
 
 Datum
-oidvectorge(PG_FUNCTION_ARGS)
+oidvectorle(PG_FUNCTION_ARGS)
 {
-       Oid                *arg1 = (Oid *) PG_GETARG_POINTER(0);
-       Oid                *arg2 = (Oid *) PG_GETARG_POINTER(1);
-       int                     i;
-
-       for (i = 0; i < INDEX_MAX_KEYS; i++)
-               if (arg1[i] != arg2[i])
-                       PG_RETURN_BOOL(arg1[i] >= arg2[i]);
-       PG_RETURN_BOOL(true);
-}
+       int32           cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
-Datum
-oidvectorgt(PG_FUNCTION_ARGS)
-{
-       Oid                *arg1 = (Oid *) PG_GETARG_POINTER(0);
-       Oid                *arg2 = (Oid *) PG_GETARG_POINTER(1);
-       int                     i;
-
-       for (i = 0; i < INDEX_MAX_KEYS; i++)
-               if (arg1[i] != arg2[i])
-                       PG_RETURN_BOOL(arg1[i] > arg2[i]);
-       PG_RETURN_BOOL(false);
+       PG_RETURN_BOOL(cmp <= 0);
 }
 
 Datum
-oid_text(PG_FUNCTION_ARGS)
+oidvectorge(PG_FUNCTION_ARGS)
 {
-       Oid                     oid = PG_GETARG_OID(0);
-       text       *result;
-       int                     len;
-       char       *str;
-
-       str = DatumGetCString(DirectFunctionCall1(oidout,
-                                                                                         ObjectIdGetDatum(oid)));
-       len = strlen(str) + VARHDRSZ;
-
-       result = (text *) palloc(len);
+       int32           cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
-       VARATT_SIZEP(result) = len;
-       memcpy(VARDATA(result), str, (len - VARHDRSZ));
-       pfree(str);
-
-       PG_RETURN_TEXT_P(result);
+       PG_RETURN_BOOL(cmp >= 0);
 }
 
 Datum
-text_oid(PG_FUNCTION_ARGS)
+oidvectorgt(PG_FUNCTION_ARGS)
 {
-       text       *string = PG_GETARG_TEXT_P(0);
-       Oid                     result;
-       int                     len;
-       char       *str;
-
-       len = (VARSIZE(string) - VARHDRSZ);
+       int32           cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
 
-       str = palloc(len + 1);
-       memcpy(str, VARDATA(string), len);
-       *(str + len) = '\0';
-
-       result = oidin_subr("text_oid", str, NULL);
-
-       pfree(str);
-
-       PG_RETURN_OID(result);
+       PG_RETURN_BOOL(cmp > 0);
 }