-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lobbinds.js
344 lines (283 loc) · 10.3 KB
/
lobbinds.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Copyright (c) 2016, 2024, 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 http://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
* lobbinds.js
*
* DESCRIPTION
* Demonstrates following LOB bind features
* 1) DML bind for an INSERT
* 2) PL/SQL bind IN for CLOB as String, and BLOB as Buffer
* 3) PL/SQL bind OUT for CLOB as String, and BLOB as Buffer
* 4) Querying a LOB and binding using PL/SQL IN OUT bind
* 5) PL/SQL OUT bind followed by PL/SQL IN OUT bind
*
******************************************************************************/
'use strict';
Error.stackTraceLimit = 50;
const fs = require('fs');
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
const demoSetup = require('./demosetup.js');
// This example runs in both node-oracledb Thin and Thick modes.
//
// Optionally run in node-oracledb Thick mode
if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') {
// Thick mode requires Oracle Client or Oracle Instant Client libraries.
// On Windows and macOS you can specify the directory containing the
// libraries at runtime or before Node.js starts. On other platforms (where
// Oracle libraries are available) the system library search path must always
// include the Oracle library path before Node.js starts. If the search path
// is not correct, you will get a DPI-1047 error. See the node-oracledb
// installation documentation.
let clientOpts = {};
// On Windows and macOS platforms, set the environment variable
// NODE_ORACLEDB_CLIENT_LIB_DIR to the Oracle Client library path
if (process.platform === 'win32' || process.platform === 'darwin') {
clientOpts = { libDir: process.env.NODE_ORACLEDB_CLIENT_LIB_DIR };
}
oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode
}
console.log(oracledb.thin ? 'Running in thin mode' : 'Running in thick mode');
const clobOutFileName1 = 'lobbindsout1.txt';
const clobOutFileName2 = 'lobbindsout2.txt';
oracledb.autoCommit = true; // for ease of demonstration
// 1. SELECTs a CLOB and inserts it back using an IN bind to an INSERT statement
async function query_bind_insert(connection) {
console.log ("1. query_bind_insert(): Inserting a CLOB using a LOB IN bind for INSERT");
let result = await connection.execute(
`SELECT c FROM no_lobs WHERE id = :id`,
{ id: 1 }
);
if (result.rows.length === 0) {
throw new Error('query_bind_insert(): No row found');
}
const clob1 = result.rows[0][0];
if (clob1 === null) {
throw new Error('query_bind_insert(): NULL clob1 found');
}
// Insert the value back as a new row
result = await connection.execute(
`INSERT INTO no_lobs (id, c) VALUES (:id, :c)`,
{
id: 10,
c: {val: clob1, type: oracledb.CLOB, dir: oracledb.BIND_IN}
}
);
// destroy the LOB and wait for it to be closed completely before continuing
clob1.destroy();
await new Promise((resolve, reject) => {
clob1.on('error', reject);
clob1.on('close', resolve);
});
console.log (" " + result.rowsAffected + " row(s) inserted");
}
// 2. Show PL/SQL bind IN for CLOB as String and for BLOB as Buffer.
async function plsql_in_as_str_buf(connection) {
console.log("2. plsql_in_as_str_buf(): Binding of String and Buffer for PL/SQL IN binds");
// Make up some data
const bigStr = 'A'.repeat(50000);
const bigBuf = Buffer.from(bigStr);
await connection.execute(
`BEGIN
no_lobs_in(:id, :c, :b);
END;`,
{
id: 20,
c: {val: bigStr, type: oracledb.STRING, dir: oracledb.BIND_IN},
b: {val: bigBuf, type: oracledb.BUFFER, dir: oracledb.BIND_IN}
}
);
console.log(" Completed");
}
// 3. Gets text and binary strings from database LOBs using PL/SQL OUT binds
async function plsql_out_as_str_buf(connection) {
console.log("3. plsql_out_as_str_buf(): Fetching as String and Buffer using PL/SQL OUT binds");
const result = await connection.execute(
`BEGIN
no_lobs_out(:id, :c, :b);
END;`,
{
id: 20,
c: {type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 50000},
b: {type: oracledb.BUFFER, dir: oracledb.BIND_OUT, maxSize: 50000}
}
);
console.log(" String length: " + result.outBinds.c.length);
console.log(" Buffer length: " + result.outBinds.b.length);
}
// 4. Queries a CLOB as a Stream and passes it to a PL/SQL procedure as an IN OUT bind
// Persistent LOBs can be bound to PL/SQL calls as IN OUT. (Temporary LOBs cannot).
async function query_plsql_inout(connection) {
console.log ("4. query_plsql_inout(): Querying then inserting a CLOB using a PL/SQL IN OUT LOB bind");
let result = await connection.execute(
`SELECT c FROM no_lobs WHERE id = :id`,
{ id: 1 }
);
if (result.rows.length === 0) {
throw new Error('query_plsql_inout(): No row found');
}
const clob1 = result.rows[0][0];
if (clob1 === null) {
throw new Error('query_plsql_inout(): NULL clob1 found');
}
// Note binding clob1 as IN OUT here causes it be autoclosed by execute().
// The returned Lob clob2 will be autoclosed because it is streamed to completion.
result = await connection.execute(
`BEGIN
no_lob_in_out(:idbv, :ciobv);
END;`,
{
idbv: 30,
ciobv: {val: clob1, type: oracledb.CLOB, dir: oracledb.BIND_INOUT}
}
);
const clob2 = result.outBinds.ciobv;
if (clob2 === null) {
throw new Error('plsql_out_inout(): NULL clob2 found');
}
// Stream the returned LOB to a file
const doStream = new Promise((resolve, reject) => {
// Set up the Lob stream
console.log(' Writing to ' + clobOutFileName1);
clob2.setEncoding('utf8'); // set the encoding so we get a 'string' not a 'buffer'
clob2.on('error', (err) => {
// console.log("clob2.on 'error' event");
reject(err);
});
clob2.on('end', () => {
// console.log("clob2.on 'end' event");
clob2.destroy();
});
clob2.on('close', () => {
// console.log("clob2.on 'close' event");
resolve();
});
// Set up the stream to write to a file
const outStream = fs.createWriteStream(clobOutFileName1);
outStream.on('error', (err) => {
// console.log("outStream.on 'error' event");
clob2.destroy(err);
});
// Switch into flowing mode and push the LOB to the file
clob2.pipe(outStream);
});
await doStream;
console.log (" Completed");
}
// 5. Get CLOB as a PL/SQL OUT bind and pass it to another procedure as IN OUT.
// Persistent LOBs can be bound to PL/SQL calls as IN OUT. (Temporary LOBs cannot).
async function plsql_out_inout(connection) {
console.log ("5. plsql_out_inout(): Getting a LOB using a PL/SQL OUT bind and inserting it using a PL/SQL IN OUT LOB bind");
const result1 = await connection.execute(
`BEGIN
no_lobs_out(:idbv, :cobv, :bobv);
END;`,
{
idbv: 1,
cobv: {type: oracledb.CLOB, dir: oracledb.BIND_OUT},
bobv: {type: oracledb.BLOB, dir: oracledb.BIND_OUT} // not used in this demo; it will be NULL anyway
}
);
const clob1 = result1.outBinds.cobv;
if (clob1 === null) {
throw new Error('plsql_out_inout(): NULL clob1 found');
}
// Note binding clob1 as IN OUT here causes it be autoclosed by execute().
// The returned Lob clob2 will be autoclosed because it is streamed to completion.
const result2 = await connection.execute(
`BEGIN
no_lob_in_out(:idbv, :ciobv);
END;`,
{
idbv: 50,
ciobv: {val: clob1, type: oracledb.CLOB, dir: oracledb.BIND_INOUT}
}
);
const doStream = new Promise((resolve, reject) => {
const clob2 = result2.outBinds.ciobv;
if (clob2 === null) {
throw new Error('plsql_out_inout(): NULL clob2 found');
}
// Stream the LOB to a file
console.log(' Writing to ' + clobOutFileName2);
clob2.setEncoding('utf8'); // set the encoding so we get a 'string' not a 'buffer'
clob2.on('error', (err) => {
// console.log("clob2.on 'error' event");
reject (err);
});
clob2.on('end', () => {
// console.log("clob2.on 'end' event");
clob2.destroy();
});
clob2.on('close', () => {
// console.log("clob2.on 'close' event");
resolve();
});
const outStream = fs.createWriteStream(clobOutFileName2);
outStream.on('error', (err) => {
// console.log("outStream.on 'error' event");
clob2.destroy(err);
});
// Switch into flowing mode and push the LOB to the file
clob2.pipe(outStream);
});
await doStream;
console.log (" Completed");
}
/*
// 6. Show the number of open temporary LOBs
async function doshowvtemplob(connection) {
console.log('6. Query from V$TEMPORARY_LOBS:');
const result = await connection.execute(
`SELECT * FROM V$TEMPORARY_LOBS`,
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
console.log(result.rows[0]);
};
*/
async function run() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
await demoSetup.setupLobs(connection, true); // create the demo table with data
await query_bind_insert(connection);
await plsql_in_as_str_buf(connection);
await plsql_out_as_str_buf(connection);
await query_plsql_inout(connection);
await plsql_out_inout(connection);
// await doshowvtemplob(connection); // Show open temporary Lobs, if desired
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();