-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjson_sql.pkb
268 lines (230 loc) · 7.08 KB
/
json_sql.pkb
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
CREATE OR REPLACE
PACKAGE BODY json_sql
IS
TYPE HandleType IS RECORD
(
cursorID INTEGER,
description dbms_sql.desc_tab,
stringColumn VARCHAR2(4000),
numberColumn NUMBER,
dateColumn DATE
);
FUNCTION openCursor(rc IN OUT SYS_REFCURSOR) RETURN HandleType;
FUNCTION openCursor(sqlCmd VARCHAR2, sqlBind jsonObject DEFAULT NULL_OBJECT) RETURN HandleType;
PROCEDURE bind(theCursor IN INTEGER, theBinding jsonObject);
PROCEDURE describeAndDefine(theHandle IN OUT NOCOPY HandleType);
FUNCTION process(theHandle IN OUT NOCOPY HandleType, format IN VARCHAR2 DEFAULT FORMAT_TAB) RETURN jsonObject;
PROCEDURE closeCursor(theHandle IN OUT NOCOPY HandleType);
----------------------------------------------------------
-- get (SYS_REFCURSOR)
--
FUNCTION get(rc IN OUT SYS_REFCURSOR, format IN VARCHAR2 DEFAULT FORMAT_TAB) RETURN jsonObject
IS
aHandle HandleType;
aObject jsonObject := jsonObject();
BEGIN
-- open cursor
aHandle := openCursor(rc=>rc);
-- process cursor
aObject := process(theHandle=>aHandle, format=>format);
-- close cursor
closeCursor(aHandle);
RETURN aObject;
END get;
----------------------------------------------------------
-- get
--
FUNCTION get(sqlCmd VARCHAR2, sqlBind jsonObject DEFAULT NULL_OBJECT, format IN VARCHAR2 DEFAULT FORMAT_TAB) RETURN jsonObject
IS
aHandle HandleType;
aObject jsonObject := jsonObject();
BEGIN
-- open cursor
aHandle := openCursor(sqlCmd=>sqlCmd, sqlBind=>sqlBind);
-- process cursor
aObject := process(theHandle=>aHandle, format=>format);
-- close cursor
closeCursor(aHandle);
RETURN aObject;
END get;
----------------------------------------------------------
-- Execute sql statement and output a json structure
--
PROCEDURE htp(sqlCmd VARCHAR2, sqlBind IN VARCHAR2 DEFAULT NULL, format IN VARCHAR2 DEFAULT FORMAT_TAB)
IS
aBinding jsonObject := jsonObject();
BEGIN
-- parse
IF (sqlBind IS NOT NULL) THEN
aBinding := jsonObject(TO_CLOB(sqlBind));
END IF;
-- process and output
get(sqlCmd=>sqlCmd, sqlBind=>aBinding, format=>UPPER(format)).htp();
END htp;
----------------------------------------------------------
-- openCursor (private)
--
FUNCTION openCursor(rc IN OUT SYS_REFCURSOR) RETURN HandleType
IS
aHandle HandleType;
BEGIN
-- open cursor
aHandle.cursorID := dbms_sql.to_cursor_number(rc=>rc);
-- describe and define columns
describeAndDefine(theHandle=>aHandle);
RETURN aHandle;
END openCursor;
----------------------------------------------------------
-- openCursor (private)
--
FUNCTION openCursor(sqlCmd VARCHAR2, sqlBind jsonObject DEFAULT NULL_OBJECT) RETURN HandleType
IS
aHandle HandleType;
aStatus INTEGER;
BEGIN
-- open cursor
aHandle.cursorID := dbms_sql.open_cursor;
-- parse statement
dbms_sql.parse(aHandle.cursorID, sqlCmd, dbms_sql.native);
-- bindings
bind(aHandle.cursorID, sqlBind);
-- describe and define columns
describeAndDefine(theHandle=>aHandle);
-- execute statement
aStatus := dbms_sql.execute(aHandle.cursorID);
RETURN aHandle;
END openCursor;
----------------------------------------------------------
-- bind (private)
--
PROCEDURE bind(theCursor IN INTEGER, theBinding jsonObject)
IS
aKeys jsonKeys := theBinding.get_keys();
aKey VARCHAR2(32767);
aValue jsonValue := jsonValue();
BEGIN
FOR i IN 1 .. aKeys.COUNT LOOP
aKey := aKeys(i);
aValue := theBinding.get(aKey);
IF (aValue.get_type() = 'NUMBER') THEN
dbms_sql.bind_variable(theCursor, ':'||aKey, aValue.get_number());
ELSIF (theBinding.get(aKey).get_type() = 'STRING') THEN
dbms_sql.bind_variable(theCursor, ':'||aKey, aValue.get_string());
ELSE
RAISE VALUE_ERROR;
END IF;
END LOOP;
END bind;
----------------------------------------------------------
-- describeAndDefine (private)
--
PROCEDURE describeAndDefine(theHandle IN OUT NOCOPY HandleType)
IS
aCount INTEGER;
aType INTEGER;
BEGIN
-- describe columns
dbms_sql.describe_columns(theHandle.cursorID, aCount, theHandle.description);
-- define columns
FOR i IN 1 .. aCount LOOP
aType := theHandle.description(i).col_type;
IF (aType IN (1, 112)) THEN
dbms_sql.define_column(theHandle.cursorID, i, theHandle.stringColumn, 4000);
ELSIF (aType = 2) THEN
dbms_sql.define_column(theHandle.cursorID, i, theHandle.numberColumn);
ELSIF (aType = 12) THEN
dbms_sql.define_column(theHandle.cursorID, i, theHandle.dateColumn);
END IF;
END LOOP;
END describeAndDefine;
----------------------------------------------------------
-- process (private)
--
FUNCTION process(theHandle IN OUT NOCOPY HandleType, format IN VARCHAR2 DEFAULT FORMAT_TAB) RETURN jsonObject
IS
aName VARCHAR2(32767);
aNames jsonArray := jsonArray();
aRowObj jsonObject := jsonObject();
aRowArr jsonArray := jsonArray();
aRows jsonArray := jsonArray();
aObject jsonObject := jsonObject();
BEGIN
IF (format NOT IN (FORMAT_OBJ, FORMAT_TAB)) THEN
RAISE VALUE_ERROR;
END IF;
-- column names
IF (format = FORMAT_TAB) THEN
FOR i IN 1 .. theHandle.description.COUNT LOOP
IF (theHandle.description(i).col_type in (1, 96)) THEN
aNames.append(theHandle.description(i).col_name);
-- number
ELSIF (theHandle.description(i).col_type = 2) THEN
aNames.append(theHandle.description(i).col_name);
-- date
ELSIF (theHandle.description(i).col_type = 12) THEN
aNames.append(theHandle.description(i).col_name);
END IF;
END LOOP;
END IF;
-- process rows
WHILE (dbms_sql.fetch_rows(theHandle.cursorID) > 0) LOOP
aRowObj := jsonObject();
aRowArr := jsonArray();
-- process columns
FOR i IN 1 .. theHandle.description.COUNT LOOP
-- column name
aName := theHandle.description(i).col_name;
-- string
IF (theHandle.description(i).col_type in (1, 96)) THEN
dbms_sql.column_value(theHandle.cursorID, i, theHandle.stringColumn);
IF (format = FORMAT_OBJ) THEN
aRowObj.put(aName, theHandle.stringColumn);
ELSE
aRowArr.append(theHandle.stringColumn);
END IF;
-- number
ELSIF (theHandle.description(i).col_type = 2) THEN
dbms_sql.column_value(theHandle.cursorID, i, theHandle.numberColumn);
IF (format = FORMAT_OBJ) THEN
aRowObj.put(aName, theHandle.numberColumn);
ELSE
aRowArr.append(theHandle.numberColumn);
END IF;
-- date
ELSIF (theHandle.description(i).col_type = 12) THEN
dbms_sql.column_value(theHandle.cursorID, i, theHandle.dateColumn);
IF (format = FORMAT_OBJ) THEN
aRowObj.put(aName, theHandle.dateColumn);
ELSE
aRowArr.append(theHandle.dateColumn);
END IF;
END IF;
END LOOP;
IF (format = FORMAT_OBJ) THEN
aRows.append(aRowObj);
ELSE
aRows.append(aRowArr);
END IF;
END LOOP;
IF (format = FORMAT_OBJ) THEN
aObject.put('rows', aRows.to_jsonValue());
ELSE
aObject.put('cols', aNames.to_jsonValue());
aObject.put('rows', aRows.to_jsonValue());
END IF;
RETURN aObject;
END process;
----------------------------------------------------------
-- closeCursor (private)
--
PROCEDURE closeCursor(theHandle IN OUT NOCOPY HandleType)
IS
BEGIN
IF (dbms_sql.is_open(theHandle.cursorID)) THEN
dbms_sql.close_cursor(theHandle.cursorID);
END IF;
theHandle.cursorID := NULL;
theHandle.description.DELETE;
END closeCursor;
END json_sql;
/