Skip to content

Commit 9675ffe

Browse files
committed
Add DBMS_OUTPUT examples
1 parent 6d7138f commit 9675ffe

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed

examples/dbmsoutputgetline.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* Copyright (c) 2015, 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+
* dbmsoutputgetline.js
20+
*
21+
* DESCRIPTION
22+
* Displays PL/SQL DBMS_OUTPUT in node-oracledb, fetching one record at a time.
23+
*
24+
*****************************************************************************/
25+
26+
var async = require('async');
27+
var oracledb = require('oracledb');
28+
var dbConfig = require('./dbconfig.js');
29+
30+
oracledb.createPool(
31+
{
32+
user : dbConfig.user,
33+
password : dbConfig.password,
34+
connectString : dbConfig.connectString
35+
},
36+
function(err, pool) {
37+
if (err)
38+
console.error(err.message);
39+
else
40+
doit(pool);
41+
});
42+
43+
var doit = function(pool) {
44+
async.waterfall(
45+
[
46+
function(cb) {
47+
pool.getConnection(cb);
48+
},
49+
enableDbmsOutput,
50+
createDbmsOutput,
51+
fetchDbmsOutputLine
52+
],
53+
function (err, conn) {
54+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
55+
conn.release(function (err) { if (err) console.error(err.message); });
56+
}
57+
);
58+
};
59+
60+
var enableDbmsOutput = function (conn, cb) {
61+
conn.execute(
62+
"BEGIN DBMS_OUTPUT.ENABLE(NULL); END;",
63+
function(err) { return cb(err, conn); });
64+
};
65+
66+
var createDbmsOutput = function (conn, cb) {
67+
conn.execute(
68+
"BEGIN " +
69+
"DBMS_OUTPUT.PUT_LINE('Hello, Oracle!')`;" +
70+
"DBMS_OUTPUT.PUT_LINE('Hello, Node!');" +
71+
"END;",
72+
function(err) { return cb(err, conn); });
73+
};
74+
75+
var fetchDbmsOutputLine = function (conn, cb) {
76+
conn.execute(
77+
"BEGIN DBMS_OUTPUT.GET_LINE(:ln, :st); END;",
78+
{ ln: { dir: oracledb.BIND_OUT, type: oracledb.STRING, maxSize: 32767 },
79+
st: { dir: oracledb.BIND_OUT, type: oracledb.NUMBER } },
80+
function(err, result) {
81+
if (err) {
82+
return cb(err, conn);
83+
} else if (result.outBinds.st == 1) {
84+
return cb(null, conn); // no more output
85+
} else {
86+
console.log(result.outBinds.ln);
87+
return fetchDbmsOutputLine(conn, cb);
88+
}
89+
});
90+
};

examples/dbmsoutputpipe.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Copyright (c) 2015, 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+
* dbmsoutputpipe.js
20+
*
21+
* DESCRIPTION
22+
* Displays PL/SQL DBMS_OUTPUT using a pipelined table.
23+
* Use demo.sql to create the dependencies or do:
24+
*
25+
* CREATE OR REPLACE TYPE dorow AS TABLE OF VARCHAR2(32767);
26+
* /
27+
* SHOW ERRORS
28+
*
29+
* CREATE OR REPLACE FUNCTION mydofetch RETURN dorow PIPELINED IS
30+
* line VARCHAR2(32767);
31+
* status INTEGER;
32+
* BEGIN LOOP
33+
* DBMS_OUTPUT.GET_LINE(line, status);
34+
* EXIT WHEN status = 1;
35+
* PIPE ROW (line);
36+
* END LOOP;
37+
* END;
38+
* /
39+
* SHOW ERRORS
40+
*
41+
*****************************************************************************/
42+
43+
var async = require('async');
44+
var oracledb = require('oracledb');
45+
var dbConfig = require('./dbconfig.js');
46+
47+
var numRows = 100; // fetch this many records at a time
48+
49+
oracledb.createPool(
50+
{
51+
user : dbConfig.user,
52+
password : dbConfig.password,
53+
connectString : dbConfig.connectString
54+
},
55+
function(err, pool) {
56+
if (err)
57+
console.error(err.message);
58+
else
59+
doit(pool);
60+
});
61+
62+
var doit = function(pool) {
63+
async.waterfall(
64+
[
65+
function(cb) {
66+
pool.getConnection(cb);
67+
},
68+
enableDbmsOutput,
69+
createDbmsOutput,
70+
fetchDbmsOutput,
71+
printDbmsOutput
72+
],
73+
function (err, conn) {
74+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
75+
conn.release(function (err) { if (err) console.error(err.message); });
76+
});
77+
};
78+
79+
var enableDbmsOutput = function (conn, cb) {
80+
conn.execute(
81+
"BEGIN DBMS_OUTPUT.ENABLE(NULL); END;",
82+
function(err) { return cb(err, conn); });
83+
};
84+
85+
var createDbmsOutput = function (conn, cb) {
86+
conn.execute(
87+
"BEGIN " +
88+
"DBMS_OUTPUT.PUT_LINE('Hello, Oracle!');" +
89+
"DBMS_OUTPUT.PUT_LINE('Hello, Node!');" +
90+
"END;",
91+
function(err) { return cb(err, conn); });
92+
};
93+
94+
var fetchDbmsOutput = function (conn, cb) {
95+
conn.execute(
96+
"SELECT * FROM TABLE(mydofetch())",
97+
[],
98+
{ resultSet: true },
99+
function (err, result) {
100+
if (err)
101+
return cb(err, conn);
102+
else
103+
return cb(null, conn, result);
104+
});
105+
};
106+
107+
var printDbmsOutput = function(conn, result, cb) {
108+
if (result.resultSet) {
109+
return fetchRowsFromRS(conn, result.resultSet, numRows, cb);
110+
} else {
111+
console.log("No results");
112+
return cb(null, conn);
113+
}
114+
};
115+
116+
var fetchRowsFromRS = function(conn, resultSet, numRows, cb) {
117+
resultSet.getRows(
118+
numRows,
119+
function (err, rows) {
120+
if (err) {
121+
return cb(err, conn);
122+
} else if (rows.length > 0) {
123+
console.log(rows);
124+
return fetchRowsFromRS(conn, resultSet, numRows, cb);
125+
} else {
126+
return cb(null, conn);
127+
}
128+
});
129+
};

examples/demo.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,20 @@ COMMIT;
8989
-- For LOB examples
9090
DROP TABLE mylobs;
9191
CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
92+
93+
-- For DBMS_OUTPUT example dbmsoutputpipe.js
94+
CREATE OR REPLACE TYPE dorow AS TABLE OF VARCHAR2(32767);
95+
/
96+
SHOW ERRORS
97+
98+
CREATE OR REPLACE FUNCTION mydofetch RETURN dorow PIPELINED IS
99+
line VARCHAR2(32767);
100+
status INTEGER;
101+
BEGIN LOOP
102+
DBMS_OUTPUT.GET_LINE(line, status);
103+
EXIT WHEN status = 1;
104+
PIPE ROW (line);
105+
END LOOP;
106+
END;
107+
/
108+
SHOW ERRORS

examples/demodrop.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ DROP TABLE j_purchaseorder_c;
3838
DROP TABLE dmlrupdtab;
3939

4040
DROP TABLE mylobs;
41+
42+
DROP TYPE dorow;
43+
44+
DROP FUNCTION mydofetch;

0 commit comments

Comments
 (0)