PostgreSQL Source Code: contrib/jsonb_plperl/jsonb_plperl.c Source File (original) (raw)

2

3#include <math.h>

4

7#include "utils/fmgrprotos.h"

9

11 .name = "jsonb_plperl",

12 .version = PG_VERSION

13);

14

17

18

19static SV *

21{

23

24 switch (jbv->type)

25 {

28

30 {

33 SV *result = newSVnv(SvNV(cstr2sv(str)));

34

36 return result;

37 }

38

40 {

42 jbv->val.string.len);

44

46 return result;

47 }

48

51

53 return newSV(0);

54

55 default:

56 elog(ERROR, "unexpected jsonb value type: %d", jbv->type);

57 return NULL;

58 }

59}

60

61static SV *

63{

68

71

72 switch (r)

73 {

75 if (v.val.array.rawScalar)

76 {

78

82 elog(ERROR, "unexpected jsonb token: %d", r);

83

85 }

86 else

87 {

88 AV *av = newAV();

89

91 {

94 }

95

96 return newRV((SV *) av);

97 }

98

100 {

101 HV *hv = newHV();

102

104 {

106 {

107

109

111 {

113

114 (void) hv_store(hv,

115 v.val.string.val, v.val.string.len,

117 }

118 }

119 }

120

121 return newRV((SV *) hv);

122 }

123

124 default:

125 elog(ERROR, "unexpected jsonb token: %d", r);

126 return NULL;

127 }

128}

129

132{

134 SSize_t pcount = av_len(in) + 1;

135 SSize_t i;

136

138

139 for (i = 0; i < pcount; i++)

140 {

141 SV **value = av_fetch(in, i, FALSE);

142

145 }

146

148}

149

152{

156 char *kstr;

157 I32 klen;

158

160

162

163 (void) hv_iterinit(obj);

164

165 while ((val = hv_iternextsv(obj, &kstr, &klen)))

166 {

167 key.val.string.val = pnstrdup(kstr, klen);

168 key.val.string.len = klen;

171 }

172

174}

175

178{

181

182

183 while (SvROK(in))

184 in = SvRV(in);

185

186 switch (SvTYPE(in))

187 {

188 case SVt_PVAV:

190

191 case SVt_PVHV:

193

194 default:

195 if (!SvOK(in))

196 {

198 }

199 else if (SvUOK(in))

200 {

201

202

203

204

205

206

208

210 out.val.numeric =

215 }

216 else if (SvIOK(in))

217 {

218 IV ival = SvIV(in);

219

222 }

223 else if (SvNOK(in))

224 {

225 double nval = SvNV(in);

226

227

228

229

230

231

232

233 if (isinf(nval))

235 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),

236 errmsg("cannot convert infinity to jsonb")));

237 if (isnan(nval))

239 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),

240 errmsg("cannot convert NaN to jsonb")));

241

243 out.val.numeric =

246 }

247 else if (SvPOK(in))

248 {

251 out.val.string.len = strlen(out.val.string.val);

252 }

253 else

254 {

255

256

257

258

260 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),

261 errmsg("cannot transform this Perl type to jsonb")));

262 return NULL;

263 }

264 }

265

266

267 return *jsonb_state

270}

271

272

274

277{

281

283}

284

285

287

290{

296

298}

Datum float8_numeric(PG_FUNCTION_ARGS)

Numeric int64_to_numeric(int64 val)

Datum numeric_out(PG_FUNCTION_ARGS)

Datum numeric_in(PG_FUNCTION_ARGS)

int errcode(int sqlerrcode)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

Datum Float8GetDatum(float8 X)

#define PG_GETARG_POINTER(n)

#define DirectFunctionCall1(func, arg1)

#define DirectFunctionCall3(func, arg1, arg2, arg3)

#define PG_RETURN_JSONB_P(x)

#define PG_GETARG_JSONB_P(x)

static SV * JsonbValue_to_SV(JsonbValue *jbv)

static SV * Jsonb_to_SV(JsonbContainer *jsonb)

PG_FUNCTION_INFO_V1(jsonb_to_plperl)

static JsonbValue * HV_to_JsonbValue(HV *obj, JsonbParseState **jsonb_state)

static JsonbValue * SV_to_JsonbValue(SV *obj, JsonbParseState **ps, bool is_elem)

Datum jsonb_to_plperl(PG_FUNCTION_ARGS)

static JsonbValue * AV_to_JsonbValue(AV *in, JsonbParseState **jsonb_state)

Datum plperl_to_jsonb(PG_FUNCTION_ARGS)

PG_MODULE_MAGIC_EXT(.name="jsonb_plperl",.version=PG_VERSION)

JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)

JsonbIterator * JsonbIteratorInit(JsonbContainer *container)

JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)

Jsonb * JsonbValueToJsonb(JsonbValue *val)

void pfree(void *pointer)

char * pnstrdup(const char *in, Size len)

static Numeric DatumGetNumeric(Datum X)

static Datum NumericGetDatum(Numeric X)

static char * sv2cstr(SV *sv)

static SV * cstr2sv(const char *str)

static Datum PointerGetDatum(const void *X)

static Datum ObjectIdGetDatum(Oid X)

static char * DatumGetCString(Datum X)

static Datum CStringGetDatum(const char *X)

static Datum Int32GetDatum(int32 X)