Re: URL Managment - C Function help - Mailing list pgsql-hackers

From Samuel ROZE
Subject Re: URL Managment - C Function help
Date
Msg-id 1256155578.2845.55.camel@samuel-laptop
Whole thread Raw
In response to URL Managment - C Function help  (Samuel ROZE <samuel.roze@gmail.com>)
List pgsql-hackers
It is solved. I'll propose my work the next weeks. :-)

Regards,
Samuel.

Le mercredi 21 octobre 2009 à 17:31 +0200, Samuel ROZE a écrit :
> 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
> 
> 




pgsql-hackers by date:

Previous
From: Josh Berkus
Date:
Subject: Re: Controlling changes in plpgsql variable resolution
Next
From: Peter Eisentraut
Date:
Subject: alpha2 bundled -- please verify