Add DBMS_OUTPUT examples

This commit is contained in:
Christopher Jones 2015-09-02 21:12:54 +10:00
parent 6d7138f8f4
commit 9675ffe378
4 changed files with 240 additions and 0 deletions

View File

@ -0,0 +1,90 @@
/* Copyright (c) 2015, 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
* dbmsoutputgetline.js
*
* DESCRIPTION
* Displays PL/SQL DBMS_OUTPUT in node-oracledb, fetching one record at a time.
*
*****************************************************************************/
var async = require('async');
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
oracledb.createPool(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, pool) {
if (err)
console.error(err.message);
else
doit(pool);
});
var doit = function(pool) {
async.waterfall(
[
function(cb) {
pool.getConnection(cb);
},
enableDbmsOutput,
createDbmsOutput,
fetchDbmsOutputLine
],
function (err, conn) {
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
conn.release(function (err) { if (err) console.error(err.message); });
}
);
};
var enableDbmsOutput = function (conn, cb) {
conn.execute(
"BEGIN DBMS_OUTPUT.ENABLE(NULL); END;",
function(err) { return cb(err, conn); });
};
var createDbmsOutput = function (conn, cb) {
conn.execute(
"BEGIN " +
"DBMS_OUTPUT.PUT_LINE('Hello, Oracle!')`;" +
"DBMS_OUTPUT.PUT_LINE('Hello, Node!');" +
"END;",
function(err) { return cb(err, conn); });
};
var fetchDbmsOutputLine = function (conn, cb) {
conn.execute(
"BEGIN DBMS_OUTPUT.GET_LINE(:ln, :st); END;",
{ ln: { dir: oracledb.BIND_OUT, type: oracledb.STRING, maxSize: 32767 },
st: { dir: oracledb.BIND_OUT, type: oracledb.NUMBER } },
function(err, result) {
if (err) {
return cb(err, conn);
} else if (result.outBinds.st == 1) {
return cb(null, conn); // no more output
} else {
console.log(result.outBinds.ln);
return fetchDbmsOutputLine(conn, cb);
}
});
};

129
examples/dbmsoutputpipe.js Normal file
View File

@ -0,0 +1,129 @@
/* Copyright (c) 2015, 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
* dbmsoutputpipe.js
*
* DESCRIPTION
* Displays PL/SQL DBMS_OUTPUT using a pipelined table.
* Use demo.sql to create the dependencies or do:
*
* CREATE OR REPLACE TYPE dorow AS TABLE OF VARCHAR2(32767);
* /
* SHOW ERRORS
*
* CREATE OR REPLACE FUNCTION mydofetch RETURN dorow PIPELINED IS
* line VARCHAR2(32767);
* status INTEGER;
* BEGIN LOOP
* DBMS_OUTPUT.GET_LINE(line, status);
* EXIT WHEN status = 1;
* PIPE ROW (line);
* END LOOP;
* END;
* /
* SHOW ERRORS
*
*****************************************************************************/
var async = require('async');
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
var numRows = 100; // fetch this many records at a time
oracledb.createPool(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, pool) {
if (err)
console.error(err.message);
else
doit(pool);
});
var doit = function(pool) {
async.waterfall(
[
function(cb) {
pool.getConnection(cb);
},
enableDbmsOutput,
createDbmsOutput,
fetchDbmsOutput,
printDbmsOutput
],
function (err, conn) {
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
conn.release(function (err) { if (err) console.error(err.message); });
});
};
var enableDbmsOutput = function (conn, cb) {
conn.execute(
"BEGIN DBMS_OUTPUT.ENABLE(NULL); END;",
function(err) { return cb(err, conn); });
};
var createDbmsOutput = function (conn, cb) {
conn.execute(
"BEGIN " +
"DBMS_OUTPUT.PUT_LINE('Hello, Oracle!');" +
"DBMS_OUTPUT.PUT_LINE('Hello, Node!');" +
"END;",
function(err) { return cb(err, conn); });
};
var fetchDbmsOutput = function (conn, cb) {
conn.execute(
"SELECT * FROM TABLE(mydofetch())",
[],
{ resultSet: true },
function (err, result) {
if (err)
return cb(err, conn);
else
return cb(null, conn, result);
});
};
var printDbmsOutput = function(conn, result, cb) {
if (result.resultSet) {
return fetchRowsFromRS(conn, result.resultSet, numRows, cb);
} else {
console.log("No results");
return cb(null, conn);
}
};
var fetchRowsFromRS = function(conn, resultSet, numRows, cb) {
resultSet.getRows(
numRows,
function (err, rows) {
if (err) {
return cb(err, conn);
} else if (rows.length > 0) {
console.log(rows);
return fetchRowsFromRS(conn, resultSet, numRows, cb);
} else {
return cb(null, conn);
}
});
};

View File

@ -89,3 +89,20 @@ COMMIT;
-- For LOB examples
DROP TABLE mylobs;
CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
-- For DBMS_OUTPUT example dbmsoutputpipe.js
CREATE OR REPLACE TYPE dorow AS TABLE OF VARCHAR2(32767);
/
SHOW ERRORS
CREATE OR REPLACE FUNCTION mydofetch RETURN dorow PIPELINED IS
line VARCHAR2(32767);
status INTEGER;
BEGIN LOOP
DBMS_OUTPUT.GET_LINE(line, status);
EXIT WHEN status = 1;
PIPE ROW (line);
END LOOP;
END;
/
SHOW ERRORS

View File

@ -38,3 +38,7 @@ DROP TABLE j_purchaseorder_c;
DROP TABLE dmlrupdtab;
DROP TABLE mylobs;
DROP TYPE dorow;
DROP FUNCTION mydofetch;