node-oracledb/examples/resultset2.js

111 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-05-17 11:09:34 +08:00
/* Copyright (c) 2015, 2022, Oracle and/or its affiliates. */
2015-07-20 15:53:29 +08:00
/******************************************************************************
*
* This software is dual-licensed to you under the Universal Permissive License
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
* 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
* either license.
2015-07-20 15:53:29 +08:00
*
* If you elect to accept the software under the Apache License, Version 2.0,
* the following applies:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
2015-07-20 15:53:29 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2015-07-20 15:53:29 +08:00
*
* 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.
2015-07-20 15:53:29 +08:00
* 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 fs = require('fs');
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
2019-11-19 10:26:03 +08:00
const demoSetup = require('./demosetup.js');
2021-05-20 14:47:23 +08:00
// On Windows and macOS, you can specify the directory containing the Oracle
// Client Libraries at runtime, or before Node.js starts. On other platforms
// the system library search path must always be set before Node.js is started.
// See the node-oracledb installation documentation.
// If the search path is not correct, you will get a DPI-1047 error.
let libPath;
if (process.platform === 'win32') { // Windows
libPath = 'C:\\oracle\\instantclient_19_12';
} else if (process.platform === 'darwin') { // macOS
libPath = process.env.HOME + '/Downloads/instantclient_19_8';
}
if (libPath && fs.existsSync(libPath)) {
oracledb.initOracleClient({ libDir: libPath });
2021-05-20 14:47:23 +08:00
}
// 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();