|
| 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 | +}; |
0 commit comments