forked from erthalion/jsonbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonbx.c
332 lines (269 loc) · 7.43 KB
/
jsonbx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "postgres.h"
#include "catalog/pg_type.h"
#include "utils/jsonb.h"
#include "utils/builtins.h"
#include "jsonbx.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(jsonb_pretty);
Datum jsonb_pretty(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(jsonb_concat);
Datum jsonb_concat(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(jsonb_delete);
Datum jsonb_delete(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(jsonb_delete_idx);
Datum jsonb_delete_idx(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(jsonb_delete_path);
Datum jsonb_delete_path(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(jsonb_set);
Datum jsonb_set(PG_FUNCTION_ARGS);
/*
* jsonb_pretty:
* Pretty-printed text for the jsonb
*/
Datum
jsonb_pretty(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB(0);
StringInfo str = makeStringInfo();
JsonbToCStringWorker(str, &jb->root, VARSIZE(jb), true);
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
}
/*
* jsonb_concat:
* Concatenation of two jsonb. There are few allowed combinations:
* - concatenation of two objects
* - concatenation of two arrays
* - concatenation of object and array
*
* The result for first two is new object and array accordingly.
* The last one return new array, which contains all elements from
* original array, and one extra element (which is actually
* other argument of this function with type jbvObject) at the first or last position.
*/
Datum
jsonb_concat(PG_FUNCTION_ARGS)
{
Jsonb *jb1 = PG_GETARG_JSONB(0);
Jsonb *jb2 = PG_GETARG_JSONB(1);
Jsonb *out = palloc(VARSIZE(jb1) + VARSIZE(jb2));
JsonbParseState *state = NULL;
JsonbValue *res;
JsonbIterator *it1, *it2;
/*
* If one of the jsonb is empty,
* just return other.
*/
if (JB_ROOT_COUNT(jb1) == 0)
{
memcpy(out, jb2, VARSIZE(jb2));
PG_RETURN_POINTER(out);
}
else if (JB_ROOT_COUNT(jb2) == 0)
{
memcpy(out, jb1, VARSIZE(jb1));
PG_RETURN_POINTER(out);
}
it1 = JsonbIteratorInit(&jb1->root);
it2 = JsonbIteratorInit(&jb2->root);
res = IteratorConcat(&it1, &it2, &state);
if (res == NULL || (res->type == jbvArray && res->val.array.nElems == 0) ||
(res->type == jbvObject && res->val.object.nPairs == 0) )
{
SET_VARSIZE(out, VARHDRSZ);
}
else
{
if (res->type == jbvArray && res->val.array.nElems > 1)
res->val.array.rawScalar = false;
out = JsonbValueToJsonb(res);
}
PG_RETURN_JSONB(out);
}
/*
* jsonb_delete:
* Return copy of jsonb with the specified item removed.
* Item is a one key or element from jsonb, specified by name.
* If there are many keys or elements with than name,
* the first one will be removed.
*/
Datum
jsonb_delete(PG_FUNCTION_ARGS)
{
Jsonb *in = PG_GETARG_JSONB(0);
text *key = PG_GETARG_TEXT_PP(1);
char *keyptr = VARDATA_ANY(key);
int keylen = VARSIZE_ANY_EXHDR(key);
JsonbParseState *state = NULL;
JsonbIterator *it;
uint32 r;
JsonbValue v, *res = NULL;
bool skipped = false;
if (JB_ROOT_IS_SCALAR(in))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot delete from scalar")));
if (JB_ROOT_COUNT(in) == 0)
{
PG_RETURN_JSONB(in);
}
it = JsonbIteratorInit(&in->root);
while((r = JsonbIteratorNext(&it, &v, false)) != 0)
{
if (!skipped && (r == WJB_ELEM || r == WJB_KEY) &&
(v.type == jbvString && keylen == v.val.string.len &&
memcmp(keyptr, v.val.string.val, keylen) == 0))
{
/* we should delete only one key/element */
skipped = true;
if (r == WJB_KEY)
{
/* skip corresponding value */
JsonbIteratorNext(&it, &v, true);
}
continue;
}
res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
}
Assert(res != NULL);
PG_RETURN_JSONB(JsonbValueToJsonb(res));
}
/*
* jsonb_delete_idx:
* Return a copy of jsonb withour specified items.
* Delete key (only from the top level of object) or element from jsonb by index (idx).
* Negative idx value is supported, and it implies the countdown from the last key/element.
* If idx is more, than numbers of keys/elements, or equal - nothing will be deleted.
* If idx is negative and -idx is more, than number of keys/elements - the last one will be deleted.
*
* TODO: take care about nesting values.
*/
Datum
jsonb_delete_idx(PG_FUNCTION_ARGS)
{
Jsonb *in = PG_GETARG_JSONB(0);
int idx = PG_GETARG_INT32(1);
JsonbParseState *state = NULL;
JsonbIterator *it;
uint32 r, i = 0, n;
JsonbValue v, *res = NULL;
if (JB_ROOT_IS_SCALAR(in))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot delete from scalar")));
if (JB_ROOT_COUNT(in) == 0)
{
PG_RETURN_JSONB(in);
}
it = JsonbIteratorInit(&in->root);
r = JsonbIteratorNext(&it, &v, false);
if (r == WJB_BEGIN_ARRAY)
n = v.val.array.nElems;
else
n = v.val.object.nPairs;
if (idx < 0)
{
if (-idx > n)
idx = n;
else
idx = n + idx;
}
if (idx >= n)
{
PG_RETURN_JSONB(in);
}
pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
while((r = JsonbIteratorNext(&it, &v, true)) != 0)
{
if (r == WJB_ELEM || r == WJB_KEY)
{
if (i++ == idx)
{
if (r == WJB_KEY)
JsonbIteratorNext(&it, &v, true); /* skip value */
continue;
}
}
res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
}
Assert (res != NULL);
PG_RETURN_JSONB(JsonbValueToJsonb(res));
}
/*
* jsonb_set:
* Replace/create value of jsonb key or jsonb element, which can be found by the specified path.
* Path must be replesented as an array of key names or indexes. If indexes will be used,
* the same rules implied as for jsonb_delete_idx (negative indexing and edge cases)
*/
Datum
jsonb_set(PG_FUNCTION_ARGS)
{
Jsonb *in = PG_GETARG_JSONB(0);
ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
Jsonb *newval = PG_GETARG_JSONB(2);
bool create = PG_GETARG_BOOL(3);
JsonbValue *res = NULL;
Datum *path_elems;
bool *path_nulls;
int path_len;
JsonbIterator *it;
JsonbParseState *st = NULL;
if (ARR_NDIM(path) > 1)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("wrong number of array subscripts")));
if (JB_ROOT_IS_SCALAR(in))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot set path in scalar")));
if (JB_ROOT_COUNT(in) == 0 && !create)
{
PG_RETURN_JSONB(in);
}
deconstruct_array(path, TEXTOID, -1, false, 'i',
&path_elems, &path_nulls, &path_len);
if (path_len == 0)
{
PG_RETURN_JSONB(in);
}
it = JsonbIteratorInit(&in->root);
res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, newval, create);
Assert (res != NULL);
PG_RETURN_JSONB(JsonbValueToJsonb(res));
}
/*
* jsonb_delete_path:
*/
Datum
jsonb_delete_path(PG_FUNCTION_ARGS)
{
Jsonb *in = PG_GETARG_JSONB(0);
ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
JsonbValue *res = NULL;
Datum *path_elems;
bool *path_nulls;
int path_len;
JsonbIterator *it;
JsonbParseState *st = NULL;
if (ARR_NDIM(path) > 1)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("wrong number of array subscripts")));
if (JB_ROOT_IS_SCALAR(in))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot delete path in scalar")));
if (JB_ROOT_COUNT(in) == 0)
{
PG_RETURN_JSONB(in);
}
deconstruct_array(path, TEXTOID, -1, false, 'i',
&path_elems, &path_nulls, &path_len);
if (path_len == 0)
{
PG_RETURN_JSONB(in);
}
it = JsonbIteratorInit(&in->root);
res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, NULL, false);
Assert (res != NULL);
PG_RETURN_JSONB(JsonbValueToJsonb(res));
}