Search Postgresql Archives

Cast user defined type to composite type

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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;


--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux