Skip to content

Update examples for node-oracledb 4.1 #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 79 additions & 94 deletions javascript/node-oracledb/README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions javascript/node-oracledb/aqmulti.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ async function deq() {
const queue = await connection.getQueue(queueName);
queue.deqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE; // Change the visibility so no explicit commit is required

console.log('Dequeuing messages');

const messages = await queue.deqMany(5); // get at most 5 messages
console.log("Dequeued " + messages.length + " messages");
for (const msg of messages) {
Expand Down
2 changes: 1 addition & 1 deletion javascript/node-oracledb/aqobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Oracle Advanced Queuing (AQ) example passing an Oracle Database object
*
* Before running this, a queue allowing an Oracle Database object
* payloads must be created, see
* payload must be created, see
* https://oracle.github.io/node-oracledb/doc/api.html#aqobjexample
*
* This example requires node-oracledb 4 or later.
Expand Down
42 changes: 19 additions & 23 deletions javascript/node-oracledb/blobhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,33 @@
* Listens for an HTTP request and returns an image queried from a BLOB column
* Also shows the connection pool's caching using a 'default' pool.
*
* Use demo.sql to create the required table or do:
* DROP TABLE mylobs;
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
*
* Run lobinsert1.js to load an image before running this example.
*
* Start the listener with 'node blobhttp.js' and then use a browser
* to load http://localhost:7000/getimage
*
* This example uses Node 8's async/await syntax.
*
*****************************************************************************/

var url = require('url');
var http = require('http');
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
const url = require('url');
const http = require('http');
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
const demoSetup = require('./demosetup.js');

var httpPort = 7000;
const httpPort = 7000;

// Main entry point. Creates a connection pool which becomes the
// 'default' pool, and then creates an HTTP server.
async function init() {
try {
await oracledb.createPool(
{
user: dbConfig.user,
password: dbConfig.password,
connectString: dbConfig.connectString
});
await oracledb.createPool(dbConfig);
console.log('Connection pool started');

// create the demo table
const connection = await oracledb.getConnection();
await demoSetup.setupLobs(connection, true);
await connection.close();

// Create HTTP server and listen on port httpPort
const server = http.createServer();
server.on('error', (err) => {
Expand All @@ -63,7 +58,7 @@ async function init() {
handleRequest(request, response);
});
await server.listen(httpPort);
console.log("Server running. Try requesting: http://localhost:" + httpPort + "/getimage");
console.log("Server is running. Try loading http://localhost:" + httpPort + "/getimage");

} catch (err) {
console.error('init() error: ' + err.message);
Expand All @@ -84,14 +79,14 @@ async function handleRequest(request, response) {
connection = await oracledb.getConnection(); // gets a connection from the 'default' connection pool

const result = await connection.execute(
"SELECT b FROM mylobs WHERE id = :id", // get the image
"SELECT b FROM no_lobs WHERE id = :id", // get the image
{ id: 2 }
);
if (result.rows.length === 0) {
throw new Error("No results. Did you run lobinsert1.js?");
throw new Error("No data selected from table.");
}

var lob = result.rows[0][0];
const lob = result.rows[0][0];
if (lob === null) {
throw new Error("BLOB was NULL");
}
Expand All @@ -117,6 +112,7 @@ async function handleRequest(request, response) {

} catch (err) {
console.error(err);
await closePoolAndExit();
} finally {
if (connection) {
try {
Expand All @@ -137,9 +133,9 @@ async function closePoolAndExit() {
console.log('\nTerminating');
try {
// Get the pool from the pool cache and close it when no
// connections are in use, or force it closed after 10 seconds
// connections are in use, or force it closed after 2 seconds
// If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file
await oracledb.getPool().close(10);
await oracledb.getPool().close(2);
console.log('Pool closed');
process.exit(0);
} catch(err) {
Expand Down
19 changes: 17 additions & 2 deletions javascript/node-oracledb/calltimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* Shows how to time out long running database calls.
* See https://oracle.github.io/node-oracledb/doc/api.html#dbcalltimeouts
*
* This example requires node-oracledb 3 or later.
* Node-oracledb must be using Oracle Client 18c libraries, or greater.
* This example requires node-oracledb 3 (or later) and Oracle Client 18c
* libraries (or later).
*
* This example uses Node 8's async/await syntax.
*
Expand All @@ -40,6 +40,11 @@ async function run() {
let connection;

try {

if (oracledb.oracleClientVersion < 1800000000) {
throw new Error("Oracle Client libraries must be 18c or later");
}

connection = await oracledb.getConnection(dbConfig);

connection.callTimeout = timeout * 1000; // milliseconds
Expand All @@ -66,4 +71,14 @@ async function run() {
}
}

process
.on('SIGTERM', function() {
console.log("\nTerminating");
process.exit(0);
})
.on('SIGINT', function() {
console.log("\nTerminating");
process.exit(0);
});

run();
2 changes: 1 addition & 1 deletion javascript/node-oracledb/connectionpool.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function init() {
password: dbConfig.password,
connectString: dbConfig.connectString
// edition: 'ORA$BASE', // used for Edition Based Redefintion
// events: true, // whether to handle Oracle Database FAN and RLB events or support CQN
// events: false, // whether to handle Oracle Database FAN and RLB events or support CQN
// externalAuth: false, // whether connections should be established using External Authentication
// homogeneous: true, // all connections in the pool have the same credentials
// poolAlias: 'default', // set an alias to allow access to the pool via a name.
Expand Down
24 changes: 22 additions & 2 deletions javascript/node-oracledb/cqn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* Run this script and when the subscription has been created, run
* these statements in a SQL*Plus session:
* INSERT INTO CQNTABLE VALUES (101);
* INSERT INTO NO_CQNTABLE VALUES (101);
* COMMIT;
*
* This example requires node-oracledb 2.3 or later.
Expand Down Expand Up @@ -81,20 +81,40 @@ function myCallback(message)

const options = {
callback : myCallback,
sql: "SELECT * FROM cqntable WHERE k > :bv",
sql: `SELECT * FROM no_cqntable WHERE k > :bv`,
binds: { bv : 100 },
timeout : 60, // Stop after 60 seconds
// ipAddress: '127.0.0.1',
// SUBSCR_QOS_QUERY: generate notifications when rows with k > 100 are changed
// SUBSCR_QOS_ROWIDS: Return ROWIDs in the notification message
qos : oracledb.SUBSCR_QOS_QUERY | oracledb.SUBSCR_QOS_ROWIDS
};

async function setup(connection) {
const stmts = [
`DROP TABLE no_cqntable`,

`CREATE TABLE no_cqntable (k NUMBER)`
];

for (const s of stmts) {
try {
await connection.execute(s);
} catch(e) {
if (e.errorNum != 942)
console.error(e);
}
}
}

async function runTest() {
let connection;

try {
connection = await oracledb.getConnection(dbConfig);

await setup(connection);

await connection.subscribe('mysub', options);

console.log("Subscription created...");
Expand Down
26 changes: 23 additions & 3 deletions javascript/node-oracledb/cqn2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* Run this script and when the subscription has been created, run
* these statements in a SQL*Plus session:
* INSERT INTO CQNTABLE VALUES (1);
* INSERT INTO NO_CQNTABLE VALUES (1);
* COMMIT;
*
* This example requires node-oracledb 2.3 or later.
Expand All @@ -40,7 +40,7 @@
const oracledb = require("oracledb");
const dbConfig = require('./dbconfig.js');

// dbConfig.events = true; // CQN needs events mode, which is true by default in 4.0
dbConfig.events = true; // CQN needs events mode

const interval = setInterval(function() {
console.log("waiting...");
Expand Down Expand Up @@ -77,7 +77,8 @@ function myCallback(message)

const options = {
callback : myCallback,
sql: "SELECT * FROM cqntable",
sql: "SELECT * FROM no_cqntable",
// ipAddress: '127.0.0.1',
// Stop after 60 seconds
timeout : 60,
// Return ROWIDs in the notification message
Expand All @@ -89,12 +90,31 @@ const options = {
groupingType : oracledb.SUBSCR_GROUPING_TYPE_SUMMARY
};

async function setup(connection) {
const stmts = [
`DROP TABLE no_cqntable`,

`CREATE TABLE no_cqntable (k NUMBER)`
];

for (const s of stmts) {
try {
await connection.execute(s);
} catch(e) {
if (e.errorNum != 942)
console.error(e);
}
}
}

async function runTest() {
let connection;

try {
connection = await oracledb.getConnection(dbConfig);

await setup(connection);

await connection.subscribe('mysub', options);

console.log("Subscription created...");
Expand Down
40 changes: 20 additions & 20 deletions javascript/node-oracledb/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,32 @@ async function run() {
connection = await oracledb.getConnection(dbConfig);

console.log('Creating table');
await connection.execute(
`BEGIN
DECLARE
e_table_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(e_table_exists, -00942);
BEGIN
EXECUTE IMMEDIATE ('DROP TABLE datetest');
EXCEPTION
WHEN e_table_exists
THEN NULL;
END;
END;`);

await connection.execute(
`CREATE TABLE datetest(

const stmts = [
`DROP TABLE no_datetab`,

`CREATE TABLE no_datetab(
id NUMBER,
timestampcol TIMESTAMP,
timestamptz TIMESTAMP WITH TIME ZONE,
timestampltz TIMESTAMP WITH LOCAL TIME ZONE,
datecol DATE)`);
datecol DATE)`
];

for (const s of stmts) {
try {
await connection.execute(s);
} catch(e) {
if (e.errorNum != 942)
console.error(e);
}
}

// When bound, JavaScript Dates are inserted using TIMESTAMP WITH LOCAL TIMEZONE
date = new Date();
console.log('Inserting JavaScript date: ' + date);
result = await connection.execute(
`INSERT INTO datetest (id, timestampcol, timestamptz, timestampltz, datecol)
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
VALUES (1, :ts, :tstz, :tsltz, :td)`,
{ ts: date, tstz: date, tsltz: date, td: date });
console.log('Rows inserted: ' + result.rowsAffected );
Expand All @@ -81,7 +81,7 @@ async function run() {
result = await connection.execute(
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
FROM datetest
FROM no_datetab
ORDER BY id`);
console.log(result.rows);

Expand All @@ -91,7 +91,7 @@ async function run() {
date = new Date();
console.log('Inserting JavaScript date: ' + date);
result = await connection.execute(
`INSERT INTO datetest (id, timestampcol, timestamptz, timestampltz, datecol)
`INSERT INTO no_datetab (id, timestampcol, timestamptz, timestampltz, datecol)
VALUES (2, :ts, :tstz, :tsltz, :td)`,
{ ts: date, tstz: date, tsltz: date, td: date });
console.log('Rows inserted: ' + result.rowsAffected );
Expand All @@ -100,7 +100,7 @@ async function run() {
result = await connection.execute(
`SELECT id, timestampcol, timestamptz, timestampltz, datecol,
TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD
FROM datetest
FROM no_datetab
ORDER BY id`);
console.log(result.rows);

Expand Down
Loading