|
| 1 | +/* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with the Apache |
| 6 | + * License, Version 2.0 (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * NAME |
| 19 | + * plsqlrecord.js |
| 20 | + * |
| 21 | + * DESCRIPTION |
| 22 | + * Shows binding of PL/SQL RECORDS |
| 23 | + * |
| 24 | + * This example requires node-oracledb 4 or later. |
| 25 | + * |
| 26 | + * This example uses Node 8's async/await syntax. |
| 27 | + * |
| 28 | + *****************************************************************************/ |
| 29 | + |
| 30 | +'use strict'; |
| 31 | + |
| 32 | +const oracledb = require('oracledb'); |
| 33 | +const dbConfig = require('./dbconfig.js'); |
| 34 | + |
| 35 | +async function run() { |
| 36 | + let connection, binds, options, result, obj; |
| 37 | + |
| 38 | + // The PL/SQL that is called in each example |
| 39 | + const plsql = `CALL rectest.myproc(:inbv, :outbv)`; |
| 40 | + |
| 41 | + try { |
| 42 | + |
| 43 | + connection = await oracledb.getConnection(dbConfig); |
| 44 | + |
| 45 | + // Create a PL/SQL package that uses a RECORD |
| 46 | + |
| 47 | + await connection.execute( |
| 48 | + `CREATE OR REPLACE PACKAGE rectest AS |
| 49 | + TYPE rectype IS RECORD (name VARCHAR2(40), pos NUMBER); |
| 50 | + PROCEDURE myproc (p_in IN rectype, p_out OUT rectype); |
| 51 | + END rectest;`); |
| 52 | + |
| 53 | + await connection.execute( |
| 54 | + `CREATE OR REPLACE PACKAGE BODY rectest AS |
| 55 | + PROCEDURE myproc (p_in IN rectype, p_out OUT rectype) AS |
| 56 | + BEGIN |
| 57 | + p_out := p_in; |
| 58 | + p_out.pos := p_out.pos * 2; |
| 59 | + END; |
| 60 | + END rectest;`); |
| 61 | + |
| 62 | + |
| 63 | + // Get the RECORD prototype object |
| 64 | + |
| 65 | + const RecTypeClass = await connection.getDbObjectClass("RECTEST.RECTYPE"); |
| 66 | + // console.log(RecTypeClass.prototype); |
| 67 | + |
| 68 | + |
| 69 | + // |
| 70 | + // Single execution |
| 71 | + // |
| 72 | + |
| 73 | + console.log('Using the constructor to create an object:'); |
| 74 | + obj = new RecTypeClass({ NAME: 'Ship', POS: 12 }); |
| 75 | + |
| 76 | + binds = { |
| 77 | + inbv: obj, |
| 78 | + outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT } |
| 79 | + }; |
| 80 | + |
| 81 | + result = await connection.execute(plsql, binds); |
| 82 | + console.log(result.outBinds.outbv); |
| 83 | + |
| 84 | + console.log('\nBinding the record values directly:'); |
| 85 | + |
| 86 | + binds = { |
| 87 | + inbv: { type: RecTypeClass, val: { NAME: 'Plane', POS: 34 } }, |
| 88 | + outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT } |
| 89 | + }; |
| 90 | + |
| 91 | + result = await connection.execute(plsql, binds); |
| 92 | + console.log(result.outBinds.outbv); |
| 93 | + |
| 94 | + // |
| 95 | + // executeMany() |
| 96 | + // |
| 97 | + |
| 98 | + console.log('\nExample with executeMany():'); |
| 99 | + |
| 100 | + binds = [ |
| 101 | + { inbv: { NAME: 'Car', POS: 56 } }, |
| 102 | + { inbv: { NAME: 'Train', POS: 78 } }, |
| 103 | + { inbv: { NAME: 'Bike', POS: 83 } } |
| 104 | + ]; |
| 105 | + |
| 106 | + options = { |
| 107 | + bindDefs: { |
| 108 | + inbv: { type: RecTypeClass }, |
| 109 | + outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }, |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + result = await connection.executeMany(plsql, binds, options); |
| 114 | + for (const b of result.outBinds) { |
| 115 | + console.log(b.outbv); |
| 116 | + } |
| 117 | + |
| 118 | + } catch (err) { |
| 119 | + console.error(err); |
| 120 | + } finally { |
| 121 | + if (connection) { |
| 122 | + try { |
| 123 | + await connection.close(); |
| 124 | + } catch (err) { |
| 125 | + console.error(err); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +run(); |
0 commit comments