forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplsqlarray.js
More file actions
148 lines (139 loc) · 3.89 KB
/
plsqlarray.js
File metadata and controls
148 lines (139 loc) · 3.89 KB
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
/* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. */
/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://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
* plsqlarray.js
*
* DESCRIPTION
* Examples of binding PL/SQL "INDEX BY" tables. Beach names and
* water depths are passed into the first PL/SQL procedure which
* inserts them into a table. The second PL/SQL procedure queries
* that table and returns the values. The third procedure accepts
* arrays, and returns the values sorted by the beach name.
*
* Use demo.sql to create the required tables and package.
*
* This example requires node-oracledb 1.6 or later.
*
*****************************************************************************/
var async = require('async');
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
var doconnect = function(cb) {
oracledb.getConnection(dbConfig, cb);
};
var dorelease = function(conn) {
conn.close(function (err) {
if (err)
console.error(err.message);
});
};
var dorollback = function(conn, cb) {
conn.rollback(function (err) {
if (err)
return cb(err, conn);
else
return cb(null, conn);
});
};
// PL/SQL array bind IN parameters:
// Pass arrays of values to a PL/SQL procedure
var doin = function (conn, cb) {
conn.execute(
"BEGIN beachpkg.array_in(:beach_in, :depth_in); END;",
{
beach_in:
{ type : oracledb.STRING,
dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
depth_in:
{ type : oracledb.NUMBER,
dir: oracledb.BIND_IN,
val: [45, 30, 67] }
},
function(err) {
if (err) {
return cb(err, conn);
} else {
return cb(null, conn);
}
});
};
// PL/SQL array bind OUT parameters:
// Fetch arrays of values from a PL/SQL procedure
var doout = function (conn, cb) {
conn.execute(
"BEGIN beachpkg.array_out(:beach_out, :depth_out); END;",
{
beach_out:
{ type: oracledb.STRING,
dir: oracledb.BIND_OUT,
maxArraySize: 3 },
depth_out:
{ type: oracledb.NUMBER,
dir: oracledb.BIND_OUT,
maxArraySize: 3 }
},
function (err, result) {
if (err) {
return cb(err, conn);
} else {
console.log("Binds returned:");
console.log(result.outBinds);
return cb(null, conn);
}
});
};
// PL/SQL array bind IN OUT parameters:
// Return input arrays sorted by beach name
var doinout = function (conn, cb) {
conn.execute(
"BEGIN beachpkg.array_inout(:beach_inout, :depth_inout); END;",
{
beach_inout:
{ type: oracledb.STRING,
dir: oracledb.BIND_INOUT,
val: ["Port Melbourne Beach", "Eighty Mile Beach", "Chesil Beach"],
maxArraySize: 3 },
depth_inout:
{ type: oracledb.NUMBER,
dir: oracledb.BIND_INOUT,
val: [8, 3, 70],
maxArraySize: 3 }
},
function (err, result) {
if (err) {
return cb(err, conn);
} else {
console.log("Binds returned:");
console.log(result.outBinds);
return cb(null, conn);
}
});
};
async.waterfall(
[
doconnect,
doin,
doout,
dorollback,
doinout
],
function (err, conn) {
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
if (conn)
dorelease(conn);
});