Re: C-Language Functions: VarChar and Text arguments

From: Brian Modra <brian(at)zwartberg(dot)com>
To: Carel Combrink <s25291930(at)tuks(dot)co(dot)za>, pgsql-novice(at)postgresql(dot)org
Subject: Re: C-Language Functions: VarChar and Text arguments
Date: 2010-04-06 09:33:27
Message-ID: u2r5a9699851004060233r44d684e6i6ae8533c1d43929a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 06/04/2010, Carel Combrink <s25291930(at)tuks(dot)co(dot)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&#65533;V"&#65533;
> arg1: arg1&#65533;&#65533;O"
> arg2: arg2&#65533;&#65533;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(at)tuks(dot)co(dot)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(at)postgresql(dot)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/

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Carel Combrink 2010-04-06 09:58:52 Re: C-Language Functions: VarChar and Text arguments
Previous Message Carel Combrink 2010-04-06 08:57:46 C-Language Functions: VarChar and Text arguments