Skip to content

Commit 3c5f3cd

Browse files
committed
Add example for PL/SQL Associative Array bind
1 parent e794b73 commit 3c5f3cd

File tree

3 files changed

+194
-2
lines changed

3 files changed

+194
-2
lines changed

examples/demo.sql

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -59,6 +59,53 @@ END;
5959
/
6060
SHOW ERRORS
6161

62+
-- For plsqlarray.js example for PL/SQL 'INDEX BY' array binds
63+
DROP TABLE waveheight;
64+
CREATE TABLE waveheight (beach VARCHAR2(50), depth NUMBER);
65+
66+
CREATE OR REPLACE PACKAGE beachpkg IS
67+
TYPE beachType IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
68+
TYPE depthType IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
69+
PROCEDURE array_in(beaches IN beachType, depths IN depthType);
70+
PROCEDURE array_out(beaches OUT beachType, depths OUT depthType);
71+
PROCEDURE array_inout(beaches IN OUT beachType, depths IN OUT depthType);
72+
END;
73+
/
74+
SHOW ERRORS
75+
76+
CREATE OR REPLACE PACKAGE BODY beachpkg IS
77+
78+
-- Insert array values into a table
79+
PROCEDURE array_in(beaches IN beachType, depths IN depthType) IS
80+
BEGIN
81+
IF beaches.COUNT <> depths.COUNT THEN
82+
RAISE_APPLICATION_ERROR(-20000, 'Array lengths must match for this example.');
83+
END IF;
84+
FORALL i IN INDICES OF beaches
85+
INSERT INTO waveheight (beach, depth) VALUES (beaches(i), depths(i));
86+
END;
87+
88+
-- Return the values from a table
89+
PROCEDURE array_out(beaches OUT beachType, depths OUT depthType) IS
90+
BEGIN
91+
SELECT beach, depth BULK COLLECT INTO beaches, depths FROM waveheight;
92+
END;
93+
94+
-- Return the arguments sorted
95+
PROCEDURE array_inout(beaches IN OUT beachType, depths IN OUT depthType) IS
96+
BEGIN
97+
IF beaches.COUNT <> depths.COUNT THEN
98+
RAISE_APPLICATION_ERROR(-20001, 'Array lengths must match for this example.');
99+
END IF;
100+
FORALL i IN INDICES OF beaches
101+
INSERT INTO waveheight (beach, depth) VALUES (beaches(i), depths(i));
102+
SELECT beach, depth BULK COLLECT INTO beaches, depths FROM waveheight ORDER BY 1;
103+
END;
104+
105+
END;
106+
/
107+
SHOW ERRORS
108+
62109
-- For selectjson.js example of JSON datatype. Requires Oracle Database 12.1.0.2
63110
DROP TABLE j_purchaseorder;
64111
-- Note if your applications always insert valid JSON, you may delete

examples/demodrop.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -31,6 +31,8 @@ DROP FUNCTION testfunc;
3131

3232
DROP PROCEDURE get_emp_rs;
3333

34+
DROP PACKAGE beachpkg;
35+
3436
DROP TABLE j_purchaseorder;
3537

3638
DROP TABLE j_purchaseorder_c;
@@ -44,3 +46,5 @@ DROP TYPE dorow;
4446
DROP FUNCTION mydofetch;
4547

4648
DROP TABLE myraw;
49+
50+
DROP TABLE waveheight;

examples/plsqlarray.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)