node-oracledb/examples/resultset2.js

88 lines
2.4 KiB
JavaScript
Raw Normal View History

/* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. */
2015-07-20 15:53:29 +08:00
/******************************************************************************
*
* 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 ResultSet to fetch batches of rows
* with getRows(). Also shows setting the fetch array size.
2015-07-20 15:53:29 +08:00
*
* This example uses Node 8's async/await syntax.
*
2015-07-20 15:53:29 +08:00
*****************************************************************************/
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
2019-11-19 10:26:03 +08:00
const demoSetup = require('./demosetup.js');
// Number of rows to return from each call to getRows()
2019-11-19 10:26:03 +08:00
const numRows = 2;
async function run() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
2019-11-19 10:26:03 +08:00
await demoSetup.setupBf(connection); // create the demo table
const result = await connection.execute(
2019-11-19 10:26:03 +08:00
`SELECT id, farmer
FROM no_banana_farmer
ORDER BY id`,
2015-07-20 15:53:29 +08:00
[], // no bind variables
{
resultSet: true // return a ResultSet (default is false)
}
);
2015-07-20 15:53:29 +08:00
// Fetch rows from the ResultSet.
//
// If getRows(numRows) returns:
// Zero rows => there were no rows, or are no more rows to return
// Fewer than numRows rows => this was the last set of rows to get
// Exactly numRows rows => there may be more rows to fetch
const rs = result.resultSet;
let rows;
do {
rows = await rs.getRows(numRows); // get numRows rows at a time
if (rows.length > 0) {
console.log("getRows(): Got " + rows.length + " rows");
2015-07-20 15:53:29 +08:00
console.log(rows);
}
} while (rows.length === numRows);
2015-07-20 15:53:29 +08:00
// always close the ResultSet
await rs.close();
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
2015-07-20 15:53:29 +08:00
}
run();