text: /* Copyright (c) 2018, 2020, 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 connection pool examples see connectionpool.js and webappawait.js
   *   For a ResultSet example see resultset1.js
   *   For a query stream example see selectstream.js
   *
   *   This example requires node-oracledb 2.2 or later.
   *
   *****************************************************************************/
  
  // Using a fixed Oracle time zone helps avoid machine and deployment differences
  process.env.ORA_SDTZ = 'UTC';
  
  const oracledb = require('oracledb');
  const dbConfig = require('./dbconfig.js');
  
  async function run() {
    let connection;
  
    try {
  
      let sql, binds, options, result;
  
      connection = await oracledb.getConnection(dbConfig);
  
      //
      // Create a table
      //
  
      const stmts = [
        `DROP TABLE no_example`,
  
        `CREATE TABLE no_example (id NUMBER, data VARCHAR2(20))`
      ];
  
      for (const s of stmts) {
        try {
          await connection.execute(s);
        } catch(e) {
          if (e.errorNum != 942)
            console.error(e);
        }
      }
  
      //
      // Insert three rows
      //
  
      sql = `INSERT INTO no_example 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 no_example`;
  
      binds = {};
  
      // For a complete list of options see the documentation.
      options = {
        outFormat: oracledb.OUT_FORMAT_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("Metadata: ");
      console.dir(result.metaData, { depth: null });
      console.log("Query results: ");
      console.dir(result.rows, { depth: null });
  
      //
      // 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']);
  
    } catch (err) {
      console.error(err);
    } finally {
      if (connection) {
        try {
          await connection.close();
        } catch (err) {
          console.error(err);
        }
      }
    }
  }
  
  run();
  