Skip to content

Commit 4124974

Browse files
committed
Add Result Set and REF CURSOR examples
1 parent bc6b88e commit 4124974

File tree

3 files changed

+321
-0
lines changed

3 files changed

+321
-0
lines changed

examples/refcursor.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

examples/resultset1.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
* resultset1.js
20+
*
21+
* DESCRIPTION
22+
* Executes a query and uses a result set to fetch rows with getRow().
23+
* Uses Oracle's sample HR schema.
24+
*
25+
*****************************************************************************/
26+
27+
var oracledb = require('oracledb');
28+
var dbConfig = require('./dbconfig.js');
29+
30+
var rowCount = 0;
31+
32+
oracledb.getConnection(
33+
{
34+
user : dbConfig.user,
35+
password : dbConfig.password,
36+
connectString : dbConfig.connectString
37+
},
38+
function(err, connection)
39+
{
40+
if (err) { console.error(err.message); return; }
41+
connection.execute(
42+
"SELECT employee_id, last_name "
43+
+ "FROM employees "
44+
+ "WHERE ROWNUM < 11 "
45+
+ "ORDER BY employee_id",
46+
[], // no bind variables
47+
{ resultSet: true }, // return a result set. Default is false
48+
function(err, result)
49+
{
50+
if (err) {
51+
console.error(err.message);
52+
doRelease(connection);
53+
return;
54+
}
55+
console.log(result);
56+
fetchOneRowFromRS(connection, result.resultSet);
57+
});
58+
});
59+
60+
function fetchOneRowFromRS(connection, resultSet)
61+
{
62+
resultSet.getRow( // get one row
63+
function (err, row)
64+
{
65+
if (err) {
66+
console.error(err.message);
67+
doClose(connection, resultSet); // always close the result set
68+
} else if (!row) { // no rows, or no more rows
69+
doClose(connection, resultSet); // always close the result set
70+
} else {
71+
rowCount++;
72+
console.log("fetchOneRowFromRS(): row " + rowCount);
73+
console.log(row);
74+
fetchOneRowFromRS(connection, resultSet);
75+
}
76+
});
77+
}
78+
79+
function doRelease(connection)
80+
{
81+
connection.release(
82+
function(err)
83+
{
84+
if (err) { console.error(err.message); }
85+
});
86+
}
87+
88+
function doClose(connection, resultSet)
89+
{
90+
resultSet.close(
91+
function(err)
92+
{
93+
if (err) { console.error(err.message); }
94+
doRelease(connection);
95+
});
96+
}

examples/resultset2.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
* resultset2.js
20+
*
21+
* DESCRIPTION
22+
* Executes a query and uses a result set to fetch batches of rows
23+
* with getRows(). Also shows setting the prefetch size.
24+
* Uses Oracle's sample HR schema.
25+
*
26+
*****************************************************************************/
27+
28+
var oracledb = require('oracledb');
29+
var dbConfig = require('./dbconfig.js');
30+
31+
// Prefetching is a tuning feature for optimizing row transfer from
32+
// the Oracle Database to node-oracledb with Result Sets. The default
33+
// prefetch size is 100. The prefetch size does not affect how, or
34+
// when, rows are returned by node-oracledb to the application.
35+
// Buffering is handled by the underlying Oracle client libraries.
36+
// Benchmark to choose the optimal size for each application or query.
37+
//
38+
//oracledb.prefetchRows = 100;
39+
40+
var numRows = 10; // number of rows to return from each call to getRows()
41+
42+
oracledb.getConnection(
43+
{
44+
user : dbConfig.user,
45+
password : dbConfig.password,
46+
connectString : dbConfig.connectString
47+
},
48+
function(err, connection)
49+
{
50+
if (err) { console.error(err.message); return; }
51+
connection.execute(
52+
"SELECT employee_id, last_name "
53+
+ "FROM employees "
54+
+ "WHERE ROWNUM < 25 "
55+
+ "ORDER BY employee_id",
56+
[], // no bind variables
57+
{
58+
resultSet: true, // return a result set. Default is false
59+
prefetchRows: 25 // the prefetch size can be set for each query
60+
},
61+
function(err, result)
62+
{
63+
if (err) {
64+
console.error(err.message);
65+
doRelease(connection);
66+
return;
67+
}
68+
console.log(result);
69+
fetchRowsFromRS(connection, result.resultSet, numRows);
70+
});
71+
});
72+
73+
function fetchRowsFromRS(connection, resultSet, numRows)
74+
{
75+
resultSet.getRows( // get numRows rows
76+
numRows,
77+
function (err, rows)
78+
{
79+
if (err) {
80+
console.log(err);
81+
doClose(connection, resultSet); // always close the result set
82+
} else if (rows.length == 0) { // no rows, or no more rows
83+
doClose(connection, resultSet); // always close the result set
84+
} else if (rows.length > 0) {
85+
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
86+
console.log(rows);
87+
fetchRowsFromRS(connection, resultSet, numRows);
88+
}
89+
});
90+
}
91+
92+
function doRelease(connection)
93+
{
94+
connection.release(
95+
function(err)
96+
{
97+
if (err) { console.error(err.message); }
98+
});
99+
}
100+
101+
function doClose(connection, resultSet)
102+
{
103+
resultSet.close(
104+
function(err)
105+
{
106+
if (err) { console.error(err.message); }
107+
doRelease(connection);
108+
});
109+
}

0 commit comments

Comments
 (0)