Code

CData: Added casts from integer and numeric to cdata.
authorSebastian Harl <sh@tokkee.org>
Mon, 30 Apr 2012 12:52:34 +0000 (14:52 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 30 Apr 2012 12:52:34 +0000 (14:52 +0200)
Both casts are marked 'AS ASSIGNMENT'.

src/cdata.c
src/postrr.h.in
src/postrr.sql.in

index 6d202e20da6f525c63c470038f5015edcdd1c001..b47e3f87d169a107f63177df503192e3c6b722fb 100644 (file)
@@ -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 : */
 
index 95582db6eaf3a7d22f2f311f0fb2fb0cef1cf0d6..92025db3589fe438f457000ed41f985558e048c5 100644 (file)
@@ -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 */
 
index cf24fced1003d0d4f57d92165c5a19e37ad664d9..ffdb3a718350859625acbef996c37a5db570c733 100644 (file)
@@ -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;