116 lines
3.1 KiB
JavaScript
116 lines
3.1 KiB
JavaScript
/* Copyright (c) 2018, 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
|
|
* example.js
|
|
*
|
|
* DESCRIPTION
|
|
* A basic node-oracledb example using Node.js 8's async/await syntax.
|
|
*
|
|
* For a connection pool example see connectionpool.js
|
|
* 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
|
|
*
|
|
*****************************************************************************/
|
|
|
|
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);
|
|
|
|
} catch (err) {
|
|
console.error(err);
|
|
} finally {
|
|
if (connection) {
|
|
try {
|
|
await connection.close();
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
run();
|