How can I return a NULL value from a function?

From: Chongbing Liu <cliu(at)cs(dot)nmsu(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Chongbing Liu <cliu(at)cs(dot)nmsu(dot)edu>
Subject: How can I return a NULL value from a function?
Date: 2003-12-19 05:51:27
Message-ID: Pine.LNX.4.58.0312182240130.15450@hongkong
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


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.
!#

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message elein 2003-12-19 06:05:10 Re: PostgreSQL speakers needed for OSCON 2004
Previous Message elein 2003-12-19 05:46:16 Re: PostgreSQL speakers needed for OSCON 2004