Re: return varchar from C function

From: "Pavel Stehule" <pavel(dot)stehule(at)hotmail(dot)com>
To: scotty(at)linuxtime(dot)it
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: return varchar from C function
Date: 2007-02-18 14:54:54
Message-ID: BAY20-F2105C029334674EEB8C8BBF98B0@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gregory Stark 2007-02-18 15:01:49 Re: return varchar from C function
Previous Message Russell Smith 2007-02-18 12:36:12 Re: Plan invalidation design