Skip to content

Commit 3844f84

Browse files
committed
Update some examples and comments
1 parent 3a54f91 commit 3844f84

File tree

8 files changed

+164
-138
lines changed

8 files changed

+164
-138
lines changed

examples/dbconfig.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,26 @@
2020
*
2121
* DESCRIPTION
2222
* Holds the credentials used by node-oracledb examples to connect
23-
* to the database.
24-
*
25-
* The connectString here uses the Easy Connect syntax
26-
* "localhost/XE". This connects to the database service XE on the
27-
* the local machine.
23+
* to the database. Production applications should instead consider
24+
* using External Authentication to avoid hard coded credentials.
2825
*
2926
* Applications can set the connectString value to an Easy Connect
30-
* string, or a Connect Name from a tnsnames.ora file, or the name
31-
* of a local Oracle database instance.
27+
* string, or a Net Service Name from a tnsnames.ora file or
28+
* external naming service, or it can be the name of a local Oracle
29+
* database instance.
3230
*
33-
* The full Easy Connect syntax is:
31+
* The Easy Connect syntax is:
3432
* [//]host_name[:port][/service_name][:server_type][/instance_name]
35-
* see https://docs.oracle.com/database/121/NETAG/naming.htm#i498306
3633
*
37-
* If a tnsnames.ora file is used, set the TNS_ADMIN environment
38-
* variable such that $TNS_ADMIN/tnsnames.ora is read.
39-
* Alternatively use $ORACLE_HOME/network/admin/tnsnames.ora or
40-
* /etc/tnsnames.ora.
34+
* If using a tnsnames.ora file, the file can be in a default
35+
* location such as $ORACLE_HOME/network/admin/tnsnames.ora or
36+
* /etc/tnsnames.ora. Alternatively set the TNS_ADMIN environment
37+
* variable and put the file in $TNS_ADMIN/tnsnames.ora.
4138
*
4239
* If connectString is not specified, the empty string "" is used
4340
* which indicates to connect to the local, default database.
4441
*
42+
* TROUBLESHOOTING
4543
* Errors like:
4644
* ORA-12541: TNS:no listener
4745
* or
@@ -58,5 +56,7 @@
5856
module.exports = {
5957
user : "hr",
6058
password : "welcome",
61-
connectString : "localhost/XE"
59+
// For information on connection strings see:
60+
// https://github.com/oracle/node-oracledb/blob/master/doc/api.md#connectionstrings
61+
connectString : "localhost/XE" // Easy Connect syntax
6262
};

examples/demo.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* DESCRIPTION
2222
* Create database objects for the examples
2323
*
24+
* Scripts to create Oracle Database's traditional sample schemas can
25+
* be found at: https://github.com/oracle/db-sample-schemas
26+
*
2427
*****************************************************************************/
2528

2629
SET ECHO ON

examples/insert2.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
*
2121
* DESCRIPTION
2222
* Show the auto commit behavior.
23-
*
23+
*
2424
* By default, node-oracledb does not commit on execute.
2525
* The driver also has commit() and rollback() methods to explicitly control transactions.
26-
*
26+
*
2727
* Note: when a connection is closed, any open transaction will be committed.
2828
*
2929
*****************************************************************************/
@@ -52,7 +52,7 @@ oracledb.getConnection(
5252
return;
5353
}
5454
console.log("Table created");
55-
55+
5656
connection1.execute(
5757
"INSERT INTO test VALUES (:id, :nm)",
5858
[1, 'Chris'], // Bind values
@@ -61,7 +61,7 @@ oracledb.getConnection(
6161
{
6262
if (err) { console.error(err.message); return; }
6363
console.log("Rows inserted: " + result.rowsAffected); // 1
64-
64+
6565
connection1.execute(
6666
"INSERT INTO test VALUES (:id, :nm)",
6767
[2, 'Alison'], // Bind values
@@ -70,7 +70,7 @@ oracledb.getConnection(
7070
{
7171
if (err) { console.error(err.message); return; }
7272
console.log("Rows inserted: " + result.rowsAffected); // 1
73-
73+
7474
// Create a second connection
7575
oracledb.getConnection(
7676
{
@@ -95,14 +95,14 @@ oracledb.getConnection(
9595
// This will only show 'Chris' because inserting 'Alison' is not commited by default.
9696
// Uncomment the isAutoCommit option above and you will see both rows
9797
console.log(result.rows);
98-
98+
9999
connection1.execute(
100100
"DROP TABLE test",
101101
function(err)
102102
{
103103
if (err) { console.error(err.message); return; }
104104
console.log("Table dropped");
105-
105+
106106
connection2.release(
107107
function(err)
108108
{
@@ -112,12 +112,12 @@ oracledb.getConnection(
112112
{
113113
if (err) { console.error(err.message); return; }
114114
});
115-
115+
116116
});
117117
});
118118
});
119119
});
120120
});
121-
});
121+
});
122122
});
123123
});

examples/rowlimit.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* Shows ways to limit the number of records fetched by queries.
2323
* Uses Oracle's sample HR schema.
2424
*
25+
* Scripts to create the HR schema can be found at:
26+
* https://github.com/oracle/db-sample-schemas
27+
*
2528
* By default, node-oracledb has a maxRows attribute that limits the
2629
* number of records fetched from a query to 100. Although
2730
* adjusting maxRows can be used to control the number of rows

examples/select1.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* Executes a basic query.
2323
* Uses Oracle's sample HR schema.
2424
*
25+
* Scripts to create the HR schema can be found at:
26+
* https://github.com/oracle/db-sample-schemas
27+
*
2528
*****************************************************************************/
2629

2730
var oracledb = require('oracledb');
@@ -48,9 +51,21 @@ oracledb.getConnection(
4851
{
4952
if (err) {
5053
console.error(err.message);
54+
doRelease(connection);
5155
return;
5256
}
53-
console.log(result.rows);
5457
console.log(result.metaData);
58+
console.log(result.rows);
59+
doRelease(connection);
5560
});
5661
});
62+
63+
function doRelease(connection)
64+
{
65+
connection.release(
66+
function(err) {
67+
if (err) {
68+
console.error(err.message);
69+
}
70+
});
71+
}

examples/select2.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@
2222
* Executes queries to show array and object output formats.
2323
* Uses Oracle's sample HR schema.
2424
*
25-
*****************************************************************************/
25+
* Scripts to create the HR schema can be found at:
26+
* https://github.com/oracle/db-sample-schemas
27+
*
28+
* *****************************************************************************/
2629

2730
var oracledb = require('oracledb');
2831
var dbConfig = require('./dbconfig.js');
2932

3033
// Properties are applicable to all connections and SQL executions.
3134
// They can also be set or overridden at the individual execute() call level
3235
//
33-
// This script sets outFormat in the execute() call but it could be set here instead
36+
// This script sets outFormat in the execute() call but it could be set here instead:
3437
// oracledb.outFormat = oracledb.OBJECT;
3538

3639
oracledb.getConnection(
@@ -54,6 +57,7 @@ oracledb.getConnection(
5457
{
5558
if (err) {
5659
console.error(err.message);
60+
doRelease(connection);
5761
return;
5862
}
5963
console.log("----- Cities beginning with 'S' (default ARRAY output format) --------");
@@ -66,24 +70,28 @@ oracledb.getConnection(
6670
+ "ORDER BY city",
6771
{}, // A bind variable parameter is needed to disambiguate the following options parameter
6872
// otherwise you will get Error: ORA-01036: illegal variable name/number
69-
{outFormat: oracledb.OBJECT}, // outFormat can be OBJECT and ARRAY. The default is ARRAY
73+
{outFormat: oracledb.OBJECT}, // outFormat can be OBJECT or ARRAY. The default is ARRAY
7074
function(err, result)
7175
{
7276
if (err) {
7377
console.error(err.message);
78+
doRelease(connection);
7479
return;
7580
}
7681
console.log("----- Cities beginning with 'S' (OBJECT output format) --------");
7782
console.log(result.rows);
7883

79-
connection.release(
80-
function(err)
81-
{
82-
if (err) {
83-
console.error(err.message);
84-
return;
85-
}
86-
});
84+
doRelease(connection);
8785
});
8886
});
8987
});
88+
89+
function doRelease(connection)
90+
{
91+
connection.release(
92+
function(err) {
93+
if (err) {
94+
console.error(err.message);
95+
}
96+
});
97+
}

examples/selectjson.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
* selectjson.js
2020
*
2121
* DESCRIPTION
22-
* Executes a query from a JSON table.
22+
* Executes a query from a JSON table.
2323
* Oracle Database 12.1.0.2 has extensive JSON datatype support.
24-
*
24+
*
2525
* Use demo.sql to create the required table or do:
26-
*
26+
*
2727
* DROP TABLE j_purchaseorder;
28-
*
28+
*
2929
* CREATE TABLE j_purchaseorder
3030
* (po_document VARCHAR2(4000) CONSTRAINT ensure_json CHECK (po_document IS JSON));
3131
*
@@ -46,16 +46,17 @@ oracledb.getConnection(
4646
console.error(err.message);
4747
return;
4848
}
49-
49+
5050
var data = { "userId": 1, "userName": "Chris" };
5151
var s = JSON.stringify(data);
52-
52+
5353
connection.execute(
5454
"INSERT INTO j_purchaseorder (po_document) VALUES (:bv)",
5555
[s],
5656
function (err) {
5757
if (err) {
5858
console.error(err.message);
59+
doRelease(connection);
5960
return;
6061
}
6162
connection.execute(
@@ -64,10 +65,22 @@ oracledb.getConnection(
6465
{
6566
if (err) {
6667
console.error(err.message);
68+
doRelease(connection);
6769
return;
6870
}
6971
js = JSON.parse(result.rows[0][0]);
7072
console.log(js.userId, js.userName);
73+
doRelease(connection);
7174
});
7275
});
7376
});
77+
78+
function doRelease(connection)
79+
{
80+
connection.release(
81+
function(err) {
82+
if (err) {
83+
console.error(err.message);
84+
}
85+
});
86+
}

0 commit comments

Comments
 (0)