Return-Path: aoki
Received: by postgres.Berkeley.EDU (5.61/1.29)
	id AA03132; Fri, 16 Jul 93 08:09:45 -0700
Message-Id: <9307161509.AA03132@postgres.Berkeley.EDU>
From: aoki@postgres.berkeley.edu (Paul M. Aoki)
Subject: Re: c-functions
To: postgres@postgres.berkeley.edu
Sender: pg_adm@postgres.berkeley.edu
In-Reply-To: Your message of Fri, 16 Jul 93 03:50:49 -0700 
	     <9307161050.AA02332@postgres.Berkeley.EDU> 
Date: Fri, 16 Jul 93 08:14:45 -0700
Sender: aoki@postgres.Berkeley.EDU
X-Mts: smtp

Lydia Wuerfl <lwuerfl@cosy.sbg.ac.at> writes:
> * retrieve (tf.all)       
> where cont_textarr(tf.a, "fritz")\g

try
	cont_textarr(tf.a, "fritz"::text)

> * retrieve (tf.all) where tf.a@fritz\g

try
	tf.a @ "fritz"::text

avi pfeffer finally fixed the default type-casting code for text/char16.
this fix will be in 4.2.  meanwhile, make sure you explicitly typecast
all instances of text and char16 in your queries.

> int
> cont_textarr (char **anArray, char aKey)
> {
>         int i;
>         int ac;
>         int ret = 0;
>         char *str;
>         char *array_out();
>         str= array_out(anArray, 25);
> 
>              ac =  array_count (str,',');
> 
>              for (i=1; i <= ac; i++)
>              {
>                ret = (strcmp (anArray[i],aKey)==0);
>                if (ret != 0) break; 
>              }
> 
>              pfree (str);
>              return ret;
> 
> }

this code is wrong.  in 4.1, both arguments should be (struct varlena *)
(see src/tmp/postgres.h).  (the implementation of arrays will change in 
4.2.)  you can also see src/doc/manual.me for a VERY brief description 
of what variable-length structures look like in postgres.

if you want to understand how to walk an array of variable-length
types, you'll have to read src/utils/adt/arrayfuncs.c.  the short
answer is: textarray is a struct varlena containing a bunch of
other struct varlenas (since text is also implemented as struct varlena).
text is *NOT* implemented as a \0-terminated string, so use of strcmp 
on text will eventually cause segmentation violations.  you can use the
function "texteq" that is built into postgres.

this interface is not well-documented, to say the least.  however, given 
that the array implementation was an awful hack (prior to 4.2), i don't 
think anybody really wanted to document it and admit how it was done :-)
--
  Paul M. Aoki  |  CS Div., Dept. of EECS, UCB  |  aoki@postgres.Berkeley.EDU
                |  Berkeley, CA 94720           |  ...!uunet!ucbvax!aoki
