Thread: return varchar from C function
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: select value from myfun(paramteres); but I can't do an insert like: insert (charfield) select value from myfun(paramteres); becuse I have an error, exactly casting error. I want that myfun returns a char,varchar, or text type. Where I can find more documentation about differences between cstring,char,text etc...? Regards, Enrico
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/ > From each according to his ability. To each according to his ability to litigate.
>"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/
Thanks for all your answers, I begin to study. Enrico -- Enrico <scotty@linuxtime.it>