URL Managment - C Function help

From: Samuel ROZE <samuel(dot)roze(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: URL Managment - C Function help
Date: 2009-10-21 15:31:45
Message-ID: 1256139105.2845.11.camel@samuel-laptop
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I'm writing two functions "parse_url_key" and "parse_url_record" which
will have one text argument and will return a record or a specific
column of it. Theses functions are calling "parse_url_exec" which parse
the URL. When theses function will works, i'll purpose them to
PostgreSQL community.

The problem is that they don't work fine... :/

Prototypes of function/struct used by them:
----------------------------------------------------
typedef struct url {
char *scheme;
char *user;
char *pass;
char *host;
unsigned short port;
char *path;
char *query;
char *fragment;
} url;

url *parse_url_exec (char* str);
----------------------------------------------------

The parse_url_key function:
----------------------------------------------------
PG_FUNCTION_INFO_V1(parse_url_key);
Datum parse_url_key (PG_FUNCTION_ARGS)
{
char str[] = "http://www.ovh.com/intenal.html";
//text *my_url = PG_GETARG_TEXT_P(0);
//char *char_url = DatumGetCString(my_url);

url *ret = parse_url_exec(str);

PG_RETURN_TEXT_P(ret->host);
}
----------------------------------------------------
Note: I'm using built-in strings to be sure that the recuperation
doesn't change anything..

This function works well:
----------------------------------------------------
postgres=# CREATE OR REPLACE FUNCTION parse_url_key(text) RETURNS text
AS '/home/samuel/parse_url.so', 'parse_url_key' LANGUAGE C;
CREATE FUNCTION
postgres=# SELECT parse_url_key('') as scheme;
scheme
------------
ww.ovh.com
(1 row)
----------------------------------------------------
Note: there's a little problem here but not important. :-)

The problem is that the other function, "parse_url_record" doesn't
return values ! The code is:
----------------------------------------------------
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));
}
----------------------------------------------------
Note: I'm just returning scheme and host fields for test, but others are
too completed by parse_url_exec.

It doesn't works fine:
----------------------------------------------------
postgres=# CREATE OR REPLACE FUNCTION parse_url_record(text) RETURNS
record AS '/home/samuel/parse_url.so', 'parse_url_record' LANGUAGE C;
CREATE FUNCTION
postgres=# SELECT * FROM parse_url_record('') as ("scheme" text, "host"
text);
scheme | host
--------+------
|
(1 row)
----------------------------------------------------

Is there anybody here who can help me ?

Thanks you very much !
Samuel ROZE.
http://www.d-sites.com

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dave Page 2009-10-21 15:39:20 Re: Client application name
Previous Message Andrew Dunstan 2009-10-21 15:29:35 Re: Application name patch - v2