-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfetchAsStringWithRefCursor.js
159 lines (141 loc) · 5.38 KB
/
fetchAsStringWithRefCursor.js
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
/* Copyright (c) 2016, 2023, Oracle and/or its affiliates. */
/******************************************************************************
*
* This software is dual-licensed to you under the Universal Permissive License
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
* either license.
*
* If you elect to accept the software under the Apache License, Version 2.0,
* the following applies:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* 139. fetchAsStringWithRefCursor.js
*
* DESCRIPTION
* Columns fetched from REF CURSORS can be mapped by fetchInfo settings
* in the execute() call. The global fetchAsString or fetchAsBuffer
* settings work as well.
*
*****************************************************************************/
'use strict';
const oracledb = require('oracledb');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
describe('139. fetchAsStringWithRefCursor.js', function() {
let connection = null;
const tableName = "nodb_tab_fetchAsRefCursor";
before(async function() {
connection = await oracledb.getConnection(dbConfig);
// PL/SQL procedure to create the table
let sql = "BEGIN \n" +
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942);\n" +
" BEGIN \n" +
" EXECUTE IMMEDIATE ('DROP TABLE " + tableName + " PURGE' ); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE ( ' \n" +
" CREATE TABLE " + tableName + " ( \n" +
" id NUMBER(10), \n" +
" content VARCHAR2(20), \n" +
" hiredate DATE \n" +
" ) \n" +
" '); \n" +
"END; ";
await connection.execute(sql);
// PL/SQL procedure to insert data into the table
sql = "DECLARE \n" +
" x NUMBER := 0; \n" +
" n VARCHAR2(20); \n" +
"BEGIN \n" +
" FOR i IN 1..300 LOOP \n" +
" x := x + 1; \n" +
" n := 'staff ' || x; \n" +
" INSERT INTO " + tableName + " VALUES(x, n, TO_DATE('2012-02-18', 'YYYY-MM-DD')); \n" +
" END LOOP; \n" +
"END; ";
await connection.execute(sql);
// PL/SQL procedure for the ref cursor
const proc = "CREATE OR REPLACE PROCEDURE nodb_proc_fetchcursor (p_in IN NUMBER, p_out OUT SYS_REFCURSOR) \n" +
"AS \n" +
"BEGIN \n" +
" OPEN p_out FOR \n" +
" SELECT * FROM " + tableName + " WHERE id > p_in; \n" +
"END; ";
await connection.execute(proc);
});
after(async function() {
let sql = "DROP PROCEDURE nodb_proc_fetchcursor";
await connection.execute(sql);
sql = "DROP TABLE " + tableName + " PURGE";
await connection.execute(sql);
await connection.close();
oracledb.fetchAsString = [];
});
it('139.1 columns fetched from REF CURSORS can be mapped by fetchInfo settings', async function() {
const result = await connection.execute(
"begin nodb_proc_fetchcursor(:in, :out); end;",
{
in: 290,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{
outFormat: oracledb.OUT_FORMAT_OBJECT,
fetchInfo:
{
"ID": { type: oracledb.STRING },
"HIREDATE": { type: oracledb.STRING }
}
}
);
const fetchRowFromRC = async function(rc) {
const row = await rc.getRow();
if (row) {
assert.strictEqual(typeof row.ID, "string");
assert.strictEqual(typeof row.HIREDATE, "string");
return await fetchRowFromRC(rc);
} else {
await rc.close();
}
}; // fetchRowFromRC()
await fetchRowFromRC(result.outBinds.out);
}); // 139.1
it('139.2 fetchAsString takes effect as well', async function() {
oracledb.fetchAsString = [ oracledb.DATE ];
const result = await connection.execute(
"begin nodb_proc_fetchcursor(:in, :out); end;",
{
in: 295,
out: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const fetchRowFromRC = async function(rc) {
const row = await rc.getRow();
if (row) {
assert.notStrictEqual(typeof row.ID, "string");
assert.strictEqual(typeof row.HIREDATE, "string");
return await fetchRowFromRC(rc);
} else {
await rc.close();
}
}; // fetchRowFromRC()
await fetchRowFromRC(result.outBinds.out);
}); // 139.2
});