Re: return varchar from C function - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: return varchar from C function
Date
Msg-id BAY20-F2DC691586B7BF38100A59F98B0@phx.gbl
Whole thread Raw
In response to Re: return varchar from C function  (Gregory Stark <stark@enterprisedb.com>)
List pgsql-hackers
>"Pavel Stehule" <pavel.stehule@hotmail.com> writes:
>
> >
> > Datum *const_fce(PG_FUNCTION_ARGS)
> > {
> >    text *txt = palloc(5 + VARHDRSZ);
> >    memcpy(VARDATA(txt), "pavel", 5);
> >    VARATT_SIZE(txt) = 5 + VARHDRSZ;
> >
> >    PG_RETURN_TEXT_P(txt);
> > }
>
>Much better practice is to use the input function of the data type you want 
>to
>convert to:
>
>{
>    text *txt = DatumGetTextP(DirectFunctionCall1(textin, 
>CStringGetDatum("pavel")));
>    PG_RETURN_TEXT_P(txt);
>}
>

Generally we want to do something with text value (concat, trim, ..) and 
then call textin isn't practic. Respective, for bussness processes 
implemented in C is textin and similar functions right. For string toolkit 
is better direct work with VARLENA struct.

Regards and thank you note

Pavel

_________________________________________________________________
Emotikony a pozadi programu MSN Messenger ozivi vasi konverzaci. 
http://messenger.msn.cz/



pgsql-hackers by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: return varchar from C function
Next
From: Peter Eisentraut
Date:
Subject: Re: [COMMITTERS] pgsql: Better fix for determining minimum and maximum int64 values that