diff --git a/javascript/node-oracledb/README.md b/javascript/node-oracledb/README.md index 634b4fd8..d4274066 100644 --- a/javascript/node-oracledb/README.md +++ b/javascript/node-oracledb/README.md @@ -1,16 +1,16 @@ # Node-oracledb Examples -This directory contains [node-oracledb 2.0](https://www.npmjs.com/package/oracledb) examples. +This directory contains [node-oracledb 2.3](https://www.npmjs.com/package/oracledb) examples. The node-oracledb add-on for Node.js powers high performance Oracle Database applications. -[Node-oracledb documentation](https://github.com/oracle/node-oracledb/blob/master/doc/api.md) +[Node-oracledb documentation](https://oracle.github.io/node-oracledb/doc/api.html) [Issues and questions](https://github.com/oracle/node-oracledb/issues) To run the examples: -- [Install node-oracledb](https://github.com/oracle/node-oracledb/blob/master/INSTALL.md). +- [Install node-oracledb](https://oracle.github.io/node-oracledb/INSTALL.html). - Use `demo.sql` to create schema objects used by the samples. For diff --git a/javascript/node-oracledb/blobhttp.js b/javascript/node-oracledb/blobhttp.js index 4205a224..aba442b9 100644 --- a/javascript/node-oracledb/blobhttp.js +++ b/javascript/node-oracledb/blobhttp.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -74,8 +74,7 @@ function handleRequest(request, response) { if (action == '/getimage') { oracledb.getConnection( // gets a connection from the 'default' connection pool - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; @@ -84,8 +83,7 @@ function handleRequest(request, response) { connection.execute( "SELECT b FROM mylobs WHERE id = :id", // get the image { id: 2 }, - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); return; @@ -104,15 +102,13 @@ function handleRequest(request, response) { lob.on( 'end', - function() - { + function() { console.log("lob.on 'end' event"); response.end(); }); lob.on( 'close', - function() - { + function() { console.log("lob.on 'close' event"); connection.close(function(err) { if (err) console.error(err); @@ -120,8 +116,7 @@ function handleRequest(request, response) { }); lob.on( 'error', - function(err) - { + function(err) { console.log("lob.on 'error' event"); console.error(err); connection.close(function(err) { diff --git a/javascript/node-oracledb/clobexample.txt b/javascript/node-oracledb/clobexample.txt index 504cc45d..95519f99 100644 --- a/javascript/node-oracledb/clobexample.txt +++ b/javascript/node-oracledb/clobexample.txt @@ -2,4 +2,4 @@ This is example text used for node-oracledb CLOB examples. The Oracle Database Node.js driver powers high performance Node.js applications. -The node-oracledb home page is at http://www.oracle.com/technetwork/database/database-technologies/scripting-languages/node_js/ +The node-oracledb home page is at http://oracle.github.io/node-oracledb/ diff --git a/javascript/node-oracledb/connect.js b/javascript/node-oracledb/connect.js index 6c58b5ee..a1afd51b 100644 --- a/javascript/node-oracledb/connect.js +++ b/javascript/node-oracledb/connect.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -33,8 +33,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; @@ -42,8 +41,7 @@ oracledb.getConnection( console.log('Connection was successful!'); connection.close( - function(err) - { + function(err) { if (err) { console.error(err.message); return; diff --git a/javascript/node-oracledb/cqn1.js b/javascript/node-oracledb/cqn1.js new file mode 100644 index 00000000..6beffd5a --- /dev/null +++ b/javascript/node-oracledb/cqn1.js @@ -0,0 +1,119 @@ +/* Copyright (c) 2018, 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 + * cqn1.js + * + * DESCRIPTION + * Shows query-level Continuous Query Notification, allowing a + * method to be invoked when a data set changes. + * + * The user must have been granted CHANGE NOTIFICATION. + * The node-oracledb host must be resolvable by the database host. + * + * This example uses Node 8 syntax, but could be written to use callbacks. + * + *****************************************************************************/ + +const oracledb = require("oracledb"); +let dbConfig = require('./dbconfig.js'); + +dbConfig.events = true; // CQN needs events mode + +let interval = setInterval(function() { + console.log("waiting..."); +}, 5000); + +function myCallback(message) +{ + // message.type is one of the oracledb.SUBSCR_EVENT_TYPE_* values + console.log("Message type:", message.type); + if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) { + clearInterval(interval); + console.log("Deregistration has taken place..."); + return; + } + console.log("Message database name:", message.dbName); + console.log("Message transaction id:", message.txId); + console.log("Message queries:"); + for (let i = 0; i < message.queries.length; i++) { + let query = message.queries[i]; + for (let j = 0; j < query.tables.length; j++) { + let table = query.tables[j]; + console.log("--> --> Table Name:", table.name); + // Note table.operation and row.operation are masks of + // oracledb.CQN_OPCODE_* values + console.log("--> --> Table Operation:", table.operation); + if (table.rows) { + console.log("--> --> Table Rows:"); + for (let k = 0; k < table.rows.length; k++) { + let row = table.rows[k]; + console.log("--> --> --> Row Rowid:", row.rowid); + console.log("--> --> --> Row Operation:", row.operation); + console.log(Array(61).join("-")); + } + } + } + console.log(Array(61).join("=")); + } +} + +const options = { + callback : myCallback, + sql: "SELECT * FROM cqntable WHERE k > :bv", + binds: { bv : 100 }, + timeout : 60, // Stop after 60 seconds + // 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 runTest() { + let conn; + + try { + conn = await oracledb.getConnection(dbConfig); + + await conn.subscribe('mysub', options); + + console.log("Subscription created..."); + + } catch (err) { + console.error(err); + clearInterval(interval); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +process + .on('SIGTERM', function() { + console.log("\nTerminating"); + process.exit(0); + }) + .on('SIGINT', function() { + console.log("\nTerminating"); + process.exit(0); + }); + +runTest(); diff --git a/javascript/node-oracledb/cqn2.js b/javascript/node-oracledb/cqn2.js new file mode 100644 index 00000000..cd49b2bb --- /dev/null +++ b/javascript/node-oracledb/cqn2.js @@ -0,0 +1,119 @@ +/* Copyright (c) 2018, 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 + * cqn2.js + * + * DESCRIPTION + * Shows object-level Continuous Query Notification (formerly known + * as Database Change Notification), allowing a method to be invoked + * when an object changes. Notification are grouped into intervals. + * + * The user must have been granted CHANGE NOTIFICATION. + * The node-oracledb host must be resolvable by the database host. + * + * This example uses Node 8 syntax, but could be written to use callbacks. + * + *****************************************************************************/ + +const oracledb = require("oracledb"); +let dbConfig = require('./dbconfig.js'); + +dbConfig.events = true; // CQN needs events mode + +let interval = setInterval(function() { + console.log("waiting..."); +}, 5000); + +function myCallback(message) +{ + // message.type is one of the oracledb.SUBSCR_EVENT_TYPE_* values + console.log("Message type:", message.type); + if (message.type == oracledb.SUBSCR_EVENT_TYPE_DEREG) { + clearInterval(interval); + console.log("Deregistration has taken place..."); + return; + } + console.log("Message database name:", message.dbName); + console.log("Message transaction id:", message.txId); + for (let i = 0; i < message.tables.length; i++) { + let table = message.tables[i]; + console.log("--> Table Name:", table.name); + // Note table.operation and row.operation are masks of + // oracledb.CQN_OPCODE_* values + console.log("--> Table Operation:", table.operation); + if (table.rows) { + for (let j = 0; j < table.rows.length; j++) { + let row = table.rows[j]; + console.log("--> --> Row Rowid:", row.rowid); + console.log("--> --> Row Operation:", row.operation); + console.log(Array(61).join("-")); + } + } + console.log(Array(61).join("=")); + } +} + +const options = { + callback : myCallback, + sql: "SELECT * FROM cqntable", + // Stop after 60 seconds + timeout : 60, + // Return ROWIDs in the notification message + qos : oracledb.SUBSCR_QOS_ROWIDS, + // Group notifications in batches covering 10 second + // intervals, and send a summary + groupingClass : oracledb.SUBSCR_GROUPING_CLASS_TIME, + groupingValue : 10, + groupingType : oracledb.SUBSCR_GROUPING_TYPE_SUMMARY +}; + +async function runTest() { + let conn; + + try { + conn = await oracledb.getConnection(dbConfig); + + await conn.subscribe('mysub', options); + + console.log("Subscription created..."); + + } catch (err) { + console.error(err); + clearInterval(interval); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +process + .on('SIGTERM', function() { + console.log("\nTerminating"); + process.exit(0); + }) + .on('SIGINT', function() { + console.log("\nTerminating"); + process.exit(0); + }); + +runTest(); diff --git a/javascript/node-oracledb/dbconfig.js b/javascript/node-oracledb/dbconfig.js index a1bc6f17..b3f155c9 100644 --- a/javascript/node-oracledb/dbconfig.js +++ b/javascript/node-oracledb/dbconfig.js @@ -23,6 +23,8 @@ * to the database. Production applications should consider using * External Authentication to avoid hard coded credentials. * + * To create a database user see https://www.youtube.com/watch?v=WDJacg0NuLo + * * Applications can set the connectString value to an Easy Connect * string, or a Net Service Name from a tnsnames.ora file or * external naming service, or it can be the name of a local Oracle @@ -75,10 +77,10 @@ module.exports = { password : process.env.NODE_ORACLEDB_PASSWORD || "welcome", // For information on connection strings see: - // https://github.com/oracle/node-oracledb/blob/master/doc/api.md#connectionstrings + // https://oracle.github.io/node-oracledb/doc/api.html#connectionstrings connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING || "localhost/orclpdb", // Setting externalAuth is optional. It defaults to false. See: - // https://github.com/oracle/node-oracledb/blob/master/doc/api.md#extauth + // https://oracle.github.io/node-oracledb/doc/api.html#extauth externalAuth : process.env.NODE_ORACLEDB_EXTERNALAUTH ? true : false }; diff --git a/javascript/node-oracledb/demo.sql b/javascript/node-oracledb/demo.sql index c454a47c..545de182 100644 --- a/javascript/node-oracledb/demo.sql +++ b/javascript/node-oracledb/demo.sql @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -19,7 +19,10 @@ * demo.sql * * DESCRIPTION - * Create database objects for the examples + * Create database objects for the examples in your database user account. + * + * The video https://www.youtube.com/watch?v=WDJacg0NuLo + * shows how to create a new database user. * * Scripts to create Oracle Database's traditional sample schemas can * be found at: https://github.com/oracle/db-sample-schemas @@ -127,7 +130,6 @@ BEGIN EXECUTE IMMEDIATE 'DROP TABLE dmlrupdtab'; EXCEPTION WHEN OTHERS THEN IF S CREATE TABLE dmlrupdtab (id NUMBER, name VARCHAR2(40)); INSERT INTO dmlrupdtab VALUES (1001, 'Venkat'); INSERT INTO dmlrupdtab VALUES (1002, 'Neeharika'); -COMMIT; -- For LOB examples BEGIN EXECUTE IMMEDIATE 'DROP TABLE mylobs'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END; @@ -183,3 +185,76 @@ SHOW ERRORS BEGIN EXECUTE IMMEDIATE 'DROP TABLE myraw'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END; / CREATE TABLE myraw (r RAW(64)); + +-- For the executemany*.js examples + +BEGIN EXECUTE IMMEDIATE 'DROP TABLE em_tab'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END; +/ +BEGIN EXECUTE IMMEDIATE 'DROP TABLE em_childtab'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END; +/ +BEGIN EXECUTE IMMEDIATE 'DROP TABLE em_parenttab'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END; +/ + +CREATE TABLE em_tab ( + id NUMBER NOT NULL, + val VARCHAR2(20) +); + +CREATE TABLE em_parenttab ( + parentid NUMBER NOT NULL, + description VARCHAR2(60) NOT NULL, + CONSTRAINT parenttab_pk PRIMARY KEY (parentid) +); + +CREATE TABLE em_childtab ( + childid NUMBER NOT NULL, + parentid NUMBER NOT NULL, + description VARCHAR2(30) NOT NULL, + CONSTRAINT em_childtab_pk PRIMARY KEY (childid), + CONSTRAINT em_childtab_fk FOREIGN KEY (parentid) REFERENCES em_parenttab +); + +INSERT INTO em_parenttab VALUES (10, 'Parent 10'); +INSERT INTO em_parenttab VALUES (20, 'Parent 20'); +INSERT INTO em_parenttab VALUES (30, 'Parent 30'); +INSERT INTO em_parenttab VALUES (40, 'Parent 40'); +INSERT INTO em_parenttab VALUES (50, 'Parent 50'); + +INSERT INTO em_childtab VALUES (1001, 10, 'Child 1001 of Parent 10'); +INSERT INTO em_childtab VALUES (1002, 20, 'Child 1002 of Parent 20'); +INSERT INTO em_childtab VALUES (1003, 20, 'Child 1003 of Parent 20'); +INSERT INTO em_childtab VALUES (1004, 20, 'Child 1004 of Parent 20'); +INSERT INTO em_childtab VALUES (1005, 30, 'Child 1005 of Parent 30'); +INSERT INTO em_childtab VALUES (1006, 30, 'Child 1006 of Parent 30'); +INSERT INTO em_childtab VALUES (1007, 40, 'Child 1007 of Parent 40'); +INSERT INTO em_childtab VALUES (1008, 40, 'Child 1008 of Parent 40'); +INSERT INTO em_childtab VALUES (1009, 40, 'Child 1009 of Parent 40'); +INSERT INTO em_childtab VALUES (1010, 40, 'Child 1010 of Parent 40'); +INSERT INTO em_childtab VALUES (1011, 40, 'Child 1011 of Parent 40'); +INSERT INTO em_childtab VALUES (1012, 50, 'Child 1012 of Parent 50'); +INSERT INTO em_childtab VALUES (1013, 50, 'Child 1013 of Parent 50'); +INSERT INTO em_childtab VALUES (1014, 50, 'Child 1014 of Parent 50'); +INSERT INTO em_childtab VALUES (1015, 50, 'Child 1015 of Parent 50'); + +CREATE OR REPLACE PROCEDURE em_testproc ( + a_num IN NUMBER, + a_outnum OUT NUMBER, + a_outstr OUT VARCHAR2) +AS +BEGIN + a_outnum := a_num * 2; + FOR i IN 1..a_num LOOP + a_outstr := a_outstr || 'X'; + END LOOP; +END; +/ + +COMMIT; + + +-- For the cqn*.js examples + +-- The DBA must grant access: +-- GRANT CHANGE NOTIFICATION TO myuser; + +create table cqntable (k number); diff --git a/javascript/node-oracledb/demodrop.sql b/javascript/node-oracledb/demodrop.sql index 68fea34e..16cbf5c0 100644 --- a/javascript/node-oracledb/demodrop.sql +++ b/javascript/node-oracledb/demodrop.sql @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -54,3 +54,13 @@ DROP PROCEDURE lob_in_out; DROP PROCEDURE lobs_in; DROP PROCEDURE lobs_out; + +DROP TABLE em_tab; + +DROP TABLE em_childtab; + +DROP TABLE em_parenttab; + +DROP PROCEDURE em_testproc; + +DROP TABLE cqntable; diff --git a/javascript/node-oracledb/dmlrupd1.js b/javascript/node-oracledb/dmlrupd1.js index 63ed2e9f..37c0a2d3 100644 --- a/javascript/node-oracledb/dmlrupd1.js +++ b/javascript/node-oracledb/dmlrupd1.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -42,10 +42,8 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { - if (err) - { + function(err, connection) { + if (err) { console.error(err); return; } @@ -58,10 +56,8 @@ oracledb.getConnection( rid: { type: oracledb.STRING, dir: oracledb.BIND_OUT } }, { autoCommit: true }, - function(err, result) - { - if (err) - { + function(err, result) { + if (err) { console.error(err); return; } diff --git a/javascript/node-oracledb/dmlrupd2.js b/javascript/node-oracledb/dmlrupd2.js index 167bbb69..28d775bd 100644 --- a/javascript/node-oracledb/dmlrupd2.js +++ b/javascript/node-oracledb/dmlrupd2.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -42,10 +42,8 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { - if (err) - { + function(err, connection) { + if (err) { console.error(err); return; } @@ -60,10 +58,8 @@ oracledb.getConnection( rids: { type: oracledb.STRING, dir: oracledb.BIND_OUT } }, { autoCommit: true }, - function(err, result) - { - if (err) - { + function(err, result) { + if (err) { console.error(err); return; } diff --git a/javascript/node-oracledb/em_batcherrors.js b/javascript/node-oracledb/em_batcherrors.js new file mode 100644 index 00000000..1a1ec150 --- /dev/null +++ b/javascript/node-oracledb/em_batcherrors.js @@ -0,0 +1,89 @@ +/* Copyright (c) 2018, 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 + * em_batcherrors.js + * + * DESCRIPTION + * Array DML example showing batchErrors behavior. Note, despite the + * autoCommit flag, no commit occurs because of data errors. However + * valid rows are part of a transaction that can be committed if + * desired. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var doinsert = function(conn, cb) { + var sql = "INSERT INTO em_childtab VALUES (:1, :2, :3)"; + + var binds = [ + [1016, 10, "Child 2 of Parent A"], + [1017, 10, "Child 3 of Parent A"], + [1018, 20, "Child 4 of Parent B"], + [1018, 20, "Child 4 of Parent B"], // duplicate key + [1019, 30, "Child 3 of Parent C"], + [1020, 40, "Child 4 of Parent D"], + [1021, 75, "Child 1 of Parent F"], // parent does not exist + [1022, 40, "Child 6 of Parent D"] + ]; + + var options = { + autoCommit: true, + batchErrors: true, + dmlRowCounts: true, + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 20 } + ] + }; + + conn.executeMany(sql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("Result is:", result); + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + doinsert + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_batcherrors_aa.js b/javascript/node-oracledb/em_batcherrors_aa.js new file mode 100644 index 00000000..168915e8 --- /dev/null +++ b/javascript/node-oracledb/em_batcherrors_aa.js @@ -0,0 +1,87 @@ +/* Copyright (c) 2018, 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 + * em_batcherrors_aa.js + * + * DESCRIPTION + * Array DML example showing batchErrors behavior. Note, despite the + * autoCommit flag, no commit occurs because of data errors. However + * valid rows are part of a transaction that can be committed if + * desired. + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const sql = "INSERT INTO em_childtab VALUES (:1, :2, :3)"; + +const binds = [ + [1016, 10, "Child 2 of Parent A"], + [1017, 10, "Child 3 of Parent A"], + [1018, 20, "Child 4 of Parent B"], + [1018, 20, "Child 4 of Parent B"], // duplicate key + [1019, 30, "Child 3 of Parent C"], + [1020, 40, "Child 4 of Parent D"], + [1021, 75, "Child 1 of Parent F"], // parent does not exist + [1022, 40, "Child 6 of Parent D"] +]; + +const options = { + autoCommit: true, + batchErrors: true, + dmlRowCounts: true, + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 20 } + ] +}; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection( + { + user : dbConfig.user, + password : dbConfig.password, + connectString : dbConfig.connectString + }); + + result = await conn.executeMany(sql, binds, options); + + console.log("Result is:", result); + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/em_dmlreturn1.js b/javascript/node-oracledb/em_dmlreturn1.js new file mode 100644 index 00000000..e58f8fb2 --- /dev/null +++ b/javascript/node-oracledb/em_dmlreturn1.js @@ -0,0 +1,93 @@ +/* Copyright (c) 2018, 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 + * em_dmlreturn1.js + * + * DESCRIPTION + * executeMany() example of DML RETURNING that returns single values + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var dotruncate = function(conn, cb) { + conn.execute("TRUNCATE TABLE em_tab", function (err) { + return cb(err, conn); + }); +}; + +var doinsert = function(conn, cb) { + var sql = "INSERT INTO em_tab VALUES (:1, :2) RETURNING ROWID, id, val INTO :3, :4, :5"; + + var binds = [ + [1, "Test 1 (One)"], + [2, "Test 2 (Two)"], + [3, "Test 3 (Three)"], + [4, null], + [5, "Test 5 (Five)"] + ]; + + var options = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 20 }, + { type: oracledb.STRING, maxSize: 18, dir: oracledb.BIND_OUT }, + { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }, + { type: oracledb.STRING, maxSize: 25, dir: oracledb.BIND_OUT } + ] + }; + + conn.executeMany(sql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("rowsAffected is:", result.rowsAffected); + console.log("Out binds:"); + for (let i = 0; i < result.outBinds.length; i++) { + console.log("-->", result.outBinds[i]); + } + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + dotruncate, + doinsert + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_dmlreturn1_aa.js b/javascript/node-oracledb/em_dmlreturn1_aa.js new file mode 100644 index 00000000..c320e9cf --- /dev/null +++ b/javascript/node-oracledb/em_dmlreturn1_aa.js @@ -0,0 +1,82 @@ +/* Copyright (c) 2018, 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 + * em_dmlreturn1_aa.js + * + * DESCRIPTION + * executeMany() example of DML RETURNING that returns single values + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const truncateSql = "TRUNCATE TABLE em_tab"; +const insertSql = "INSERT INTO em_tab VALUES (:1, :2) RETURNING ROWID, id, val INTO :3, :4, :5"; + +const binds = [ + [1, "Test 1 (One)"], + [2, "Test 2 (Two)"], + [3, "Test 3 (Three)"], + [4, null], + [5, "Test 5 (Five)"] +]; + +const options = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 20 }, + { type: oracledb.STRING, maxSize: 18, dir: oracledb.BIND_OUT }, + { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }, + { type: oracledb.STRING, maxSize: 25, dir: oracledb.BIND_OUT } + ] +}; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection(dbConfig); + + await conn.execute(truncateSql); + + result = await conn.executeMany(insertSql, binds, options); + + console.log("rowsAffected is:", result.rowsAffected); + console.log("Out binds:"); + for (let i = 0; i < result.outBinds.length; i++) { + console.log("-->", result.outBinds[i]); + } + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/em_dmlreturn2.js b/javascript/node-oracledb/em_dmlreturn2.js new file mode 100644 index 00000000..764b1ff1 --- /dev/null +++ b/javascript/node-oracledb/em_dmlreturn2.js @@ -0,0 +1,116 @@ +/* Copyright (c) 2018, 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 + * em_dmlreturn2.js + * + * DESCRIPTION + * executeMany() example of DML RETURNING that returns multiple values + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var dotruncate = function(conn, cb) { + conn.execute("TRUNCATE TABLE em_tab", function (err) { + return cb(err, conn); + }); +}; + +var doinsert = function(conn, cb) { + var sql = "INSERT INTO em_tab VALUES (:1, :2)"; + + var binds = [ + [1, "Test 1 (One)"], + [2, "Test 2 (Two)"], + [3, "Test 3 (Three)"], + [4, "Test 4 (Four)"], + [5, "Test 5 (Five)"], + [6, "Test 6 (Six)"], + [7, "Test 7 (Seven)"], + [8, "Test 8 (Eight)"] + ]; + + var options = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 20 } + ] + }; + + conn.executeMany(sql, binds, options, function (err) { + return cb(err, conn); + }); +}; + +var dodelete = function(conn, cb) { + var sql = "DELETE FROM em_tab WHERE id < :1 RETURNING id, val INTO :2, :3"; + + var binds = [ + [2], + [6], + [8] + ]; + + var options = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }, + { type: oracledb.STRING, maxSize: 25, dir: oracledb.BIND_OUT } + ] + }; + + conn.executeMany(sql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("rowsAffected is:", result.rowsAffected); + console.log("Out binds:"); + for (let i = 0; i < result.outBinds.length; i++) { + console.log("-->", result.outBinds[i]); + } + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + dotruncate, + doinsert, + dodelete + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_dmlreturn2_aa.js b/javascript/node-oracledb/em_dmlreturn2_aa.js new file mode 100644 index 00000000..6d992bf0 --- /dev/null +++ b/javascript/node-oracledb/em_dmlreturn2_aa.js @@ -0,0 +1,101 @@ +/* Copyright (c) 2018, 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 + * em_dmlreturn2_aa.js + * + * DESCRIPTION + * executeMany() example of DML RETURNING that returns multiple values + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const truncateSql = "TRUNCATE TABLE em_tab"; + +const insertSql = "INSERT INTO em_tab VALUES (:1, :2)"; + +const insertData = [ + [1, "Test 1 (One)"], + [2, "Test 2 (Two)"], + [3, "Test 3 (Three)"], + [4, "Test 4 (Four)"], + [5, "Test 5 (Five)"], + [6, "Test 6 (Six)"], + [7, "Test 7 (Seven)"], + [8, "Test 8 (Eight)"] +]; + +const insertOptions = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 20 } + ] +}; + +const deleteSql = "DELETE FROM em_tab WHERE id < :1 RETURNING id, val INTO :2, :3"; + +const deleteData = [ + [2], + [6], + [8] +]; + +const deleteOptions = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }, + { type: oracledb.STRING, maxSize: 25, dir: oracledb.BIND_OUT } + ] +}; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection(dbConfig); + + await conn.execute(truncateSql); + + await conn.executeMany(insertSql, insertData, insertOptions); + + result = await conn.executeMany(deleteSql, deleteData, deleteOptions); + + console.log("rowsAffected is:", result.rowsAffected); + console.log("Out binds:"); + for (let i = 0; i < result.outBinds.length; i++) { + console.log("-->", result.outBinds[i]); + } + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/em_insert1.js b/javascript/node-oracledb/em_insert1.js new file mode 100644 index 00000000..b4d20c89 --- /dev/null +++ b/javascript/node-oracledb/em_insert1.js @@ -0,0 +1,88 @@ +/* Copyright (c) 2018, 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 + * em_insert1.js + * + * DESCRIPTION + * Array DML example using executeMany() with bind-by-name syntax. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var dotruncate = function(conn, cb) { + conn.execute("TRUNCATE TABLE em_tab", function (err) { + return cb(err, conn); + }); +}; + +var doinsert = function(conn, cb) { + var sql = "INSERT INTO em_tab VALUES (:a, :b)"; + + var binds = [ + { a: 1, b: "Test 1 (One)" }, + { a: 2, b: "Test 2 (Two)" }, + { a: 3, b: "Test 3 (Three)" }, + { a: 4 }, + { a: 5, b: "Test 5 (Five)" } + ]; + + // bindDefs is optional for IN binds but it is generally recommended. + // Without it the data must be scanned to find sizes and types. + var options = { + autoCommit: true, + bindDefs: { + a: { type: oracledb.NUMBER }, + b: { type: oracledb.STRING, maxSize: 15 } + } }; + + conn.executeMany(sql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("Result is:", result); + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + dotruncate, + doinsert + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_insert1_aa.js b/javascript/node-oracledb/em_insert1_aa.js new file mode 100644 index 00000000..c869c5a8 --- /dev/null +++ b/javascript/node-oracledb/em_insert1_aa.js @@ -0,0 +1,78 @@ +/* Copyright (c) 2018, 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 + * em_insert1_aa.js + * + * DESCRIPTION + * Array DML example using executeMany() with bind-by-name syntax. + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const truncateSql = "TRUNCATE TABLE em_tab"; +const insertSql = "INSERT INTO em_tab values (:a, :b)"; + +const binds = [ + { a: 1, b: "Test 1 (One)" }, + { a: 2, b: "Test 2 (Two)" }, + { a: 3, b: "Test 3 (Three)" }, + { a: 4 }, + { a: 5, b: "Test 5 (Five)" } +]; + +// bindDefs is optional for IN binds but it is generally recommended. +// Without it the data must be scanned to find sizes and types. +const options = { + autoCommit: true, + bindDefs: { + a: { type: oracledb.NUMBER }, + b: { type: oracledb.STRING, maxSize: 15 } + } +}; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection(dbConfig); + + await conn.execute(truncateSql); + + result = await conn.executeMany(insertSql, binds, options); + + console.log("Result is:", result); + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/em_insert2.js b/javascript/node-oracledb/em_insert2.js new file mode 100644 index 00000000..06aab3c6 --- /dev/null +++ b/javascript/node-oracledb/em_insert2.js @@ -0,0 +1,88 @@ +/* Copyright (c) 2018, 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 + * em_insert2.js + * + * DESCRIPTION + * Array DML example using executeMany() with bind-by-position syntax. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var dotruncate = function(conn, cb) { + conn.execute("TRUNCATE TABLE em_tab", function (err) { + return cb(err, conn); + }); +}; + +var doinsert = function(conn, cb) { + var sql = "INSERT INTO em_tab VALUES (:1, :2)"; + + var binds = [ + [1, "Test 1 (One)"], + [2, "Test 2 (Two)"], + [3, "Test 3 (Three)"], + [4, null], + [5, "Test 5 (Five)"] + ]; + + // bindDefs is optional for IN binds but it is generally recommended. + // Without it the data must be scanned to find sizes and types. + var options = { + autoCommit: true, + bindDefs: + [ { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 15 } + ] }; + + conn.executeMany(sql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("Result is:", result); + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + dotruncate, + doinsert + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_insert2_aa.js b/javascript/node-oracledb/em_insert2_aa.js new file mode 100644 index 00000000..253b4fd9 --- /dev/null +++ b/javascript/node-oracledb/em_insert2_aa.js @@ -0,0 +1,78 @@ +/* Copyright (c) 2018, 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 + * em_insert2.js + * + * DESCRIPTION + * Array DML example using executeMany() with bind by position. + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const truncateSql = "TRUNCATE TABLE em_tab"; +const insertSql = "INSERT INTO em_tab values (:1, :2)"; + +const binds = [ + [1, "Test 1 (One)"], + [2, "Test 2 (Two)"], + [3, "Test 3 (Three)"], + [4, null], + [5, "Test 5 (Five)"] +]; + +// bindDefs is optional for IN binds but it is generally recommended. +// Without it the data must be scanned to find sizes and types. +const options = { + autoCommit: true, + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.STRING, maxSize: 15 } + ] +}; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection(dbConfig); + + await conn.execute(truncateSql); + + result = await conn.executeMany(insertSql, binds, options); + + console.log("Result is:", result); + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/em_plsql.js b/javascript/node-oracledb/em_plsql.js new file mode 100644 index 00000000..c665ac40 --- /dev/null +++ b/javascript/node-oracledb/em_plsql.js @@ -0,0 +1,80 @@ +/* Copyright (c) 2018, 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 + * em_plsql.js + * + * DESCRIPTION + * executeMany() example calling PL/SQL. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var doplsql = function(conn, cb) { + var plsql = "BEGIN em_testproc(:1, :2, :3); END;"; + + var binds = [ + [1], + [2], + [3], + [4], + [6] + ]; + + var options = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }, + { type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 20 } + ] + }; + + conn.executeMany(plsql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("Result is:", result); + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + doplsql + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_plsql_aa.js b/javascript/node-oracledb/em_plsql_aa.js new file mode 100644 index 00000000..f4181cb0 --- /dev/null +++ b/javascript/node-oracledb/em_plsql_aa.js @@ -0,0 +1,73 @@ +/* Copyright (c) 2018, 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 + * em_plsql_aa.js + * + * DESCRIPTION + * executeMany() example calling PL/SQL. + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const sql = "BEGIN em_testproc(:1, :2, :3); END;"; + +const binds = [ + [1], + [2], + [3], + [4], + [6] +]; + +const options = { + bindDefs: [ + { type: oracledb.NUMBER }, + { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }, + { type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 20 } + ] +}; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection(dbConfig); + + result = await conn.executeMany(sql, binds, options); + + console.log("Result is:", result); + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/em_rowcounts.js b/javascript/node-oracledb/em_rowcounts.js new file mode 100644 index 00000000..da65c9ad --- /dev/null +++ b/javascript/node-oracledb/em_rowcounts.js @@ -0,0 +1,74 @@ +/* Copyright (c) 2018, 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 + * em_rowcounts.js + * + * DESCRIPTION + * executeMany() example showing dmlRowCounts. + * For this example, there no commit so it is re-runnable. + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var async = require('async'); +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +var doconnect = function(cb) { + oracledb.getConnection(dbConfig, cb); +}; + +var dorelease = function(conn) { + conn.close(function (err) { + if (err) + console.error(err.message); + }); +}; + +var dodelete = function(conn, cb) { + var sql = "DELETE FROM em_childtab WHERE parentid = :1"; + + var binds = [ + [20], + [30], + [50] + ]; + + var options = { dmlRowCounts: true }; + + conn.executeMany(sql, binds, options, function (err, result) { + if (err) + return cb(err, conn); + else { + console.log("Result is:", result); + return cb(null, conn); + } + }); +}; + +async.waterfall( + [ + doconnect, + dodelete + ], + function (err, conn) { + if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } + if (conn) + dorelease(conn); + }); diff --git a/javascript/node-oracledb/em_rowcounts_aa.js b/javascript/node-oracledb/em_rowcounts_aa.js new file mode 100644 index 00000000..1e5c7c38 --- /dev/null +++ b/javascript/node-oracledb/em_rowcounts_aa.js @@ -0,0 +1,66 @@ +/* Copyright (c) 2018, 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 + * em_rowcounts_aa.js + * + * DESCRIPTION + * executeMany() example showing dmlRowCounts. + * For this example, there no commit so it is re-runnable. + * This example also uses Async/Await of Node 8. + * Use demo.sql to create the required schema. + * + *****************************************************************************/ + +var oracledb = require('oracledb'); +var dbConfig = require('./dbconfig.js'); + +const sql = "DELETE FROM em_childtab WHERE parentid = :1"; + +const binds = [ + [20], + [30], + [50] +]; + +const options = { dmlRowCounts: true }; + +async function run() { + let conn; + let result; + + try { + conn = await oracledb.getConnection(dbConfig); + + result = await conn.executeMany(sql, binds, options); + + console.log("Result is:", result); + + } catch (err) { + console.error(err); + } finally { + if (conn) { + try { + await conn.close(); + } catch (err) { + console.error(err); + } + } + } +} + +run(); diff --git a/javascript/node-oracledb/endtoend.js b/javascript/node-oracledb/endtoend.js index 2936e54b..d39541a2 100644 --- a/javascript/node-oracledb/endtoend.js +++ b/javascript/node-oracledb/endtoend.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -35,8 +35,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; } // These end-to-end tracing attributes are sent to the database on @@ -46,8 +45,7 @@ oracledb.getConnection( connection.action = "Query departments"; connection.execute("SELECT * FROM dual", - function(err, result) - { + function(err, result) { if (err) { doRelease(connection); console.error(err.message); return; } console.log(result.rows); // Sleep 10 seconds to keep the connection open. This allows @@ -62,8 +60,7 @@ oracledb.getConnection( }); // Release the connection -function doRelease(connection) -{ +function doRelease(connection) { connection.close( function(err) { if (err) { diff --git a/javascript/node-oracledb/fetchinfo.js b/javascript/node-oracledb/fetchinfo.js index 8d446337..4cc17e85 100644 --- a/javascript/node-oracledb/fetchinfo.js +++ b/javascript/node-oracledb/fetchinfo.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -40,8 +40,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; @@ -56,8 +55,7 @@ oracledb.getConnection( "COMMISSION_PCT": { type : oracledb.DEFAULT } // override oracledb.fetchAsString } }, - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -68,8 +66,7 @@ oracledb.getConnection( }); }); -function doRelease(connection) -{ +function doRelease(connection) { connection.close( function(err) { if (err) { diff --git a/javascript/node-oracledb/insert1.js b/javascript/node-oracledb/insert1.js index a1cac623..52f817dc 100644 --- a/javascript/node-oracledb/insert1.js +++ b/javascript/node-oracledb/insert1.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -53,8 +53,7 @@ var dodrop = function (conn, cb) { RAISE; END IF; END;`, - function(err) - { + function(err) { if (err) { return cb(err, conn); } else { @@ -67,8 +66,7 @@ var dodrop = function (conn, cb) { var docreate = function (conn, cb) { conn.execute( "CREATE TABLE test (id NUMBER, name VARCHAR2(20))", - function(err) - { + function(err) { if (err) { return cb(err, conn); } else { @@ -82,8 +80,7 @@ var doinsert1 = function (conn, cb) { conn.execute( "INSERT INTO test VALUES (:id, :nm)", { id : {val: 1 }, nm : {val: 'Chris'} }, // 'bind by name' syntax - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -97,8 +94,7 @@ var doinsert2 = function (conn, cb) { conn.execute( "INSERT INTO test VALUES (:id, :nm)", [2, 'Alison'], // 'bind by position' syntax - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -113,8 +109,7 @@ var doupdate = function (conn, cb) { "UPDATE test SET name = :nm", ['Bambi'], { autoCommit: true }, // commit once for all DML in the script - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { diff --git a/javascript/node-oracledb/insert2.js b/javascript/node-oracledb/insert2.js index ea81e223..5d61572c 100644 --- a/javascript/node-oracledb/insert2.js +++ b/javascript/node-oracledb/insert2.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -58,8 +58,7 @@ var dodrop = function (conn, cb) { RAISE; END IF; END;`, - function(err) - { + function(err) { if (err) { return cb(err, conn); } else { @@ -72,8 +71,7 @@ var dodrop = function (conn, cb) { var docreate = function (conn, cb) { conn.execute( "CREATE TABLE test (id NUMBER, name VARCHAR2(20))", - function(err) - { + function(err) { if (err) { return cb(err, conn); } else { @@ -89,8 +87,7 @@ var doinsert_autocommit = function (conn, cb) { "INSERT INTO test VALUES (:id, :nm)", [1, 'Chris'], // Bind values { autoCommit: true}, // Override the default non-autocommit behavior - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -106,8 +103,7 @@ var doinsert_nocommit = function (conn, cb) { "INSERT INTO test VALUES (:id, :nm)", [2, 'Alison'], // Bind values // { autoCommit: true}, // Since this isn't set, operations using a second connection won't see this row - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -125,16 +121,14 @@ var doquery = function (conn, cb) { password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection2) - { + function(err, connection2) { if (err) { console.error(err.message); return cb(err, conn); } connection2.execute( "SELECT * FROM test", - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); return cb(err, conn); @@ -145,8 +139,7 @@ var doquery = function (conn, cb) { console.log(result.rows); connection2.close( - function(err) - { + function(err) { if (err) { console.error(err.message); return cb(err, conn); diff --git a/javascript/node-oracledb/lobinsert1.js b/javascript/node-oracledb/lobinsert1.js index 3be8b0d9..788bb549 100644 --- a/javascript/node-oracledb/lobinsert1.js +++ b/javascript/node-oracledb/lobinsert1.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -75,8 +75,7 @@ var doclobinsert = function(conn, cb) { conn.execute( "INSERT INTO mylobs (id, c) VALUES (:id, :c)", { id: 1, c: str }, - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } @@ -93,8 +92,7 @@ var doblobinsert = function(conn, cb) { conn.execute( "INSERT INTO mylobs (id, b) VALUES (:id, :b)", { id: 2, b: buf }, - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } diff --git a/javascript/node-oracledb/lobinsert2.js b/javascript/node-oracledb/lobinsert2.js index d6ef1f2f..d08c4b54 100644 --- a/javascript/node-oracledb/lobinsert2.js +++ b/javascript/node-oracledb/lobinsert2.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -64,8 +64,7 @@ oracledb.getConnection( function() { console.log("lob.on 'close' event"); connection.commit( - function(err) - { + function(err) { if (!errorHandled) { errorHandled = true; if (err) { diff --git a/javascript/node-oracledb/lobinserttemp.js b/javascript/node-oracledb/lobinserttemp.js index bf0d58be..8e2fcaca 100644 --- a/javascript/node-oracledb/lobinserttemp.js +++ b/javascript/node-oracledb/lobinserttemp.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -80,15 +80,13 @@ var doloadtemplob = function (conn, templob, cb) { templob.on( 'close', - function() - { + function() { console.log("templob.on 'close' event"); }); templob.on( 'error', - function(err) - { + function(err) { console.log("templob.on 'error' event"); if (!errorHandled) { errorHandled = true; @@ -98,8 +96,7 @@ var doloadtemplob = function (conn, templob, cb) { templob.on( 'finish', - function() - { + function() { console.log("templob.on 'finish' event"); // The data was loaded into the temporary LOB if (!errorHandled) { @@ -111,8 +108,7 @@ var doloadtemplob = function (conn, templob, cb) { var inStream = fs.createReadStream(inFileName); inStream.on( 'error', - function(err) - { + function(err) { console.log("inStream.on 'error' event"); if (!errorHandled) { errorHandled = true; @@ -130,8 +126,7 @@ var doinsert = function (conn, templob, cb) { { idbv: 3, lobbv: templob }, // type and direction are optional for IN binds { autoCommit: true }, - function(err, result) - { + function(err, result) { if (err) { return cb(err); } diff --git a/javascript/node-oracledb/lobplsqltemp.js b/javascript/node-oracledb/lobplsqltemp.js index e62692b2..ca449ebb 100644 --- a/javascript/node-oracledb/lobplsqltemp.js +++ b/javascript/node-oracledb/lobplsqltemp.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -80,15 +80,13 @@ var loadtemplob = function (conn, templob, cb) { templob.on( 'close', - function() - { + function() { console.log("templob.on 'close' event"); }); templob.on( 'error', - function(err) - { + function(err) { console.log("templob.on 'error' event"); if (!errorHandled) { errorHandled = true; @@ -98,8 +96,7 @@ var loadtemplob = function (conn, templob, cb) { templob.on( 'finish', - function() - { + function() { console.log("templob.on 'finish' event"); // The data was loaded into the temporary LOB if (!errorHandled) { @@ -111,8 +108,7 @@ var loadtemplob = function (conn, templob, cb) { var inStream = fs.createReadStream(inFileName); inStream.on( 'error', - function(err) - { + function(err) { console.log("inStream.on 'error' event"); if (!errorHandled) { errorHandled = true; @@ -130,8 +126,7 @@ var doinsert = function (conn, templob, cb) { { id: 3, c: templob }, // type and direction are optional for IN binds { autoCommit: true }, - function(err) - { + function(err) { if (err) { return cb(err); } diff --git a/javascript/node-oracledb/lobselect.js b/javascript/node-oracledb/lobselect.js index 36816ec3..6b647bc8 100644 --- a/javascript/node-oracledb/lobselect.js +++ b/javascript/node-oracledb/lobselect.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, 2017 Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -72,8 +72,7 @@ var doclobquery = function(conn, cb) { [1], // An alternative to oracledb.fetchAsString is to use fetchInfo on the column: // { fetchInfo: {"C": {type: oracledb.STRING}} }, - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } @@ -93,8 +92,7 @@ var doblobquery = function(conn, cb) { [2], // An alternative to oracledb.fetchAsBuffer is to use fetchInfo on the column: // { fetchInfo: {"B": {type: oracledb.BUFFER}} }, - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } diff --git a/javascript/node-oracledb/lobstream2.js b/javascript/node-oracledb/lobstream2.js index 87c7ef7c..10d2da46 100644 --- a/javascript/node-oracledb/lobstream2.js +++ b/javascript/node-oracledb/lobstream2.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -41,9 +41,9 @@ var doconnect = function(cb) { connectString : dbConfig.connectString }, function(err, conn) { - if (err) + if (err) { return cb(err); - else { + } else { return cb(null, conn); } }); @@ -59,8 +59,7 @@ var dorelease = function(conn) { var doquery = function(conn, cb) { conn.execute( "SELECT c FROM mylobs WHERE id = 1", - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } @@ -88,8 +87,7 @@ var dostream = function(conn, clob, cb) { clob.setEncoding('utf8'); // set the encoding so we get a 'string' not a 'buffer' clob.on( 'error', - function(err) - { + function(err) { console.log("clob.on 'error' event"); if (!errorHandled) { errorHandled = true; @@ -100,23 +98,20 @@ var dostream = function(conn, clob, cb) { }); clob.on( 'data', - function(chunk) - { + function(chunk) { console.log("clob.on 'data' event. Got %d bytes of data", chunk.length); // Build up the string. For larger LOBs you might want to print or use each chunk separately myclob += chunk; // or use Buffer.concat() for BLOBS }); clob.on( 'end', - function() - { + function() { console.log("clob.on 'end' event"); console.log(myclob); }); clob.on( 'close', - function() - { + function() { console.log("clob.on 'close' event"); if (!errorHandled) { return cb(null, conn); diff --git a/javascript/node-oracledb/metadata.js b/javascript/node-oracledb/metadata.js index c3810a11..8e1be22d 100644 --- a/javascript/node-oracledb/metadata.js +++ b/javascript/node-oracledb/metadata.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -52,8 +52,7 @@ var dorelease = function(conn) { var basic = function (conn, cb) { conn.execute( "SELECT location_id, city FROM locations", - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -69,8 +68,7 @@ var extended = function (conn, cb) { "SELECT location_id, city FROM locations", {}, // no binds { extendedMetaData: true }, // enable the extra metadata - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { diff --git a/javascript/node-oracledb/plsqlfunc.js b/javascript/node-oracledb/plsqlfunc.js index 1fb09b96..6c1376b4 100644 --- a/javascript/node-oracledb/plsqlfunc.js +++ b/javascript/node-oracledb/plsqlfunc.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -40,8 +40,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function (err, connection) - { + function (err, connection) { if (err) { console.error(err.message); return; } var bindvars = { @@ -51,9 +50,10 @@ oracledb.getConnection( }; connection.execute( "BEGIN :ret := testfunc(:p1, :p2); END;", + // The equivalent call with PL/SQL named parameter syntax is: + // "BEGIN :ret := testfunc(p1_in => :p1, p2_in => :p2); END;", bindvars, - function (err, result) - { + function (err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -64,8 +64,7 @@ oracledb.getConnection( }); }); -function doRelease(connection) -{ +function doRelease(connection) { connection.close( function(err) { if (err) { diff --git a/javascript/node-oracledb/plsqlproc.js b/javascript/node-oracledb/plsqlproc.js index 5bc51bd1..4e35e943 100644 --- a/javascript/node-oracledb/plsqlproc.js +++ b/javascript/node-oracledb/plsqlproc.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -41,8 +41,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function (err, connection) - { + function (err, connection) { if (err) { console.error(err.message); return; } var bindvars = { @@ -52,9 +51,10 @@ oracledb.getConnection( }; connection.execute( "BEGIN testproc(:i, :io, :o); END;", + // The equivalent call with PL/SQL named parameter syntax is: + // "BEGIN testproc(p_in => :i, p_inout => :io, p_out => :o); END;", bindvars, - function (err, result) - { + function (err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -65,8 +65,7 @@ oracledb.getConnection( }); }); -function doRelease(connection) -{ +function doRelease(connection) { connection.close( function(err) { if (err) { diff --git a/javascript/node-oracledb/refcursor.js b/javascript/node-oracledb/refcursor.js index 1ccbe982..b6d6d6d1 100644 --- a/javascript/node-oracledb/refcursor.js +++ b/javascript/node-oracledb/refcursor.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -48,8 +48,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; } var bindvars = { sal: 12000, @@ -58,8 +57,7 @@ oracledb.getConnection( connection.execute( "BEGIN get_emp_rs(:sal, :cursor); END;", bindvars, - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -70,12 +68,10 @@ oracledb.getConnection( }); }); -function fetchRowsFromRS(connection, resultSet, numRows) -{ +function fetchRowsFromRS(connection, resultSet, numRows) { resultSet.getRows( // get numRows rows numRows, - function (err, rows) - { + function (err, rows) { if (err) { console.log(err); doClose(connection, resultSet); // always close the ResultSet @@ -89,20 +85,16 @@ function fetchRowsFromRS(connection, resultSet, numRows) }); } -function doRelease(connection) -{ +function doRelease(connection) { connection.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } }); } -function doClose(connection, resultSet) -{ +function doClose(connection, resultSet) { resultSet.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } doRelease(connection); }); diff --git a/javascript/node-oracledb/refcursortoquerystream.js b/javascript/node-oracledb/refcursortoquerystream.js index 2e68ad5c..474f9436 100644 --- a/javascript/node-oracledb/refcursortoquerystream.js +++ b/javascript/node-oracledb/refcursortoquerystream.js @@ -71,7 +71,7 @@ oracledb.getConnection( doRelease(connection); }); - queryStream.on('end', function () { + queryStream.on('close', function () { doRelease(connection); }); } diff --git a/javascript/node-oracledb/resultset1.js b/javascript/node-oracledb/resultset1.js index ec0ccdfb..f030a134 100644 --- a/javascript/node-oracledb/resultset1.js +++ b/javascript/node-oracledb/resultset1.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -47,8 +47,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; } connection.execute( `SELECT employee_id, last_name @@ -57,8 +56,7 @@ oracledb.getConnection( ORDER BY employee_id`, [], // no bind variables { resultSet: true }, // return a ResultSet. Default is false - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -69,11 +67,9 @@ oracledb.getConnection( }); }); -function fetchOneRowFromRS(connection, resultSet) -{ +function fetchOneRowFromRS(connection, resultSet) { resultSet.getRow( // get one row - function (err, row) - { + function (err, row) { if (err) { console.error(err.message); doClose(connection, resultSet); // always close the ResultSet @@ -88,20 +84,16 @@ function fetchOneRowFromRS(connection, resultSet) }); } -function doRelease(connection) -{ +function doRelease(connection) { connection.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } }); } -function doClose(connection, resultSet) -{ +function doClose(connection, resultSet) { resultSet.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } doRelease(connection); }); diff --git a/javascript/node-oracledb/resultset2.js b/javascript/node-oracledb/resultset2.js index 87d16073..5d9821ae 100644 --- a/javascript/node-oracledb/resultset2.js +++ b/javascript/node-oracledb/resultset2.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -38,8 +38,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; } connection.execute( `SELECT employee_id, last_name @@ -50,8 +49,7 @@ oracledb.getConnection( { resultSet: true // return a ResultSet. Default is false }, - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -62,12 +60,10 @@ oracledb.getConnection( }); }); -function fetchRowsFromRS(connection, resultSet, numRows) -{ +function fetchRowsFromRS(connection, resultSet, numRows) { resultSet.getRows( numRows, // get this many rows - function (err, rows) - { + function (err, rows) { if (err) { console.error(err); doClose(connection, resultSet); // always close the ResultSet @@ -84,20 +80,16 @@ function fetchRowsFromRS(connection, resultSet, numRows) }); } -function doRelease(connection) -{ +function doRelease(connection) { connection.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } }); } -function doClose(connection, resultSet) -{ +function doClose(connection, resultSet) { resultSet.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } doRelease(connection); }); diff --git a/javascript/node-oracledb/resultsettoquerystream.js b/javascript/node-oracledb/resultsettoquerystream.js index 772cf934..63c123a7 100644 --- a/javascript/node-oracledb/resultsettoquerystream.js +++ b/javascript/node-oracledb/resultsettoquerystream.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -35,8 +35,7 @@ var dbConfig = require('./dbconfig.js'); oracledb.getConnection( dbConfig, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; } var sql = "SELECT employee_id, last_name FROM employees WHERE ROWNUM < 25 ORDER BY employee_id"; connection.execute( @@ -45,10 +44,8 @@ oracledb.getConnection( { resultSet: true }, - function(err, result) - { - if (err) - { + function(err, result) { + if (err) { console.error(err.message); doRelease(connection); return; @@ -64,7 +61,7 @@ oracledb.getConnection( doRelease(connection); }); - queryStream.on('end', function() { + queryStream.on('close', function() { doRelease(connection); }); } @@ -72,11 +69,9 @@ oracledb.getConnection( } ); -function doRelease(connection) -{ +function doRelease(connection) { connection.close( - function(err) - { + function(err) { if (err) { console.error(err.message); } }); } diff --git a/javascript/node-oracledb/rowlimit.js b/javascript/node-oracledb/rowlimit.js index bee74670..dc75fd2a 100644 --- a/javascript/node-oracledb/rowlimit.js +++ b/javascript/node-oracledb/rowlimit.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -51,8 +51,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function (err, connection) - { + function (err, connection) { if (err) { console.error(err.message); return; @@ -71,8 +70,8 @@ oracledb.getConnection( connection.execute( sql, - {offset: myoffset, maxnumrows: mymaxnumrows}, - {maxRows: 150}, + { offset: myoffset, maxnumrows: mymaxnumrows }, + { maxRows: 150 }, function(err, result) { if (err) { console.error(err.message); diff --git a/javascript/node-oracledb/select1.js b/javascript/node-oracledb/select1.js index 02e708e6..369d4a14 100644 --- a/javascript/node-oracledb/select1.js +++ b/javascript/node-oracledb/select1.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -42,8 +42,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; @@ -68,8 +67,7 @@ oracledb.getConnection( }, // The callback function handles the SQL execution results - function(err, result) - { + function(err, result) { if (err) { console.error(err.message); doRelease(connection); @@ -82,8 +80,7 @@ oracledb.getConnection( }); // Note: connections should always be released when not needed -function doRelease(connection) -{ +function doRelease(connection) { connection.close( function(err) { if (err) { diff --git a/javascript/node-oracledb/select2.js b/javascript/node-oracledb/select2.js index 35444653..f58d5969 100644 --- a/javascript/node-oracledb/select2.js +++ b/javascript/node-oracledb/select2.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -69,8 +69,7 @@ var dorelease = function(conn) { var doquery_array = function (conn, cb) { conn.execute( "SELECT location_id, city FROM locations WHERE city LIKE 'S%' ORDER BY city", - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -88,8 +87,7 @@ var doquery_object = function (conn, cb) { {}, // A bind variable parameter is needed to disambiguate the following options parameter // otherwise you will get Error: ORA-01036: illegal variable name/number { outFormat: oracledb.OBJECT }, // outFormat can be OBJECT or ARRAY. The default is ARRAY - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { diff --git a/javascript/node-oracledb/selectjson.js b/javascript/node-oracledb/selectjson.js index 4b56bce7..89a03b8f 100644 --- a/javascript/node-oracledb/selectjson.js +++ b/javascript/node-oracledb/selectjson.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -77,8 +77,7 @@ var dojsonquery = function (conn, cb) { console.log('1. Selecting JSON stored in a VARCHAR2 column'); conn.execute( "SELECT po_document FROM j_purchaseorder WHERE JSON_EXISTS (po_document, '$.location')", - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -89,13 +88,27 @@ var dojsonquery = function (conn, cb) { }); }; -// 2. Using JSON_VALUE to extract a value from a JSON column +// 2. Extract a value from a JSON column. This syntax requires Oracle Database 12.2 +var dorelationalquerydot = function (conn, cb) { + console.log('2. Using dot-notation to extract a value from a JSON column'); + conn.execute( + "SELECT po.po_document.location FROM j_purchaseorder po", + function(err, result) { + if (err) { + return cb(err, conn); + } else { + console.log('Query results: ', result.rows[0][0]); // just show first record + return cb(null, conn); + } + }); +}; + +// 3. Using JSON_VALUE to extract a value from a JSON column var dorelationalquery = function (conn, cb) { - console.log('2. Using JSON_VALUE to extract a value from a JSON column'); + console.log('3. Using JSON_VALUE to extract a value from a JSON column'); conn.execute( "SELECT JSON_VALUE(po_document, '$.location') FROM j_purchaseorder", - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -105,9 +118,9 @@ var dorelationalquery = function (conn, cb) { }); }; -// 3. Using JSON_OBJECT to extract relational data as JSON +// 4. Using JSON_OBJECT to extract relational data as JSON var dojsonfromrelational = function (conn, cb) { - console.log('3. Using JSON_OBJECT to extract relational data as JSON'); + console.log('4. Using JSON_OBJECT to extract relational data as JSON'); if (conn.oracleServerVersion < 1202000000) { // JSON_OBJECT is new in Oracle Database 12.2 console.log('The JSON_OBJECT example only works with Oracle Database 12.2 or greater'); return cb(null, conn); @@ -117,8 +130,7 @@ var dojsonfromrelational = function (conn, cb) { FROM departments d WHERE department_id < :did`, [50], - function(err, result) - { + function(err, result) { if (err) { return cb(err, conn); } else { @@ -136,6 +148,7 @@ async.waterfall( checkver, doinsert, dojsonquery, + dorelationalquerydot, dorelationalquery, dojsonfromrelational ], diff --git a/javascript/node-oracledb/selectjsonblob.js b/javascript/node-oracledb/selectjsonblob.js index 0e67523a..3c856096 100644 --- a/javascript/node-oracledb/selectjsonblob.js +++ b/javascript/node-oracledb/selectjsonblob.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -19,7 +19,7 @@ * selectjsonblob.js * * DESCRIPTION - * Executes sample insert and query using a JSON column with BLOB storage. + * Executes sample insert and query statements using a JSON column with BLOB storage. * Requires Oracle Database 12.1.0.2, which has extensive JSON datatype support. * See https://docs.oracle.com/database/122/ADJSN/toc.htm * @@ -60,8 +60,7 @@ var doinsert = function (conn, cb) { "INSERT INTO j_purchaseorder_b (po_document) VALUES (:lobbv)", { lobbv: b }, // { autoCommit: true }, // uncomment if you want data to persist - function(err) - { + function(err) { if (err) { return cb(err, conn); } else { @@ -71,33 +70,49 @@ var doinsert = function (conn, cb) { }); }; -// Select JSON stored in a BLOB column +// Select JSON with JSON_EXISTS var dojsonquery = function (conn, cb) { - console.log('Selecting JSON stored in a BLOB column'); + console.log('Selecting JSON stored in a BLOB column:'); conn.execute( "SELECT po_document FROM j_purchaseorder_b WHERE JSON_EXISTS (po_document, '$.location')", [], { fetchInfo: { "PO_DOCUMENT": { type: oracledb.BUFFER } } }, // Fetch as a Buffer instead of a Stream - function(err, result) - { + function(err, result) { if (err) return cb(err, conn); if (result.rows.length === 0) return cb(new Error('No results'), conn); - console.log('Query results:'); console.log(result.rows[0][0].toString('utf8')); return cb(null, conn); }); }; +// Select a JSON value using dot-notation. This syntax requires Oracle Database 12.2 + +var dojsonquerydot = function (conn, cb) { + console.log('Selecting a JSON value:'); + conn.execute( + "SELECT pob.po_document.location FROM j_purchaseorder_b pob", + function(err, result) { + if (err) + return cb(err, conn); + if (result.rows.length === 0) + return cb(new Error('No results'), conn); + + console.log(result.rows[0][0]); + return cb(null, conn); + }); +}; + async.waterfall( [ doconnect, checkver, doinsert, - dojsonquery + dojsonquery, + dojsonquerydot ], function (err, conn) { if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } diff --git a/javascript/node-oracledb/selectstream.js b/javascript/node-oracledb/selectstream.js index 59a433d1..b8d79605 100644 --- a/javascript/node-oracledb/selectstream.js +++ b/javascript/node-oracledb/selectstream.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -38,8 +38,7 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; diff --git a/javascript/node-oracledb/version.js b/javascript/node-oracledb/version.js index 76dc010d..1f63308f 100644 --- a/javascript/node-oracledb/version.js +++ b/javascript/node-oracledb/version.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */ /****************************************************************************** * @@ -26,27 +26,15 @@ var oracledb = require('oracledb'); var dbConfig = require('./dbconfig.js'); -var addonVer, clientVer, serverVer; -var major, minor, update, port, portUpdate; - console.log("Run at: " + new Date()); console.log("Node.js version: " + process.version + " (" + process.platform, process.arch + ")"); -addonVer = oracledb.version; -major = Math.floor(addonVer / 10000); -minor = Math.floor(addonVer / 100) % 100; -update = addonVer % 100; -//console.log("Node-oracledb version: " + addonVer); -console.log("Node-oracledb text format: " + major + "." + minor + "." + update); +// console.log("Node-oracledb version:", oracledb.version); // numeric version format is useful for comparisons +// console.log("Node-oracledb version suffix:", oracledb.versionSuffix); // e.g. "-beta.1", or empty for production releases +console.log("Node-oracledb version:", oracledb.versionString); // version (including the suffix) -clientVer = oracledb.oracleClientVersion; -major = Math.floor (clientVer / 100000000); -minor = Math.floor (clientVer / 1000000) % 100 ; -update = Math.floor (clientVer / 10000) % 100 ; -port = Math.floor (clientVer / 100) % 100 ; -portUpdate = clientVer % 100 ; -//console.log("Oracle Client library version: " + clientVer); -console.log("Oracle Client library text format: " + major + "." + minor + "." + update + "." + port + "." + portUpdate); +//console.log("Oracle Client library version:", oracledb.oracleClientVersion); // numeric version format +console.log("Oracle Client library version:", oracledb.oracleClientVersionString); oracledb.getConnection( { @@ -54,19 +42,12 @@ oracledb.getConnection( password : dbConfig.password, connectString : dbConfig.connectString }, - function(err, connection) - { + function(err, connection) { if (err) { console.error(err.message); return; } - serverVer = connection.oracleServerVersion; - major = Math.floor (serverVer / 100000000); - minor = Math.floor (serverVer / 1000000) % 100 ; - update = Math.floor (serverVer / 10000) % 100 ; - port = Math.floor (serverVer / 100) % 100 ; - portUpdate = serverVer % 100 ; - // console.log("Oracle Database version: " + serverVer); - console.log("Oracle Database text format: " + major + "." + minor + "." + update + "." + port + "." + portUpdate); + // console.log("Oracle Database version:", connection.oracleServerVersion); // numeric version format + console.log("Oracle Database version:", connection.oracleServerVersionString); }); diff --git a/javascript/node-oracledb/webapp.js b/javascript/node-oracledb/webapp.js index 5ee2abb6..41f1b6fc 100644 --- a/javascript/node-oracledb/webapp.js +++ b/javascript/node-oracledb/webapp.js @@ -47,15 +47,16 @@ function init() { password: dbConfig.password, connectString: dbConfig.connectString // Default values shown below + // events: false, // whether to handle Oracle Database FAN and RLB events // externalAuth: false, // whether connections should be established using External Authentication + // poolAlias: 'myalias' // set an alias to allow access to the pool via a name + // poolIncrement: 1, // only grow the pool by one connection at a time // poolMax: 4, // maximum size of the pool. Increase UV_THREADPOOL_SIZE if you increase poolMax // poolMin: 0, // start with no connections; let the pool shrink completely - // poolIncrement: 1, // only grow the pool by one connection at a time - // poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds // poolPingInterval: 60, // check aliveness of connection if in the pool for 60 seconds + // poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds // queueRequests: true, // let Node.js queue new getConnection() requests if all pool connections are in use // queueTimeout: 60000, // terminate getConnection() calls in the queue longer than 60000 milliseconds - // poolAlias: 'myalias' // could set an alias to allow access to the pool via a name // stmtCacheSize: 30 // number of statements that are cached in the statement cache of each connection }, function(err, pool) { diff --git a/javascript/node-oracledb/webapppromises.js b/javascript/node-oracledb/webapppromises.js index fbedfefa..4a53d417 100644 --- a/javascript/node-oracledb/webapppromises.js +++ b/javascript/node-oracledb/webapppromises.js @@ -47,15 +47,16 @@ function init() { password: dbConfig.password, connectString: dbConfig.connectString // Default values shown below + // events: false, // whether to handle Oracle Database FAN and RLB events // externalAuth: false, // whether connections should be established using External Authentication + // poolAlias: 'myalias' // set an alias to allow access to the pool via a name. + // poolIncrement: 1, // only grow the pool by one connection at a time // poolMax: 4, // maximum size of the pool. Increase UV_THREADPOOL_SIZE if you increase poolMax // poolMin: 0, // start with no connections; let the pool shrink completely - // poolIncrement: 1, // only grow the pool by one connection at a time - // poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds // poolPingInterval: 60, // check aliveness of connection if in the pool for 60 seconds + // poolTimeout: 60, // terminate connections that are idle in the pool for 60 seconds // queueRequests: true, // let Node.js queue new getConnection() requests if all pool connections are in use // queueTimeout: 60000, // terminate getConnection() calls in the queue longer than 60000 milliseconds - // poolAlias: 'myalias' // could set an alias to allow access to the pool via a name. // stmtCacheSize: 30 // number of statements that are cached in the statement cache of each connection }) .then(function(pool) {