8
8
*
9
9
*/
10
10
11
+ #include "postgres.h"
12
+ #include "miscadmin.h"
11
13
#include "access/compression.h"
12
14
#include "utils/builtins.h"
13
15
#include "utils/json_generic.h"
@@ -19,15 +21,88 @@ static Json *JsonExpand(Json *tmp, Datum value, bool freeValue,
19
21
20
22
JsonContainerOps jsonvContainerOps ;
21
23
22
- #if 0
23
- static JsonValue *
24
- JsonValueCopy (JsonValue * val )
24
+ JsonValue *
25
+ JsonValueCopy (JsonValue * res , const JsonValue * val )
25
26
{
26
- JsonValue * copy = palloc (sizeof (JsonValue ));
27
- memcpy (copy , val , sizeof (JsonValue ));
28
- return copy ;
27
+ check_stack_depth ();
28
+
29
+ if (!res )
30
+ res = (JsonValue * ) palloc (sizeof (JsonValue ));
31
+
32
+ res -> type = val -> type ;
33
+
34
+ switch (val -> type )
35
+ {
36
+ case jbvNull :
37
+ break ;
38
+
39
+ case jbvBool :
40
+ res -> val .boolean = val -> val .boolean ;
41
+ break ;
42
+
43
+ case jbvString :
44
+ { /* copy string values in the current context */
45
+ char * buf = palloc (val -> val .string .len + 1 );
46
+ memcpy (buf , val -> val .string .val , val -> val .string .len );
47
+ buf [val -> val .string .len ] = 0 ;
48
+ res -> val .string .val = buf ;
49
+ res -> val .string .len = val -> val .string .len ;
50
+ break ;
51
+ }
52
+
53
+ case jbvNumeric :
54
+ /* same for numeric */
55
+ res -> val .numeric =
56
+ DatumGetNumeric (DirectFunctionCall1 (numeric_uplus ,
57
+ NumericGetDatum (val -> val .numeric )));
58
+ break ;
59
+
60
+ case jbvArray :
61
+ {
62
+ int i ;
63
+
64
+ res -> val .array = val -> val .array ;
65
+ res -> val .array .elems = (JsonValue * )
66
+ palloc (sizeof (JsonValue ) * val -> val .array .nElems );
67
+
68
+ for (i = 0 ; i < val -> val .array .nElems ; i ++ )
69
+ JsonValueCopy (& res -> val .array .elems [i ],
70
+ & val -> val .array .elems [i ]);
71
+
72
+ break ;
73
+ }
74
+
75
+ case jbvObject :
76
+ {
77
+ int i ;
78
+
79
+ res -> val .object = val -> val .object ;
80
+ res -> val .object .pairs = (JsonPair * )
81
+ palloc (sizeof (JsonPair ) * val -> val .object .nPairs );
82
+
83
+ for (i = 0 ; i < val -> val .object .nPairs ; i ++ )
84
+ {
85
+ res -> val .object .pairs [i ].order = val -> val .object .pairs [i ].order ;
86
+ JsonValueCopy (& res -> val .object .pairs [i ].key ,
87
+ & val -> val .object .pairs [i ].key );
88
+ JsonValueCopy (& res -> val .object .pairs [i ].value ,
89
+ & val -> val .object .pairs [i ].value );
90
+ }
91
+
92
+ break ;
93
+ }
94
+
95
+ case jbvBinary :
96
+ res -> val .binary = val -> val .binary ;
97
+ res -> val .binary .data = JsonCopy (val -> val .binary .data );
98
+ break ;
99
+
100
+ default :
101
+ elog (ERROR , "unknown json value type %d" , val -> type );
102
+ }
103
+
104
+ return res ;
29
105
}
30
- #endif
31
106
32
107
static inline JsonValue *
33
108
jsonFindKeyInObjectInternal (JsonContainer * obj , const JsonValue * key , bool last )
@@ -550,6 +625,17 @@ jsonvGetArraySize(JsonContainer *arrc)
550
625
}
551
626
}
552
627
628
+ static JsonContainer *
629
+ jsonvCopy (JsonContainer * jc )
630
+ {
631
+ JsonContainerData * res = JsonContainerAlloc ();
632
+
633
+ * res = * jc ;
634
+ res -> data = JsonValueCopy (NULL , (JsonValue * ) jc -> data );
635
+
636
+ return res ;
637
+ }
638
+
553
639
JsonContainerOps
554
640
jsonvContainerOps =
555
641
{
@@ -562,6 +648,7 @@ jsonvContainerOps =
562
648
jsonvGetArrayElement ,
563
649
jsonvGetArraySize ,
564
650
JsonbToCStringRaw ,
651
+ jsonvCopy ,
565
652
};
566
653
567
654
JsonValue *
@@ -1031,6 +1118,21 @@ JsonValueToJson(JsonValue *val)
1031
1118
}
1032
1119
}
1033
1120
1121
+ JsonContainer *
1122
+ JsonCopyFlat (JsonContainer * jc )
1123
+ {
1124
+ JsonContainerData * res = JsonContainerAlloc ();
1125
+
1126
+ * res = * jc ;
1127
+ res -> data = palloc (jc -> len );
1128
+ memcpy (res -> data , jc -> data , jc -> len );
1129
+
1130
+ if (jc -> ops -> compressionOps && jc -> ops -> compressionOps -> copyOptions )
1131
+ res -> options = jc -> ops -> compressionOps -> copyOptions (jc -> options );
1132
+
1133
+ return res ;
1134
+ }
1135
+
1034
1136
JsonValue *
1035
1137
JsonContainerExtractKeys (JsonContainer * jsc )
1036
1138
{
0 commit comments