forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefcursor.js
More file actions
101 lines (94 loc) · 3.02 KB
/
Copy pathrefcursor.js
File metadata and controls
101 lines (94 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */
/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* refcursor.js
*
* DESCRIPTION
* Shows using a ResultSet to fetch rows from a REF CURSOR using getRows().
* Streaming is also possible (this is not shown).
*
* Uses Oracle's sample HR schema.
* Use demo.sql to create the required procedure or do:
*
* CREATE OR REPLACE PROCEDURE get_emp_rs (p_sal IN NUMBER, p_recordset OUT SYS_REFCURSOR)
* AS
* BEGIN
* OPEN p_recordset FOR
* SELECT first_name, salary, hire_date
* FROM employees
* WHERE salary > p_sal;
* END;
* /
*
*****************************************************************************/
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
var numRows = 10; // number of rows to return from each call to getRows()
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, connection) {
if (err) { console.error(err.message); return; }
var bindvars = {
sal: 12000,
cursor: { type: oracledb.CURSOR, dir : oracledb.BIND_OUT }
};
connection.execute(
"BEGIN get_emp_rs(:sal, :cursor); END;",
bindvars,
function(err, result) {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.outBinds.cursor.metaData);
fetchRowsFromRS(connection, result.outBinds.cursor, numRows);
});
});
function fetchRowsFromRS(connection, resultSet, numRows) {
resultSet.getRows( // get numRows rows
numRows,
function (err, rows) {
if (err) {
console.log(err);
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length === 0) { // no rows, or no more rows
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length > 0) {
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
console.log(rows);
fetchRowsFromRS(connection, resultSet, numRows);
}
});
}
function doRelease(connection) {
connection.close(
function(err) {
if (err) { console.error(err.message); }
});
}
function doClose(connection, resultSet) {
resultSet.close(
function(err) {
if (err) { console.error(err.message); }
doRelease(connection);
});
}