Very observant of you. The postgres function system doesn't have a way
to tell which parameter is null, so the function just returns null.
I don't like it and we hope to get it changed for PGv6.5 (at least I
do).
Is it on the TODO guys? Jan, do you think you'll have the time to get
to it by beta?
*** Blatant flattery start ***
I'm still willing and looking for that backend mentor to truly get me
started, and I want you, Jan and Bruce, because I want the best.
BTW don't give me license to bug you unless you mean it.
*** Blatant flattery end ***
-DEJ
> -----Original Message-----
> I wrote a c func (ATTACHED BELOW) which takes 2 pointers to
> type text, and
> a bool field. When a field in a row passed to the func is
> null, espically
> the 'first' field, Postgresql does not return the value that my code
> produced.
>
> For example:
> First lastco nocompany
> ------------------------------------------
> NULL Walt Bigelow True
>
> the above data is contained in the database, and when passed
> to my c func
> like:
>
> SELECT lastfirst(first, lastco, nocompany) as name from
> tbladdress where
> agencyid = 691;
>
> I get:
> name
> ----
>
> (1 row)
>
> Not the expected output of 'Walt Bigelow, '.
>
> When I update that row with first = '', the result is correct, but the
> value is no null, so it works.
>
> What am I missing??
>
> Any help is appricated,
> Walt
>
> -------------
> name.c:
>
> #include <string.h>
> #include "postgres.h"
>
> #define COMMA ", "
>
> text *lastfirst (text *first, text *last, bool nocompany)
> {
> /* this function will take in the first name, and last name
> * and based on nocompany set the output to either return
> * 'last, first' or just the company name.
> */
>
> int32 ret_size;
> text *return_text;
>
> if (nocompany) {
>
> if (first == NULL) {
>
> ret_size = VARSIZE(last) + sizeof(COMMA);
> return_text = (text *) palloc(ret_size);
>
> memset(return_text, 0, ret_size);
>
> VARSIZE(return_text) = ret_size;
>
> strncpy (VARDATA(return_text), VARDATA(last),
> VARSIZE(last)-VARHDRSZ);
>
> strncat (VARDATA(return_text), COMMA,
> sizeof(COMMA));
>
> return (return_text);
>
> } else {
>
> ret_size = VARSIZE(first) + VARSIZE(last) +
> sizeof(COMMA) - VARHDRSZ;
>
> return_text = (text *) palloc(ret_size);
>
> memset(return_text, 0, ret_size);
>
> VARSIZE(return_text) = ret_size;
>
> strncpy (VARDATA(return_text),
> VARDATA(last), VARSIZE(last)-VARHDRSZ);
> strncat (VARDATA(return_text), COMMA,
> sizeof(COMMA));
> strncat (VARDATA(return_text),
> VARDATA(first), VARSIZE(first)-VARHDRSZ);
>
> return (return_text);
>
> }
>
> } else {
> /* Just the company name is returned here */
>
> ret_size = VARSIZE(last);
> return_text = (text *) palloc(ret_size);
>
> VARSIZE(return_text) = ret_size;
> strncpy(VARDATA(return_text), VARDATA(last),
> VARSIZE(last)-VARHDRSZ);
>
> return (return_text);
> }
> }