diff options
Diffstat (limited to 'contrib/jsonb_plperl/jsonb_plperl.c')
-rw-r--r-- | contrib/jsonb_plperl/jsonb_plperl.c | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/contrib/jsonb_plperl/jsonb_plperl.c b/contrib/jsonb_plperl/jsonb_plperl.c index ad9e65516f..837bae2ab5 100644 --- a/contrib/jsonb_plperl/jsonb_plperl.c +++ b/contrib/jsonb_plperl/jsonb_plperl.c @@ -1,11 +1,14 @@ #include "postgres.h" +#include <float.h> +#include <math.h> + +/* Defined by Perl */ #undef _ #include "fmgr.h" #include "plperl.h" #include "plperl_helpers.h" - #include "utils/jsonb.h" #include "utils/fmgrprotos.h" @@ -188,46 +191,51 @@ SV_to_JsonbValue(SV *in, JsonbParseState **jsonb_state, bool is_elem) case SVt_PVHV: return HV_to_JsonbValue((HV *) in, jsonb_state); - case SVt_NV: - case SVt_IV: + case SVt_NULL: + out.type = jbvNull; + break; + + default: + if (SvIOK(in)) { - char *str = sv2cstr(in); + IV ival = SvIV(in); - /* - * Use case-insensitive comparison because infinity - * representation varies across Perl versions. - */ - if (pg_strcasecmp(str, "inf") == 0) + out.type = jbvNumeric; + out.val.numeric = + DatumGetNumeric(DirectFunctionCall1(int8_numeric, + Int64GetDatum((int64) ival))); + } + else if (SvNOK(in)) + { + double nval = SvNV(in); + + if (isinf(nval)) ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errmsg("cannot convert infinite value to jsonb")))); out.type = jbvNumeric; - out.val.numeric = DatumGetNumeric(DirectFunctionCall3(numeric_in, - CStringGetDatum(str), 0, -1)); + out.val.numeric = + DatumGetNumeric(DirectFunctionCall1(float8_numeric, + Float8GetDatum(nval))); + } + else if (SvPOK(in)) + { + out.type = jbvString; + out.val.string.val = sv2cstr(in); + out.val.string.len = strlen(out.val.string.val); + } + else + { + /* + * XXX It might be nice if we could include the Perl type in + * the error message. + */ + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + (errmsg("cannot transform this Perl type to jsonb")))); + return NULL; } - break; - - case SVt_NULL: - out.type = jbvNull; - break; - - case SVt_PV: /* string */ - out.type = jbvString; - out.val.string.val = sv2cstr(in); - out.val.string.len = strlen(out.val.string.val); - break; - - default: - - /* - * XXX It might be nice if we could include the Perl type in the - * error message. - */ - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - (errmsg("cannot transform this Perl type to jsonb")))); - return NULL; } /* Push result into 'jsonb_state' unless it is a raw scalar. */ |