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);
        }
}



Reply via email to