Code

CData: Added casts from integer and numeric to cdata.
[postrr.git] / src / cdata.c
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 : */