Cast user defined type to composite type - Mailing list pgsql-general

From whiplash
Subject Cast user defined type to composite type
Date
Msg-id 52624D12.1060306@bss.org.ua
Whole thread Raw
List pgsql-general
Hello!

I need use user defined type and operate it with low-level functions on
language C. In databasepreferred using composite type. Can i write more
performance casting that vector3_cast_vector3c?

// in C

typedef struct {
     x, y, z double;
} vector3;

input, output and other functions...

// in database

CREATE TYPE vector3
(
     internallength = 24,
     input = vector3_in,
     output = vector3_out,
     ...
);

CREATE TYPE vector3c AS
(
     x double precision,
     y double precision,
     z double precision
);

CREATE OR REPLACE FUNCTION vector3_cast_vector3c ( v0 vector3 )
RETURNS vector3c AS
$BODY$
DECLARE
     s text[];
     v vector3c;
BEGIN
     -- for example v0::text = '(0.0,1.0,0.0)'
     s := string_to_array ( trim ( BOTH '()' FROM v0::text ), ',' );
     v.x := s[1];
     v.y := s[2];
     v.z := s[3];
     /*
     or
     v.x := vector3_x ( v0 ); -- call function on C code
     v.y := vector3_y ( v0 );-- call function on C code
     v.z := vector3_z ( v0 ); -- call function on C code
*/
     RETURN v;
END
$BODY$
LANGUAGE plpgsql IMMUTABLE;

CREATE CAST ( vector3 AS vector3c ) WITH FUNCTION vector3_cast_vector3c
( v0 vector3 ) AS IMPLICIT;


pgsql-general by date:

Previous
From: Scott Ribe
Date:
Subject: Re: like & optimization
Next
From: Brian Crowell
Date:
Subject: Preserving the source code of views