]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - jsonb_utilsx.c
Add missing files
[postgresql/pg_jsonb_opx] / jsonb_utilsx.c
1 /*
2  * jsonb_utilsx.c
3  *     Test jsonb delete and concatenate operator functions for 9.4
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>
8  *
9  * This is purely experimentation and will contain many errors and bad form
10  * DO NOT USE ON PRODUCTION SYSTEMS.
11  *
12  */
13
14 #include "postgres.h"
15 #include "utils/jsonb.h"
16 #include "catalog/pg_type.h"
17 #include "utils/builtins.h"
18 #include "jsonb_opx.h"
19
20 JsonbValue * 
21 pushJsonbBinary(JsonbParseState *pstate, JsonbContainer *jsonb_container)
22 {
23     JsonbIterator *jsonb_iterator;
24     JsonbValue jsonb_iterator_value;
25     int32 jsonb_iterator_token;
26     JsonbValue *return_jsonb_value = NULL;
27
28     jsonb_iterator = JsonbIteratorInit((void *)jsonb_container);
29     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, false)) != WJB_DONE)
30     {
31         return_jsonb_value = pushJsonbValue(&pstate, jsonb_iterator_token, &jsonb_iterator_value);
32     }
33     return return_jsonb_value;
34 }
35
36 Jsonb * 
37 jsonbModifyPath(Jsonb *jsonb_a, ArrayType *array_path, Jsonb *jsonb_b)
38 {
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;
42
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;
48
49     JsonbIterator *jsonb_replacement_iterator;
50     JsonbValue jsonb_replacement_iterator_value;
51     int32 jsonb_replacement_iterator_token;
52     
53     /* 
54      * array element variables for use during deconstruction 
55      * count is the depth we will be looking from the first matching key
56      */
57     Datum *datums;
58     bool *nulls;
59     int32 count; 
60
61     /* the current key we are looking for, starting with the first key */
62     text *key_on = NULL;
63     //text *key_on;
64     int32 index_on = 0; 
65     int32 nest_level = 0;
66     int32 array_level = 0;
67     int32 skip_level = 0;
68     int32 push_nest_level = 0;
69     bool push = true;
70
71     /* assert input_array is a text array type */
72     Assert(ARR_ELEMTYPE(array_path) == TEXTOID);
73
74     /* check input_array is one-dimensional */
75     if (ARR_NDIM(array_path) > 1)
76         ereport(ERROR,
77             (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
78                 errmsg("1 dimensional text array expected")));
79
80     /* deconstruct array elements */
81     deconstruct_array(array_path, TEXTOID, -1, false, 'i',
82                         &datums, &nulls, &count);
83
84     /* can't follow key based paths on non objects */ 
85     if (!JB_ROOT_IS_OBJECT(jsonb_a) && (count > 1))
86         ereport(ERROR,
87             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
88                 errmsg("cannot call with path deeper than 1 on a non-object")));
89
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))
92         return jsonb_a;
93
94     if (!nulls[index_on])
95         key_on = DatumGetTextP(datums[index_on]);
96
97     jsonb_iterator = JsonbIteratorInit(&jsonb_a->root);
98
99     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, false)) != WJB_DONE)
100     {    
101         push = true;
102
103         switch (jsonb_iterator_token)
104         {
105             case WJB_BEGIN_ARRAY:
106                 array_level++;
107             break;
108             case WJB_BEGIN_OBJECT:
109                nest_level++;
110             break;
111             case WJB_ELEM:
112             case WJB_KEY:
113                 /*
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
117                  */
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)))
120                 {
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)))
124                     {   
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) 
127                         {
128                             index_on++;
129                             if (!nulls[index_on])
130                                 key_on = DatumGetTextP(datums[index_on]);
131                             else
132                                 key_on = NULL;
133                         }
134                         /* if we have reached the last index, the we can modify this level */ 
135                         else 
136                         {
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);
140
141                                 while ((jsonb_replacement_iterator_token = JsonbIteratorNext(&jsonb_replacement_iterator, &jsonb_replacement_iterator_value, false)) != WJB_DONE)
142                                 {
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)))) 
148                                     {
149                                         push_nest_level++;
150                                     }
151
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))
156                                     {
157                                         return_jsonb_value = pushJsonbValue(&state, jsonb_replacement_iterator_token, &jsonb_replacement_iterator_value);
158                                     }
159
160                                     if (((jsonb_last_token == WJB_BEGIN_ARRAY) || 
161                                         (jsonb_last_token == WJB_VALUE)) && 
162                                         (jsonb_replacement_iterator_token == WJB_END_ARRAY))
163                                     {
164                                         push_nest_level--;
165                                     }
166                                     else if (((jsonb_last_token == WJB_BEGIN_OBJECT) || 
167                                         (jsonb_last_token == WJB_VALUE)) && 
168                                         (jsonb_replacement_iterator_token == WJB_END_OBJECT))
169                                     {
170                                         push_nest_level--;
171                                     }
172                                 }
173                             }
174                             if (jsonb_iterator_token == WJB_ELEM) 
175                                 push = false;
176                             else
177                                 skip_level = nest_level;
178                         }
179                     } 
180                 }
181         }
182
183         if (push && (skip_level == 0 || nest_level < skip_level)) 
184         {
185             return_jsonb_value = pushJsonbValue(&state, jsonb_iterator_token, &jsonb_iterator_value);
186             jsonb_last_token = jsonb_iterator_token;
187         }
188
189         switch (jsonb_iterator_token) 
190         {
191             case WJB_END_OBJECT:
192                 nest_level--;
193                 if (skip_level == nest_level && array_level == 0)
194                     skip_level = 0;
195             break;
196             case WJB_END_ARRAY:
197                 array_level--;
198                 if (skip_level == nest_level && array_level == 0)
199                     skip_level = 0;
200             break;
201             case WJB_VALUE:
202                 if (skip_level == nest_level)
203                     skip_level = 0;
204         }
205     }
206
207     return JsonbValueToJsonb(return_jsonb_value);
208 }