Skip to content

Commit 0249378

Browse files
committed
Rework schema setup in example scripts
1 parent d81b40a commit 0249378

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1266
-966
lines changed

doc/api.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,6 @@ below. As well as [Async/Await](#asyncawaitoverview) functions,
512512
node-oracledb can also use [Callbacks](#callbackoverview), and
513513
[Promises](#promiseoverview).
514514

515-
Scripts to create Oracle's sample schemas can be found at
516-
[github.com/oracle/db-sample-schemas][4].
517-
518515
Locate your Oracle Database [user name and password][91], and the database
519516
[connection string](#connectionstrings). The connection string is
520517
commonly of the format `hostname/servicename`, using the host name
@@ -577,7 +574,7 @@ async function run() {
577574
run();
578575
```
579576

580-
With Oracle's sample HR schema, the output is:
577+
With Oracle's sample [HR schema][4], the output is:
581578

582579
```
583580
[ { MANAGER_ID: 103, DEPARTMENT_ID: 60, DEPARTMENT_NAME: 'IT' } ]

examples/README.md

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ To run the examples:
88

99

1010
- Edit `dbconfig.js` and set your username, password and the database
11-
connection string:
11+
connection string, for example:
1212

1313
```
1414
module.exports = {
@@ -23,95 +23,88 @@ connection string:
2323
`NODE_ORACLEDB_PASSWORD`, which you must set before running
2424
examples.
2525

26-
- Then run the samples like:
26+
- Review the samples and then run them like:
2727

2828
```
2929
node example.js
3030
```
3131

32-
- Some examples require schema objects created by `demo.sql`. For
33-
example, to load them in the HR schema run:
32+
- After running example, the demonstration objects can be dropped with `demodrop.js`:
3433

3534
```
36-
sqlplus hr/welcome@localhost/orclpdb1 @demo.sql
37-
```
38-
39-
The demonstration objects can be dropped with `demodrop.sql`:
40-
41-
```
42-
sqlplus hr/welcome@localhost/orclpdb1 @demodrop.sql
35+
node demodrop.js
4336
```
4437

4538
## Example Overview
4639

4740
If this is your first time with node-oracledb, start with
4841
[`example.js`](example.js).
4942

50-
File Name | Description
51-
----------------------------|----------------------------------------------------------------------------------
52-
aqoptions.js | Oracle Advanced Queuing (AQ) example setting options and message attributes
53-
aqmulti.js | Oracle Advanced Queuing (AQ) example passing multiple messages
54-
aqobject.js | Oracle Advanced Queuing (AQ) example passing an Oracle Database object
55-
aqraw.js | Basic Oracle Advanced Queuing (AQ) example passing text messages
56-
blobhttp.js | Simple web app that streams an image
57-
calltimeout.js | Shows how to cancel a SQL statement if it doesn't complete in a specified time
58-
connect.js | Basic example for creating a standalone (non-pooled) connection
59-
connectionpool.js | Basic example creating a pool of connections
60-
cqn1.js | Basic Continuous Query Notification (CQN) example
61-
cqn2.js | Continuous Query Notification with notification grouping
62-
date.js | Show some DATE and TIMESTAMP behaviors
63-
dbconfig.js | Common file used by examples for setting connection credentials
64-
dbmsoutputgetline.js | Show fetching DBMS_OUTPUT by binding buffers
65-
dbmsoutputpipe.js | Show fetching DBMS_OUTPUT by using a pipelined table
66-
demo.sql | SQL script to create extra schema objects for the examples
67-
demodrop.sql | SQL script to drop the extra schema objects for the examples
68-
dmlrupd1.js | Example of DML RETURNING with a single row match
69-
dmlrupd2.js | Example of DML RETURNING where multiple rows are matched
70-
em_batcherrors.js | `executeMany()` example showing handling data errors
71-
em_dmlreturn1.js | `executeMany()` example of DML RETURNING that returns single values
72-
em_dmlreturn2.js | `executeMany()` example of DML RETURNING that returns multiple values
73-
em_insert1.js | Array DML example using `executeMany()` with bind-by-name syntax
74-
em_insert2.js | Array DML example using `executeMany()` with bind by position
75-
em_plsql.js | `executeMany()` example calling PL/SQL multiple times with one call
76-
em_rowcounts.js | `executeMany()` example showing how to find the number of rows affected by each input row
77-
endtoend.js | Example showing setting tracing attributes
78-
example.js | Basic example showing creating a table, inserting multiple rows, and querying rows
79-
fetchinfo.js | Show how numbers and dates can be returned as strings using `fetchAsString` and `fetchInfo`
80-
impres.js | Shows PL/SQL 'Implict Results' returning multiple query results from PL/SQL code.
81-
insert1.js | Basic example creating a table and inserting data. Shows DDL and DML
82-
insert2.js | Basic example showing auto commit behavior
83-
lobbinds.js | Demonstrates how to bind and query LOBs
84-
lobinsert1.js | Shows inserting a file into a CLOB column
85-
lobinsert2.js | Inserts text into a CLOB column using the RETURNING INTO method.
86-
lobinserttemp.js | Writes data to a Temporary CLOB and then inserts it into the database
87-
lobplsqltemp.js | Streams data into a Temporary CLOB and then passes it to PL/SQL
88-
lobselect.js | Shows basic, non-streaming CLOB and BLOB queries
89-
lobstream1.js | Shows how to stream LOBs to files
90-
lobstream2.js | Shows using Stream data events to fetch a CLOB
91-
metadata.js | Shows the metadata available after executing SELECT statements
92-
plsqlarray.js | Examples of binding PL/SQL "INDEX BY" tables
93-
plsqlfunc.js | How to call a PL/SQL function
94-
plsqlproc.js | How to call a PL/SQL procedure
95-
plsqlrecord.js | Shows binding of PL/SQL RECORDS
96-
plsqlvarrayrecord.js | Shows binding a VARRAY of RECORD in PL/SQL
97-
raw1.js | Shows using a Buffer to insert and select a RAW
98-
refcursor.js | Shows using a ResultSet to fetch rows from a REF CURSOR
99-
refcursortoquerystream.js | Converts a REF CURSOR returned from `execute()` to a query stream.
100-
resultset1.js | Executes a query and uses a ResultSet to fetch rows with `getRow()`
101-
resultset2.js | Executes a query and uses a ResultSet to fetch batches of rows with `getRows()`
102-
resultsettoquerystream.js | Converts a ResultSet returned from `execute()` into a Readable Stream.
103-
rowlimit.js | Shows ways to limit the number of records fetched by queries
104-
select1.js | Executes a basic query without using a connection pool or ResultSet
105-
select2.js | Executes queries to show array and object output formats
106-
selectgeometry.js | Insert and query Oracle Spatial geometries
107-
selectjson.js | Shows some JSON features of Oracle Database
108-
selectjsonblob.js | Shows how to use a BLOB as a JSON column store
109-
selectobject.js | Insert and query a named Oracle database object
110-
selectstream.js | Executes a basic query using a Readable Stream
111-
selectvarray.js | Shows inserting and selecting from a VARRAY column
112-
sessionfixup.js | Shows a pooled connection callback to efficiently set session state
113-
sessiontagging1.js | Simple pooled connection tagging for setting session state
114-
sessiontagging2.js | More complex example of pooled connection tagging for setting session state
115-
soda1.js | Basic Simple Oracle Document Access (SODA) example
116-
version.js | Shows the node-oracledb version attributes
117-
webappawait.js | A simple web application using a connection pool
43+
File Name | Description
44+
----------------------------------------------------------|----------------------------------------------------------------------------------
45+
[`aqmulti.js`](aqmulti.js) | Oracle Advanced Queuing (AQ) example passing multiple messages
46+
[`aqobject.js`](aqobject.js) | Oracle Advanced Queuing (AQ) example passing an Oracle Database object
47+
[`aqoptions.js`](aqoptions.js) | Oracle Advanced Queuing (AQ) example setting options and message attributes
48+
[`aqraw.js`](aqraw.js) | Basic Oracle Advanced Queuing (AQ) example passing text messages
49+
[`blobhttp.js`](blobhttp.js) | Simple web app that streams an image
50+
[`calltimeout.js`](calltimeout.js) | Shows how to cancel a SQL statement if it doesn't complete in a specified time
51+
[`connect.js`](connect.js) | Basic example for creating a standalone (non-pooled) connection
52+
[`connectionpool.js`](connectionpool.js) | Basic example creating a pool of connections
53+
[`cqn1.js`](cqn1.js) | Basic Continuous Query Notification (CQN) example
54+
[`cqn2.js`](cqn2.js) | Continuous Query Notification with notification grouping
55+
[`date.js`](date.js) | Show some DATE and TIMESTAMP behaviors
56+
[`dbconfig.js`](dbconfig.js) | Common file used by examples for setting connection credentials
57+
[`dbmsoutputgetline.js`](dbmsoutputgetline.js) | Show fetching DBMS_OUTPUT by binding buffers
58+
[`dbmsoutputpipe.js`](dbmsoutputpipe.js) | Show fetching DBMS_OUTPUT by using a pipelined table
59+
[`demodrop.js`](demodrop.js) | Drops the schema objects created by the examples
60+
[`demosetup.js`](demosetup.js) | Used to create common schema objects for the examples
61+
[`dmlrupd1.js`](dmlrupd1.js) | Example of DML RETURNING with a single row match
62+
[`dmlrupd2.js`](dmlrupd2.js) | Example of DML RETURNING where multiple rows are matched
63+
[`em_batcherrors.js`](em_batcherrors.js) | `executeMany()` example showing handling data errors
64+
[`em_dmlreturn1.js`](em_dmlreturn1.js) | `executeMany()` example of DML RETURNING that returns single values
65+
[`em_dmlreturn2.js`](em_dmlreturn2.js) | `executeMany()` example of DML RETURNING that returns multiple values
66+
[`em_insert1.js`](em_insert1.js) | Array DML example using `executeMany()` with bind-by-name syntax
67+
[`em_insert2.js`](em_insert2.js) | Array DML example using `executeMany()` with bind by position
68+
[`em_plsql.js`](em_plsql.js) | `executeMany()` example calling PL/SQL multiple times with one call
69+
[`em_rowcounts.js`](em_rowcounts.js) | `executeMany()` example showing how to find the number of rows affected by each input row
70+
[`endtoend.js`](endtoend.js) | Example showing setting tracing attributes
71+
[`example.js`](example.js) | Basic example showing creating a table, inserting multiple rows, and querying rows
72+
[`fetchinfo.js`](fetchinfo.js) | Show how numbers and dates can be returned as strings using `fetchAsString` and `fetchInfo`
73+
[`impres.js`](impres.js) | Shows PL/SQL 'Implict Results' returning multiple query results from PL/SQL code.
74+
[`insert1.js`](insert1.js) | Basic example creating a table and inserting data. Shows DDL and DML
75+
[`insert2.js`](insert2.js) | Basic example showing auto commit behavior
76+
[`lobbinds.js`](lobbinds.js) | Demonstrates how to bind and query LOBs
77+
[`lobinsert1.js`](lobinsert1.js) | Shows inserting a file into a CLOB column
78+
[`lobinsert2.js`](lobinsert2.js) | Inserts text into a CLOB column using the RETURNING INTO method.
79+
[`lobinserttemp.js`](lobinserttemp.js) | Writes data to a Temporary CLOB and then inserts it into the database
80+
[`lobplsqltemp.js`](lobplsqltemp.js) | Streams data into a Temporary CLOB and then passes it to PL/SQL
81+
[`lobselect.js`](lobselect.js) | Shows basic, non-streaming CLOB and BLOB queries
82+
[`lobstream1.js`](lobstream1.js) | Shows how to stream LOBs to files
83+
[`lobstream2.js`](lobstream2.js) | Shows using Stream data events to fetch a CLOB
84+
[`metadata.js`](metadata.js) | Shows the metadata available after executing SELECT statements
85+
[`plsqlarray.js`](plsqlarray.js) | Examples of binding PL/SQL "INDEX BY" tables
86+
[`plsqlfunc.js`](plsqlfunc.js) | How to call a PL/SQL function
87+
[`plsqlproc.js`](plsqlproc.js) | How to call a PL/SQL procedure
88+
[`plsqlrecord.js`](plsqlrecord.js) | Shows binding of PL/SQL RECORDS
89+
[`plsqlvarrayrecord.js`](plsqlvarrayrecord.js) | Shows binding a VARRAY of RECORD in PL/SQL
90+
[`raw.js`](raw.js) | Shows using a Buffer to insert and select a RAW
91+
[`refcursor.js`](refcursor.js) | Shows using a ResultSet to fetch rows from a REF CURSOR
92+
[`refcursortoquerystream.js`](refcursortoquerystream.js) | Converts a REF CURSOR returned from `execute()` to a query stream.
93+
[`resultset1.js`](resultset1.js) | Executes a query and uses a ResultSet to fetch rows with `getRow()`
94+
[`resultset2.js`](resultset2.js) | Executes a query and uses a ResultSet to fetch batches of rows with `getRows()`
95+
[`resultsettoquerystream.js`](resultsettoquerystream.js) | Converts a ResultSet returned from `execute()` into a Readable Stream.
96+
[`rowlimit.js`](rowlimit.js) | Shows ways to limit the number of records fetched by queries
97+
[`select1.js`](select1.js) | Executes a basic query without using a connection pool or ResultSet
98+
[`select2.js`](select2.js) | Executes queries to show array and object output formats
99+
[`selectgeometry.js`](selectgeometry.js) | Insert and query Oracle Spatial geometries
100+
[`selectjson.js`](selectjson.js) | Shows some JSON features of Oracle Database
101+
[`selectjsonblob.js`](selectjsonblob.js) | Shows how to use a BLOB as a JSON column store
102+
[`selectobject.js`](selectobject.js) | Insert and query a named Oracle database object
103+
[`selectstream.js`](selectstream.js) | Executes a basic query using a Readable Stream
104+
[`selectvarray.js`](selectvarray.js) | Shows inserting and selecting from a VARRAY column
105+
[`sessionfixup.js`](sessionfixup.js) | Shows a pooled connection callback to efficiently set session state
106+
[`sessiontagging1.js`](sessiontagging1.js) | Simple pooled connection tagging for setting session state
107+
[`sessiontagging2.js`](sessiontagging2.js) | More complex example of pooled connection tagging for setting session state
108+
[`soda1.js`](soda1.js) | Basic Simple Oracle Document Access (SODA) example
109+
[`version.js`](version.js) | Shows the node-oracledb version attributes
110+
[`webappawait.js`](webappawait.js) | A simple web application using a connection pool

examples/aqmulti.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ async function deq() {
7777
const queue = await connection.getQueue(queueName);
7878
queue.deqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE; // Change the visibility so no explicit commit is required
7979

80+
console.log('Dequeuing messages');
81+
8082
const messages = await queue.deqMany(5); // get at most 5 messages
8183
console.log("Dequeued " + messages.length + " messages");
8284
for (const msg of messages) {

examples/aqobject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Oracle Advanced Queuing (AQ) example passing an Oracle Database object
2323
*
2424
* Before running this, a queue allowing an Oracle Database object
25-
* payloads must be created, see
25+
* payload must be created, see
2626
* https://oracle.github.io/node-oracledb/doc/api.html#aqobjexample
2727
*
2828
* This example requires node-oracledb 4 or later.

examples/blobhttp.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,33 @@
2222
* Listens for an HTTP request and returns an image queried from a BLOB column
2323
* Also shows the connection pool's caching using a 'default' pool.
2424
*
25-
* Use demo.sql to create the required table or do:
26-
* DROP TABLE mylobs;
27-
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
28-
*
29-
* Run lobinsert1.js to load an image before running this example.
30-
*
3125
* Start the listener with 'node blobhttp.js' and then use a browser
3226
* to load http://localhost:7000/getimage
3327
*
3428
* This example uses Node 8's async/await syntax.
3529
*
3630
*****************************************************************************/
3731

38-
var url = require('url');
39-
var http = require('http');
40-
var oracledb = require('oracledb');
41-
var dbConfig = require('./dbconfig.js');
32+
const url = require('url');
33+
const http = require('http');
34+
const oracledb = require('oracledb');
35+
const dbConfig = require('./dbconfig.js');
36+
const demoSetup = require('./demosetup.js');
4237

43-
var httpPort = 7000;
38+
const httpPort = 7000;
4439

4540
// Main entry point. Creates a connection pool which becomes the
4641
// 'default' pool, and then creates an HTTP server.
4742
async function init() {
4843
try {
49-
await oracledb.createPool(
50-
{
51-
user: dbConfig.user,
52-
password: dbConfig.password,
53-
connectString: dbConfig.connectString
54-
});
44+
await oracledb.createPool(dbConfig);
5545
console.log('Connection pool started');
5646

47+
// create the demo table
48+
const connection = await oracledb.getConnection();
49+
await demoSetup.setupLobs(connection, true);
50+
await connection.close();
51+
5752
// Create HTTP server and listen on port httpPort
5853
const server = http.createServer();
5954
server.on('error', (err) => {
@@ -63,7 +58,7 @@ async function init() {
6358
handleRequest(request, response);
6459
});
6560
await server.listen(httpPort);
66-
console.log("Server running. Try requesting: http://localhost:" + httpPort + "/getimage");
61+
console.log("Server is running. Try loading http://localhost:" + httpPort + "/getimage");
6762

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

8681
const result = await connection.execute(
87-
"SELECT b FROM mylobs WHERE id = :id", // get the image
82+
"SELECT b FROM no_lobs WHERE id = :id", // get the image
8883
{ id: 2 }
8984
);
9085
if (result.rows.length === 0) {
91-
throw new Error("No results. Did you run lobinsert1.js?");
86+
throw new Error("No data selected from table.");
9287
}
9388

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

118113
} catch (err) {
119114
console.error(err);
115+
await closePoolAndExit();
120116
} finally {
121117
if (connection) {
122118
try {
@@ -137,9 +133,9 @@ async function closePoolAndExit() {
137133
console.log('\nTerminating');
138134
try {
139135
// Get the pool from the pool cache and close it when no
140-
// connections are in use, or force it closed after 10 seconds
136+
// connections are in use, or force it closed after 2 seconds
141137
// If this hangs, you may need DISABLE_OOB=ON in a sqlnet.ora file
142-
await oracledb.getPool().close(10);
138+
await oracledb.getPool().close(2);
143139
console.log('Pool closed');
144140
process.exit(0);
145141
} catch(err) {

0 commit comments

Comments
 (0)