From 5f0b12845c8dd3cf14e5533eac10e369897f4b2a Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 10 May 2012 15:10:58 +0200 Subject: [PATCH] CData: added a cast to double precision. The cast may only be done explicitly. --- src/cdata.c | 16 ++++++++++++++++ src/postrr.h.in | 2 ++ src/postrr.sql.in | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/src/cdata.c b/src/cdata.c index 32e8e4d..03fefbc 100644 --- a/src/cdata.c +++ b/src/cdata.c @@ -79,6 +79,7 @@ PG_FUNCTION_INFO_V1(cdata_typmodout); PG_FUNCTION_INFO_V1(cdata_to_cdata); PG_FUNCTION_INFO_V1(int32_to_cdata); +PG_FUNCTION_INFO_V1(cdata_to_float8); PG_FUNCTION_INFO_V1(cdata_update); @@ -331,6 +332,21 @@ int32_to_cdata(PG_FUNCTION_ARGS) PG_RETURN_CDATA_P(data); } /* int32_to_cdata */ +Datum +cdata_to_float8(PG_FUNCTION_ARGS) +{ + cdata_t *data; + + if (PG_NARGS() != 1) + ereport(ERROR, ( + errmsg("cdata_to_float8() expects one argument"), + errhint("Usage: cdata_to_float8(cdata)") + )); + + data = PG_GETARG_CDATA_P(0); + PG_RETURN_FLOAT8(data->value); +} /* cdata_to_float8 */ + Datum cdata_update(PG_FUNCTION_ARGS) { diff --git a/src/postrr.h.in b/src/postrr.h.in index c309ef0..798fd3f 100644 --- a/src/postrr.h.in +++ b/src/postrr.h.in @@ -134,6 +134,8 @@ Datum cdata_to_cdata(PG_FUNCTION_ARGS); Datum int32_to_cdata(PG_FUNCTION_ARGS); +Datum +cdata_to_float8(PG_FUNCTION_ARGS); /* aux. functions */ Datum diff --git a/src/postrr.sql.in b/src/postrr.sql.in index 9339e32..68c47df 100644 --- a/src/postrr.sql.in +++ b/src/postrr.sql.in @@ -280,6 +280,15 @@ CREATE CAST (integer AS cdata) WITH FUNCTION CData(integer, integer, boolean) AS ASSIGNMENT; +CREATE OR REPLACE FUNCTION Float8(cdata) + RETURNS double precision + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_to_float8' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE CAST (cdata AS double precision) + WITH FUNCTION Float8(cdata); + -- EXPLICIT + CREATE OR REPLACE FUNCTION CData_update(cdata, cdata) RETURNS cdata AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_update' -- 2.30.2