@oino-ts/db-postgresql 0.6.0 → 0.7.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.
|
@@ -8,7 +8,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.OINODbPostgresql = void 0;
|
|
9
9
|
const db_1 = require("@oino-ts/db");
|
|
10
10
|
const pg_1 = require("pg");
|
|
11
|
-
const EMPTY_ROW = [];
|
|
12
11
|
/**
|
|
13
12
|
* Implmentation of OINODbDataSet for Postgresql.
|
|
14
13
|
*
|
|
@@ -73,9 +72,16 @@ class OINOPostgresqlData extends db_1.OINODbDataSet {
|
|
|
73
72
|
return this._rows[this._currentRow];
|
|
74
73
|
}
|
|
75
74
|
else {
|
|
76
|
-
return
|
|
75
|
+
return db_1.OINODB_EMPTY_ROW;
|
|
77
76
|
}
|
|
78
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Gets all rows of data.
|
|
80
|
+
*
|
|
81
|
+
*/
|
|
82
|
+
async getAllRows() {
|
|
83
|
+
return this._rows; // at the moment theres no result streaming, so we can just return the rows
|
|
84
|
+
}
|
|
79
85
|
}
|
|
80
86
|
/**
|
|
81
87
|
* Implementation of Postgresql-database.
|
|
@@ -93,20 +99,21 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
93
99
|
if (this._params.type !== "OINODbPostgresql") {
|
|
94
100
|
throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
|
|
95
101
|
}
|
|
96
|
-
const ssl_enabled = !(
|
|
97
|
-
this._pool = new pg_1.Pool({ host:
|
|
102
|
+
const ssl_enabled = !(this._params.url == "localhost" || this._params.url == "127.0.0.1");
|
|
103
|
+
this._pool = new pg_1.Pool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, ssl: ssl_enabled });
|
|
104
|
+
delete this._params.password;
|
|
98
105
|
this._pool.on("error", (err) => {
|
|
99
|
-
db_1.OINOLog.error("OINODbPostgresql error", { err: err });
|
|
100
|
-
});
|
|
101
|
-
this._pool.on("connect", (message) => {
|
|
102
|
-
// OINOLog.info("OINODbPostgresql connect")
|
|
103
|
-
});
|
|
104
|
-
this._pool.on("release", (message) => {
|
|
105
|
-
// OINOLog.info("OINODbPostgresql notice")
|
|
106
|
-
});
|
|
107
|
-
this._pool.on("acquire", () => {
|
|
108
|
-
// OINOLog.info("OINODbPostgresql end")
|
|
106
|
+
db_1.OINOLog.error("OINODbPostgresql error event", { err: err });
|
|
109
107
|
});
|
|
108
|
+
// this._pool.on("connect", (message: any) => {
|
|
109
|
+
// OINOLog.info("OINODbPostgresql connect")
|
|
110
|
+
// })
|
|
111
|
+
// this._pool.on("release", (message: any) => {
|
|
112
|
+
// OINOLog.info("OINODbPostgresql notice")
|
|
113
|
+
// })
|
|
114
|
+
// this._pool.on("acquire", () => {
|
|
115
|
+
// OINOLog.info("OINODbPostgresql end")
|
|
116
|
+
// })
|
|
110
117
|
}
|
|
111
118
|
_parseFieldLength(fieldLength) {
|
|
112
119
|
let result = parseInt((fieldLength || "0").toString());
|
|
@@ -122,10 +129,18 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
122
129
|
return Promise.resolve(query_result.rows);
|
|
123
130
|
}
|
|
124
131
|
async _exec(sql) {
|
|
125
|
-
// OINOLog.debug("OINODbPostgresql.
|
|
132
|
+
// OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
|
|
126
133
|
const query_result = await this._pool.query({ rowMode: "array", text: sql });
|
|
127
|
-
// OINOLog.debug("OINODbPostgresql.
|
|
128
|
-
|
|
134
|
+
// OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
|
|
135
|
+
if (Array.isArray(query_result) == true) {
|
|
136
|
+
return Promise.resolve(query_result.flatMap((q) => q.rows));
|
|
137
|
+
}
|
|
138
|
+
else if (query_result.rows) {
|
|
139
|
+
return Promise.resolve(query_result.rows);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
return Promise.resolve(db_1.OINODB_EMPTY_ROWS); // return empty row if no rows returned
|
|
143
|
+
}
|
|
129
144
|
}
|
|
130
145
|
/**
|
|
131
146
|
* Print a table name using database specific SQL escaping.
|
|
@@ -225,17 +240,18 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
225
240
|
*
|
|
226
241
|
*/
|
|
227
242
|
async connect() {
|
|
243
|
+
let result = new db_1.OINOResult();
|
|
228
244
|
try {
|
|
229
245
|
// make sure that any items are correctly URL encoded in the connection string
|
|
230
246
|
// OINOLog.debug("OINODbPostgresql.connect")
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return Promise.resolve(true);
|
|
247
|
+
await this._pool.connect();
|
|
248
|
+
this.isConnected = true;
|
|
234
249
|
}
|
|
235
250
|
catch (err) {
|
|
236
|
-
|
|
237
|
-
|
|
251
|
+
result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect");
|
|
252
|
+
db_1.OINOLog.error(result.statusMessage, { error: err });
|
|
238
253
|
}
|
|
254
|
+
return result;
|
|
239
255
|
}
|
|
240
256
|
/**
|
|
241
257
|
* Validate connection to database is working.
|
|
@@ -246,24 +262,25 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
246
262
|
let result = new db_1.OINOResult();
|
|
247
263
|
try {
|
|
248
264
|
const sql = this._getValidateSql(this._params.database);
|
|
249
|
-
// OINOLog.debug("
|
|
265
|
+
// OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
|
|
250
266
|
const sql_res = await this.sqlSelect(sql);
|
|
251
|
-
// OINOLog.debug("
|
|
267
|
+
// OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
|
|
252
268
|
if (sql_res.isEmpty()) {
|
|
253
|
-
result.setError(400, "DB returned no rows for select!", "
|
|
269
|
+
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
254
270
|
}
|
|
255
271
|
else if (sql_res.getRow().length == 0) {
|
|
256
|
-
result.setError(400, "DB returned no values for database!", "
|
|
272
|
+
result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate");
|
|
257
273
|
}
|
|
258
274
|
else if (sql_res.getRow()[0] == "0") {
|
|
259
|
-
result.setError(400, "DB returned no schema for database!", "
|
|
275
|
+
result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate");
|
|
260
276
|
}
|
|
261
277
|
else {
|
|
262
|
-
|
|
278
|
+
this.isValidated = true;
|
|
263
279
|
}
|
|
264
280
|
}
|
|
265
|
-
catch (
|
|
266
|
-
result.setError(500,
|
|
281
|
+
catch (err) {
|
|
282
|
+
result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate");
|
|
283
|
+
db_1.OINOLog.error(result.statusMessage, { error: err });
|
|
267
284
|
}
|
|
268
285
|
db_1.OINOBenchmark.end("OINODb", "validate");
|
|
269
286
|
return result;
|
|
@@ -283,7 +300,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
283
300
|
result = new OINOPostgresqlData(rows, []);
|
|
284
301
|
}
|
|
285
302
|
catch (e) {
|
|
286
|
-
result = new OINOPostgresqlData(
|
|
303
|
+
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
|
|
287
304
|
}
|
|
288
305
|
db_1.OINOBenchmark.end("OINODb", "sqlSelect");
|
|
289
306
|
return result;
|
|
@@ -303,7 +320,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
303
320
|
result = new OINOPostgresqlData(rows, []);
|
|
304
321
|
}
|
|
305
322
|
catch (e) {
|
|
306
|
-
result = new OINOPostgresqlData(
|
|
323
|
+
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
307
324
|
}
|
|
308
325
|
db_1.OINOBenchmark.end("OINODb", "sqlExec");
|
|
309
326
|
return result;
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
|
-
import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult } from "@oino-ts/db";
|
|
6
|
+
import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
|
|
7
7
|
import { Pool } from "pg";
|
|
8
|
-
const EMPTY_ROW = [];
|
|
9
8
|
/**
|
|
10
9
|
* Implmentation of OINODbDataSet for Postgresql.
|
|
11
10
|
*
|
|
@@ -70,9 +69,16 @@ class OINOPostgresqlData extends OINODbDataSet {
|
|
|
70
69
|
return this._rows[this._currentRow];
|
|
71
70
|
}
|
|
72
71
|
else {
|
|
73
|
-
return
|
|
72
|
+
return OINODB_EMPTY_ROW;
|
|
74
73
|
}
|
|
75
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Gets all rows of data.
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
async getAllRows() {
|
|
80
|
+
return this._rows; // at the moment theres no result streaming, so we can just return the rows
|
|
81
|
+
}
|
|
76
82
|
}
|
|
77
83
|
/**
|
|
78
84
|
* Implementation of Postgresql-database.
|
|
@@ -90,20 +96,21 @@ export class OINODbPostgresql extends OINODb {
|
|
|
90
96
|
if (this._params.type !== "OINODbPostgresql") {
|
|
91
97
|
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
|
|
92
98
|
}
|
|
93
|
-
const ssl_enabled = !(
|
|
94
|
-
this._pool = new Pool({ host:
|
|
99
|
+
const ssl_enabled = !(this._params.url == "localhost" || this._params.url == "127.0.0.1");
|
|
100
|
+
this._pool = new Pool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, ssl: ssl_enabled });
|
|
101
|
+
delete this._params.password;
|
|
95
102
|
this._pool.on("error", (err) => {
|
|
96
|
-
OINOLog.error("OINODbPostgresql error", { err: err });
|
|
97
|
-
});
|
|
98
|
-
this._pool.on("connect", (message) => {
|
|
99
|
-
// OINOLog.info("OINODbPostgresql connect")
|
|
100
|
-
});
|
|
101
|
-
this._pool.on("release", (message) => {
|
|
102
|
-
// OINOLog.info("OINODbPostgresql notice")
|
|
103
|
-
});
|
|
104
|
-
this._pool.on("acquire", () => {
|
|
105
|
-
// OINOLog.info("OINODbPostgresql end")
|
|
103
|
+
OINOLog.error("OINODbPostgresql error event", { err: err });
|
|
106
104
|
});
|
|
105
|
+
// this._pool.on("connect", (message: any) => {
|
|
106
|
+
// OINOLog.info("OINODbPostgresql connect")
|
|
107
|
+
// })
|
|
108
|
+
// this._pool.on("release", (message: any) => {
|
|
109
|
+
// OINOLog.info("OINODbPostgresql notice")
|
|
110
|
+
// })
|
|
111
|
+
// this._pool.on("acquire", () => {
|
|
112
|
+
// OINOLog.info("OINODbPostgresql end")
|
|
113
|
+
// })
|
|
107
114
|
}
|
|
108
115
|
_parseFieldLength(fieldLength) {
|
|
109
116
|
let result = parseInt((fieldLength || "0").toString());
|
|
@@ -119,10 +126,18 @@ export class OINODbPostgresql extends OINODb {
|
|
|
119
126
|
return Promise.resolve(query_result.rows);
|
|
120
127
|
}
|
|
121
128
|
async _exec(sql) {
|
|
122
|
-
// OINOLog.debug("OINODbPostgresql.
|
|
129
|
+
// OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
|
|
123
130
|
const query_result = await this._pool.query({ rowMode: "array", text: sql });
|
|
124
|
-
// OINOLog.debug("OINODbPostgresql.
|
|
125
|
-
|
|
131
|
+
// OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
|
|
132
|
+
if (Array.isArray(query_result) == true) {
|
|
133
|
+
return Promise.resolve(query_result.flatMap((q) => q.rows));
|
|
134
|
+
}
|
|
135
|
+
else if (query_result.rows) {
|
|
136
|
+
return Promise.resolve(query_result.rows);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
return Promise.resolve(OINODB_EMPTY_ROWS); // return empty row if no rows returned
|
|
140
|
+
}
|
|
126
141
|
}
|
|
127
142
|
/**
|
|
128
143
|
* Print a table name using database specific SQL escaping.
|
|
@@ -222,17 +237,18 @@ export class OINODbPostgresql extends OINODb {
|
|
|
222
237
|
*
|
|
223
238
|
*/
|
|
224
239
|
async connect() {
|
|
240
|
+
let result = new OINOResult();
|
|
225
241
|
try {
|
|
226
242
|
// make sure that any items are correctly URL encoded in the connection string
|
|
227
243
|
// OINOLog.debug("OINODbPostgresql.connect")
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return Promise.resolve(true);
|
|
244
|
+
await this._pool.connect();
|
|
245
|
+
this.isConnected = true;
|
|
231
246
|
}
|
|
232
247
|
catch (err) {
|
|
233
|
-
|
|
234
|
-
|
|
248
|
+
result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect");
|
|
249
|
+
OINOLog.error(result.statusMessage, { error: err });
|
|
235
250
|
}
|
|
251
|
+
return result;
|
|
236
252
|
}
|
|
237
253
|
/**
|
|
238
254
|
* Validate connection to database is working.
|
|
@@ -243,24 +259,25 @@ export class OINODbPostgresql extends OINODb {
|
|
|
243
259
|
let result = new OINOResult();
|
|
244
260
|
try {
|
|
245
261
|
const sql = this._getValidateSql(this._params.database);
|
|
246
|
-
// OINOLog.debug("
|
|
262
|
+
// OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
|
|
247
263
|
const sql_res = await this.sqlSelect(sql);
|
|
248
|
-
// OINOLog.debug("
|
|
264
|
+
// OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
|
|
249
265
|
if (sql_res.isEmpty()) {
|
|
250
|
-
result.setError(400, "DB returned no rows for select!", "
|
|
266
|
+
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
251
267
|
}
|
|
252
268
|
else if (sql_res.getRow().length == 0) {
|
|
253
|
-
result.setError(400, "DB returned no values for database!", "
|
|
269
|
+
result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate");
|
|
254
270
|
}
|
|
255
271
|
else if (sql_res.getRow()[0] == "0") {
|
|
256
|
-
result.setError(400, "DB returned no schema for database!", "
|
|
272
|
+
result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate");
|
|
257
273
|
}
|
|
258
274
|
else {
|
|
259
|
-
|
|
275
|
+
this.isValidated = true;
|
|
260
276
|
}
|
|
261
277
|
}
|
|
262
|
-
catch (
|
|
263
|
-
result.setError(500,
|
|
278
|
+
catch (err) {
|
|
279
|
+
result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate");
|
|
280
|
+
OINOLog.error(result.statusMessage, { error: err });
|
|
264
281
|
}
|
|
265
282
|
OINOBenchmark.end("OINODb", "validate");
|
|
266
283
|
return result;
|
|
@@ -280,7 +297,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
280
297
|
result = new OINOPostgresqlData(rows, []);
|
|
281
298
|
}
|
|
282
299
|
catch (e) {
|
|
283
|
-
result = new OINOPostgresqlData(
|
|
300
|
+
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
|
|
284
301
|
}
|
|
285
302
|
OINOBenchmark.end("OINODb", "sqlSelect");
|
|
286
303
|
return result;
|
|
@@ -300,7 +317,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
300
317
|
result = new OINOPostgresqlData(rows, []);
|
|
301
318
|
}
|
|
302
319
|
catch (e) {
|
|
303
|
-
result = new OINOPostgresqlData(
|
|
320
|
+
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
304
321
|
}
|
|
305
322
|
OINOBenchmark.end("OINODb", "sqlExec");
|
|
306
323
|
return result;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-postgresql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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.7.0",
|
|
24
24
|
"pg": "^8.11.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
package/src/OINODbPostgresql.ts
CHANGED
|
@@ -4,11 +4,10 @@
|
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINO_ERROR_PREFIX, OINODataRow, OINODataCell, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult } from "@oino-ts/db";
|
|
7
|
+
import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINO_ERROR_PREFIX, OINODataRow, OINODataCell, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
|
|
8
8
|
|
|
9
9
|
import { Pool, QueryResult } from "pg";
|
|
10
10
|
|
|
11
|
-
const EMPTY_ROW:string[] = []
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* Implmentation of OINODbDataSet for Postgresql.
|
|
@@ -77,9 +76,17 @@ class OINOPostgresqlData extends OINODbDataSet {
|
|
|
77
76
|
if ((this._currentRow >=0) && (this._currentRow < this._rows.length)) {
|
|
78
77
|
return this._rows[this._currentRow]
|
|
79
78
|
} else {
|
|
80
|
-
return
|
|
79
|
+
return OINODB_EMPTY_ROW
|
|
81
80
|
}
|
|
82
81
|
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Gets all rows of data.
|
|
85
|
+
*
|
|
86
|
+
*/
|
|
87
|
+
async getAllRows(): Promise<OINODataRow[]> {
|
|
88
|
+
return this._rows // at the moment theres no result streaming, so we can just return the rows
|
|
89
|
+
}
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
/**
|
|
@@ -101,20 +108,22 @@ export class OINODbPostgresql extends OINODb {
|
|
|
101
108
|
if (this._params.type !== "OINODbPostgresql") {
|
|
102
109
|
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type)
|
|
103
110
|
}
|
|
104
|
-
const ssl_enabled:boolean = !(
|
|
105
|
-
this._pool = new Pool({ host:
|
|
111
|
+
const ssl_enabled:boolean = !(this._params.url == "localhost" || this._params.url == "127.0.0.1")
|
|
112
|
+
this._pool = new Pool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, ssl: ssl_enabled })
|
|
113
|
+
delete this._params.password
|
|
114
|
+
|
|
106
115
|
this._pool.on("error", (err: any) => {
|
|
107
|
-
OINOLog.error("OINODbPostgresql error", {err:err})
|
|
108
|
-
})
|
|
109
|
-
this._pool.on("connect", (message: any) => {
|
|
110
|
-
// OINOLog.info("OINODbPostgresql connect")
|
|
111
|
-
})
|
|
112
|
-
this._pool.on("release", (message: any) => {
|
|
113
|
-
// OINOLog.info("OINODbPostgresql notice")
|
|
114
|
-
})
|
|
115
|
-
this._pool.on("acquire", () => {
|
|
116
|
-
// OINOLog.info("OINODbPostgresql end")
|
|
116
|
+
OINOLog.error("OINODbPostgresql error event", {err:err})
|
|
117
117
|
})
|
|
118
|
+
// this._pool.on("connect", (message: any) => {
|
|
119
|
+
// OINOLog.info("OINODbPostgresql connect")
|
|
120
|
+
// })
|
|
121
|
+
// this._pool.on("release", (message: any) => {
|
|
122
|
+
// OINOLog.info("OINODbPostgresql notice")
|
|
123
|
+
// })
|
|
124
|
+
// this._pool.on("acquire", () => {
|
|
125
|
+
// OINOLog.info("OINODbPostgresql end")
|
|
126
|
+
// })
|
|
118
127
|
}
|
|
119
128
|
|
|
120
129
|
private _parseFieldLength(fieldLength:OINODataCell):number {
|
|
@@ -133,10 +142,16 @@ export class OINODbPostgresql extends OINODb {
|
|
|
133
142
|
}
|
|
134
143
|
|
|
135
144
|
private async _exec(sql:string):Promise<OINODataRow[]> {
|
|
136
|
-
// OINOLog.debug("OINODbPostgresql.
|
|
145
|
+
// OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
|
|
137
146
|
const query_result:QueryResult = await this._pool.query({rowMode: "array", text: sql})
|
|
138
|
-
// OINOLog.debug("OINODbPostgresql.
|
|
139
|
-
|
|
147
|
+
// OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
|
|
148
|
+
if (Array.isArray(query_result) == true) {
|
|
149
|
+
return Promise.resolve(query_result.flatMap((q) => q.rows))
|
|
150
|
+
} else if (query_result.rows) {
|
|
151
|
+
return Promise.resolve(query_result.rows)
|
|
152
|
+
} else {
|
|
153
|
+
return Promise.resolve(OINODB_EMPTY_ROWS) // return empty row if no rows returned
|
|
154
|
+
}
|
|
140
155
|
}
|
|
141
156
|
|
|
142
157
|
/**
|
|
@@ -240,17 +255,19 @@ export class OINODbPostgresql extends OINODb {
|
|
|
240
255
|
* Connect to database.
|
|
241
256
|
*
|
|
242
257
|
*/
|
|
243
|
-
async connect(): Promise<
|
|
258
|
+
async connect(): Promise<OINOResult> {
|
|
259
|
+
let result:OINOResult = new OINOResult()
|
|
244
260
|
try {
|
|
245
261
|
// make sure that any items are correctly URL encoded in the connection string
|
|
246
262
|
// OINOLog.debug("OINODbPostgresql.connect")
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
} catch (err) {
|
|
251
|
-
|
|
252
|
-
|
|
263
|
+
await this._pool.connect()
|
|
264
|
+
this.isConnected = true
|
|
265
|
+
|
|
266
|
+
} catch (err:any) {
|
|
267
|
+
result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect")
|
|
268
|
+
OINOLog.error(result.statusMessage, {error:err})
|
|
253
269
|
}
|
|
270
|
+
return result
|
|
254
271
|
}
|
|
255
272
|
|
|
256
273
|
/**
|
|
@@ -262,23 +279,24 @@ export class OINODbPostgresql extends OINODb {
|
|
|
262
279
|
let result:OINOResult = new OINOResult()
|
|
263
280
|
try {
|
|
264
281
|
const sql = this._getValidateSql(this._params.database)
|
|
265
|
-
// OINOLog.debug("
|
|
282
|
+
// OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
|
|
266
283
|
const sql_res:OINODbDataSet = await this.sqlSelect(sql)
|
|
267
|
-
// OINOLog.debug("
|
|
284
|
+
// OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
|
|
268
285
|
if (sql_res.isEmpty()) {
|
|
269
|
-
result.setError(400, "DB returned no rows for select!", "
|
|
286
|
+
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate")
|
|
270
287
|
|
|
271
288
|
} else if (sql_res.getRow().length == 0) {
|
|
272
|
-
result.setError(400, "DB returned no values for database!", "
|
|
289
|
+
result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate")
|
|
273
290
|
|
|
274
291
|
} else if (sql_res.getRow()[0] == "0") {
|
|
275
|
-
result.setError(400, "DB returned no schema for database!", "
|
|
292
|
+
result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate")
|
|
276
293
|
|
|
277
294
|
} else {
|
|
278
|
-
|
|
295
|
+
this.isValidated = true
|
|
279
296
|
}
|
|
280
|
-
} catch (
|
|
281
|
-
result.setError(500,
|
|
297
|
+
} catch (err:any) {
|
|
298
|
+
result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate")
|
|
299
|
+
OINOLog.error(result.statusMessage, {error:err})
|
|
282
300
|
}
|
|
283
301
|
OINOBenchmark.end("OINODb", "validate")
|
|
284
302
|
return result
|
|
@@ -299,7 +317,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
299
317
|
result = new OINOPostgresqlData(rows, [])
|
|
300
318
|
|
|
301
319
|
} catch (e:any) {
|
|
302
|
-
result = new OINOPostgresqlData(
|
|
320
|
+
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"])
|
|
303
321
|
}
|
|
304
322
|
OINOBenchmark.end("OINODb", "sqlSelect")
|
|
305
323
|
return result
|
|
@@ -320,7 +338,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
320
338
|
result = new OINOPostgresqlData(rows, [])
|
|
321
339
|
|
|
322
340
|
} catch (e:any) {
|
|
323
|
-
result = new OINOPostgresqlData(
|
|
341
|
+
result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
|
|
324
342
|
}
|
|
325
343
|
OINOBenchmark.end("OINODb", "sqlExec")
|
|
326
344
|
return result
|