-
Notifications
You must be signed in to change notification settings - Fork 15
/
jsonobject.tpb
279 lines (247 loc) · 9.24 KB
/
jsonobject.tpb
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
CREATE OR REPLACE
TYPE BODY jsonObject IS
----------------------------------------------------------
-- jsonObject
--
CONSTRUCTOR FUNCTION jsonObject(SELF IN OUT NOCOPY jsonObject) RETURN SELF AS result
IS
BEGIN
SELF.nodes := jsonNodes();
SELF.lastID := NULL;
RETURN;
END jsonObject;
----------------------------------------------------------
-- jsonObject
--
CONSTRUCTOR FUNCTION jsonObject(SELF IN OUT NOCOPY jsonObject, theData IN jsonValue) RETURN SELF AS result
IS
BEGIN
IF (theData.typ != json_utils.NODE_TYPE_OBJECT) THEN
raise_application_error(-20100, 'jsonObject exception: unable to convert node ('||theData.typ||') to an object');
ELSE
SELF.nodes := theData.nodes;
SELF.lastID := NULL;
END IF;
RETURN;
END jsonObject;
----------------------------------------------------------
-- jsonObject
--
CONSTRUCTOR FUNCTION jsonObject(SELF IN OUT NOCOPY jsonObject, theJSONString IN CLOB) RETURN SELF AS result
IS
BEGIN
SELF.nodes := json_parser.parser(theJSONString);
SELF.lastID := NULL;
RETURN;
END jsonObject;
----------------------------------------------------------
-- put
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add the node
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theName));
END put;
----------------------------------------------------------
-- put (VARCHAR2)
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN VARCHAR2)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add the node
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theName, theValue));
END put;
----------------------------------------------------------
-- put (CLOB)
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN CLOB)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add the node
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theName, theValue));
END put;
----------------------------------------------------------
-- put (NUMBER)
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN NUMBER)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add the node
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theName, theValue));
END put;
----------------------------------------------------------
-- put (DATE)
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN DATE)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add the node
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theName, theValue));
END put;
----------------------------------------------------------
-- put (BOOLEAN)
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN BOOLEAN)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add the node
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theName, theValue));
END put;
----------------------------------------------------------
-- put
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN jsonObject)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add a new object node that will be used as the root for all the sub notes
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(json_utils.NODE_TYPE_OBJECT, theName, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
-- copy the sub-nodes
json_utils.copyNodes(theTargetNodes=>SELF.nodes, theTargetNodeID=>aNodeID, theLastID=>SELF.lastID, theName=>theName, theSourceNodes=>theValue.nodes);
END put;
----------------------------------------------------------
-- put
--
MEMBER PROCEDURE put(SELF IN OUT NOCOPY jsonObject, theName IN VARCHAR2, theValue IN jsonValue)
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>theName);
BEGIN
-- remove the existing node if we change an existing property
IF (aNodeID IS NOT NULL) THEN
SELF.lastID := json_utils.removeNode(theNodes=>SELF.nodes, theNodeID=>aNodeID);
END IF;
-- add a new object node that will be used as the root for all the sub notes
aNodeID := json_utils.addNode(theNodes=>SELF.nodes, theLastID=>SELF.lastID, theNode=>jsonNode(theValue.typ, theName, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
-- copy the sub-nodes
json_utils.copyNodes(theTargetNodes=>SELF.nodes, theTargetNodeID=>aNodeID, theLastID=>SELF.lastID, theName=>theName, theSourceNodes=>theValue.nodes);
END put;
----------------------------------------------------------
-- count
--
MEMBER FUNCTION count(SELF IN jsonObject) RETURN NUMBER
IS
BEGIN
RETURN json_utils.getNodeCount(theNodes=>SELF.nodes);
END count;
----------------------------------------------------------
-- get
--
MEMBER FUNCTION get(SELF IN jsonObject, thePropertyName IN VARCHAR2) RETURN jsonValue
IS
aNodeID BINARY_INTEGER := json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>thePropertyName);
BEGIN
IF (aNodeID IS NOT NULL) THEN
RETURN json_utils.createSubTree(theSourceNodes=>SELF.nodes, theSourceNodeID=>aNodeID);
ELSE
raise_application_error(-20100, 'jsonObject exception: property ('||thePropertyName||') does not exit');
RETURN NULL;
END IF;
END get;
----------------------------------------------------------
-- exist
--
MEMBER FUNCTION exist(SELF IN jsonObject, thePropertyName IN VARCHAR2) RETURN BOOLEAN
IS
BEGIN
RETURN (json_utils.getNodeIDByName(theNodes=>SELF.nodes, thePropertyName=>thePropertyName) IS NOT NULL);
END exist;
----------------------------------------------------------
-- get_keys
--
MEMBER FUNCTION get_keys RETURN jsonKeys
IS
keys jsonKeys := jsonKeys();
i BINARY_INTEGER := SELF.nodes.FIRST;
BEGIN
WHILE (i IS NOT NULL) LOOP
keys.EXTEND(1);
keys(keys.LAST) := SELF.nodes(i).nam;
i := SELF.nodes(i).nex;
END LOOP;
RETURN keys;
END get_keys;
----------------------------------------------------------
-- to_jsonValue
--
MEMBER FUNCTION to_jsonValue(SELF IN jsonObject) RETURN jsonValue
IS
BEGIN
RETURN jsonValue(json_utils.NODE_TYPE_OBJECT, SELF.nodes);
END to_jsonValue;
----------------------------------------------------------
-- to_clob
--
MEMBER PROCEDURE to_clob(SELF IN jsonObject, theLobBuf IN OUT NOCOPY CLOB, theEraseLob BOOLEAN DEFAULT TRUE)
IS
aIndentation INTEGER := 0;
aStrBuf VARCHAR2(32767);
BEGIN
IF (theEraseLob) THEN
json_utils.erase_clob(theLobBuf);
END IF;
json_utils.object_to_clob(theLobBuf=>theLobBuf, theStrBuf=>aStrBuf, theNodes=>SELF.nodes, theNodeID=>SELF.nodes.FIRST, theIndentation=>aIndentation);
END to_clob;
----------------------------------------------------------
-- to_text
--
MEMBER FUNCTION to_text(SELF IN jsonObject) RETURN VARCHAR2
IS
aIndentation INTEGER := 0;
aStrBuf VARCHAR2(32767);
aLobLoc CLOB;
BEGIN
dbms_lob.createtemporary(lob_loc=>aLobLoc, cache=>TRUE, dur=>dbms_lob.session);
json_utils.object_to_clob(theLobBuf=>aLobLoc, theStrBuf=>aStrBuf, theNodes=>SELF.nodes, theNodeID=>SELF.nodes.FIRST, theIndentation=>aIndentation);
aStrBuf := dbms_lob.substr(aLobLoc, 32767, 1);
dbms_lob.freetemporary(lob_loc=>aLobLoc);
RETURN aStrBuf;
END to_text;
----------------------------------------------------------
-- htp
--
MEMBER PROCEDURE htp(SELF IN jsonObject, theJSONP IN VARCHAR2 DEFAULT NULL)
IS
aLob CLOB := empty_clob();
BEGIN
dbms_lob.createtemporary(aLob, TRUE);
SELF.to_clob(aLob);
json_utils.htp_output_clob(aLob, theJSONP);
dbms_lob.freetemporary(aLob);
END htp;
END;
/