node-oracledb/examples/example.js

128 lines
3.5 KiB
JavaScript
Raw Normal View History

/* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. */
2018-09-26 16:02:52 +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
* example.js
*
* DESCRIPTION
* A basic node-oracledb example using Node.js 8's async/await syntax.
*
2018-11-26 11:36:14 +08:00
* For a connection pool example see connectionpool.js
2018-09-26 16:02:52 +08:00
* For a ResultSet example see resultset2.js
* For a query stream example see selectstream.js
* For a Promise example see promises.js
* For a callback example see select1.js
*
* This example requires node-oracledb 2.2 or later.
*
2018-09-26 16:02:52 +08:00
*****************************************************************************/
2019-01-11 05:55:31 +08:00
// Using a fixed Oracle time zone helps avoid machine and deployment differences
process.env.ORA_SDTZ = 'UTC';
2018-09-26 16:02:52 +08:00
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
async function run() {
let connection;
try {
let sql, binds, options, result;
connection = await oracledb.getConnection( {
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
});
// Create a table
await connection.execute(
`BEGIN
EXECUTE IMMEDIATE 'DROP TABLE mytab';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE NOT IN (-00942) THEN
RAISE;
END IF;
END;`);
await connection.execute(
`CREATE TABLE mytab (id NUMBER, data VARCHAR2(20))`);
// Insert some data
sql = `INSERT INTO mytab VALUES (:1, :2)`;
binds = [ [101, "Alpha" ], [102, "Beta" ], [103, "Gamma" ] ];
// For a complete list of options see the documentation.
options = {
autoCommit: true,
// batchErrors: true, // continue processing even if there are data errors
bindDefs: [
{ type: oracledb.NUMBER },
{ type: oracledb.STRING, maxSize: 20 }
]
};
result = await connection.executeMany(sql, binds, options);
console.log("Number of rows inserted:", result.rowsAffected);
// Query the data
sql = `SELECT * FROM mytab`;
binds = {};
// For a complete list of options see the documentation.
options = {
outFormat: oracledb.OBJECT // query result format
// extendedMetaData: true, // get extra metadata
// fetchArraySize: 100 // internal buffer allocation size for tuning
};
result = await connection.execute(sql, binds, options);
console.log("Column metadata: ", result.metaData);
console.log("Query results: ");
console.log(result.rows);
2019-01-11 05:55:31 +08:00
// Show the date. The value of ORA_SDTZ affects the output
sql = `SELECT TO_CHAR(CURRENT_DATE, 'DD-Mon-YYYY HH24:MI') AS CD FROM DUAL`;
result = await connection.execute(sql, binds, options);
console.log("Current date query results: ");
console.log(result.rows[0]['CD']);
2018-09-26 16:02:52 +08:00
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();