> Here's a revised patch. Changes:
authorBruce Momjian
Fri, 14 Sep 2001 17:46:40 +0000 (17:46 +0000)
committerBruce Momjian
Fri, 14 Sep 2001 17:46:40 +0000 (17:46 +0000)
>
> 1. Now outputs '\\' instead of '\134' when using encode(bytea, 'escape')
> Note that I ended up leaving \0 as \000 so that there are no ambiguities
> when decoding something like, for example, \0123.
>
> 2. Fixed bug in byteain which allowed input values which were not valid
> octals (e.g. \789), to be parsed as if they were octals.
>
> Joe
>

Here's rev 2 of the bytea string support patch. Changes:

1. Added missing declaration for MatchBytea function
2. Added PQescapeBytea to fe-exec.c
3. Applies cleanly on cvs tip from this afternoon

I'm hoping that someone can review/approve/apply this before beta starts, so
I guess I'd vote (not that it counts for much) to delay beta a few days :-)

Joe Conway

src/backend/utils/adt/encode.c
src/backend/utils/adt/like.c
src/backend/utils/adt/oracle_compat.c
src/backend/utils/adt/selfuncs.c
src/backend/utils/adt/varlena.c
src/include/catalog/catversion.h
src/include/catalog/pg_operator.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h
src/interfaces/libpq/fe-exec.c
src/interfaces/libpq/libpq-fe.h

index 89ba357aa57dc29bde3aee43582e47f1809fc999..538258518479b45c046edf6d3c6fef5d06137cbe 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.1 2001/07/12 14:05:31 momjian Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/utils/adt/encode.c,v 1.2 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -19,8 +19,8 @@
 
 struct pg_encoding
 {
-   unsigned        (*encode_len) (unsigned dlen);
-   unsigned        (*decode_len) (unsigned dlen);
+   unsigned        (*encode_len) (const uint8 *data, unsigned dlen);
+   unsigned        (*decode_len) (const uint8 *data, unsigned dlen);
    unsigned        (*encode) (const uint8 *data, unsigned dlen, uint8 *res);
    unsigned        (*decode) (const uint8 *data, unsigned dlen, uint8 *res);
 };
@@ -50,7 +50,7 @@ binary_encode(PG_FUNCTION_ARGS)
    if (enc == NULL)
        elog(ERROR, "No such encoding");
 
-   resultlen = enc->encode_len(datalen);
+   resultlen = enc->encode_len(VARDATA(data), datalen);
    result = palloc(VARHDRSZ + resultlen);
 
    res = enc->encode(VARDATA(data), datalen, VARDATA(result));
@@ -81,7 +81,7 @@ binary_decode(PG_FUNCTION_ARGS)
    if (enc == NULL)
        elog(ERROR, "No such encoding");
 
-   resultlen = enc->decode_len(datalen);
+   resultlen = enc->decode_len(VARDATA(data), datalen);
    result = palloc(VARHDRSZ + resultlen);
 
    res = enc->decode(VARDATA(data), datalen, VARDATA(result));
@@ -169,13 +169,13 @@ hex_decode(const uint8 * src, unsigned len, uint8 * dst)
 }
 
 static unsigned
-hex_enc_len(unsigned srclen)
+hex_enc_len(const uint8 * src, unsigned srclen)
 {
    return srclen << 1;
 }
 
 static unsigned
-hex_dec_len(unsigned srclen)
+hex_dec_len(const uint8 * src, unsigned srclen)
 {
    return srclen >> 1;
 }
@@ -308,18 +308,188 @@ b64_decode(const uint8 * src, unsigned len, uint8 * dst)
 
 
 static unsigned
-b64_enc_len(unsigned srclen)
+b64_enc_len(const uint8 * src, unsigned srclen)
 {
    /* 3 bytes will be converted to 4, linefeed after 76 chars */
    return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
 }
 
 static unsigned
-b64_dec_len(unsigned srclen)
+b64_dec_len(const uint8 * src, unsigned srclen)
 {
    return (srclen * 3) >> 2;
 }
 
+/*
+ * Escape
+ * Minimally escape bytea to text.
+ * De-escape text to bytea.
+ * 
+ * Only two characters are escaped:
+ * \0 (null) and \\ (backslash)
+ * 
+ * De-escapes \\ and any \### octal
+ */
+
+#define VAL(CH)            ((CH) - '0')
+#define DIG(VAL)       ((VAL) + '0')
+
+static unsigned
+esc_encode(const uint8 *src, unsigned srclen, uint8 *dst)
+{
+   const uint8    *end = src + srclen;
+   uint8           *rp = dst;
+   int             val;
+   int             len = 0;
+
+   while (src < end)
+   {
+       if (*src == '\0')
+       {
+           val = *src;
+           rp[0] = '\\';
+           rp[1] = '0';
+           rp[2] = '0';
+           rp[3] = '0';
+           rp += 4;
+           len += 4;
+       }
+       else if (*src == '\\')
+       {
+           val = *src;
+           rp[0] = '\\';
+           rp[1] = '\\';
+           rp += 2;
+           len += 2;
+       }
+       else
+       {
+           *rp++ = *src;
+           len++;
+       }
+
+       src++;
+   }
+   *rp = '\0';
+
+   return len;
+}
+
+static unsigned
+esc_decode(const uint8 *src, unsigned srclen, uint8 *dst)
+{
+   const uint8     *end = src + srclen;
+   uint8           *rp = dst;
+   int             val;
+   int             len = 0;
+
+   while (src < end)
+   {
+       if (src[0] != '\\')
+       {
+           *rp++ = *src++;
+       }
+       else if ( (src[0] == '\\') &&
+                   (src[1] >= '0' && src[1] <= '3') &&
+                   (src[2] >= '0' && src[2] <= '7') &&
+                   (src[3] >= '0' && src[3] <= '7') )
+       {
+           val = VAL(src[1]);
+           val <<= 3;
+           val += VAL(src[2]);
+           val <<= 3;
+           *rp++ = val + VAL(src[3]);
+           src += 4;
+       }
+       else if ( (src[0] == '\\') &&
+                   (src[1] == '\\') )
+       {
+           *rp++ = '\\';
+           src += 2;
+       }
+       else
+       {
+           /*
+            * One backslash, not followed by ### valid octal.
+            * Should never get here, since esc_dec_len does same check.
+            */
+           elog(ERROR, "decode: Bad input string for type bytea");
+       }
+
+       len++;
+   }
+   return len;
+}
+
+static unsigned
+esc_enc_len(const uint8 *src, unsigned srclen)
+{
+   const uint8     *end = src + srclen;
+   int             len = 0;
+
+   while (src < end)
+   {
+       if (*src == '\0')
+           len += 4;
+       else if (*src == '\\')
+           len += 2;
+       else
+           len++;
+
+       src++;
+   }
+
+   /*
+    * Allow for null terminator
+    */
+   len++;
+
+   return len;
+}
+
+static unsigned
+esc_dec_len(const uint8 *src, unsigned srclen)
+{
+   const uint8     *end = src + srclen;
+   int             len = 0;
+
+   while (src < end)
+   {
+       if (src[0] != '\\')
+       {
+           src++;
+       }
+       else if ( (src[0] == '\\') &&
+                   (src[1] >= '0' && src[1] <= '3') &&
+                   (src[2] >= '0' && src[2] <= '7') &&
+                   (src[3] >= '0' && src[3] <= '7') )
+       {
+           /*
+            * backslash + valid octal
+            */
+           src += 4;
+       }
+       else if ( (src[0] == '\\') &&
+                   (src[1] == '\\') )
+       {
+           /*
+            * two backslashes = backslash
+            */
+           src += 2;
+       }
+       else
+       {
+           /*
+            * one backslash, not followed by ### valid octal
+            */
+           elog(ERROR, "decode: Bad input string for type bytea");
+       }
+
+       len++;
+   }
+   return len;
+}
+
 /*
  * Common
  */
@@ -330,6 +500,7 @@ static struct {
 } enclist[] = {
    {"hex", { hex_enc_len, hex_dec_len, hex_encode, hex_decode }},
    {"base64", { b64_enc_len, b64_dec_len, b64_encode, b64_decode }},
+   {"escape", { esc_enc_len, esc_dec_len, esc_encode, esc_decode }},
    {NULL, { NULL, NULL, NULL, NULL } }
 };
 
index f27bc067d6a305dd3950e76e27671493db88ef2c..4f6d6d0212beedbcca09c46b9762305e91bca348 100644 (file)
@@ -11,7 +11,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.45 2001/03/22 03:59:51 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.46 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -34,6 +34,8 @@ static int MatchText(unsigned char *t, int tlen,
          unsigned char *p, int plen);
 static int MatchTextIC(unsigned char *t, int tlen,
            unsigned char *p, int plen);
+static int MatchBytea(unsigned char *t, int tlen,
+         unsigned char *p, int plen);
 
 
 #ifdef MULTIBYTE
@@ -120,6 +122,9 @@ iwchareq(unsigned char *p1, unsigned char *p2)
 #define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
 #endif
 
+#define BYTEA_CHAREQ(p1, p2) (*(p1) == *(p2))
+#define BYTEA_NextChar(p, plen) ((p)++, (plen)--)
+#define BYTEA_CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
 
 /*
  * interface routines called by the function manager
@@ -209,6 +214,48 @@ textnlike(PG_FUNCTION_ARGS)
    PG_RETURN_BOOL(result);
 }
 
+Datum
+bytealike(PG_FUNCTION_ARGS)
+{
+   bytea      *str = PG_GETARG_BYTEA_P(0);
+   bytea      *pat = PG_GETARG_BYTEA_P(1);
+   bool        result;
+   unsigned char *s,
+              *p;
+   int         slen,
+               plen;
+
+   s = VARDATA(str);
+   slen = (VARSIZE(str) - VARHDRSZ);
+   p = VARDATA(pat);
+   plen = (VARSIZE(pat) - VARHDRSZ);
+
+   result = (MatchBytea(s, slen, p, plen) == LIKE_TRUE);
+
+   PG_RETURN_BOOL(result);
+}
+
+Datum
+byteanlike(PG_FUNCTION_ARGS)
+{
+   bytea      *str = PG_GETARG_BYTEA_P(0);
+   bytea      *pat = PG_GETARG_BYTEA_P(1);
+   bool        result;
+   unsigned char *s,
+              *p;
+   int         slen,
+               plen;
+
+   s = VARDATA(str);
+   slen = (VARSIZE(str) - VARHDRSZ);
+   p = VARDATA(pat);
+   plen = (VARSIZE(pat) - VARHDRSZ);
+
+   result = (MatchBytea(s, slen, p, plen) != LIKE_TRUE);
+
+   PG_RETURN_BOOL(result);
+}
+
 /*
  * Case-insensitive versions
  */
@@ -395,6 +442,103 @@ like_escape(PG_FUNCTION_ARGS)
    PG_RETURN_TEXT_P(result);
 }
 
+/*
+ * like_escape_bytea() --- given a pattern and an ESCAPE string,
+ * convert the pattern to use Postgres' standard backslash escape convention.
+ */
+Datum
+like_escape_bytea(PG_FUNCTION_ARGS)
+{
+   bytea      *pat = PG_GETARG_BYTEA_P(0);
+   bytea      *esc = PG_GETARG_BYTEA_P(1);
+   bytea      *result;
+   unsigned char *p,
+              *e,
+              *r;
+   int         plen,
+               elen;
+   bool        afterescape;
+
+   p = VARDATA(pat);
+   plen = (VARSIZE(pat) - VARHDRSZ);
+   e = VARDATA(esc);
+   elen = (VARSIZE(esc) - VARHDRSZ);
+
+   /*
+    * Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
+    * trying to calculate the size more accurately than that.
+    */
+   result = (text *) palloc(plen * 2 + VARHDRSZ);
+   r = VARDATA(result);
+
+   if (elen == 0)
+   {
+
+       /*
+        * No escape character is wanted.  Double any backslashes in the
+        * pattern to make them act like ordinary characters.
+        */
+       while (plen > 0)
+       {
+           if (*p == '\\')
+               *r++ = '\\';
+           BYTEA_CopyAdvChar(r, p, plen);
+       }
+   }
+   else
+   {
+
+       /*
+        * The specified escape must be only a single character.
+        */
+       BYTEA_NextChar(e, elen);
+       if (elen != 0)
+           elog(ERROR, "ESCAPE string must be empty or one character");
+       e = VARDATA(esc);
+
+       /*
+        * If specified escape is '\', just copy the pattern as-is.
+        */
+       if (*e == '\\')
+       {
+           memcpy(result, pat, VARSIZE(pat));
+           PG_RETURN_BYTEA_P(result);
+       }
+
+       /*
+        * Otherwise, convert occurrences of the specified escape
+        * character to '\', and double occurrences of '\' --- unless they
+        * immediately follow an escape character!
+        */
+       afterescape = false;
+       while (plen > 0)
+       {
+           if (BYTEA_CHAREQ(p, e) && !afterescape)
+           {
+               *r++ = '\\';
+               BYTEA_NextChar(p, plen);
+               afterescape = true;
+           }
+           else if (*p == '\\')
+           {
+               *r++ = '\\';
+               if (!afterescape)
+                   *r++ = '\\';
+               BYTEA_NextChar(p, plen);
+               afterescape = false;
+           }
+           else
+           {
+               BYTEA_CopyAdvChar(r, p, plen);
+               afterescape = false;
+           }
+       }
+   }
+
+   VARATT_SIZEP(result) = r - ((unsigned char *) result);
+
+   PG_RETURN_BYTEA_P(result);
+}
 
 /*
 ** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
@@ -614,3 +758,91 @@ MatchTextIC(unsigned char *t, int tlen, unsigned char *p, int plen)
     */
    return LIKE_ABORT;
 }  /* MatchTextIC() */
+
+/*
+ * Same as above, but specifically for bytea (binary) datatype
+ */
+static int
+MatchBytea(unsigned char *t, int tlen, unsigned char *p, int plen)
+{
+   /* Fast path for match-everything pattern */
+   if ((plen == 1) && (*p == '%'))
+       return LIKE_TRUE;
+
+   while ((tlen > 0) && (plen > 0))
+   {
+       if (*p == '\\')
+       {
+           /* Next pattern char must match literally, whatever it is */
+           BYTEA_NextChar(p, plen);
+           if ((plen <= 0) || !BYTEA_CHAREQ(t, p))
+               return LIKE_FALSE;
+       }
+       else if (*p == '%')
+       {
+           /* %% is the same as % according to the SQL standard */
+           /* Advance past all %'s */
+           while ((plen > 0) && (*p == '%'))
+               BYTEA_NextChar(p, plen);
+           /* Trailing percent matches everything. */
+           if (plen <= 0)
+               return LIKE_TRUE;
+
+           /*
+            * Otherwise, scan for a text position at which we can match
+            * the rest of the pattern.
+            */
+           while (tlen > 0)
+           {
+
+               /*
+                * Optimization to prevent most recursion: don't recurse
+                * unless first pattern char might match this text char.
+                */
+               if (BYTEA_CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
+               {
+                   int         matched = MatchBytea(t, tlen, p, plen);
+
+                   if (matched != LIKE_FALSE)
+                       return matched; /* TRUE or ABORT */
+               }
+
+               BYTEA_NextChar(t, tlen);
+           }
+
+           /*
+            * End of text with no match, so no point in trying later
+            * places to start matching this pattern.
+            */
+           return LIKE_ABORT;
+       }
+       else if ((*p != '_') && !BYTEA_CHAREQ(t, p))
+       {
+
+           /*
+            * Not the single-character wildcard and no explicit match?
+            * Then time to quit...
+            */
+           return LIKE_FALSE;
+       }
+
+       BYTEA_NextChar(t, tlen);
+       BYTEA_NextChar(p, plen);
+   }
+
+   if (tlen > 0)
+       return LIKE_FALSE;      /* end of pattern, but not of text */
+
+   /* End of input string.  Do we have matching pattern remaining? */
+   while ((plen > 0) && (*p == '%'))   /* allow multiple %'s at end of
+                                        * pattern */
+       BYTEA_NextChar(p, plen);
+   if (plen <= 0)
+       return LIKE_TRUE;
+
+   /*
+    * End of text with no match, so no point in trying later places to
+    * start matching this pattern.
+    */
+   return LIKE_ABORT;
+}  /* MatchBytea() */
index b91230b1f0a8e1ec19d5d9cb7aa4380cce367039..a0018522e8a7bf513ebf604bd519fea482304443 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Edmund Mergl 
  *
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.31 2001/03/22 03:59:52 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.32 2001/09/14 17:46:40 momjian Exp $
  *
  */
 
@@ -349,6 +349,78 @@ btrim(PG_FUNCTION_ARGS)
    PG_RETURN_TEXT_P(ret);
 }
 
+/********************************************************************
+ *
+ * byteatrim
+ *
+ * Syntax:
+ *
+ *  bytea byteatrim(byta string, bytea set)
+ *
+ * Purpose:
+ *
+ *  Returns string with characters removed from the front and back
+ *  up to the first character not in set.
+ *
+ * Cloned from btrim and modified as required.
+ ********************************************************************/
+
+Datum
+byteatrim(PG_FUNCTION_ARGS)
+{
+   bytea      *string = PG_GETARG_BYTEA_P(0);
+   bytea      *set = PG_GETARG_BYTEA_P(1);
+   bytea      *ret;
+   char       *ptr,
+              *end,
+              *ptr2,
+              *end2;
+   int         m;
+
+   if ((m = VARSIZE(string) - VARHDRSZ) <= 0 ||
+       (VARSIZE(set) - VARHDRSZ) <= 0)
+       PG_RETURN_BYTEA_P(string);
+
+   ptr = VARDATA(string);
+   end = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
+   end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
+
+   while (m > 0)
+   {
+       ptr2 = VARDATA(set);
+       while (ptr2 <= end2)
+       {
+           if (*ptr == *ptr2)
+               break;
+           ++ptr2;
+       }
+       if (ptr2 > end2)
+           break;
+       ptr++;
+       m--;
+   }
+
+   while (m > 0)
+   {
+       ptr2 = VARDATA(set);
+       while (ptr2 <= end2)
+       {
+           if (*end == *ptr2)
+               break;
+           ++ptr2;
+       }
+       if (ptr2 > end2)
+           break;
+       end--;
+       m--;
+   }
+
+   ret = (bytea *) palloc(VARHDRSZ + m);
+   VARATT_SIZEP(ret) = VARHDRSZ + m;
+   memcpy(VARDATA(ret), ptr, m);
+
+   PG_RETURN_BYTEA_P(ret);
+}
 
 /********************************************************************
  *
index 406916c88735cd1750ce7c2f188b71769fe6886f..f92f3c20f1ec82f53d6ed5491ed2086b4f9f67fe 100644 (file)
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.96 2001/08/13 18:45:35 tgl Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.97 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2992,6 +2992,11 @@ string_lessthan(const char *str1, const char *str2, Oid datatype)
                                                      datum1, datum2));
            break;
 
+       case BYTEAOID:
+           result = DatumGetBool(DirectFunctionCall2(bytealt,
+                                                     datum1, datum2));
+           break;
+
        default:
            elog(ERROR, "string_lessthan: unexpected datatype %u", datatype);
            result = false;
index 75eee05eeae22ff228e9b2839f58dcd4d78ad1c3..1c6de93f01464c946a25a0fff6c42a96cd16dc0b 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.72 2001/09/11 05:18:59 ishii Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.73 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,14 +53,28 @@ byteain(PG_FUNCTION_ARGS)
 
    for (byte = 0, tp = inputText; *tp != '\0'; byte++)
    {
-       if (*tp++ == '\\')
+       if (tp[0] != '\\')
        {
-           if (*tp == '\\')
-               tp++;
-           else if (!isdigit((unsigned char) *tp++) ||
-                    !isdigit((unsigned char) *tp++) ||
-                    !isdigit((unsigned char) *tp++))
-               elog(ERROR, "Bad input string for type bytea");
+           tp++;
+       }
+       else if ( (tp[0] == '\\') &&
+                   (tp[1] >= '0' && tp[1] <= '3') &&
+                   (tp[2] >= '0' && tp[2] <= '7') &&
+                   (tp[3] >= '0' && tp[3] <= '7') )
+       {
+           tp += 4;
+       }
+       else if ( (tp[0] == '\\') &&
+               (tp[1] == '\\') )
+       {
+           tp += 2;
+       }
+       else
+       {
+           /*
+            * one backslash, not followed by 0 or ### valid octal
+            */
+           elog(ERROR, "Bad input string for type bytea");
        }
    }
 
@@ -72,15 +86,35 @@ byteain(PG_FUNCTION_ARGS)
    rp = result->vl_dat;
    while (*tp != '\0')
    {
-       if (*tp != '\\' || *++tp == '\\')
+       if (tp[0] != '\\')
+       {
            *rp++ = *tp++;
-       else
+       }
+       else if ( (tp[0] == '\\') &&
+                   (tp[1] >= '0' && tp[1] <= '3') &&
+                   (tp[2] >= '0' && tp[2] <= '7') &&
+                   (tp[3] >= '0' && tp[3] <= '7') )
        {
-           byte = VAL(*tp++);
+           byte = VAL(tp[1]);
            byte <<= 3;
-           byte += VAL(*tp++);
+           byte += VAL(tp[2]);
            byte <<= 3;
-           *rp++ = byte + VAL(*tp++);
+           *rp++ = byte + VAL(tp[3]);
+           tp += 4;
+       }
+       else if ( (tp[0] == '\\') &&
+               (tp[1] == '\\') )
+       {
+           *rp++ = '\\';
+           tp += 2;
+       }
+       else
+       {
+           /*
+            * We should never get here. The first pass should
+            * not allow it.
+            */
+           elog(ERROR, "Bad input string for type bytea");
        }
    }
 
@@ -671,6 +705,147 @@ byteaoctetlen(PG_FUNCTION_ARGS)
    PG_RETURN_INT32(VARSIZE(v) - VARHDRSZ);
 }
 
+/*
+ * byteacat -
+ *   takes two bytea* and returns a bytea* that is the concatenation of
+ *   the two.
+ *
+ * Cloned from textcat and modified as required.
+ */
+Datum
+byteacat(PG_FUNCTION_ARGS)
+{
+   bytea      *t1 = PG_GETARG_BYTEA_P(0);
+   bytea      *t2 = PG_GETARG_BYTEA_P(1);
+   int         len1,
+               len2,
+               len;
+   bytea      *result;
+   char       *ptr;
+
+   len1 = (VARSIZE(t1) - VARHDRSZ);
+   if (len1 < 0)
+       len1 = 0;
+
+   len2 = (VARSIZE(t2) - VARHDRSZ);
+   if (len2 < 0)
+       len2 = 0;
+
+   len = len1 + len2 + VARHDRSZ;
+   result = (bytea *) palloc(len);
+
+   /* Set size of result string... */
+   VARATT_SIZEP(result) = len;
+
+   /* Fill data field of result string... */
+   ptr = VARDATA(result);
+   if (len1 > 0)
+       memcpy(ptr, VARDATA(t1), len1);
+   if (len2 > 0)
+       memcpy(ptr + len1, VARDATA(t2), len2);
+
+   PG_RETURN_BYTEA_P(result);
+}
+
+/*
+ * bytea_substr()
+ * Return a substring starting at the specified position.
+ * Cloned from text_substr and modified as required.
+ *
+ * Input:
+ * - string
+ * - starting position (is one-based)
+ * - string length
+ *
+ * If the starting position is zero or less, then return from the start of the string
+ * adjusting the length to be consistant with the "negative start" per SQL92.
+ * If the length is less than zero, return the remaining string.
+ *
+ */
+Datum
+bytea_substr(PG_FUNCTION_ARGS)
+{
+   bytea      *string = PG_GETARG_BYTEA_P(0);
+   int32       m = PG_GETARG_INT32(1);
+   int32       n = PG_GETARG_INT32(2);
+   bytea      *ret;
+   int         len;
+
+   len = VARSIZE(string) - VARHDRSZ;
+
+   /* starting position after the end of the string? */
+   if (m > len)
+   {
+       m = 1;
+       n = 0;
+   }
+
+   /*
+    * starting position before the start of the string? then offset into
+    * the string per SQL92 spec...
+    */
+   else if (m < 1)
+   {
+       n += (m - 1);
+       m = 1;
+   }
+
+   /* m will now become a zero-based starting position */
+   m--;
+   if (((m + n) > len) || (n < 0))
+       n = (len - m);
+
+   ret = (bytea *) palloc(VARHDRSZ + n);
+   VARATT_SIZEP(ret) = VARHDRSZ + n;
+
+   memcpy(VARDATA(ret), VARDATA(string) + m, n);
+
+   PG_RETURN_BYTEA_P(ret);
+}
+
+/*
+ * byteapos -
+ *   Return the position of the specified substring.
+ *   Implements the SQL92 POSITION() function.
+ * Cloned from textpos and modified as required.
+ */
+Datum
+byteapos(PG_FUNCTION_ARGS)
+{
+   bytea      *t1 = PG_GETARG_BYTEA_P(0);
+   bytea      *t2 = PG_GETARG_BYTEA_P(1);
+   int         pos;
+   int         px,
+               p;
+   int         len1,
+               len2;
+   char        *p1,
+               *p2;
+
+   if (VARSIZE(t2) <= VARHDRSZ)
+       PG_RETURN_INT32(1);     /* result for empty pattern */
+
+   len1 = (VARSIZE(t1) - VARHDRSZ);
+   len2 = (VARSIZE(t2) - VARHDRSZ);
+
+   p1 = VARDATA(t1);
+   p2 = VARDATA(t2);
+
+   pos = 0;
+   px = (len1 - len2);
+   for (p = 0; p <= px; p++)
+   {
+       if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
+       {
+           pos = p + 1;
+           break;
+       };
+       p1++;
+   };
+
+   PG_RETURN_INT32(pos);
+}
+
 /*-------------------------------------------------------------
  * byteaGetByte
  *
index d583d886288d8a444f1987ddfd9d3326985ee8b2..5008df24716e68893fa769d6fd334d1894016f81 100644 (file)
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: catversion.h,v 1.94 2001/09/08 15:24:00 petere Exp $
+ * $Id: catversion.h,v 1.95 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*                         yyyymmddN */
-#define CATALOG_VERSION_NO 200109081
+#define CATALOG_VERSION_NO 200109101
 
 #endif
index ef4806f17ab0d9ff84faf6aee4c9d61db9f1bb17..4919e3e37b1fdaa580d4358215cc17dc9776835f 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_operator.h,v 1.92 2001/08/13 18:45:36 tgl Exp $
+ * $Id: pg_operator.h,v 1.93 2001/09/14 17:46:40 momjian Exp $
  *
  * NOTES
  *   the genbki.sh script reads this file and generates .bki
@@ -811,11 +811,15 @@ DATA(insert OID = 1921 (  "+"    PGUID 0 l t f   0 1700 1700  0   0   0   0 numeric_
 
 /* bytea operators */
 DATA(insert OID = 1955 ( "="      PGUID 0 b t t 17 17  16 1955 1956 1957 1957 byteaeq eqsel eqjoinsel ));
-DATA(insert OID = 1956 ( "<>"     PGUID 0 b t f 17 17  16 1956 1955  0 0 byteane neqsel neqjoinsel ));
-DATA(insert OID = 1957 ( "<"      PGUID 0 b t f 17 17  16 1959 1960  0 0 bytealt scalarltsel scalarltjoinsel ));
-DATA(insert OID = 1958 ( "<="     PGUID 0 b t f 17 17  16 1960 1959  0 0 byteale scalarltsel scalarltjoinsel ));
-DATA(insert OID = 1959 ( ">"      PGUID 0 b t f 17 17  16 1957 1958  0 0 byteagt scalargtsel scalargtjoinsel ));
-DATA(insert OID = 1960 ( ">="     PGUID 0 b t f 17 17  16 1958 1957  0 0 byteage scalargtsel scalargtjoinsel ));
+DATA(insert OID = 1956 ( "<>"     PGUID 0 b t f 17 17  16 1956 1955 0    0 byteane neqsel neqjoinsel ));
+DATA(insert OID = 1957 ( "<"      PGUID 0 b t f 17 17  16 1959 1960 0    0 bytealt scalarltsel scalarltjoinsel ));
+DATA(insert OID = 1958 ( "<="     PGUID 0 b t f 17 17  16 1960 1959 0    0 byteale scalarltsel scalarltjoinsel ));
+DATA(insert OID = 1959 ( ">"      PGUID 0 b t f 17 17  16 1957 1958 0    0 byteagt scalargtsel scalargtjoinsel ));
+DATA(insert OID = 1960 ( ">="     PGUID 0 b t f 17 17  16 1958 1957 0    0 byteage scalargtsel scalargtjoinsel ));
+DATA(insert OID = 2016 (  "~~"    PGUID 0 b t f 17 17  16 0    2017 0    0 bytealike likesel likejoinsel ));
+#define OID_BYTEA_LIKE_OP      2016
+DATA(insert OID = 2017 (  "!~~"       PGUID 0 b t f 17 17  16 0    2016 0    0 byteanlike nlikesel nlikejoinsel ));
+DATA(insert OID = 2018 (  "||"    PGUID 0 b t f 17 17  17 0    0    0    0 byteacat - - ));
 
 /*
  * function prototypes
index c6fb35999386f07e182edc962ccdb2f53cf9d315..90718bc10adc59b80db72c2a7c1f7d4ed29aa953 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.210 2001/09/08 01:10:20 tgl Exp $
+ * $Id: pg_proc.h,v 1.211 2001/09/14 17:46:40 momjian Exp $
  *
  * NOTES
  *   The script catalog/genbki.sh reads this file and generates .bki
@@ -2740,6 +2740,28 @@ DESCR("larger of two");
 DATA(insert OID = 1966 (  oidsmaller      PGUID 12 f t t t 2 f 26 "26 26" 100 0 0 100  oidsmaller - ));
 DESCR("smaller of two");
 
+DATA(insert OID = 2005 (  bytealike           PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100 bytealike - ));
+DESCR("matches LIKE expression");
+DATA(insert OID = 2006 (  byteanlike      PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100 byteanlike - ));
+DESCR("does not match LIKE expression");
+DATA(insert OID = 2007 (  like            PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100  bytealike - ));
+DESCR("matches LIKE expression");
+DATA(insert OID = 2008 (  notlike         PGUID 12 f t t t 2 f 16 "17 17" 100 0 0 100  byteanlike - ));
+DESCR("does not match LIKE expression");
+DATA(insert OID = 2009 (  like_escape     PGUID 12 f t t t 2 f 17 "17 17" 100 0 0 100 like_escape_bytea - ));
+DESCR("convert match pattern to use backslash escapes");
+DATA(insert OID = 2010 (  length          PGUID 12 f t t t 1 f 23 "17" 100 0 0 100  byteaoctetlen - ));
+DESCR("octet length");
+DATA(insert OID = 2011 (  byteacat        PGUID 12 f t t t 2 f 17 "17 17" 100 0 0 100  byteacat - ));
+DESCR("concatenate");
+DATA(insert OID = 2012 (  substring           PGUID 12 f t t t 3 f 17 "17 23 23" 100 0 0 100  bytea_substr - ));
+DESCR("return portion of string");
+DATA(insert OID = 2013 (  substring           PGUID 14 f t t t 2 f 17 "17 23" 100 0 0 100  "select substring($1, $2, -1)" - ));
+DESCR("return portion of string");
+DATA(insert OID = 2014 (  position        PGUID 12 f t t t 2 f 23 "17 17" 100 0 0 100  byteapos - ));
+DESCR("return position of substring");
+DATA(insert OID = 2015 (  btrim               PGUID 12 f t t t 2 f 17 "17 17" 100 0 0 100  byteatrim - ));
+DESCR("trim both ends of string");
  
 /*
  * prototypes for functions pg_proc.c
index cb3a0a8913b2d863e5ea0b6c5aad472d8cfa1b89..671d72684d5d79fa57924a000602fbf145d385f1 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: builtins.h,v 1.163 2001/09/06 04:57:29 ishii Exp $
+ * $Id: builtins.h,v 1.164 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -421,6 +421,9 @@ extern Datum byteale(PG_FUNCTION_ARGS);
 extern Datum byteagt(PG_FUNCTION_ARGS);
 extern Datum byteage(PG_FUNCTION_ARGS);
 extern Datum byteacmp(PG_FUNCTION_ARGS);
+extern Datum byteacat(PG_FUNCTION_ARGS);
+extern Datum byteapos(PG_FUNCTION_ARGS);
+extern Datum bytea_substr(PG_FUNCTION_ARGS);
 
 /* version.c */
 extern Datum pgsql_version(PG_FUNCTION_ARGS);
@@ -434,7 +437,10 @@ extern Datum textlike(PG_FUNCTION_ARGS);
 extern Datum textnlike(PG_FUNCTION_ARGS);
 extern Datum texticlike(PG_FUNCTION_ARGS);
 extern Datum texticnlike(PG_FUNCTION_ARGS);
+extern Datum bytealike(PG_FUNCTION_ARGS);
+extern Datum byteanlike(PG_FUNCTION_ARGS);
 extern Datum like_escape(PG_FUNCTION_ARGS);
+extern Datum like_escape_bytea(PG_FUNCTION_ARGS);
 
 /* oracle_compat.c */
 extern Datum lower(PG_FUNCTION_ARGS);
@@ -443,6 +449,7 @@ extern Datum initcap(PG_FUNCTION_ARGS);
 extern Datum lpad(PG_FUNCTION_ARGS);
 extern Datum rpad(PG_FUNCTION_ARGS);
 extern Datum btrim(PG_FUNCTION_ARGS);
+extern Datum byteatrim(PG_FUNCTION_ARGS);
 extern Datum ltrim(PG_FUNCTION_ARGS);
 extern Datum rtrim(PG_FUNCTION_ARGS);
 extern Datum translate(PG_FUNCTION_ARGS);
index e7495dabf7f38bcc79e28305ec1ec0f79139d60c..2741ec67aa6eefe965c0019c9c1c7010f7b445b0 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.111 2001/09/13 17:00:34 momjian Exp $
+ *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.112 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -104,7 +104,79 @@ PQescapeString (char *to, const char *from, size_t length)
    return target - to;
 }
 
+/*
+ *     PQescapeBytea   - converts from binary string to the
+ *     minimal encoding necessary to include the string in an SQL
+ *     INSERT statement with a bytea type column as the target.
+ *
+ *     The following transformations are applied
+ *     '\0' == ASCII  0 == \\000
+ *     '\'' == ASCII 39 == \'
+ *     '\\' == ASCII 92 == \\\\
+ */
+unsigned char *
+PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen)
+{
+   unsigned char      *vp;
+   unsigned char      *rp;
+   unsigned char      *result;
+   size_t              i;
+   size_t              len;
+
+   /*
+    * empty string has 1 char ('\0')
+    */
+   len = 1;
+
+   vp = bintext;
+   for (i = binlen; i != 0; i--, vp++)
+   {
+       if (*vp == 0)
+           len += 5;
+       else if (*vp == 39)
+           len += 2;
+       else if (*vp == 92)
+           len += 4;
+       else
+           len++;
+   }
+
+   rp = result = (unsigned char *) malloc(len);
+   vp = bintext;
+   *bytealen = len;
 
+   for (i = binlen; i != 0; i--, vp++)
+   {
+       if (*vp == 0)
+       {
+           rp[0] = '\\';
+           rp[1] = '\\';
+           rp[2] = '0';
+           rp[3] = '0';
+           rp[4] = '0';
+           rp += 5;
+       }
+       else if (*vp == 39)
+       {
+           rp[0] = '\\';
+           rp[1] = '\'';
+           rp += 2;
+       }
+       else if (*vp == 92)
+       {
+           rp[0] = '\\';
+           rp[1] = '\\';
+           rp[2] = '\\';
+           rp[3] = '\\';
+           rp += 4;
+       }
+       else
+           *rp++ = *vp;
+   }
+   *rp = '\0';
+
+   return result;
+}
 
 /* ----------------
  * Space management for PGresult.
index d3b472f9ccbc4f0be55a5bcd7b55ac70d1cb1d42..02448ef84369ec39ad96bb3f761c09a45b2dba94 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: libpq-fe.h,v 1.74 2001/09/07 22:02:32 momjian Exp $
+ * $Id: libpq-fe.h,v 1.75 2001/09/14 17:46:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -253,6 +253,7 @@ extern      "C"
 
    /* Quoting strings before inclusion in queries. */
    extern size_t PQescapeString (char *to, const char *from, size_t length);
+   extern unsigned char *PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen);
 
    /* Simple synchronous query */
    extern PGresult *PQexec(PGconn *conn, const char *query);