Re: return varchar from C function
Hello
cstring is clasic c (zero terminated) string and is used only in some
PostgreSQL functions. This type isn't compatible with text and you have to
explicit cast trick with textin function.
root=# select textin(('abc'::cstring));
textin
--------
abc
(1 row)
Standard is using VARLENA types like text, varchar, ... You can find info in
PostgreSQL FAQ. These types are similar Pascal string -> first four bytes
cary length and next bytes are data without spec. ending symbol.
http://www.varlena.com/GeneralBits/68.php
using text type in C function is simple:
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);
}
please look to source code my orafce contrib module (you can find it on
pgfoundry).
Regards
Pavel Stehule
_________________________________________________________________
Najdete si svou lasku a nove pratele na Match.com. http://www.msn.cz/
"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);
}
--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
On Sun, Feb 18, 2007 at 12:56:08PM -0500, scotty@linuxtime.it wrote:
Hi,
just for fun, I wrote a little postgresql contrib,
who has a C function called myfun inside it.
The function myfun returns a value , now I return
a cstring type value, and it works fine if
I run from psql shell:
You don't provide the definition you used, but:
select value from myfun(paramteres);
This isn't the usual way to return things, unless it is a set returning
function. Did you provide the return type at declaration time?
Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
Show quoted text
From each according to his ability. To each according to his ability to litigate.
Import Notes
Reply to msg id not found: 1171821368468.scotty@linuxtime.itReference msg id not found: 1171821368468.scotty@linuxtime.it | Resolved by subject fallback
"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/