Thread: Datum problem

Datum problem

From
Enrico
Date:
Hi,
I'm newbie of programming postgresql sever side,
I write the function below and when I execute

SELECT (anag_art, 150) AS esistenza from anag_art order by 1;

result is something about this:

| esistenza |

 ("(""0000002      "",""ARTICOLO PREZ. VEND.              "",,PZ,32,1,20,""1              "",""
"","""",N,,0,-67.00000,-23.00000,,N,N)",150)


So postgresql outputs with a tuple, instead of an integer value as I wish.
Can anyone helps me?

Thanks in advantage.
Enrico


------------------- Start C code ---------------

#include "postgres.h"
#include "executor/executor.h"

PG_FUNCTION_INFO_V1(visualizza_esistenza);

Datum
visualizza_esistenza(PG_FUNCTION_ARGS)
{
    HeapTupleHeader  t = PG_GETARG_HEAPTUPLEHEADER(0);
    int16            limit = PG_GETARG_INT16(1);
    bool isnull;
    Datum esistenza;
    int16 es;

    esistenza = GetAttributeByName(t, "esistenza", &isnull);
    es = DatumGetInt16(esistenza);
    if (esistenza > limit)
      PG_RETURN_INT16(es);

}

------------------- End C code ---------------

CREATE OR REPLACE FUNCTION visualizza_esistenza(anag_art, integer) RETURNS int4
    as '/home/scotty/enrico/postgresql_c/prova.so', 'visualizza_esistenza'
    LANGUAGE C;


--
If Bill Gates had a penny for everytime Windows crashed,he'd be a multi-billionaire by now .......oh look, he already
is!!!! 
scotty@linuxtime.it - Skype:sscotty71
http://www.linuxtime.it/enricopirozzi

Re: Datum problem

From
Martijn van Oosterhout
Date:
On Thu, Nov 09, 2006 at 06:12:08PM +0100, Enrico wrote:
> Hi,
> I'm newbie of programming postgresql sever side,
> I write the function below and when I execute
>
> SELECT (anag_art, 150) AS esistenza from anag_art order by 1;
>
> result is something about this:

Well, you didn't actually call any function, so it just returned the
two parameters as you asked... You have other problems though.

> visualizza_esistenza(PG_FUNCTION_ARGS)
> {
>     HeapTupleHeader  t = PG_GETARG_HEAPTUPLEHEADER(0);
>     int16            limit = PG_GETARG_INT16(1);

An "integer" is not an "int16", so this is wrong, you want INT32

>     bool isnull;
>     Datum esistenza;
>     int16 es;
>
>     esistenza = GetAttributeByName(t, "esistenza", &isnull);
>     es = DatumGetInt16(esistenza);

Possibly same problem here.

>     if (esistenza > limit)
>       PG_RETURN_INT16(es);

You declared you were going to return an int4, yet you're returing
something else.

> }

What are you returning if the if statement doesn't get run?

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: Datum problem

From
"A. Kretschmer"
Date:
am  Thu, dem 09.11.2006, um 18:12:08 +0100 mailte Enrico folgendes:
> Hi,
> I'm newbie of programming postgresql sever side,
> I write the function below and when I execute
>
> SELECT (anag_art, 150) AS esistenza from anag_art order by 1;

Try select *, 150 AS esistenza from anag_art order by 1;


Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47215,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

Re: Datum problem

From
Enrico
Date:
Thanks for your answeres.

--
If Bill Gates had a penny for everytime Windows crashed,he'd be a multi-billionaire by now .......oh look, he already
is!!!! 
scotty@linuxtime.it - Skype:sscotty71
http://www.linuxtime.it/enricopirozzi