Cast user defined type to composite type

From: whiplash <whiplash(at)bss(dot)org(dot)ua>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Cast user defined type to composite type
Date: 2013-10-19 09:12:50
Message-ID: 52624D12.1060306@bss.org.ua
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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;

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2013-10-19 22:26:10 Re: like & optimization
Previous Message David Johnston 2013-10-19 01:37:05 Re: Bug? Function with side effects not evaluated in CTE