Re: URL Managment - C Function help

From: Samuel ROZE <samuel(dot)roze(at)gmail(dot)com>
To: Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: URL Managment - C Function help
Date: 2009-10-21 16:13:32
Message-ID: 1256141612.2845.12.camel@samuel-laptop
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks for your reply.

--------------------------------------------------------
PG_FUNCTION_INFO_V1(parse_url_record);
Datum parse_url_record (PG_FUNCTION_ARGS)
{
// Vars about the params
//text *str2 = PG_GETARG_TEXT_P(0);
char str[] = "http://www.ovh.com/intenal.html";

// Some vars which will used to create the composite output type
TupleDesc tupdesc;
char **values;
HeapTuple tuple;
AttInMetadata *attinmeta;
bool nulls[2];
int tuplen;

// Check NULLs values
if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
PG_RETURN_NULL();
}

url *ret = parse_url_exec(str);

if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
}
attinmeta = TupleDescGetAttInMetadata(tupdesc);

// ...
values = (char **) palloc(2 * sizeof(char *));

// Add datas into the values Datum
values[0] = (char *) ret->scheme;
values[1] = (char *) ret->host;

// Convert values into a composite type
memset(nulls, 0, sizeof(nulls));

// build tuple from datum array
tuple = BuildTupleFromCStrings(attinmeta, values);

// Return the composite type
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
-------------------------------------------------------

This code doesn't works better... :/

Le mercredi 21 octobre 2009 à 18:42 +0300, Heikki Linnakangas a écrit :
> Samuel ROZE wrote:
> > PG_FUNCTION_INFO_V1(parse_url_record);
> > Datum parse_url_record (PG_FUNCTION_ARGS)
> > {
> > // Vars about the params
> > //text *str2 = PG_GETARG_TEXT_P(0);
> > char str[] = "http://www.ovh.com/intenal.html";
> >
> > // Some vars which will used to create the composite output type
> > TupleDesc tupdesc;
> > Datum values[2]; // 8 values
> > HeapTuple tuple;
> > bool nulls[2];
> > int tuplen;
> >
> > // Check NULLs values
> > if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
> > PG_RETURN_NULL();
> > }
> >
> > url *ret = parse_url_exec(str);
> >
> > // Add datas into the values Datum
> > values[0] = PointerGetDatum(ret->scheme);
> > values[1] = PointerGetDatum(ret->host);
> >
> > // Convert values into a composite type
> > /*tuplen = tupdesc->natts;
> > nulls = palloc(tuplen * sizeof(bool));*/
> > memset(nulls, 0, sizeof(nulls));
> >
> > // build tuple from datum array
> > tuple = heap_form_tuple(tupdesc, values, nulls);
> > // Free null values
> > /*pfree(nulls);*/
> >
> > // Return the composite type
> > PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
> > }
>
> You haven't initialized tupdesc.
>
> BTW, there's a fine example in the manual:
> http://www.postgresql.org/docs/8.4/interactive/xfunc-c.html#AEN44968
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Samuel ROZE 2009-10-21 16:19:22 Re: URL Managment - C Function help
Previous Message Kevin Grittner 2009-10-21 16:12:16 Re: Application name patch - v2