-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Result Set and REF CURSOR examples
- Loading branch information
Showing
3 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* Copyright (c) 2015, 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 Result Set to fetch rows from a REF CURSOR | ||
* 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'); | ||
|
||
// Prefetching is a tuning feature for optimizing row transfer from | ||
// the Oracle Database to node-oracledb with Result Sets. The default | ||
// prefetch size is 100. The prefetch size does not affect how, or | ||
// when, rows are returned by node-oracledb to the application. | ||
// Buffering is handled by the underlying Oracle client libraries. | ||
// Benchmark to choose the optimal size for each application or query. | ||
// | ||
//oracledb.prefetchRows = 100; | ||
|
||
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 result set | ||
} else if (rows.length == 0) { // no rows, or no more rows | ||
doClose(connection, resultSet); // always close the result set | ||
} else if (rows.length > 0) { | ||
console.log("fetchRowsFromRS(): Got " + rows.length + " rows"); | ||
console.log(rows); | ||
fetchRowsFromRS(connection, resultSet, numRows); | ||
} | ||
}); | ||
} | ||
|
||
function doRelease(connection) | ||
{ | ||
connection.release( | ||
function(err) | ||
{ | ||
if (err) { console.error(err.message); } | ||
}); | ||
} | ||
|
||
function doClose(connection, resultSet) | ||
{ | ||
resultSet.close( | ||
function(err) | ||
{ | ||
if (err) { console.error(err.message); } | ||
doRelease(connection); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* Copyright (c) 2015, 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 | ||
* resultset1.js | ||
* | ||
* DESCRIPTION | ||
* Executes a query and uses a result set to fetch rows with getRow(). | ||
* Uses Oracle's sample HR schema. | ||
* | ||
*****************************************************************************/ | ||
|
||
var oracledb = require('oracledb'); | ||
var dbConfig = require('./dbconfig.js'); | ||
|
||
var rowCount = 0; | ||
|
||
oracledb.getConnection( | ||
{ | ||
user : dbConfig.user, | ||
password : dbConfig.password, | ||
connectString : dbConfig.connectString | ||
}, | ||
function(err, connection) | ||
{ | ||
if (err) { console.error(err.message); return; } | ||
connection.execute( | ||
"SELECT employee_id, last_name " | ||
+ "FROM employees " | ||
+ "WHERE ROWNUM < 11 " | ||
+ "ORDER BY employee_id", | ||
[], // no bind variables | ||
{ resultSet: true }, // return a result set. Default is false | ||
function(err, result) | ||
{ | ||
if (err) { | ||
console.error(err.message); | ||
doRelease(connection); | ||
return; | ||
} | ||
console.log(result); | ||
fetchOneRowFromRS(connection, result.resultSet); | ||
}); | ||
}); | ||
|
||
function fetchOneRowFromRS(connection, resultSet) | ||
{ | ||
resultSet.getRow( // get one row | ||
function (err, row) | ||
{ | ||
if (err) { | ||
console.error(err.message); | ||
doClose(connection, resultSet); // always close the result set | ||
} else if (!row) { // no rows, or no more rows | ||
doClose(connection, resultSet); // always close the result set | ||
} else { | ||
rowCount++; | ||
console.log("fetchOneRowFromRS(): row " + rowCount); | ||
console.log(row); | ||
fetchOneRowFromRS(connection, resultSet); | ||
} | ||
}); | ||
} | ||
|
||
function doRelease(connection) | ||
{ | ||
connection.release( | ||
function(err) | ||
{ | ||
if (err) { console.error(err.message); } | ||
}); | ||
} | ||
|
||
function doClose(connection, resultSet) | ||
{ | ||
resultSet.close( | ||
function(err) | ||
{ | ||
if (err) { console.error(err.message); } | ||
doRelease(connection); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* Copyright (c) 2015, 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 | ||
* resultset2.js | ||
* | ||
* DESCRIPTION | ||
* Executes a query and uses a result set to fetch batches of rows | ||
* with getRows(). Also shows setting the prefetch size. | ||
* Uses Oracle's sample HR schema. | ||
* | ||
*****************************************************************************/ | ||
|
||
var oracledb = require('oracledb'); | ||
var dbConfig = require('./dbconfig.js'); | ||
|
||
// Prefetching is a tuning feature for optimizing row transfer from | ||
// the Oracle Database to node-oracledb with Result Sets. The default | ||
// prefetch size is 100. The prefetch size does not affect how, or | ||
// when, rows are returned by node-oracledb to the application. | ||
// Buffering is handled by the underlying Oracle client libraries. | ||
// Benchmark to choose the optimal size for each application or query. | ||
// | ||
//oracledb.prefetchRows = 100; | ||
|
||
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; } | ||
connection.execute( | ||
"SELECT employee_id, last_name " | ||
+ "FROM employees " | ||
+ "WHERE ROWNUM < 25 " | ||
+ "ORDER BY employee_id", | ||
[], // no bind variables | ||
{ | ||
resultSet: true, // return a result set. Default is false | ||
prefetchRows: 25 // the prefetch size can be set for each query | ||
}, | ||
function(err, result) | ||
{ | ||
if (err) { | ||
console.error(err.message); | ||
doRelease(connection); | ||
return; | ||
} | ||
console.log(result); | ||
fetchRowsFromRS(connection, result.resultSet, 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 result set | ||
} else if (rows.length == 0) { // no rows, or no more rows | ||
doClose(connection, resultSet); // always close the result set | ||
} else if (rows.length > 0) { | ||
console.log("fetchRowsFromRS(): Got " + rows.length + " rows"); | ||
console.log(rows); | ||
fetchRowsFromRS(connection, resultSet, numRows); | ||
} | ||
}); | ||
} | ||
|
||
function doRelease(connection) | ||
{ | ||
connection.release( | ||
function(err) | ||
{ | ||
if (err) { console.error(err.message); } | ||
}); | ||
} | ||
|
||
function doClose(connection, resultSet) | ||
{ | ||
resultSet.close( | ||
function(err) | ||
{ | ||
if (err) { console.error(err.message); } | ||
doRelease(connection); | ||
}); | ||
} |