Re: C-Language Functions: VarChar and Text arguments - Mailing list pgsql-novice

From Brian Modra
Subject Re: C-Language Functions: VarChar and Text arguments
Date
Msg-id u2r5a9699851004060233r44d684e6i6ae8533c1d43929a@mail.gmail.com
Whole thread Raw
In response to C-Language Functions: VarChar and Text arguments  (Carel Combrink <s25291930@tuks.co.za>)
List pgsql-novice
On 06/04/2010, Carel Combrink <s25291930@tuks.co.za> wrote:
> Hi,
>
> I have problems using 'varchar' and 'text' arguments for C functions.
> Perhaps I am doing something wrong. I get garbage when I want to use
> the arguments passed to the function. See the example:
>
> My function as defined in my C program:
> /*===========================================*/
> PG_FUNCTION_INFO_V1(Test_Function);
>
> Datum Test_Function(PG_FUNCTION_ARGS) /* varChar(10) varChar Text*/
> {
>      VarChar* arg0_varChar10 = PG_GETARG_VARCHAR_P(0);
>      VarChar* arg1_varChar = PG_GETARG_VARCHAR_P(1);
>      text* arg2_textp = PG_GETARG_VARCHAR_P(2);
>
>      ereport( INFO, ( errcode( ERRCODE_SUCCESSFUL_COMPLETION ),
> errmsg("Inputs :\n\targ0: %s\n\targ1: %s\n\targ2:
> %s\n",VARDATA(arg0_varChar10), VARDATA(arg1_varChar),
> VARDATA(arg2_textp))));
>
>      PG_RETURN_INT32(0);
> }
> /*=============================================*/
>
> I run the following in PostgreSQL 8.4 to create the function:
> ------------------------------------------------
> CREATE OR REPLACE FUNCTION Test_Function(varchar(10), varchar, text)
> RETURNS integer
>    AS '$libdir/myDir/myLib', 'Test_Function'
>    LANGUAGE C
>    VOLATILE
>    STRICT
>    SECURITY DEFINER;
> ------------------------------------------------
>
> The output I get when I call the function:
> --------------------------------------------
> My_database=# SELECT Test_Function('arg0', 'arg1', 'arg2');
>
> is:
>
> INFO:  Inputs :
>          arg0: arg�V"�
>          arg1: arg1��O"
>          arg2: arg2��O"
>
>   test_function
> ---------------
>               0
> (1 row)
> --------------------------------------------
>
> What is the 'garbage' I see at the end of the output?
> Am I calling the correct functions in my C code to retrieve the
> arguments and then the correct ones to display them?

You need to use a text* structure. See this page, it explains it all:
http://www.postgresql.org/docs/8.2/static/xfunc-c.html

>
> In my original function I want to send the name of an entity to the
> database and then add the name to a table but this is a problem if the
> name is garbled.
>
> Using: PostgreSQL 8.4
> OS: Linux Ubuntu 9.10 Karmic Koala
>
> --
> Carel Combrink
> s25291930@tuks.co.za
>
> This message and attachments are subject to a disclaimer. Please refer
> to www.it.up.ac.za/documentation/governance/disclaimer/ for full
> details. / Hierdie boodskap en aanhangsels is aan 'n vrywaringsklousule
> onderhewig. Volledige besonderhede is by
> www.it.up.ac.za/documentation/governance/disclaimer/ beskikbaar.
>
>
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice
>

--
Brian Modra   Land line: +27 23 5411 462
Mobile: +27 79 69 77 082
5 Jan Louw Str, Prince Albert, 6930
Postal: P.O. Box 2, Prince Albert 6930
South Africa
http://www.zwartberg.com/

pgsql-novice by date:

Previous
From: Carel Combrink
Date:
Subject: Re: C-Language Functions: VarChar and Text arguments
Next
From: Carel Combrink
Date:
Subject: Re: C-Language Functions: VarChar and Text arguments