|
- var oracledb = require("oracledb");
- var log = require("./log.js");
- var util = require("./util.js");
-
- var getPoolConnection = async function (config) {
- let pool;
- try {
- pool = await oracledb.createPool({
- user: config.user,
- password: config.password, // mypw contains the hr schema password
- connectString: `${config.ip_address}:${config.port}/${config.dbname}`,
- queueTimeout: 600000,
- });
- } catch (e) {
- pool.close();
- return e.message;
- } finally {
- if (pool) {
- return pool;
- }
- }
- };
-
- var getConnected = async function (config, sql, params) {
- let pool;
- let conn;
- var result;
- try {
- pool = await getPoolConnection(config);
-
- conn = await pool.getConnection();
- var changeTz = await conn.execute(
- `
- begin
- execute immediate 'alter session set time_zone=''UTC''';
- end;
- `,
- {}
- );
-
- data = await conn.execute(sql, params, { outFormat: oracledb.OBJECT });
- result = { STATUS: 1, DATA: data.rows };
- } catch (e) {
- result = { STATUS: 0, DATA: e.message };
- util.throwNotif(e.message);
- log.createLog(0, e.message);
- } finally {
- if (conn) {
- await conn.close();
- }
- if (pool) {
- await pool.close();
- }
- return result;
- }
- };
-
- var getConnectedInsert = async function (config, sql, params) {
- let pool;
- let conn;
- var result;
- try {
- pool = await getPoolConnection(config);
-
- conn = await pool.getConnection();
- var changeTz = await conn.execute(
- `
- begin
- execute immediate 'alter session set time_zone=''UTC''';
- end;
- `,
- {}
- );
- data = await conn.execute(sql, params, { outFormat: oracledb.OBJECT });
- await conn.commit();
- result = { STATUS: 1, DATA: data };
- } catch (e) {
- await conn.rollback();
- result = { STATUS: 0, DATA: e.message };
- log.createLog(0, e.message);
- util.throwNotif(e.message);
- } finally {
- if (conn) {
- await conn.close();
- }
- if (pool) {
- await pool.close();
- }
- return result;
- }
- };
-
- var getConnectedInsertMany = async function (config, sql, params) {
- let pool;
- let conn;
- var result;
-
- try {
- pool = await getPoolConnection(config);
-
- conn = await pool.getConnection();
-
- var changeTz = await conn.execute(
- `
- begin
- execute immediate 'alter session set time_zone=''UTC''';
- end;
- `,
- {}
- );
-
- data = await conn.executeMany(sql, params, { outFormat: oracledb.OBJECT });
- await conn.commit();
- result = { STATUS: 1, DATA: data };
- } catch (e) {
- await conn.rollback();
- result = { STATUS: 0, DATA: e };
- } finally {
- if (conn) {
- await conn.close();
- }
- if (pool) {
- await pool.close();
- }
- return result;
- }
- };
-
- module.exports.getConnected = getConnected;
- module.exports.getConnectedInsert = getConnectedInsert;
- module.exports.getConnectedInsertMany = getConnectedInsertMany;
|