06.09.2014 Views

Bases de datos: Software libre - Universitat Oberta de Catalunya

Bases de datos: Software libre - Universitat Oberta de Catalunya

Bases de datos: Software libre - Universitat Oberta de Catalunya

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

© FUOC • 71Z799014MO 69 <strong>Bases</strong> <strong>de</strong> <strong>datos</strong> en PostgreSQL<br />

PG_FUNCTION_INFO_V1(complex_in);<br />

Datum<br />

complex_in(PG_FUNCTION_ARGS)<br />

{<br />

char *str = PG_GETARG_CSTRING(0);<br />

double x,<br />

y;<br />

Complex *result;<br />

if (sscanf(str, “ ( %lf , %lf )”, &x, &y) != 2)<br />

ereport(ERROR,<br />

(errco<strong>de</strong>(ERRCODE_INVALID_TEXT_REPRESENTATION),<br />

errmsg(“invalid input syntax for complex: \”%s\”“, str)));<br />

}<br />

result = (Complex *) palloc(sizeof(Complex));<br />

result->x = x;<br />

result->y = y;<br />

PG_RETURN_POINTER(result);<br />

PG_FUNCTION_INFO_V1(complex_out);<br />

Datum<br />

complex_out(PG_FUNCTION_ARGS)<br />

{<br />

Complex *complex = (Complex *) PG_GETARG_POINTER(0);<br />

char *result;<br />

}<br />

result = (char *) palloc(100);<br />

snprintf(result, 100, “(%g,%g)”, complex->x, complex->y);<br />

PG_RETURN_CSTRING(result);<br />

Ahora estamos en condiciones <strong>de</strong> <strong>de</strong>finir las funciones, y el tipo:<br />

create function complex_in(cstring)<br />

returns complex<br />

as ‘filename’<br />

language c immutable strict;<br />

create function complex_out(complex)<br />

returns cstring<br />

as ‘filename’<br />

language c immutable strict;<br />

create type complex (<br />

internallength = 16,<br />

input = complex_in,<br />

output = complex_out,<br />

alignment = double<br />

);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!