|
| 1 | +/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with the Apache |
| 6 | + * License, Version 2.0 (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * NAME |
| 19 | + * refcursor.js |
| 20 | + * |
| 21 | + * DESCRIPTION |
| 22 | + * Shows using a Result Set to fetch rows from a REF CURSOR |
| 23 | + * Uses Oracle's sample HR schema. |
| 24 | + * Use demo.sql to create the required procedure or do: |
| 25 | + * |
| 26 | + * CREATE OR REPLACE PROCEDURE get_emp_rs (p_sal IN NUMBER, p_recordset OUT SYS_REFCURSOR) |
| 27 | + * AS |
| 28 | + * BEGIN |
| 29 | + * OPEN p_recordset FOR |
| 30 | + * SELECT first_name, salary, hire_date |
| 31 | + * FROM employees |
| 32 | + * WHERE salary > p_sal; |
| 33 | + * END; |
| 34 | + * / |
| 35 | + * |
| 36 | + *****************************************************************************/ |
| 37 | + |
| 38 | +var oracledb = require('oracledb'); |
| 39 | +var dbConfig = require('./dbconfig.js'); |
| 40 | + |
| 41 | +// Prefetching is a tuning feature for optimizing row transfer from |
| 42 | +// the Oracle Database to node-oracledb with Result Sets. The default |
| 43 | +// prefetch size is 100. The prefetch size does not affect how, or |
| 44 | +// when, rows are returned by node-oracledb to the application. |
| 45 | +// Buffering is handled by the underlying Oracle client libraries. |
| 46 | +// Benchmark to choose the optimal size for each application or query. |
| 47 | +// |
| 48 | +//oracledb.prefetchRows = 100; |
| 49 | + |
| 50 | +var numRows = 10; // number of rows to return from each call to getRows() |
| 51 | + |
| 52 | +oracledb.getConnection( |
| 53 | + { |
| 54 | + user : dbConfig.user, |
| 55 | + password : dbConfig.password, |
| 56 | + connectString : dbConfig.connectString |
| 57 | + }, |
| 58 | + function(err, connection) |
| 59 | + { |
| 60 | + if (err) { console.error(err.message); return; } |
| 61 | + var bindvars = { |
| 62 | + sal: 12000, |
| 63 | + cursor: { type: oracledb.CURSOR, dir : oracledb.BIND_OUT } |
| 64 | + } |
| 65 | + connection.execute( |
| 66 | + "BEGIN get_emp_rs(:sal, :cursor); END;", |
| 67 | + bindvars, |
| 68 | + function(err, result) |
| 69 | + { |
| 70 | + if (err) { |
| 71 | + console.error(err.message); |
| 72 | + doRelease(connection); |
| 73 | + return; |
| 74 | + } |
| 75 | + console.log(result.outBinds.cursor.metaData); |
| 76 | + fetchRowsFromRS(connection, result.outBinds.cursor, numRows); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | +function fetchRowsFromRS(connection, resultSet, numRows) |
| 81 | +{ |
| 82 | + resultSet.getRows( // get numRows rows |
| 83 | + numRows, |
| 84 | + function (err, rows) |
| 85 | + { |
| 86 | + if (err) { |
| 87 | + console.log(err); |
| 88 | + doClose(connection, resultSet); // always close the result set |
| 89 | + } else if (rows.length == 0) { // no rows, or no more rows |
| 90 | + doClose(connection, resultSet); // always close the result set |
| 91 | + } else if (rows.length > 0) { |
| 92 | + console.log("fetchRowsFromRS(): Got " + rows.length + " rows"); |
| 93 | + console.log(rows); |
| 94 | + fetchRowsFromRS(connection, resultSet, numRows); |
| 95 | + } |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +function doRelease(connection) |
| 100 | +{ |
| 101 | + connection.release( |
| 102 | + function(err) |
| 103 | + { |
| 104 | + if (err) { console.error(err.message); } |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +function doClose(connection, resultSet) |
| 109 | +{ |
| 110 | + resultSet.close( |
| 111 | + function(err) |
| 112 | + { |
| 113 | + if (err) { console.error(err.message); } |
| 114 | + doRelease(connection); |
| 115 | + }); |
| 116 | +} |
0 commit comments