|
| 1 | +/* Copyright (c) 2025, Oracle and/or its affiliates. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * This software is dual-licensed to you under the Universal Permissive License |
| 6 | + * (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 7 | + * 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 8 | + * either license. |
| 9 | + * |
| 10 | + * If you elect to accept the software under the Apache License, Version 2.0, |
| 11 | + * the following applies: |
| 12 | + * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | + * you may not use this file except in compliance with the License. |
| 15 | + * You may obtain a copy of the License at |
| 16 | + * |
| 17 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + * |
| 19 | + * Unless required by applicable law or agreed to in writing, software |
| 20 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | + * See the License for the specific language governing permissions and |
| 23 | + * limitations under the License. |
| 24 | + * |
| 25 | + * NAME |
| 26 | + * dbobjassocarray.js |
| 27 | + * |
| 28 | + * DESCRIPTION |
| 29 | + * Tests the use of Associative Array-type dbObjects indexed by integer. |
| 30 | + * |
| 31 | + * |
| 32 | + *****************************************************************************/ |
| 33 | + |
| 34 | +'use strict'; |
| 35 | + |
| 36 | +Error.stackTraceLimit = 50; |
| 37 | + |
| 38 | +const oracledb = require('oracledb'); |
| 39 | +const dbConfig = require('./dbconfig.js'); |
| 40 | + |
| 41 | +// This example runs in both node-oracledb Thin and Thick modes. |
| 42 | +// |
| 43 | +// Optionally run in node-oracledb Thick mode |
| 44 | +if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') { |
| 45 | + |
| 46 | + // Thick mode requires Oracle Client or Oracle Instant Client libraries. |
| 47 | + // On Windows and macOS you can specify the directory containing the |
| 48 | + // libraries at runtime or before Node.js starts. On other platforms (where |
| 49 | + // Oracle libraries are available) the system library search path must always |
| 50 | + // include the Oracle library path before Node.js starts. If the search path |
| 51 | + // is not correct, you will get a DPI-1047 error. See the node-oracledb |
| 52 | + // installation documentation. |
| 53 | + let clientOpts = {}; |
| 54 | + // On Windows and macOS platforms, set the environment variable |
| 55 | + // NODE_ORACLEDB_CLIENT_LIB_DIR to the Oracle Client library path |
| 56 | + if (process.platform === 'win32' || process.platform === 'darwin') { |
| 57 | + clientOpts = { libDir: process.env.NODE_ORACLEDB_CLIENT_LIB_DIR }; |
| 58 | + } |
| 59 | + oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode |
| 60 | +} |
| 61 | + |
| 62 | +console.log(oracledb.thin ? 'Running in thin mode' : 'Running in thick mode'); |
| 63 | + |
| 64 | +async function run() { |
| 65 | + |
| 66 | + let connection; |
| 67 | + |
| 68 | + const PKG1 = 'NODB_PKG_OBJ_1_PLS_INTEGER'; |
| 69 | + const TYPE1 = 'NODB_TYP_OBJ_1_PLS_ARR_INTEGER'; |
| 70 | + |
| 71 | + try { |
| 72 | + // Get a non-pooled connection |
| 73 | + connection = await oracledb.getConnection(dbConfig); |
| 74 | + |
| 75 | + // Create the package with the Associative Array |
| 76 | + let pkgSql = |
| 77 | + `CREATE OR REPLACE PACKAGE ${PKG1} AUTHID DEFINER AS |
| 78 | + TYPE ${TYPE1} IS TABLE OF NUMBER INDEX BY PLS_INTEGER; |
| 79 | + FUNCTION F1 RETURN ${TYPE1}; |
| 80 | + END;`; |
| 81 | + await connection.execute(pkgSql); |
| 82 | + |
| 83 | + pkgSql = `CREATE OR REPLACE PACKAGE BODY ${PKG1} AS |
| 84 | + FUNCTION F1 RETURN ${TYPE1} IS |
| 85 | + R ${TYPE1}; |
| 86 | + BEGIN |
| 87 | + R(5):=55; |
| 88 | + R(2):=22; |
| 89 | + R(10):=120; |
| 90 | + RETURN R; |
| 91 | + END ; |
| 92 | + END ${PKG1} ;`; |
| 93 | + await connection.execute(pkgSql); |
| 94 | + |
| 95 | + const result = await connection.execute( |
| 96 | + `BEGIN |
| 97 | + :ret := ${PKG1}.f1; |
| 98 | + END;`, |
| 99 | + { |
| 100 | + ret: { |
| 101 | + dir: oracledb.BIND_OUT, |
| 102 | + type: `${PKG1}.${TYPE1}` |
| 103 | + } |
| 104 | + }); |
| 105 | + const dbObj = result.outBinds.ret; |
| 106 | + const dbObj1 = dbObj.copy(); |
| 107 | + console.log('The original DbObject returned is:', dbObj.toMap()); |
| 108 | + console.log('The copied DbObject is:', dbObj1.toMap()); |
| 109 | + console.log('The element at position 2 in the original DbObject:', dbObj[2]); |
| 110 | + console.log('The element at position 2 in the copied DbObject:', dbObj1[2]); |
| 111 | + |
| 112 | + dbObj1.deleteElement(2); |
| 113 | + console.log("\nDeleted element at index 2 in the copied DbObject"); |
| 114 | + console.log("Now, the copied DbObject is:", dbObj1.toMap()); |
| 115 | + console.log("The original DbObject is still:", dbObj.toMap()); |
| 116 | + |
| 117 | + dbObj.append(67); |
| 118 | + console.log("\nAppended the value 67 to the original DbObject"); |
| 119 | + console.log("Now the length of original DbObject:", dbObj.length); |
| 120 | + console.log("The original DbObject is now:", dbObj.toMap()); |
| 121 | + console.log("The keys in the original DbObject are:", dbObj.getKeys()); |
| 122 | + console.log("The values in the original DbObject are:", dbObj.getValues()); |
| 123 | + |
| 124 | + } catch (err) { |
| 125 | + console.error(err); |
| 126 | + } finally { |
| 127 | + if (connection) { |
| 128 | + try { |
| 129 | + await connection.execute( |
| 130 | + `DECLARE |
| 131 | + e_type_missing EXCEPTION; |
| 132 | + PRAGMA EXCEPTION_INIT(e_type_missing, -4043); |
| 133 | + BEGIN |
| 134 | + EXECUTE IMMEDIATE ('DROP TYPE ${TYPE1} FORCE'); |
| 135 | + EXCEPTION |
| 136 | + WHEN e_type_missing THEN NULL; |
| 137 | + END;` |
| 138 | + ); |
| 139 | + await connection.execute(`DROP package ${PKG1}`); |
| 140 | + await connection.close(); |
| 141 | + } catch (err) { |
| 142 | + console.error(err); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +run(); |
0 commit comments