@oino-ts/db-postgresql 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -113,19 +113,56 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
113
113
|
return result;
|
|
114
114
|
}
|
|
115
115
|
async _query(sql) {
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
let connection = null;
|
|
117
|
+
try {
|
|
118
|
+
connection = await this._pool.connect();
|
|
119
|
+
const query_result = await connection.query({ rowMode: "array", text: sql });
|
|
120
|
+
let rows;
|
|
121
|
+
if (Array.isArray(query_result) == true) {
|
|
122
|
+
rows = query_result.flatMap((q) => q.rows);
|
|
123
|
+
}
|
|
124
|
+
else if (query_result.rows) {
|
|
125
|
+
rows = query_result.rows;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
rows = db_1.OINODB_EMPTY_ROWS; // return empty row if no rows returned
|
|
129
|
+
}
|
|
130
|
+
return new OINOPostgresqlData(rows, []);
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
return new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbPostgresql._query");
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
if (connection) {
|
|
137
|
+
connection.release();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
118
140
|
}
|
|
119
141
|
async _exec(sql) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
142
|
+
let connection = null;
|
|
143
|
+
try {
|
|
144
|
+
connection = await this._pool.connect();
|
|
145
|
+
const query_result = await connection.query({ rowMode: "array", text: sql });
|
|
146
|
+
let rows;
|
|
147
|
+
if (Array.isArray(query_result) == true) {
|
|
148
|
+
rows = query_result.flatMap((q) => q.rows);
|
|
149
|
+
}
|
|
150
|
+
else if (query_result.rows) {
|
|
151
|
+
rows = query_result.rows;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
rows = db_1.OINODB_EMPTY_ROWS; // return empty row if no rows returned
|
|
155
|
+
}
|
|
156
|
+
// if (rows.length > 0) { console.log("OINODbPostgresql._exec: rows", rows) }
|
|
157
|
+
return new OINOPostgresqlData(rows, []);
|
|
123
158
|
}
|
|
124
|
-
|
|
125
|
-
return
|
|
159
|
+
catch (e) {
|
|
160
|
+
return new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbPostgresql._exec");
|
|
126
161
|
}
|
|
127
|
-
|
|
128
|
-
|
|
162
|
+
finally {
|
|
163
|
+
if (connection) {
|
|
164
|
+
connection.release();
|
|
165
|
+
}
|
|
129
166
|
}
|
|
130
167
|
}
|
|
131
168
|
/**
|
|
@@ -227,15 +264,24 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
227
264
|
*/
|
|
228
265
|
async connect() {
|
|
229
266
|
let result = new common_1.OINOResult();
|
|
267
|
+
if (this.isConnected) {
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
let connection = null;
|
|
230
271
|
try {
|
|
231
272
|
// make sure that any items are correctly URL encoded in the connection string
|
|
232
|
-
await this._pool.connect();
|
|
273
|
+
connection = await this._pool.connect();
|
|
233
274
|
this.isConnected = true;
|
|
234
275
|
}
|
|
235
276
|
catch (e) {
|
|
236
277
|
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect");
|
|
237
278
|
common_1.OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "connect", "exception in connect", { message: e.message, stack: e.stack });
|
|
238
279
|
}
|
|
280
|
+
finally {
|
|
281
|
+
if (connection) {
|
|
282
|
+
connection.release();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
239
285
|
return result;
|
|
240
286
|
}
|
|
241
287
|
/**
|
|
@@ -247,7 +293,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
247
293
|
let result = new common_1.OINOResult();
|
|
248
294
|
try {
|
|
249
295
|
const sql = this._getValidateSql(this._params.database);
|
|
250
|
-
const sql_res = await this.
|
|
296
|
+
const sql_res = await this._query(sql);
|
|
251
297
|
if (sql_res.isEmpty()) {
|
|
252
298
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
253
299
|
}
|
|
@@ -268,6 +314,19 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
268
314
|
common_1.OINOBenchmark.endMetric("OINODb", "validate");
|
|
269
315
|
return result;
|
|
270
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Disconnect from database.
|
|
319
|
+
*
|
|
320
|
+
*/
|
|
321
|
+
async disconnect() {
|
|
322
|
+
if (this.isConnected) {
|
|
323
|
+
this._pool.end().catch((e) => {
|
|
324
|
+
common_1.OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "disconnect", "exception in pool end", { message: e.message, stack: e.stack });
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
this.isConnected = false;
|
|
328
|
+
this.isValidated = false;
|
|
329
|
+
}
|
|
271
330
|
/**
|
|
272
331
|
* Execute a select operation.
|
|
273
332
|
*
|
|
@@ -275,15 +334,11 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
275
334
|
*
|
|
276
335
|
*/
|
|
277
336
|
async sqlSelect(sql) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
try {
|
|
281
|
-
const rows = await this._query(sql);
|
|
282
|
-
result = new OINOPostgresqlData(rows, []);
|
|
283
|
-
}
|
|
284
|
-
catch (e) {
|
|
285
|
-
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
|
|
337
|
+
if (!this.isValidated) {
|
|
338
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
286
339
|
}
|
|
340
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
341
|
+
let result = await this._query(sql);
|
|
287
342
|
common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
288
343
|
return result;
|
|
289
344
|
}
|
|
@@ -294,15 +349,11 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
294
349
|
*
|
|
295
350
|
*/
|
|
296
351
|
async sqlExec(sql) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
try {
|
|
300
|
-
const rows = await this._exec(sql);
|
|
301
|
-
result = new OINOPostgresqlData(rows, []);
|
|
302
|
-
}
|
|
303
|
-
catch (e) {
|
|
304
|
-
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
352
|
+
if (!this.isValidated) {
|
|
353
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
305
354
|
}
|
|
355
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
356
|
+
let result = await this._exec(sql);
|
|
306
357
|
common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
307
358
|
return result;
|
|
308
359
|
}
|
|
@@ -359,7 +410,7 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
359
410
|
*
|
|
360
411
|
*/
|
|
361
412
|
async initializeApiDatamodel(api) {
|
|
362
|
-
const schema_res = await this.
|
|
413
|
+
const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName.toLowerCase()));
|
|
363
414
|
while (!schema_res.isEof()) {
|
|
364
415
|
const row = schema_res.getRow();
|
|
365
416
|
const field_name = row[0]?.toString() || "";
|
|
@@ -110,19 +110,56 @@ export class OINODbPostgresql extends OINODb {
|
|
|
110
110
|
return result;
|
|
111
111
|
}
|
|
112
112
|
async _query(sql) {
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
let connection = null;
|
|
114
|
+
try {
|
|
115
|
+
connection = await this._pool.connect();
|
|
116
|
+
const query_result = await connection.query({ rowMode: "array", text: sql });
|
|
117
|
+
let rows;
|
|
118
|
+
if (Array.isArray(query_result) == true) {
|
|
119
|
+
rows = query_result.flatMap((q) => q.rows);
|
|
120
|
+
}
|
|
121
|
+
else if (query_result.rows) {
|
|
122
|
+
rows = query_result.rows;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
rows = OINODB_EMPTY_ROWS; // return empty row if no rows returned
|
|
126
|
+
}
|
|
127
|
+
return new OINOPostgresqlData(rows, []);
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
return new OINOPostgresqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbPostgresql._query");
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
if (connection) {
|
|
134
|
+
connection.release();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
115
137
|
}
|
|
116
138
|
async _exec(sql) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
139
|
+
let connection = null;
|
|
140
|
+
try {
|
|
141
|
+
connection = await this._pool.connect();
|
|
142
|
+
const query_result = await connection.query({ rowMode: "array", text: sql });
|
|
143
|
+
let rows;
|
|
144
|
+
if (Array.isArray(query_result) == true) {
|
|
145
|
+
rows = query_result.flatMap((q) => q.rows);
|
|
146
|
+
}
|
|
147
|
+
else if (query_result.rows) {
|
|
148
|
+
rows = query_result.rows;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
rows = OINODB_EMPTY_ROWS; // return empty row if no rows returned
|
|
152
|
+
}
|
|
153
|
+
// if (rows.length > 0) { console.log("OINODbPostgresql._exec: rows", rows) }
|
|
154
|
+
return new OINOPostgresqlData(rows, []);
|
|
120
155
|
}
|
|
121
|
-
|
|
122
|
-
return
|
|
156
|
+
catch (e) {
|
|
157
|
+
return new OINOPostgresqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbPostgresql._exec");
|
|
123
158
|
}
|
|
124
|
-
|
|
125
|
-
|
|
159
|
+
finally {
|
|
160
|
+
if (connection) {
|
|
161
|
+
connection.release();
|
|
162
|
+
}
|
|
126
163
|
}
|
|
127
164
|
}
|
|
128
165
|
/**
|
|
@@ -224,15 +261,24 @@ export class OINODbPostgresql extends OINODb {
|
|
|
224
261
|
*/
|
|
225
262
|
async connect() {
|
|
226
263
|
let result = new OINOResult();
|
|
264
|
+
if (this.isConnected) {
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
let connection = null;
|
|
227
268
|
try {
|
|
228
269
|
// make sure that any items are correctly URL encoded in the connection string
|
|
229
|
-
await this._pool.connect();
|
|
270
|
+
connection = await this._pool.connect();
|
|
230
271
|
this.isConnected = true;
|
|
231
272
|
}
|
|
232
273
|
catch (e) {
|
|
233
274
|
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect");
|
|
234
275
|
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "connect", "exception in connect", { message: e.message, stack: e.stack });
|
|
235
276
|
}
|
|
277
|
+
finally {
|
|
278
|
+
if (connection) {
|
|
279
|
+
connection.release();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
236
282
|
return result;
|
|
237
283
|
}
|
|
238
284
|
/**
|
|
@@ -244,7 +290,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
244
290
|
let result = new OINOResult();
|
|
245
291
|
try {
|
|
246
292
|
const sql = this._getValidateSql(this._params.database);
|
|
247
|
-
const sql_res = await this.
|
|
293
|
+
const sql_res = await this._query(sql);
|
|
248
294
|
if (sql_res.isEmpty()) {
|
|
249
295
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
250
296
|
}
|
|
@@ -265,6 +311,19 @@ export class OINODbPostgresql extends OINODb {
|
|
|
265
311
|
OINOBenchmark.endMetric("OINODb", "validate");
|
|
266
312
|
return result;
|
|
267
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Disconnect from database.
|
|
316
|
+
*
|
|
317
|
+
*/
|
|
318
|
+
async disconnect() {
|
|
319
|
+
if (this.isConnected) {
|
|
320
|
+
this._pool.end().catch((e) => {
|
|
321
|
+
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "disconnect", "exception in pool end", { message: e.message, stack: e.stack });
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
this.isConnected = false;
|
|
325
|
+
this.isValidated = false;
|
|
326
|
+
}
|
|
268
327
|
/**
|
|
269
328
|
* Execute a select operation.
|
|
270
329
|
*
|
|
@@ -272,15 +331,11 @@ export class OINODbPostgresql extends OINODb {
|
|
|
272
331
|
*
|
|
273
332
|
*/
|
|
274
333
|
async sqlSelect(sql) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
try {
|
|
278
|
-
const rows = await this._query(sql);
|
|
279
|
-
result = new OINOPostgresqlData(rows, []);
|
|
280
|
-
}
|
|
281
|
-
catch (e) {
|
|
282
|
-
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
|
|
334
|
+
if (!this.isValidated) {
|
|
335
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
283
336
|
}
|
|
337
|
+
OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
338
|
+
let result = await this._query(sql);
|
|
284
339
|
OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
285
340
|
return result;
|
|
286
341
|
}
|
|
@@ -291,15 +346,11 @@ export class OINODbPostgresql extends OINODb {
|
|
|
291
346
|
*
|
|
292
347
|
*/
|
|
293
348
|
async sqlExec(sql) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
try {
|
|
297
|
-
const rows = await this._exec(sql);
|
|
298
|
-
result = new OINOPostgresqlData(rows, []);
|
|
299
|
-
}
|
|
300
|
-
catch (e) {
|
|
301
|
-
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
349
|
+
if (!this.isValidated) {
|
|
350
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
302
351
|
}
|
|
352
|
+
OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
353
|
+
let result = await this._exec(sql);
|
|
303
354
|
OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
304
355
|
return result;
|
|
305
356
|
}
|
|
@@ -356,7 +407,7 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
356
407
|
*
|
|
357
408
|
*/
|
|
358
409
|
async initializeApiDatamodel(api) {
|
|
359
|
-
const schema_res = await this.
|
|
410
|
+
const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName.toLowerCase()));
|
|
360
411
|
while (!schema_res.isEof()) {
|
|
361
412
|
const row = schema_res.getRow();
|
|
362
413
|
const field_name = row[0]?.toString() || "";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-postgresql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "OINO TS package for using Postgresql databases.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"module": "./dist/esm/index.js",
|
|
21
21
|
"types": "./dist/types/index.d.ts",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@oino-ts/db": "0.
|
|
23
|
+
"@oino-ts/db": "0.20.0",
|
|
24
24
|
"pg": "^8.11.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
package/src/OINODbPostgresql.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import { OINO_ERROR_PREFIX, OINOBenchmark, OINOLog, OINOResult } from "@oino-ts/common";
|
|
8
8
|
import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINODataRow, OINODataCell, OINODatetimeDataField, OINOBlobDataField, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
|
|
9
9
|
|
|
10
|
-
import { Pool, QueryResult } from "pg";
|
|
10
|
+
import { Pool, PoolClient, QueryResult } from "pg";
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -124,19 +124,50 @@ export class OINODbPostgresql extends OINODb {
|
|
|
124
124
|
return result
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
private async _query(sql:string):Promise<
|
|
128
|
-
|
|
129
|
-
|
|
127
|
+
private async _query(sql:string):Promise<OINODbDataSet> {
|
|
128
|
+
let connection:PoolClient|null = null
|
|
129
|
+
try {
|
|
130
|
+
connection = await this._pool.connect()
|
|
131
|
+
const query_result = await connection.query({rowMode: "array", text: sql})
|
|
132
|
+
let rows:OINODataRow[]
|
|
133
|
+
if (Array.isArray(query_result) == true) {
|
|
134
|
+
rows = query_result.flatMap((q) => q.rows)
|
|
135
|
+
} else if (query_result.rows) {
|
|
136
|
+
rows = query_result.rows
|
|
137
|
+
} else {
|
|
138
|
+
rows = OINODB_EMPTY_ROWS // return empty row if no rows returned
|
|
139
|
+
}
|
|
140
|
+
return new OINOPostgresqlData(rows, [])
|
|
141
|
+
} catch (e:any) {
|
|
142
|
+
return new OINOPostgresqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbPostgresql._query") as OINOPostgresqlData
|
|
143
|
+
} finally {
|
|
144
|
+
if (connection) {
|
|
145
|
+
connection.release()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
130
148
|
}
|
|
131
149
|
|
|
132
|
-
private async _exec(sql:string):Promise<
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
150
|
+
private async _exec(sql:string):Promise<OINODbDataSet> {
|
|
151
|
+
let connection:PoolClient|null = null
|
|
152
|
+
try {
|
|
153
|
+
connection = await this._pool.connect()
|
|
154
|
+
const query_result:QueryResult = await connection.query({rowMode: "array", text: sql})
|
|
155
|
+
let rows:OINODataRow[]
|
|
156
|
+
if (Array.isArray(query_result) == true) {
|
|
157
|
+
rows = query_result.flatMap((q) => q.rows)
|
|
158
|
+
} else if (query_result.rows) {
|
|
159
|
+
rows = query_result.rows
|
|
160
|
+
} else {
|
|
161
|
+
rows = OINODB_EMPTY_ROWS // return empty row if no rows returned
|
|
162
|
+
}
|
|
163
|
+
// if (rows.length > 0) { console.log("OINODbPostgresql._exec: rows", rows) }
|
|
164
|
+
return new OINOPostgresqlData(rows, [])
|
|
165
|
+
} catch (e:any) {
|
|
166
|
+
return new OINOPostgresqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbPostgresql._exec") as OINOPostgresqlData
|
|
167
|
+
} finally {
|
|
168
|
+
if (connection) {
|
|
169
|
+
connection.release()
|
|
170
|
+
}
|
|
140
171
|
}
|
|
141
172
|
}
|
|
142
173
|
|
|
@@ -243,15 +274,24 @@ export class OINODbPostgresql extends OINODb {
|
|
|
243
274
|
*/
|
|
244
275
|
async connect(): Promise<OINOResult> {
|
|
245
276
|
let result:OINOResult = new OINOResult()
|
|
277
|
+
if (this.isConnected) {
|
|
278
|
+
return result
|
|
279
|
+
}
|
|
280
|
+
let connection:PoolClient|null = null
|
|
246
281
|
try {
|
|
247
282
|
// make sure that any items are correctly URL encoded in the connection string
|
|
248
|
-
await this._pool.connect()
|
|
283
|
+
connection = await this._pool.connect()
|
|
249
284
|
this.isConnected = true
|
|
250
285
|
|
|
251
286
|
} catch (e:any) {
|
|
252
287
|
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect")
|
|
253
288
|
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "connect", "exception in connect", {message:e.message, stack:e.stack})
|
|
254
|
-
}
|
|
289
|
+
} finally {
|
|
290
|
+
if (connection) {
|
|
291
|
+
connection.release()
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
255
295
|
return result
|
|
256
296
|
}
|
|
257
297
|
|
|
@@ -264,7 +304,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
264
304
|
let result:OINOResult = new OINOResult()
|
|
265
305
|
try {
|
|
266
306
|
const sql = this._getValidateSql(this._params.database)
|
|
267
|
-
const sql_res:OINODbDataSet = await this.
|
|
307
|
+
const sql_res:OINODbDataSet = await this._query(sql)
|
|
268
308
|
if (sql_res.isEmpty()) {
|
|
269
309
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate")
|
|
270
310
|
|
|
@@ -285,6 +325,21 @@ export class OINODbPostgresql extends OINODb {
|
|
|
285
325
|
return result
|
|
286
326
|
}
|
|
287
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Disconnect from database.
|
|
330
|
+
*
|
|
331
|
+
*/
|
|
332
|
+
async disconnect(): Promise<void> {
|
|
333
|
+
if (this.isConnected) {
|
|
334
|
+
this._pool.end().catch((e:any) => {
|
|
335
|
+
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "disconnect", "exception in pool end", {message:e.message, stack:e.stack})
|
|
336
|
+
})
|
|
337
|
+
}
|
|
338
|
+
this.isConnected = false
|
|
339
|
+
this.isValidated = false
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
|
|
288
343
|
/**
|
|
289
344
|
* Execute a select operation.
|
|
290
345
|
*
|
|
@@ -292,15 +347,11 @@ export class OINODbPostgresql extends OINODb {
|
|
|
292
347
|
*
|
|
293
348
|
*/
|
|
294
349
|
async sqlSelect(sql:string): Promise<OINODbDataSet> {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
try {
|
|
298
|
-
const rows:OINODataRow[] = await this._query(sql)
|
|
299
|
-
result = new OINOPostgresqlData(rows, [])
|
|
300
|
-
|
|
301
|
-
} catch (e:any) {
|
|
302
|
-
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"])
|
|
350
|
+
if (!this.isValidated) {
|
|
351
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
|
|
303
352
|
}
|
|
353
|
+
OINOBenchmark.startMetric("OINODb", "sqlSelect")
|
|
354
|
+
let result:OINODbDataSet = await this._query(sql)
|
|
304
355
|
OINOBenchmark.endMetric("OINODb", "sqlSelect")
|
|
305
356
|
return result
|
|
306
357
|
}
|
|
@@ -312,15 +363,11 @@ export class OINODbPostgresql extends OINODb {
|
|
|
312
363
|
*
|
|
313
364
|
*/
|
|
314
365
|
async sqlExec(sql:string): Promise<OINODbDataSet> {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
try {
|
|
318
|
-
const rows:OINODataRow[] = await this._exec(sql)
|
|
319
|
-
result = new OINOPostgresqlData(rows, [])
|
|
320
|
-
|
|
321
|
-
} catch (e:any) {
|
|
322
|
-
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
|
|
366
|
+
if (!this.isValidated) {
|
|
367
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
|
|
323
368
|
}
|
|
369
|
+
OINOBenchmark.startMetric("OINODb", "sqlExec")
|
|
370
|
+
let result:OINODbDataSet = await this._exec(sql)
|
|
324
371
|
OINOBenchmark.endMetric("OINODb", "sqlExec")
|
|
325
372
|
return result
|
|
326
373
|
}
|
|
@@ -383,7 +430,7 @@ WHERE col.table_catalog = '${dbName}'`
|
|
|
383
430
|
*/
|
|
384
431
|
async initializeApiDatamodel(api:OINODbApi): Promise<void> {
|
|
385
432
|
|
|
386
|
-
const schema_res:OINODbDataSet = await this.
|
|
433
|
+
const schema_res:OINODbDataSet = await this._query(this._getSchemaSql(this._params.database, api.params.tableName.toLowerCase()))
|
|
387
434
|
while (!schema_res.isEof()) {
|
|
388
435
|
const row:OINODataRow = schema_res.getRow()
|
|
389
436
|
const field_name:string = row[0]?.toString() || ""
|