Hello, can you please tell me how to return a NULL value
from a function? I am running postgresql 7.3.2. and the
following is my case. Thank you very much.

Chongbing

============ header file ===============

#include "postgres.h"
#include "fmgr.h"
#include <string.h>
#include <sys/types.h>

#ifndef pname_h
#define pname_h 1



struct pname
{
  int  size;
  char name[1];
};

typedef struct pname pname;

pname * pname_in( char * in);

char * pname_out( pname * nm);

#endif

============ C code =============
#include "postgres.h"
#include "fmgr.h"
#include "pname.h"
#include <ctype.h>
#include <string.h>

#ifndef pname_c
#define pname_c 1

pname * pname_in(char * in) {
  int i;
  pname * n;
  n = (pname *) palloc(strlen(in)+VARHDRSZ);
  n->size=strlen(in)+VARHDRSZ;
  memcpy(n->name, in, strlen(in));
  if(strlen(in)<4)
    return (NULL);
  return n;
}


char * pname_out(pname* nm) {
   char *t;
   if(nm==NULL)
     return (NULL);
   t = (char *) palloc(VARSIZE(nm)-VARHDRSZ+1);
   strcpy(t,nm->name);
   return t;
}

#endif


============== psql command for function installation =============
CREATE FUNCTION pname_in (opaque)
    RETURNS pname
    AS '/home/cliu/types_lib/pname.so', 'pname_in'
    LANGUAGE 'c';

CREATE FUNCTION pname_out(opaque)
    RETURNS opaque
    AS '/home/cliu/types_lib/pname.so','pname_out'
    LANGUAGE 'c';


CREATE TYPE pname (
    input = pname_in,
    output = pname_out,
    internallength = VARIABLE,
    externallength = VARIABLE,
    default = " "
);

================== test cases ===============

cliu=# select pname('abcdefghijk');
NOTICE:  ok2:abcdefghijk

    pname
-------------
 abcdefghijk
(1 row)

cliu=# select pname('ab');
NOTICE:  ok2:ab

server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!#


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faqs/FAQ.html

Reply via email to