@oino-ts/db-mariadb 0.18.1 → 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.
@@ -6,6 +6,7 @@
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINODbMariadb = void 0;
9
+ const common_1 = require("@oino-ts/common");
9
10
  const db_1 = require("@oino-ts/db");
10
11
  const mariadb_1 = require("mariadb");
11
12
  /**
@@ -21,7 +22,7 @@ class OINOMariadbData extends db_1.OINODbDataSet {
21
22
  constructor(data, messages = []) {
22
23
  super(data, messages);
23
24
  if (data == null) {
24
- this.messages.push(db_1.OINO_INFO_PREFIX + "SQL result is empty");
25
+ this.messages.push(common_1.OINO_INFO_PREFIX + "SQL result is empty");
25
26
  }
26
27
  else if (Array.isArray(data)) {
27
28
  this._rows = data;
@@ -100,7 +101,7 @@ class OINODbMariadb extends db_1.OINODb {
100
101
  constructor(params) {
101
102
  super(params);
102
103
  if (this._params.type !== "OINODbMariadb") {
103
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
104
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
104
105
  }
105
106
  this._pool = mariadb_1.default.createPool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, acquireTimeout: 2000, debug: false, rowsAsArray: true, multipleStatements: true });
106
107
  delete this._params.password; // do not store password in db object
@@ -114,30 +115,48 @@ class OINODbMariadb extends db_1.OINODb {
114
115
  }
115
116
  async _query(sql) {
116
117
  let connection = null;
118
+ let rows = db_1.OINODB_EMPTY_ROWS;
117
119
  try {
118
120
  connection = await this._pool.getConnection();
119
- const result = await connection.query(sql);
120
- return Promise.resolve(result);
121
+ const sql_res = await connection.query(sql);
122
+ // console.log("_query: sql=", sql, " result=", result)
123
+ if (Array.isArray(sql_res)) {
124
+ rows = sql_res.filter((r) => Array.isArray(r)); // filter out OkPacket results from multiple statements
125
+ }
126
+ }
127
+ catch (e) {
128
+ common_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "_query", "exception in SQL select", { message: e.message, stack: e.stack });
129
+ return new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMariadb._query");
121
130
  }
122
131
  finally {
123
132
  if (connection) {
124
- await connection.end();
133
+ connection.release();
125
134
  }
126
135
  }
136
+ return new OINOMariadbData(rows, []);
127
137
  }
128
138
  async _exec(sql) {
129
139
  let connection = null;
140
+ let rows = db_1.OINODB_EMPTY_ROWS;
130
141
  try {
131
142
  connection = await this._pool.getConnection();
132
- const result = await connection.query(sql);
133
- // console.log(result);
134
- return Promise.resolve(result);
143
+ const sql_res = await connection.query(sql);
144
+ // console.log("OINODbMariadb._exec: result=", result)
145
+ if (Array.isArray(sql_res)) {
146
+ rows = sql_res.filter((r) => Array.isArray(r)); // filter out OkPacket results from multiple statements
147
+ }
148
+ }
149
+ catch (e) {
150
+ const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
151
+ common_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "_exec", "exception in SQL exec", { message: msg_parts[2], stack: e.stack });
152
+ return new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec [" + msg_parts[2] + "]", "OINODbMariadb._exec");
135
153
  }
136
154
  finally {
137
155
  if (connection) {
138
- await connection.end();
156
+ connection.release();
139
157
  }
140
158
  }
159
+ return new OINOMariadbData(rows, []);
141
160
  }
142
161
  /**
143
162
  * Print a table name using database specific SQL escaping.
@@ -253,7 +272,10 @@ class OINODbMariadb extends db_1.OINODb {
253
272
  *
254
273
  */
255
274
  async connect() {
256
- const result = new db_1.OINOResult();
275
+ let result = new common_1.OINOResult();
276
+ if (this.isConnected) {
277
+ return result;
278
+ }
257
279
  let connection = null;
258
280
  try {
259
281
  // make sure that any items are correctly URL encoded in the connection string
@@ -263,11 +285,11 @@ class OINODbMariadb extends db_1.OINODb {
263
285
  catch (e) {
264
286
  const msg_parts = e.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
265
287
  result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
266
- db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", "exception in connect", { message: e.message, stack: e.stack });
288
+ common_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", "exception in connect", { message: e.message, stack: e.stack });
267
289
  }
268
290
  finally {
269
291
  if (connection) {
270
- await connection.end();
292
+ await connection.release();
271
293
  }
272
294
  }
273
295
  return Promise.resolve(result);
@@ -277,11 +299,11 @@ class OINODbMariadb extends db_1.OINODb {
277
299
  *
278
300
  */
279
301
  async validate() {
280
- db_1.OINOBenchmark.startMetric("OINODb", "validate");
281
- let result = new db_1.OINOResult();
302
+ common_1.OINOBenchmark.startMetric("OINODb", "validate");
303
+ let result = new common_1.OINOResult();
282
304
  try {
283
305
  const sql = this._getValidateSql(this._params.database);
284
- const sql_res = await this.sqlSelect(sql);
306
+ const sql_res = await this._query(sql);
285
307
  if (sql_res.isEmpty()) {
286
308
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
287
309
  }
@@ -297,11 +319,22 @@ class OINODbMariadb extends db_1.OINODb {
297
319
  }
298
320
  catch (e) {
299
321
  result.setError(500, "Exception validating connection: " + e.message, "OINODbMariadb.validate");
300
- db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "validate", "exception in validate", { message: e.message, stack: e.stack });
322
+ common_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "validate", "exception in validate", { message: e.message, stack: e.stack });
301
323
  }
302
- db_1.OINOBenchmark.endMetric("OINODb", "validate");
324
+ common_1.OINOBenchmark.endMetric("OINODb", "validate");
303
325
  return result;
304
326
  }
327
+ /**
328
+ * Disconnect from database.
329
+ *
330
+ */
331
+ async disconnect() {
332
+ if (this.isConnected) {
333
+ await this._pool.end();
334
+ }
335
+ this.isConnected = false;
336
+ this.isValidated = false;
337
+ }
305
338
  /**
306
339
  * Execute a select operation.
307
340
  *
@@ -309,17 +342,12 @@ class OINODbMariadb extends db_1.OINODb {
309
342
  *
310
343
  */
311
344
  async sqlSelect(sql) {
312
- db_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
313
- let result;
314
- try {
315
- const rows = await this._query(sql);
316
- result = new OINOMariadbData(rows, []);
317
- }
318
- catch (e) {
319
- db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "exception in SQL select", { message: e.message, stack: e.stack });
320
- result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
345
+ if (!this.isValidated) {
346
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
321
347
  }
322
- db_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
348
+ common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
349
+ let result = await this._query(sql);
350
+ common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
323
351
  return result;
324
352
  }
325
353
  /**
@@ -329,18 +357,12 @@ class OINODbMariadb extends db_1.OINODb {
329
357
  *
330
358
  */
331
359
  async sqlExec(sql) {
332
- db_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
333
- let result;
334
- try {
335
- const sql_res = await this._exec(sql);
336
- result = new OINOMariadbData(sql_res, []);
337
- }
338
- catch (e) {
339
- const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
340
- db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "exception in SQL exec", { message: msg_parts[2], stack: e.stack });
341
- result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + msg_parts[2] + "]"]);
360
+ if (!this.isValidated) {
361
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
342
362
  }
343
- db_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
363
+ common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
364
+ let result = await this._exec(sql);
365
+ common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
344
366
  return result;
345
367
  }
346
368
  _getSchemaSql(dbName, tableName) {
@@ -374,7 +396,7 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
374
396
  *
375
397
  */
376
398
  async initializeApiDatamodel(api) {
377
- const schema_res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName));
399
+ const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName));
378
400
  while (!schema_res.isEof()) {
379
401
  const row = schema_res.getRow();
380
402
  // console.log("OINODbMariadb.initializeApiDatamodel row", row)
@@ -391,9 +413,9 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
391
413
  isNotNull: row[2] == "NO"
392
414
  };
393
415
  if (api.isFieldIncluded(field_name) == false) {
394
- db_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", ".initializeApiDatamodel", "Field excluded in API parameters", { field: field_name });
416
+ common_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", ".initializeApiDatamodel", "Field excluded in API parameters", { field: field_name });
395
417
  if (field_params.isPrimaryKey) {
396
- throw new Error(db_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
418
+ throw new Error(common_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
397
419
  }
398
420
  }
399
421
  else {
@@ -426,13 +448,13 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
426
448
  }
427
449
  }
428
450
  else {
429
- db_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length1: field_length1, field_length2: field_length2, field_params: field_params });
451
+ common_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length1: field_length1, field_length2: field_length2, field_params: field_params });
430
452
  api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
431
453
  }
432
454
  }
433
455
  await schema_res.next();
434
456
  }
435
- db_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
457
+ common_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
436
458
  return Promise.resolve();
437
459
  }
438
460
  }
@@ -3,7 +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, OINO_INFO_PREFIX, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS, OINOLog, OINOResult } from "@oino-ts/db";
6
+ import { OINO_ERROR_PREFIX, OINOBenchmark, OINO_INFO_PREFIX, OINOLog, OINOResult } from "@oino-ts/common";
7
+ import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODatetimeDataField, OINOBlobDataField, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
7
8
  import mariadb from "mariadb";
8
9
  /**
9
10
  * Implmentation of OINODbDataSet for MariaDb.
@@ -111,30 +112,48 @@ export class OINODbMariadb extends OINODb {
111
112
  }
112
113
  async _query(sql) {
113
114
  let connection = null;
115
+ let rows = OINODB_EMPTY_ROWS;
114
116
  try {
115
117
  connection = await this._pool.getConnection();
116
- const result = await connection.query(sql);
117
- return Promise.resolve(result);
118
+ const sql_res = await connection.query(sql);
119
+ // console.log("_query: sql=", sql, " result=", result)
120
+ if (Array.isArray(sql_res)) {
121
+ rows = sql_res.filter((r) => Array.isArray(r)); // filter out OkPacket results from multiple statements
122
+ }
123
+ }
124
+ catch (e) {
125
+ OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "_query", "exception in SQL select", { message: e.message, stack: e.stack });
126
+ return new OINOMariadbData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMariadb._query");
118
127
  }
119
128
  finally {
120
129
  if (connection) {
121
- await connection.end();
130
+ connection.release();
122
131
  }
123
132
  }
133
+ return new OINOMariadbData(rows, []);
124
134
  }
125
135
  async _exec(sql) {
126
136
  let connection = null;
137
+ let rows = OINODB_EMPTY_ROWS;
127
138
  try {
128
139
  connection = await this._pool.getConnection();
129
- const result = await connection.query(sql);
130
- // console.log(result);
131
- return Promise.resolve(result);
140
+ const sql_res = await connection.query(sql);
141
+ // console.log("OINODbMariadb._exec: result=", result)
142
+ if (Array.isArray(sql_res)) {
143
+ rows = sql_res.filter((r) => Array.isArray(r)); // filter out OkPacket results from multiple statements
144
+ }
145
+ }
146
+ catch (e) {
147
+ const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
148
+ OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "_exec", "exception in SQL exec", { message: msg_parts[2], stack: e.stack });
149
+ return new OINOMariadbData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec [" + msg_parts[2] + "]", "OINODbMariadb._exec");
132
150
  }
133
151
  finally {
134
152
  if (connection) {
135
- await connection.end();
153
+ connection.release();
136
154
  }
137
155
  }
156
+ return new OINOMariadbData(rows, []);
138
157
  }
139
158
  /**
140
159
  * Print a table name using database specific SQL escaping.
@@ -250,7 +269,10 @@ export class OINODbMariadb extends OINODb {
250
269
  *
251
270
  */
252
271
  async connect() {
253
- const result = new OINOResult();
272
+ let result = new OINOResult();
273
+ if (this.isConnected) {
274
+ return result;
275
+ }
254
276
  let connection = null;
255
277
  try {
256
278
  // make sure that any items are correctly URL encoded in the connection string
@@ -264,7 +286,7 @@ export class OINODbMariadb extends OINODb {
264
286
  }
265
287
  finally {
266
288
  if (connection) {
267
- await connection.end();
289
+ await connection.release();
268
290
  }
269
291
  }
270
292
  return Promise.resolve(result);
@@ -278,7 +300,7 @@ export class OINODbMariadb extends OINODb {
278
300
  let result = new OINOResult();
279
301
  try {
280
302
  const sql = this._getValidateSql(this._params.database);
281
- const sql_res = await this.sqlSelect(sql);
303
+ const sql_res = await this._query(sql);
282
304
  if (sql_res.isEmpty()) {
283
305
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
284
306
  }
@@ -299,6 +321,17 @@ export class OINODbMariadb extends OINODb {
299
321
  OINOBenchmark.endMetric("OINODb", "validate");
300
322
  return result;
301
323
  }
324
+ /**
325
+ * Disconnect from database.
326
+ *
327
+ */
328
+ async disconnect() {
329
+ if (this.isConnected) {
330
+ await this._pool.end();
331
+ }
332
+ this.isConnected = false;
333
+ this.isValidated = false;
334
+ }
302
335
  /**
303
336
  * Execute a select operation.
304
337
  *
@@ -306,16 +339,11 @@ export class OINODbMariadb extends OINODb {
306
339
  *
307
340
  */
308
341
  async sqlSelect(sql) {
309
- OINOBenchmark.startMetric("OINODb", "sqlSelect");
310
- let result;
311
- try {
312
- const rows = await this._query(sql);
313
- result = new OINOMariadbData(rows, []);
314
- }
315
- catch (e) {
316
- OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "exception in SQL select", { message: e.message, stack: e.stack });
317
- result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
342
+ if (!this.isValidated) {
343
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
318
344
  }
345
+ OINOBenchmark.startMetric("OINODb", "sqlSelect");
346
+ let result = await this._query(sql);
319
347
  OINOBenchmark.endMetric("OINODb", "sqlSelect");
320
348
  return result;
321
349
  }
@@ -326,17 +354,11 @@ export class OINODbMariadb extends OINODb {
326
354
  *
327
355
  */
328
356
  async sqlExec(sql) {
329
- OINOBenchmark.startMetric("OINODb", "sqlExec");
330
- let result;
331
- try {
332
- const sql_res = await this._exec(sql);
333
- result = new OINOMariadbData(sql_res, []);
334
- }
335
- catch (e) {
336
- const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
337
- OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "exception in SQL exec", { message: msg_parts[2], stack: e.stack });
338
- result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + msg_parts[2] + "]"]);
357
+ if (!this.isValidated) {
358
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
339
359
  }
360
+ OINOBenchmark.startMetric("OINODb", "sqlExec");
361
+ let result = await this._exec(sql);
340
362
  OINOBenchmark.endMetric("OINODb", "sqlExec");
341
363
  return result;
342
364
  }
@@ -371,7 +393,7 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
371
393
  *
372
394
  */
373
395
  async initializeApiDatamodel(api) {
374
- const schema_res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName));
396
+ const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName));
375
397
  while (!schema_res.isEof()) {
376
398
  const row = schema_res.getRow();
377
399
  // console.log("OINODbMariadb.initializeApiDatamodel row", row)
@@ -1,4 +1,5 @@
1
- import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell, OINOResult } from "@oino-ts/db";
1
+ import { OINOResult } from "@oino-ts/common";
2
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
2
3
  /**
3
4
  * Implementation of MariaDb/MySql-database.
4
5
  *
@@ -65,6 +66,11 @@ export declare class OINODbMariadb extends OINODb {
65
66
  *
66
67
  */
67
68
  validate(): Promise<OINOResult>;
69
+ /**
70
+ * Disconnect from database.
71
+ *
72
+ */
73
+ disconnect(): Promise<void>;
68
74
  /**
69
75
  * Execute a select operation.
70
76
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-mariadb",
3
- "version": "0.18.1",
3
+ "version": "0.20.0",
4
4
  "description": "OINO TS package for using Mariadb databases.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -21,7 +21,8 @@
21
21
  "module": "./dist/esm/index.js",
22
22
  "types": "./dist/types/index.d.ts",
23
23
  "dependencies": {
24
- "@oino-ts/db": "^0.18.1",
24
+ "@oino-ts/common": "^0.20.0",
25
+ "@oino-ts/db": "^0.20.0",
25
26
  "mariadb": "^3.2.3"
26
27
  },
27
28
  "devDependencies": {
@@ -4,7 +4,8 @@
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, OINO_INFO_PREFIX, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS, OINOLog, OINOResult } from "@oino-ts/db";
7
+ import { OINO_ERROR_PREFIX, OINOBenchmark, OINO_INFO_PREFIX, OINOLog, OINOResult } from "@oino-ts/common";
8
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINODataRow, OINODataCell, OINODatetimeDataField, OINOBlobDataField, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
8
9
 
9
10
  import mariadb from "mariadb";
10
11
 
@@ -124,33 +125,50 @@ export class OINODbMariadb extends OINODb {
124
125
  return result
125
126
  }
126
127
 
127
- private async _query(sql:string):Promise<OINODataRow[]> {
128
- let connection:mariadb.Connection|null = null
128
+ private async _query(sql:string):Promise<OINODbDataSet> {
129
+ let connection:mariadb.PoolConnection|null = null
130
+ let rows:OINODataRow[] = OINODB_EMPTY_ROWS
129
131
  try {
130
- connection = await this._pool.getConnection();
131
- const result = await connection.query(sql);
132
- return Promise.resolve(result)
133
-
132
+ connection = await this._pool.getConnection()
133
+ const sql_res = await connection.query(sql)
134
+ // console.log("_query: sql=", sql, " result=", result)
135
+ if (Array.isArray(sql_res)) {
136
+ rows = sql_res.filter((r) => Array.isArray(r)) as OINODataRow[] // filter out OkPacket results from multiple statements
137
+ }
138
+ } catch (e:any) {
139
+ OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "_query", "exception in SQL select", {message:e.message, stack:e.stack})
140
+ return new OINOMariadbData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMariadb._query") as OINOMariadbData
141
+
134
142
  } finally {
135
143
  if (connection) {
136
- await connection.end()
144
+ connection.release()
137
145
  }
138
146
  }
147
+ return new OINOMariadbData(rows, [])
139
148
  }
140
149
 
141
- private async _exec(sql:string):Promise<any> {
142
- let connection:mariadb.Connection|null = null
150
+ private async _exec(sql:string):Promise<OINODbDataSet> {
151
+ let connection:mariadb.PoolConnection|null = null
152
+ let rows:OINODataRow[] = OINODB_EMPTY_ROWS
143
153
  try {
144
- connection = await this._pool.getConnection();
145
- const result = await connection.query(sql);
146
- // console.log(result);
147
- return Promise.resolve(result)
154
+ connection = await this._pool.getConnection()
155
+ const sql_res = await connection.query(sql)
156
+ // console.log("OINODbMariadb._exec: result=", result)
157
+ if (Array.isArray(sql_res)) {
158
+ rows = sql_res.filter((r) => Array.isArray(r)) // filter out OkPacket results from multiple statements
159
+ }
148
160
 
161
+ } catch (e:any) {
162
+ const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
163
+ OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "_exec", "exception in SQL exec", {message:msg_parts[2], stack:e.stack})
164
+ return new OINOMariadbData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec [" + msg_parts[2] + "]", "OINODbMariadb._exec") as OINOMariadbData
165
+
149
166
  } finally {
150
167
  if (connection) {
151
- await connection.end()
168
+ connection.release()
152
169
  }
153
170
  }
171
+ return new OINOMariadbData(rows, [])
154
172
  }
155
173
 
156
174
  /**
@@ -270,8 +288,11 @@ export class OINODbMariadb extends OINODb {
270
288
  *
271
289
  */
272
290
  async connect(): Promise<OINOResult> {
273
- const result:OINOResult = new OINOResult()
274
- let connection:mariadb.Connection|null = null
291
+ let result:OINOResult = new OINOResult()
292
+ if (this.isConnected) {
293
+ return result
294
+ }
295
+ let connection:mariadb.PoolConnection|null = null
275
296
  try {
276
297
  // make sure that any items are correctly URL encoded in the connection string
277
298
  connection = await this._pool.getConnection()
@@ -283,7 +304,7 @@ export class OINODbMariadb extends OINODb {
283
304
  OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", "exception in connect", {message:e.message, stack:e.stack})
284
305
  } finally {
285
306
  if (connection) {
286
- await connection.end()
307
+ await connection.release()
287
308
  }
288
309
  }
289
310
 
@@ -299,7 +320,7 @@ export class OINODbMariadb extends OINODb {
299
320
  let result:OINOResult = new OINOResult()
300
321
  try {
301
322
  const sql = this._getValidateSql(this._params.database)
302
- const sql_res:OINODbDataSet = await this.sqlSelect(sql)
323
+ const sql_res:OINODbDataSet = await this._query(sql)
303
324
  if (sql_res.isEmpty()) {
304
325
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate")
305
326
 
@@ -320,6 +341,18 @@ export class OINODbMariadb extends OINODb {
320
341
  return result
321
342
  }
322
343
 
344
+ /**
345
+ * Disconnect from database.
346
+ *
347
+ */
348
+ async disconnect(): Promise<void> {
349
+ if (this.isConnected) {
350
+ await this._pool.end()
351
+ }
352
+ this.isConnected = false
353
+ this.isValidated = false
354
+ }
355
+
323
356
  /**
324
357
  * Execute a select operation.
325
358
  *
@@ -327,16 +360,11 @@ export class OINODbMariadb extends OINODb {
327
360
  *
328
361
  */
329
362
  async sqlSelect(sql:string): Promise<OINODbDataSet> {
330
- OINOBenchmark.startMetric("OINODb", "sqlSelect")
331
- let result:OINODbDataSet
332
- try {
333
- const rows:OINODataRow[] = await this._query(sql)
334
- result = new OINOMariadbData(rows, [])
335
-
336
- } catch (e:any) {
337
- OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "exception in SQL select", {message:e.message, stack:e.stack})
338
- result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message])
363
+ if (!this.isValidated) {
364
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
339
365
  }
366
+ OINOBenchmark.startMetric("OINODb", "sqlSelect")
367
+ let result:OINODbDataSet = await this._query(sql)
340
368
  OINOBenchmark.endMetric("OINODb", "sqlSelect")
341
369
  return result
342
370
  }
@@ -348,17 +376,11 @@ export class OINODbMariadb extends OINODb {
348
376
  *
349
377
  */
350
378
  async sqlExec(sql:string): Promise<OINODbDataSet> {
351
- OINOBenchmark.startMetric("OINODb", "sqlExec")
352
- let result:OINODbDataSet
353
- try {
354
- const sql_res:OINODataRow[] = await this._exec(sql)
355
- result = new OINOMariadbData(sql_res, [])
356
-
357
- } catch (e:any) {
358
- const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
359
- OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "exception in SQL exec", {message:msg_parts[2], stack:e.stack})
360
- result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + msg_parts[2] + "]"])
379
+ if (!this.isValidated) {
380
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
361
381
  }
382
+ OINOBenchmark.startMetric("OINODb", "sqlExec")
383
+ let result:OINODbDataSet = await this._exec(sql)
362
384
  OINOBenchmark.endMetric("OINODb", "sqlExec")
363
385
  return result
364
386
  }
@@ -400,7 +422,7 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`
400
422
  */
401
423
  async initializeApiDatamodel(api:OINODbApi): Promise<void> {
402
424
 
403
- const schema_res:OINODbDataSet = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName))
425
+ const schema_res:OINODbDataSet = await this._query(this._getSchemaSql(this._params.database, api.params.tableName))
404
426
  while (!schema_res.isEof()) {
405
427
  const row:OINODataRow = schema_res.getRow()
406
428
  // console.log("OINODbMariadb.initializeApiDatamodel row", row)