|
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. */ |
2 | 2 |
|
3 | 3 | /******************************************************************************
|
4 | 4 | *
|
|
20 | 20 | *
|
21 | 21 | * DESCRIPTION
|
22 | 22 | * 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 |
24 | 24 | *
|
25 | 25 | * This example uses Node 8's async/await syntax.
|
26 | 26 | *
|
@@ -60,62 +60,81 @@ async function run() {
|
60 | 60 | //
|
61 | 61 |
|
62 | 62 | 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) |
64 | 64 | AS
|
65 | 65 | BEGIN
|
66 | 66 | OPEN p_recordset FOR
|
67 | 67 | SELECT farmer, weight, ripeness
|
68 | 68 | FROM no_banana_farmer
|
69 |
| - WHERE id < p_id; |
| 69 | + WHERE id < p_maxid; |
70 | 70 | END;`
|
71 | 71 | );
|
72 | 72 |
|
73 | 73 | //
|
74 |
| - // Get a REF CURSOR result set |
| 74 | + // Fetch rows from a REF CURSOR using one getRows() call. |
75 | 75 | //
|
| 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:'); |
76 | 81 |
|
77 | 82 | const result = await connection.execute(
|
78 | 83 | `BEGIN
|
79 |
| - no_get_rs(:id, :cursor); |
| 84 | + no_get_rs(:maxid, :cursor); |
80 | 85 | END;`,
|
81 | 86 | {
|
82 |
| - id: 3, |
| 87 | + maxid: 3, |
83 | 88 | cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
|
84 | 89 | });
|
85 | 90 |
|
| 91 | + const resultSet1 = result.outBinds.cursor; |
| 92 | + |
86 | 93 | 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 |
88 | 100 |
|
89 | 101 | //
|
90 |
| - // Fetch rows from the REF CURSOR. |
| 102 | + // Fetch rows from a REF CURSOR using multiple getRows() calls to fetch |
| 103 | + // batches of rows. |
91 | 104 | //
|
92 | 105 |
|
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; |
96 | 121 |
|
97 | 122 | // 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 |
99 | 124 | // Fewer than numRows rows => this was the last set of rows to get
|
100 | 125 | // Exactly numRows rows => there may be more rows to fetch
|
101 | 126 |
|
| 127 | + const numRows = 10; // number of rows to return from each call to getRows() |
| 128 | + let rows2; |
102 | 129 | 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); |
107 | 134 | }
|
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); |
116 | 136 |
|
117 |
| - // always close the ResultSet |
118 |
| - await resultSet.close(); |
| 137 | + await resultSet2.close(); // always close the ResultSet |
119 | 138 |
|
120 | 139 | } catch (err) {
|
121 | 140 | console.error(err);
|
|
0 commit comments