3 * Test jsonb delete and concatenate operator functions for 9.4
5 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
7 * Author: Glyn Astill <glyn@8kb.co.uk>
9 * This is purely experimentation and will contain many errors and bad form
10 * DO NOT USE ON PRODUCTION SYSTEMS.
15 #include "utils/jsonb.h"
16 #include "catalog/pg_type.h"
17 #include "utils/builtins.h"
18 #include "jsonb_opx.h"
21 pushJsonbBinary(JsonbParseState *pstate, JsonbContainer *jsonb_container)
23 JsonbIterator *jsonb_iterator;
24 JsonbValue jsonb_iterator_value;
25 int32 jsonb_iterator_token;
26 JsonbValue *return_jsonb_value = NULL;
28 jsonb_iterator = JsonbIteratorInit((void *)jsonb_container);
29 while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, false)) != WJB_DONE)
31 return_jsonb_value = pushJsonbValue(&pstate, jsonb_iterator_token, &jsonb_iterator_value);
33 return return_jsonb_value;
37 jsonbModifyPath(Jsonb *jsonb_a, ArrayType *array_path, Jsonb *jsonb_b)
39 /* pointers to return jsonb value data and state to be converted to jsonb on return */
40 JsonbParseState *state = NULL;
41 JsonbValue *return_jsonb_value = NULL;
43 /* pointer to iterator for input jsonb */
44 JsonbIterator *jsonb_iterator;
45 JsonbValue jsonb_iterator_value;
46 int32 jsonb_iterator_token;
47 int32 jsonb_last_token = 0;
49 JsonbIterator *jsonb_replacement_iterator;
50 JsonbValue jsonb_replacement_iterator_value;
51 int32 jsonb_replacement_iterator_token;
54 * array element variables for use during deconstruction
55 * count is the depth we will be looking from the first matching key
61 /* the current key we are looking for, starting with the first key */
66 int32 array_level = 0;
68 int32 push_nest_level = 0;
71 /* assert input_array is a text array type */
72 Assert(ARR_ELEMTYPE(array_path) == TEXTOID);
74 /* check input_array is one-dimensional */
75 if (ARR_NDIM(array_path) > 1)
77 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
78 errmsg("1 dimensional text array expected")));
80 /* deconstruct array elements */
81 deconstruct_array(array_path, TEXTOID, -1, false, 'i',
82 &datums, &nulls, &count);
84 /* can't follow key based paths on non objects */
85 if (!JB_ROOT_IS_OBJECT(jsonb_a) && (count > 1))
87 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
88 errmsg("cannot call with path deeper than 1 on a non-object")));
90 /* if the array is empty there's no work to do so return the input value */
91 if ((count == 0) || (JB_ROOT_COUNT(jsonb_a) == 0))
95 key_on = DatumGetTextP(datums[index_on]);
97 jsonb_iterator = JsonbIteratorInit(&jsonb_a->root);
99 while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, false)) != WJB_DONE)
103 switch (jsonb_iterator_token)
105 case WJB_BEGIN_ARRAY:
108 case WJB_BEGIN_OBJECT:
114 * If we are not skipping the current nest level check that the nesting level follows the array index
115 * and if it does check the current key. For array elements only check the root level (array_level==1).
116 * If there is no match we just keep on pushing
118 if (skip_level == 0 && ((jsonb_iterator_token == WJB_KEY && nest_level-1 == index_on && array_level == 0) ||
119 (jsonb_iterator_token == WJB_ELEM && nest_level == 0 && array_level == 1)))
121 if ((jsonb_iterator_value.type == jbvNull && key_on == NULL) ||
122 (key_on != NULL && (jsonb_iterator_value.val.string.len == VARSIZE_ANY_EXHDR(key_on)) &&
123 (memcmp(jsonb_iterator_value.val.string.val, VARDATA_ANY(key_on), jsonb_iterator_value.val.string.len) == 0)))
125 /* if we have not yet reached the last index in the array / key chain move on and check the next */
126 if (index_on < count-1)
129 if (!nulls[index_on])
130 key_on = DatumGetTextP(datums[index_on]);
134 /* if we have reached the last index, the we can modify this level */
137 /* if jsonb_b is not null unwrap it with iterator into replacement_jsonb_value */
138 if (jsonb_b != NULL) {
139 jsonb_replacement_iterator = JsonbIteratorInit(&jsonb_b->root);
141 while ((jsonb_replacement_iterator_token = JsonbIteratorNext(&jsonb_replacement_iterator, &jsonb_replacement_iterator_value, false)) != WJB_DONE)
143 if (((jsonb_last_token == jsonb_replacement_iterator_token) &&
144 (jsonb_last_token != WJB_VALUE)) ||
145 ((jsonb_last_token == WJB_VALUE) &&
146 ((jsonb_replacement_iterator_token == WJB_BEGIN_OBJECT) ||
147 (jsonb_replacement_iterator_token == WJB_BEGIN_ARRAY))))
152 if ((jsonb_replacement_iterator_token == WJB_KEY) ||
153 (jsonb_replacement_iterator_token == WJB_VALUE) ||
154 (jsonb_replacement_iterator_token == WJB_ELEM) ||
155 (push_nest_level != 1))
157 return_jsonb_value = pushJsonbValue(&state, jsonb_replacement_iterator_token, &jsonb_replacement_iterator_value);
160 if (((jsonb_last_token == WJB_BEGIN_ARRAY) ||
161 (jsonb_last_token == WJB_VALUE)) &&
162 (jsonb_replacement_iterator_token == WJB_END_ARRAY))
166 else if (((jsonb_last_token == WJB_BEGIN_OBJECT) ||
167 (jsonb_last_token == WJB_VALUE)) &&
168 (jsonb_replacement_iterator_token == WJB_END_OBJECT))
174 if (jsonb_iterator_token == WJB_ELEM)
177 skip_level = nest_level;
183 if (push && (skip_level == 0 || nest_level < skip_level))
185 return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
186 jsonb_last_token = jsonb_iterator_token;
189 switch (jsonb_iterator_token)
193 if (skip_level == nest_level && array_level == 0)
198 if (skip_level == nest_level && array_level == 0)
202 if (skip_level == nest_level)
207 return JsonbValueToJsonb(return_jsonb_value);