1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const oracledb = require("oracledb");
|
4 | const base_1 = require("../error/base");
|
5 | const db_service_1 = require("./db_service");
|
6 | const db_1 = require("../error/db");
|
7 | exports.DB_CONNECT_ERROR = 'DB_QUERY';
|
8 | exports.DB_ORACLE_ERROR = 'DB_ORACLE_ERROR';
|
9 | exports.DB_ORACLE_FETCH_ERROR = 'DB_ORACLE_FETCH_ERROR';
|
10 | exports.DB_ORACLE_CLOSE_ERROR = 'DB_ORACLE_CLOSE_ERROR';
|
11 | exports.DB_ORACLE_RELEASE_ERROR = 'DB_ORACLE_RELEASE_ERROR';
|
12 | exports.DB_ORACLE_EXECUTE_ERROR = 'DB_ORACLE_EXECUTE_ERROR';
|
13 | class OracleService {
|
14 | constructor(connectionParams, options) {
|
15 | this.poolMax = 100;
|
16 | this.maxQueryAttempts = 10;
|
17 | this._queryFailsCounter = {};
|
18 | this.options = options || {};
|
19 | this.connectionParams = connectionParams;
|
20 | }
|
21 | async run(query, params, options = {}) {
|
22 | return await this._run(query, params, options);
|
23 | }
|
24 | async getManyRows(query, params = []) {
|
25 | try {
|
26 | return await this._run(query, params, { resultSet: true, prefetchRows: 500 });
|
27 | }
|
28 | catch (error) {
|
29 | throw new db_1.DbError({
|
30 | code: exports.DB_ORACLE_ERROR,
|
31 | innerDetails: {
|
32 | query,
|
33 | message: error.message,
|
34 | params
|
35 | }
|
36 | });
|
37 | }
|
38 | }
|
39 | async fetchRows(resultSet, numRows) {
|
40 | let rows;
|
41 | try {
|
42 | rows = await resultSet.getRows(numRows);
|
43 | }
|
44 | catch (error) {
|
45 | await this.closeResultSet(resultSet);
|
46 | throw new db_1.DbError({
|
47 | code: exports.DB_ORACLE_FETCH_ERROR,
|
48 | innerDetails: error.message
|
49 | });
|
50 | }
|
51 | if (rows.length === 0) {
|
52 | await this.closeResultSet(resultSet);
|
53 | return [];
|
54 | }
|
55 | return rows;
|
56 | }
|
57 | async closeResultSet(resultSet, connection) {
|
58 | try {
|
59 | await resultSet.close();
|
60 | }
|
61 | catch (error) {
|
62 | throw new db_1.DbError({
|
63 | code: exports.DB_ORACLE_FETCH_ERROR,
|
64 | innerDetails: error.message
|
65 | });
|
66 | }
|
67 | if (connection) {
|
68 | await this._disconnect(connection);
|
69 | }
|
70 | }
|
71 | async getRows(query, params = [], options = {}) {
|
72 | try {
|
73 | const result = await this._run(query, params, options);
|
74 | return result.rows;
|
75 | }
|
76 | catch (error) {
|
77 | throw new db_1.DbError({
|
78 | code: exports.DB_ORACLE_ERROR,
|
79 | innerDetails: {
|
80 | query,
|
81 | message: error.message,
|
82 | params
|
83 | }
|
84 | });
|
85 | }
|
86 | }
|
87 | async getRow(query, params = [], options = {}) {
|
88 | const rows = await this.getRows(query, params, options);
|
89 | if (rows.length === 0) {
|
90 | return false;
|
91 | }
|
92 | if (rows.length > 1) {
|
93 | console.warn(db_service_1.ONE_ROW_WARNING, rows.length, query);
|
94 | }
|
95 | return rows[0];
|
96 | }
|
97 | async mustGetRow(errorCode, query, params = [], options = {}) {
|
98 | const row = await this.getRow(query, params, options);
|
99 | if (row === false) {
|
100 | throw new db_1.DbError({
|
101 | code: errorCode,
|
102 | status: base_1.BaseError.CODE_NOT_FOUND,
|
103 | innerDetails: {}
|
104 | });
|
105 | }
|
106 | return row;
|
107 | }
|
108 | async _connect() {
|
109 | try {
|
110 | if (!this.pool) {
|
111 | this.pool = await oracledb.createPool(Object.assign({}, this.connectionParams, { poolMax: this.poolMax }));
|
112 | }
|
113 | return await this.pool.getConnection();
|
114 | }
|
115 | catch (error) {
|
116 | throw new db_1.DbError({
|
117 | code: exports.DB_CONNECT_ERROR,
|
118 | innerDetails: error.message
|
119 | });
|
120 | }
|
121 | }
|
122 | async _disconnect(connection) {
|
123 | try {
|
124 | await connection.close();
|
125 | }
|
126 | catch (error) {
|
127 | throw new db_1.DbError({
|
128 | code: exports.DB_ORACLE_RELEASE_ERROR,
|
129 | innerDetails: error.message
|
130 | });
|
131 | }
|
132 | }
|
133 | async _run(query, params, options) {
|
134 | let connection;
|
135 | options = Object.assign({}, this.options, options);
|
136 | try {
|
137 | connection = await this._connect();
|
138 | const result = await connection.execute(query, params, options);
|
139 | await this._disconnect(connection);
|
140 | return result;
|
141 | }
|
142 | catch (error) {
|
143 | if (connection) {
|
144 | await this._disconnect(connection);
|
145 | }
|
146 | if (this._needRetry(arguments, error)) {
|
147 | await this.pool.close();
|
148 | this.pool = null;
|
149 | return this._run(query, params, options);
|
150 | }
|
151 | else {
|
152 | throw new db_1.DbError({
|
153 | publicMessage: error.message,
|
154 | innerDetails: error
|
155 | });
|
156 | }
|
157 | }
|
158 | }
|
159 | _needRetry(runArguments, error) {
|
160 | if (!error || !error.message) {
|
161 | return false;
|
162 | }
|
163 | const retryErrorsRegexp = /^(ORA-03113|ORA-04068|ORA-00028|ORA-02020)/ig;
|
164 | if (!retryErrorsRegexp.test(error.message)) {
|
165 | return false;
|
166 | }
|
167 | const failsCounterKey = JSON.stringify(runArguments);
|
168 | if (!this._queryFailsCounter[failsCounterKey]) {
|
169 | this._queryFailsCounter[failsCounterKey] = 0;
|
170 | }
|
171 | this._queryFailsCounter[failsCounterKey]++;
|
172 | return this._queryFailsCounter[failsCounterKey] < this.maxQueryAttempts;
|
173 | }
|
174 | }
|
175 | exports.OracleService = OracleService;
|