Thread: How can I return a NULL value from a function?

How can I return a NULL value from a function?

From
Chongbing Liu
Date:
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
whileprocessing the request.
 
The connection to the server was lost. Attempting reset: Failed.
!#



Re: How can I return a NULL value from a function?

From
Tom Lane
Date:
Chongbing Liu <cliu@cs.nmsu.edu> writes:
> Hello, can you please tell me how to return a NULL value
> from a function?

You can't unless you use the V1 function calling convention.  See the
programmer's guide.
        regards, tom lane


How can I return a pointer to a use structure from a function

From
Chongbing Liu
Date:

Thank you very much for your help.

In V1 function calling convention, there are micros
like PG_RETURN_TEXT_P and so on.  Most of them are
used to return values of the built-in datatypes.

If I want the XXX_in function (for my own data type)
to return a pointer to a particular structure, what
micro should I use? i.e., how should I organize the
return value?

Thank you and wish you and everybody a merry Christmas.

Chongbing


On Fri, 19 Dec 2003, Tom Lane wrote:

> Chongbing Liu <cliu@cs.nmsu.edu> writes:
> > Hello, can you please tell me how to return a NULL value
> > from a function?
>
> You can't unless you use the V1 function calling convention.  See the
> programmer's guide.
>
>             regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>


Re: How can I return a pointer to a use structure from a function

From
Tom Lane
Date:
Chongbing Liu <cliu@cs.nmsu.edu> writes:
> If I want the XXX_in function (for my own data type)
> to return a pointer to a particular structure, what
> micro should I use?

You can just use PG_RETURN_POINTER if you're feeling lazy.
PG_RETURN_TEXT_P and other wrappers around PG_RETURN_POINTER
exist mainly as a simple form of documentation.  If you like
that kind of documentation you can define your own GET and RETURN
macros for your own datatype (look in fmgr.h for examples).
If you don't, don't bother.
        regards, tom lane