]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blobdiff - jsonb_opx.c
Make a couple of minor changes after initial viewing of work at https://github.com...
[postgresql/pg_jsonb_opx] / jsonb_opx.c
index 43f3cbf30c36a402d17aadeb4a947cd83d7fe21c..a27de35ee977ff133d709ad7950f9e524de51d67 100755 (executable)
@@ -37,11 +37,11 @@ Datum
 jsonb_delete_text(PG_FUNCTION_ARGS)
 {
     /* general loops */
-       int i;
+    int i;
 
     /* pointers to incoming jsonb and text[] data */
     Jsonb *input_jsonb = PG_GETARG_JSONB(0);
-       ArrayType *input_array = PG_GETARG_ARRAYTYPE_P(1);
+    ArrayType *input_array = PG_GETARG_ARRAYTYPE_P(1);
     
     /* pointers to return jsonb_value data and state to be converted to jsonb on return */
     JsonbParseState *state = NULL;
@@ -59,10 +59,10 @@ jsonb_delete_text(PG_FUNCTION_ARGS)
     int32 nest_level = 0;
     int32 array_level = 0;
 
-       /* array element variables for use during deconstruction */
-       Datum *datums;
-       bool *nulls;
-       int32 count;
+    /* array element variables for use during deconstruction */
+    Datum *datums;
+    bool *nulls;
+    int32 count;
 
     /* individual array values values from incoming text[] */
     text *array_element_text;
@@ -87,6 +87,10 @@ jsonb_delete_text(PG_FUNCTION_ARGS)
     deconstruct_array(input_array, TEXTOID, -1, false, 'i', 
                        &datums, &nulls, &count);
 
+    /* if the array is empty there's no work to do so return the input value */ 
+    if (count == 0) 
+        PG_RETURN_JSONB(input_jsonb);
+
     /* first check to make sure at least one key exists - this is potentially just extra unwanted work */
     for (i=0; i<count; i++)
     {
@@ -233,6 +237,19 @@ jsonb_delete_jsonb(PG_FUNCTION_ARGS)
     /* pointer to lookup on input_jsonb_b */
     JsonbValue *jsonb_lookup_value = NULL;
     
+    /* check that supplied jsonb isn't non object, i.e. scalar or array */
+    if (!JB_ROOT_IS_OBJECT(input_jsonb_a) || !JB_ROOT_IS_OBJECT(input_jsonb_b))
+        ereport(ERROR,
+            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                errmsg("cannot call on a non-object")));
+
+    /*
+     * check if either supplied jsonb is empty and return the other if so
+     * this idea was copied from https://github.com/erthalion/jsonbx/blob/master/jsonbx.c
+     */
+    if (JB_ROOT_COUNT(input_jsonb_b) == 0)
+        PG_RETURN_JSONB(input_jsonb_a);
+
     jsonb_iterator = JsonbIteratorInit(&input_jsonb_a->root);
 
     while ((jsonb_iterator_token = JsonbIteratorNext(&jsonb_iterator, &jsonb_iterator_value, skip_nested)) != WJB_DONE) {
@@ -362,6 +379,15 @@ jsonb_concat_jsonb(PG_FUNCTION_ARGS)
         ereport(ERROR,
             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("cannot call on a non-object")));
+    
+    /*
+     * check if either supplied jsonb is empty and return the other if so
+     * this idea was copied from https://github.com/erthalion/jsonbx/blob/master/jsonbx.c
+     */
+    if (JB_ROOT_COUNT(input_jsonb_a) == 0)
+        PG_RETURN_JSONB(input_jsonb_b);
+    else if (JB_ROOT_COUNT(input_jsonb_b) == 0)
+        PG_RETURN_JSONB(input_jsonb_a);
 
     /*
      * The following is essentially a cut 'n shut job; discarding the closing root