From: Sebastian Harl Date: Mon, 30 Apr 2012 12:52:34 +0000 (+0200) Subject: CData: Added casts from integer and numeric to cdata. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fd01c2e8b891abe2e801e406555642284e6eef36;p=postrr.git CData: Added casts from integer and numeric to cdata. Both casts are marked 'AS ASSIGNMENT'. --- diff --git a/src/cdata.c b/src/cdata.c index 6d202e2..b47e3f8 100644 --- a/src/cdata.c +++ b/src/cdata.c @@ -78,6 +78,7 @@ PG_FUNCTION_INFO_V1(cdata_typmodin); PG_FUNCTION_INFO_V1(cdata_typmodout); PG_FUNCTION_INFO_V1(cdata_to_cdata); +PG_FUNCTION_INFO_V1(int32_to_cdata); /* * public API @@ -296,5 +297,37 @@ cdata_to_cdata(PG_FUNCTION_ARGS) PG_RETURN_CDATA_P(data); } /* cdata_to_cdata */ +Datum +int32_to_cdata(PG_FUNCTION_ARGS) +{ + int32 i_val; + int32 typmod; + + cdata_t *data; + + if (PG_NARGS() != 3) + ereport(ERROR, ( + errmsg("int32_to_cdata() expects three arguments"), + errhint("Usage: int32_to_cdata" + "(integer, typmod, is_explicit)") + )); + + i_val = PG_GETARG_INT32(0); + typmod = PG_GETARG_INT32(1); + + data = (cdata_t *)palloc0(sizeof(*data)); + + data->value = (float8)i_val; + data->undef_num = 0; + data->val_num = 1; + + if (typmod >= 0) + data->cf = typmod; + else + data->cf = CF_AVG; + + PG_RETURN_CDATA_P(data); +} /* int32_to_cdata */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/postrr.h.in b/src/postrr.h.in index 95582db..92025db 100644 --- a/src/postrr.h.in +++ b/src/postrr.h.in @@ -128,6 +128,8 @@ cdata_typmodout(PG_FUNCTION_ARGS); /* casts */ Datum cdata_to_cdata(PG_FUNCTION_ARGS); +Datum +int32_to_cdata(PG_FUNCTION_ARGS); #endif /* ! POSTRR_H */ diff --git a/src/postrr.sql.in b/src/postrr.sql.in index cf24fce..ffdb3a7 100644 --- a/src/postrr.sql.in +++ b/src/postrr.sql.in @@ -248,6 +248,19 @@ CREATE CAST (cdata AS cdata) WITH FUNCTION CData(cdata, integer, boolean) AS IMPLICIT; +CREATE CAST (numeric AS cdata) + WITH INOUT + AS ASSIGNMENT; + +CREATE OR REPLACE FUNCTION CData(integer, integer, boolean) + RETURNS cdata + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'int32_to_cdata' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE CAST (integer AS cdata) + WITH FUNCTION CData(integer, integer, boolean) + AS ASSIGNMENT; + COMMIT; SET client_min_messages TO DEFAULT;