Skip to content

Commit 0a8d213

Browse files
committed
Improve getRows() example
1 parent 6b78afe commit 0a8d213

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

examples/refcursor.js

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -20,7 +20,7 @@
2020
*
2121
* DESCRIPTION
2222
* Shows using a ResultSet to fetch rows from a REF CURSOR using getRows().
23-
* Streaming is also possible (this is not shown).
23+
* Streaming is also possible, see refcursortoquerystream.js
2424
*
2525
* This example uses Node 8's async/await syntax.
2626
*
@@ -60,62 +60,81 @@ async function run() {
6060
//
6161

6262
await connection.execute(
63-
`CREATE OR REPLACE PROCEDURE no_get_rs (p_id IN NUMBER, p_recordset OUT SYS_REFCURSOR)
63+
`CREATE OR REPLACE PROCEDURE no_get_rs (p_maxid IN NUMBER, p_recordset OUT SYS_REFCURSOR)
6464
AS
6565
BEGIN
6666
OPEN p_recordset FOR
6767
SELECT farmer, weight, ripeness
6868
FROM no_banana_farmer
69-
WHERE id < p_id;
69+
WHERE id < p_maxid;
7070
END;`
7171
);
7272

7373
//
74-
// Get a REF CURSOR result set
74+
// Fetch rows from a REF CURSOR using one getRows() call.
7575
//
76+
// This is useful when the ResultSet is known to contain a small number of rows
77+
// that will always fit in memory.
78+
//
79+
80+
console.log('Single getRows() call:');
7681

7782
const result = await connection.execute(
7883
`BEGIN
79-
no_get_rs(:id, :cursor);
84+
no_get_rs(:maxid, :cursor);
8085
END;`,
8186
{
82-
id: 3,
87+
maxid: 3,
8388
cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
8489
});
8590

91+
const resultSet1 = result.outBinds.cursor;
92+
8693
console.log("Cursor metadata:");
87-
console.log(result.outBinds.cursor.metaData);
94+
console.log(resultSet1.metaData);
95+
96+
const rows1 = await resultSet1.getRows(); // no parameter means get all rows
97+
console.log(rows1);
98+
99+
await resultSet1.close(); // always close the ResultSet
88100

89101
//
90-
// Fetch rows from the REF CURSOR.
102+
// Fetch rows from a REF CURSOR using multiple getRows() calls to fetch
103+
// batches of rows.
91104
//
92105

93-
const resultSet = result.outBinds.cursor;
94-
const numRows = 10; // number of rows to return from each call to getRows()
95-
let rows;
106+
console.log('\nLooping getRows() calls:');
107+
108+
const result2 = await connection.execute(
109+
`BEGIN
110+
OPEN :cursor FOR
111+
SELECT 'row ' || level
112+
FROM dual
113+
CONNECT BY LEVEL <= :nr;
114+
END;`,
115+
{
116+
nr: 23, // number of rows to fetch
117+
cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
118+
});
119+
120+
const resultSet2 = result2.outBinds.cursor;
96121

97122
// If getRows(numRows) returns:
98-
// Zero rows => there were no rows, or are no more rows to return
123+
// Zero rows => the table was empty, or there are no more rows
99124
// Fewer than numRows rows => this was the last set of rows to get
100125
// Exactly numRows rows => there may be more rows to fetch
101126

127+
const numRows = 10; // number of rows to return from each call to getRows()
128+
let rows2;
102129
do {
103-
rows = await resultSet.getRows(numRows); // get numRows rows at a time
104-
if (rows.length > 0) {
105-
console.log("getRows(): Got " + rows.length + " rows");
106-
console.log(rows);
130+
rows2 = await resultSet2.getRows(numRows); // get numRows rows at a time
131+
if (rows2.length > 0) {
132+
console.log("getRows(): Got " + rows2.length + " rows");
133+
console.log(rows2);
107134
}
108-
} while (rows.length === numRows);
109-
110-
// From node-oracledb 5.2, you can alternatively fetch all rows in one call.
111-
// This is useful when the ResultSet is known to contain a small number of
112-
// rows that will always fit in memory.
113-
//
114-
// rows = await resultSet.getRows(); // no parameter means get all rows
115-
// console.log(rows);
135+
} while (rows2.length === numRows);
116136

117-
// always close the ResultSet
118-
await resultSet.close();
137+
await resultSet2.close(); // always close the ResultSet
119138

120139
} catch (err) {
121140
console.error(err);

examples/refcursortoquerystream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -21,7 +21,7 @@
2121
* DESCRIPTION
2222
* Converts a refcursor returned from execute() to a query stream.
2323
* This is an alternative means of processing instead of using
24-
* resultSet.getRows().
24+
* resultSet.getRows() shown in refcursor.js.
2525
*
2626
* This example requires node-oracledb 1.9 or later.
2727
*

0 commit comments

Comments
 (0)