Thread: version 1 C-Language Functions

version 1 C-Language Functions

From
"Gowey, Geoffrey"
Date:
Hi all,

I've been trying for days to figure this out, but just not have been able to
figure this out.  I'm creating a test function for me to start out using the
v1 functions, but it fails.

I'm doing a simple function:

PG_FUNCTION_INFO_V1(testfunction)

Datum
testfunction(PG_FUNCTION_ARGS)
{
    char *testb = PG_GETARG_CSTRING(0);
    int tr;
    tr = 1;
    elog(NOTICE, "%s\n",testb);
    PG_RETURN_INT16(tr);
}

I've declared it with "create function testfunction(text) returns int4 as
'....so' language 'c';"

and an trying to call it by "select testfunction('test@test.com');"

Unfortunately, this function fails.  What am i doing wrong?  Also, the docs
behind v1 functions is sketchy and example code seems to be pretty much
nonexistent (everything in the contrib is V0).  Where can I get further info
on writing sp's for pgsql in c?  Thanks.

Geoff

Re: version 1 C-Language Functions

From
Tom Lane
Date:
"Gowey, Geoffrey" <ggowey@rxhope.com> writes:
> Unfortunately, this function fails.  What am i doing wrong?

"text *" is not "char *".  The equivalent V0 code would fail just the
same way.

            regards, tom lane

RE: version 1 C-Language Functions

From
"Gowey, Geoffrey"
Date:
What would the correct syntax be in order to do what I want to?  I need to
enter in a variable length email address as the parameter to my planned
function.  The param will then become part of a select statement to get
records (which I currently plan to accomplish by using strcat to join the
beginning of the select plus the param plus the closing '.  Also where is
some good documentation on V1 command usage and examples?

Geoff

-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: Saturday, August 25, 2001 7:29 PM
To: Gowey, Geoffrey
Cc: 'pgsql-general@postgresql.org'
Subject: Re: [GENERAL] version 1 C-Language Functions


"Gowey, Geoffrey" <ggowey@rxhope.com> writes:
> Unfortunately, this function fails.  What am i doing wrong?

"text *" is not "char *".  The equivalent V0 code would fail just the
same way.

            regards, tom lane

Re: version 1 C-Language Functions

From
Francesco Casadei
Date:
On Sat, Aug 25, 2001 at 08:04:37PM -0400, Gowey, Geoffrey wrote:
> What would the correct syntax be in order to do what I want to?  I need to
> enter in a variable length email address as the parameter to my planned
> function.  The param will then become part of a select statement to get
> records (which I currently plan to accomplish by using strcat to join the
> beginning of the select plus the param plus the closing '.  Also where is
> some good documentation on V1 command usage and examples?
>
> Geoff
>
> -----Original Message-----
> From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
> Sent: Saturday, August 25, 2001 7:29 PM
> To: Gowey, Geoffrey
> Cc: 'pgsql-general@postgresql.org'
> Subject: Re: [GENERAL] version 1 C-Language Functions
>
>
> "Gowey, Geoffrey" <ggowey@rxhope.com> writes:
> > Unfortunately, this function fails.  What am i doing wrong?
>
> "text *" is not "char *".  The equivalent V0 code would fail just the
> same way.
>
>             regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
>
> end of the original message

Read the "Programmer's Guide", part II "Server Programming", section 13.4.3
"Version-1 Calling Conventions for C-Language Functions". Look at the example
functions copytext and concat_text.

    Francesco Casadei

RE: version 1 C-Language Functions

From
"Gowey, Geoffrey"
Date:
Those functions demonstrate copying text input.  I am not.  I'm trying to
use a parameter entered as part of a select statement that will generated.
I need PG_GETARG_CSTRING(x) for this task, not PG_GETARG_TEST.  The way the
program will work is it will sandwitch together the select statement into a
single var (w/ the param in the middle) using strcat and then feed it to SPI
for execution.  No example that I have run across illustrates how to convert
a param entered to be used w/ regular c functions (in this case strcat).

Geoff

-----Original Message-----
From: Francesco Casadei [mailto:f_casadei@libero.it]
Sent: Sunday, August 26, 2001 11:41 AM
To: Gowey, Geoffrey
Cc: 'Tom Lane'; 'pgsql-general@postgresql.org'
Subject: Re: [GENERAL] version 1 C-Language Functions


On Sat, Aug 25, 2001 at 08:04:37PM -0400, Gowey, Geoffrey wrote:
> What would the correct syntax be in order to do what I want to?  I need to
> enter in a variable length email address as the parameter to my planned
> function.  The param will then become part of a select statement to get
> records (which I currently plan to accomplish by using strcat to join the
> beginning of the select plus the param plus the closing '.  Also where is
> some good documentation on V1 command usage and examples?
>
> Geoff
>
> -----Original Message-----
> From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
> Sent: Saturday, August 25, 2001 7:29 PM
> To: Gowey, Geoffrey
> Cc: 'pgsql-general@postgresql.org'
> Subject: Re: [GENERAL] version 1 C-Language Functions
>
>
> "Gowey, Geoffrey" <ggowey@rxhope.com> writes:
> > Unfortunately, this function fails.  What am i doing wrong?
>
> "text *" is not "char *".  The equivalent V0 code would fail just the
> same way.
>
>             regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
>
> end of the original message

Read the "Programmer's Guide", part II "Server Programming", section 13.4.3
"Version-1 Calling Conventions for C-Language Functions". Look at the
example
functions copytext and concat_text.

    Francesco Casadei

Re: version 1 C-Language Functions

From
Tom Lane
Date:
"Gowey, Geoffrey" <ggowey@rxhope.com> writes:
> No example that I have run across illustrates how to convert
> a param entered to be used w/ regular c functions (in this case
> strcat).

I think the technique exhibited in the 7.1 contrib/soundex module is
about the cleanest: use the text type's I/O conversion functions.
Preferably with macros to hide the notational cruft:


#define _textin(str) DatumGetPointer(DirectFunctionCall1(textin, CStringGetDatum(str)))
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))

/*
 * SQL function: text_soundex(text) returns text
 */
PG_FUNCTION_INFO_V1(text_soundex);

Datum
text_soundex(PG_FUNCTION_ARGS)
{
    char        outstr[SOUNDEX_LEN + 1];
    char       *arg;

    arg = _textout(PG_GETARG_TEXT_P(0));

    soundex(arg, outstr);

    PG_RETURN_TEXT_P(_textin(outstr));
}

'arg' and 'outstr' are null-terminated strings here.

            regards, tom lane