|
| 1 | +/* Copyright (c) 2016, 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 | + * plsqlarray.js |
| 20 | + * |
| 21 | + * DESCRIPTION |
| 22 | + * Examples of binding PL/SQL "INDEX BY" tables. Beach names and |
| 23 | + * water depths are passed into the first PL/SQL procedure which |
| 24 | + * inserts them into a table. The second PL/SQL procedure queries |
| 25 | + * that table and returns the values. The third procedure accepts |
| 26 | + * arrays, and returns the values sorted by the beach name. |
| 27 | + * |
| 28 | + * Use demo.sql to create the required tables and package. |
| 29 | + * |
| 30 | + *****************************************************************************/ |
| 31 | + |
| 32 | +var async = require('async'); |
| 33 | +var oracledb = require('oracledb'); |
| 34 | +var dbConfig = require('./dbconfig.js'); |
| 35 | + |
| 36 | +var doconnect = function(cb) { |
| 37 | + oracledb.getConnection(dbConfig, cb); |
| 38 | +}; |
| 39 | + |
| 40 | +var dorelease = function(conn) { |
| 41 | + conn.release(function (err) { |
| 42 | + if (err) |
| 43 | + console.error(err.message); |
| 44 | + }); |
| 45 | +}; |
| 46 | + |
| 47 | +var dorollback = function(conn, cb) { |
| 48 | + conn.rollback(function (err) { |
| 49 | + if (err) |
| 50 | + return cb(err, conn); |
| 51 | + else |
| 52 | + return cb(null, conn); |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +// PL/SQL array bind IN parameters: |
| 57 | +// Pass arrays of values to a PL/SQL procedure |
| 58 | +var doin = function (conn, cb) { |
| 59 | + conn.execute( |
| 60 | + "BEGIN beachpkg.array_in(:beach_in, :depth_in); END;", |
| 61 | + { |
| 62 | + beach_in: { type : oracledb.STRING, |
| 63 | + dir: oracledb.BIND_IN, |
| 64 | + val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] }, |
| 65 | + depth_in: { type : oracledb.NUMBER, |
| 66 | + dir: oracledb.BIND_IN, |
| 67 | + val: [45, 30, 67] |
| 68 | + } |
| 69 | + }, |
| 70 | + function(err) { |
| 71 | + if (err) { |
| 72 | + return cb(err, conn); |
| 73 | + } else { |
| 74 | + return cb(null, conn); |
| 75 | + } |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +// PL/SQL array bind OUT parameters: |
| 80 | +// Fetch arrays of values from a PL/SQL procedure |
| 81 | +var doout = function (conn, cb) { |
| 82 | + conn.execute( |
| 83 | + "BEGIN beachpkg.array_out(:beach_out, :depth_out); END;", |
| 84 | + { |
| 85 | + beach_out: { type: oracledb.STRING, |
| 86 | + dir: oracledb.BIND_OUT, |
| 87 | + maxArraySize: 3}, |
| 88 | + depth_out: { type: oracledb.NUMBER, |
| 89 | + dir: oracledb.BIND_OUT, |
| 90 | + maxArraySize: 3} |
| 91 | + }, |
| 92 | + function (err, result) { |
| 93 | + if (err) { |
| 94 | + return cb(err, conn); |
| 95 | + } else { |
| 96 | + console.log("Binds returned:"); |
| 97 | + console.log(result.outBinds); |
| 98 | + return cb(null, conn); |
| 99 | + } |
| 100 | + }); |
| 101 | +}; |
| 102 | + |
| 103 | +// PL/SQL array bind IN OUT parameters: |
| 104 | +// Return input arrays sorted by beach name |
| 105 | +var doinout = function (conn, cb) { |
| 106 | + conn.execute( |
| 107 | + "BEGIN beachpkg.array_inout(:beach_inout, :depth_inout); END;", |
| 108 | + { |
| 109 | + beach_inout: { type: oracledb.STRING, |
| 110 | + dir: oracledb.BIND_INOUT, |
| 111 | + val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"], |
| 112 | + maxArraySize: 3}, |
| 113 | + depth_inout: { type: oracledb.NUMBER, |
| 114 | + dir: oracledb.BIND_INOUT, |
| 115 | + val: [8, 3, 70], |
| 116 | + maxArraySize: 3} |
| 117 | + }, |
| 118 | + function (err, result) { |
| 119 | + if (err) { |
| 120 | + return cb(err, conn); |
| 121 | + } else { |
| 122 | + console.log("Binds returned:"); |
| 123 | + console.log(result.outBinds); |
| 124 | + return cb(null, conn); |
| 125 | + } |
| 126 | + }); |
| 127 | +}; |
| 128 | + |
| 129 | +async.waterfall( |
| 130 | + [ |
| 131 | + doconnect, |
| 132 | + doin, |
| 133 | + doout, |
| 134 | + dorollback, |
| 135 | + doinout |
| 136 | + ], |
| 137 | + function (err, conn) { |
| 138 | + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } |
| 139 | + if (conn) |
| 140 | + dorelease(conn); |
| 141 | + }); |
0 commit comments