@oino-ts/db-mariadb 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.
- package/dist/cjs/OINODbMariadb.js +51 -30
- package/dist/esm/OINODbMariadb.js +51 -30
- package/dist/types/OINODbMariadb.d.ts +5 -0
- package/package.json +3 -3
- package/src/OINODbMariadb.ts +59 -38
|
@@ -115,30 +115,48 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
115
115
|
}
|
|
116
116
|
async _query(sql) {
|
|
117
117
|
let connection = null;
|
|
118
|
+
let rows = db_1.OINODB_EMPTY_ROWS;
|
|
118
119
|
try {
|
|
119
120
|
connection = await this._pool.getConnection();
|
|
120
|
-
const
|
|
121
|
-
|
|
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");
|
|
122
130
|
}
|
|
123
131
|
finally {
|
|
124
132
|
if (connection) {
|
|
125
|
-
|
|
133
|
+
connection.release();
|
|
126
134
|
}
|
|
127
135
|
}
|
|
136
|
+
return new OINOMariadbData(rows, []);
|
|
128
137
|
}
|
|
129
138
|
async _exec(sql) {
|
|
130
139
|
let connection = null;
|
|
140
|
+
let rows = db_1.OINODB_EMPTY_ROWS;
|
|
131
141
|
try {
|
|
132
142
|
connection = await this._pool.getConnection();
|
|
133
|
-
const
|
|
134
|
-
// console.log(result)
|
|
135
|
-
|
|
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");
|
|
136
153
|
}
|
|
137
154
|
finally {
|
|
138
155
|
if (connection) {
|
|
139
|
-
|
|
156
|
+
connection.release();
|
|
140
157
|
}
|
|
141
158
|
}
|
|
159
|
+
return new OINOMariadbData(rows, []);
|
|
142
160
|
}
|
|
143
161
|
/**
|
|
144
162
|
* Print a table name using database specific SQL escaping.
|
|
@@ -254,7 +272,10 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
254
272
|
*
|
|
255
273
|
*/
|
|
256
274
|
async connect() {
|
|
257
|
-
|
|
275
|
+
let result = new common_1.OINOResult();
|
|
276
|
+
if (this.isConnected) {
|
|
277
|
+
return result;
|
|
278
|
+
}
|
|
258
279
|
let connection = null;
|
|
259
280
|
try {
|
|
260
281
|
// make sure that any items are correctly URL encoded in the connection string
|
|
@@ -268,7 +289,7 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
268
289
|
}
|
|
269
290
|
finally {
|
|
270
291
|
if (connection) {
|
|
271
|
-
await connection.
|
|
292
|
+
await connection.release();
|
|
272
293
|
}
|
|
273
294
|
}
|
|
274
295
|
return Promise.resolve(result);
|
|
@@ -282,7 +303,7 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
282
303
|
let result = new common_1.OINOResult();
|
|
283
304
|
try {
|
|
284
305
|
const sql = this._getValidateSql(this._params.database);
|
|
285
|
-
const sql_res = await this.
|
|
306
|
+
const sql_res = await this._query(sql);
|
|
286
307
|
if (sql_res.isEmpty()) {
|
|
287
308
|
result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
|
|
288
309
|
}
|
|
@@ -303,6 +324,17 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
303
324
|
common_1.OINOBenchmark.endMetric("OINODb", "validate");
|
|
304
325
|
return result;
|
|
305
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
|
+
}
|
|
306
338
|
/**
|
|
307
339
|
* Execute a select operation.
|
|
308
340
|
*
|
|
@@ -310,16 +342,11 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
310
342
|
*
|
|
311
343
|
*/
|
|
312
344
|
async sqlSelect(sql) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
try {
|
|
316
|
-
const rows = await this._query(sql);
|
|
317
|
-
result = new OINOMariadbData(rows, []);
|
|
318
|
-
}
|
|
319
|
-
catch (e) {
|
|
320
|
-
common_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "exception in SQL select", { message: e.message, stack: e.stack });
|
|
321
|
-
result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [common_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!");
|
|
322
347
|
}
|
|
348
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
349
|
+
let result = await this._query(sql);
|
|
323
350
|
common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
324
351
|
return result;
|
|
325
352
|
}
|
|
@@ -330,17 +357,11 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
330
357
|
*
|
|
331
358
|
*/
|
|
332
359
|
async sqlExec(sql) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
try {
|
|
336
|
-
const sql_res = await this._exec(sql);
|
|
337
|
-
result = new OINOMariadbData(sql_res, []);
|
|
338
|
-
}
|
|
339
|
-
catch (e) {
|
|
340
|
-
const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
|
|
341
|
-
common_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "exception in SQL exec", { message: msg_parts[2], stack: e.stack });
|
|
342
|
-
result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [common_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!");
|
|
343
362
|
}
|
|
363
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
364
|
+
let result = await this._exec(sql);
|
|
344
365
|
common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
345
366
|
return result;
|
|
346
367
|
}
|
|
@@ -375,7 +396,7 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
375
396
|
*
|
|
376
397
|
*/
|
|
377
398
|
async initializeApiDatamodel(api) {
|
|
378
|
-
const schema_res = await this.
|
|
399
|
+
const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName));
|
|
379
400
|
while (!schema_res.isEof()) {
|
|
380
401
|
const row = schema_res.getRow();
|
|
381
402
|
// console.log("OINODbMariadb.initializeApiDatamodel row", row)
|
|
@@ -112,30 +112,48 @@ export class OINODbMariadb extends OINODb {
|
|
|
112
112
|
}
|
|
113
113
|
async _query(sql) {
|
|
114
114
|
let connection = null;
|
|
115
|
+
let rows = OINODB_EMPTY_ROWS;
|
|
115
116
|
try {
|
|
116
117
|
connection = await this._pool.getConnection();
|
|
117
|
-
const
|
|
118
|
-
|
|
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");
|
|
119
127
|
}
|
|
120
128
|
finally {
|
|
121
129
|
if (connection) {
|
|
122
|
-
|
|
130
|
+
connection.release();
|
|
123
131
|
}
|
|
124
132
|
}
|
|
133
|
+
return new OINOMariadbData(rows, []);
|
|
125
134
|
}
|
|
126
135
|
async _exec(sql) {
|
|
127
136
|
let connection = null;
|
|
137
|
+
let rows = OINODB_EMPTY_ROWS;
|
|
128
138
|
try {
|
|
129
139
|
connection = await this._pool.getConnection();
|
|
130
|
-
const
|
|
131
|
-
// console.log(result)
|
|
132
|
-
|
|
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");
|
|
133
150
|
}
|
|
134
151
|
finally {
|
|
135
152
|
if (connection) {
|
|
136
|
-
|
|
153
|
+
connection.release();
|
|
137
154
|
}
|
|
138
155
|
}
|
|
156
|
+
return new OINOMariadbData(rows, []);
|
|
139
157
|
}
|
|
140
158
|
/**
|
|
141
159
|
* Print a table name using database specific SQL escaping.
|
|
@@ -251,7 +269,10 @@ export class OINODbMariadb extends OINODb {
|
|
|
251
269
|
*
|
|
252
270
|
*/
|
|
253
271
|
async connect() {
|
|
254
|
-
|
|
272
|
+
let result = new OINOResult();
|
|
273
|
+
if (this.isConnected) {
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
255
276
|
let connection = null;
|
|
256
277
|
try {
|
|
257
278
|
// make sure that any items are correctly URL encoded in the connection string
|
|
@@ -265,7 +286,7 @@ export class OINODbMariadb extends OINODb {
|
|
|
265
286
|
}
|
|
266
287
|
finally {
|
|
267
288
|
if (connection) {
|
|
268
|
-
await connection.
|
|
289
|
+
await connection.release();
|
|
269
290
|
}
|
|
270
291
|
}
|
|
271
292
|
return Promise.resolve(result);
|
|
@@ -279,7 +300,7 @@ export class OINODbMariadb extends OINODb {
|
|
|
279
300
|
let result = new OINOResult();
|
|
280
301
|
try {
|
|
281
302
|
const sql = this._getValidateSql(this._params.database);
|
|
282
|
-
const sql_res = await this.
|
|
303
|
+
const sql_res = await this._query(sql);
|
|
283
304
|
if (sql_res.isEmpty()) {
|
|
284
305
|
result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
|
|
285
306
|
}
|
|
@@ -300,6 +321,17 @@ export class OINODbMariadb extends OINODb {
|
|
|
300
321
|
OINOBenchmark.endMetric("OINODb", "validate");
|
|
301
322
|
return result;
|
|
302
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
|
+
}
|
|
303
335
|
/**
|
|
304
336
|
* Execute a select operation.
|
|
305
337
|
*
|
|
@@ -307,16 +339,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
307
339
|
*
|
|
308
340
|
*/
|
|
309
341
|
async sqlSelect(sql) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
try {
|
|
313
|
-
const rows = await this._query(sql);
|
|
314
|
-
result = new OINOMariadbData(rows, []);
|
|
315
|
-
}
|
|
316
|
-
catch (e) {
|
|
317
|
-
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "exception in SQL select", { message: e.message, stack: e.stack });
|
|
318
|
-
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!");
|
|
319
344
|
}
|
|
345
|
+
OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
346
|
+
let result = await this._query(sql);
|
|
320
347
|
OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
321
348
|
return result;
|
|
322
349
|
}
|
|
@@ -327,17 +354,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
327
354
|
*
|
|
328
355
|
*/
|
|
329
356
|
async sqlExec(sql) {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
try {
|
|
333
|
-
const sql_res = await this._exec(sql);
|
|
334
|
-
result = new OINOMariadbData(sql_res, []);
|
|
335
|
-
}
|
|
336
|
-
catch (e) {
|
|
337
|
-
const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
|
|
338
|
-
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "exception in SQL exec", { message: msg_parts[2], stack: e.stack });
|
|
339
|
-
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!");
|
|
340
359
|
}
|
|
360
|
+
OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
361
|
+
let result = await this._exec(sql);
|
|
341
362
|
OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
342
363
|
return result;
|
|
343
364
|
}
|
|
@@ -372,7 +393,7 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
372
393
|
*
|
|
373
394
|
*/
|
|
374
395
|
async initializeApiDatamodel(api) {
|
|
375
|
-
const schema_res = await this.
|
|
396
|
+
const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName));
|
|
376
397
|
while (!schema_res.isEof()) {
|
|
377
398
|
const row = schema_res.getRow();
|
|
378
399
|
// console.log("OINODbMariadb.initializeApiDatamodel row", row)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-mariadb",
|
|
3
|
-
"version": "0.
|
|
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,8 +21,8 @@
|
|
|
21
21
|
"module": "./dist/esm/index.js",
|
|
22
22
|
"types": "./dist/types/index.d.ts",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@oino-ts/common": "^0.
|
|
25
|
-
"@oino-ts/db": "^0.
|
|
24
|
+
"@oino-ts/common": "^0.20.0",
|
|
25
|
+
"@oino-ts/db": "^0.20.0",
|
|
26
26
|
"mariadb": "^3.2.3"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
package/src/OINODbMariadb.ts
CHANGED
|
@@ -125,33 +125,50 @@ export class OINODbMariadb extends OINODb {
|
|
|
125
125
|
return result
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
private async _query(sql:string):Promise<
|
|
129
|
-
let connection:mariadb.
|
|
128
|
+
private async _query(sql:string):Promise<OINODbDataSet> {
|
|
129
|
+
let connection:mariadb.PoolConnection|null = null
|
|
130
|
+
let rows:OINODataRow[] = OINODB_EMPTY_ROWS
|
|
130
131
|
try {
|
|
131
|
-
connection = await this._pool.getConnection()
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
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
|
+
|
|
135
142
|
} finally {
|
|
136
143
|
if (connection) {
|
|
137
|
-
|
|
144
|
+
connection.release()
|
|
138
145
|
}
|
|
139
146
|
}
|
|
147
|
+
return new OINOMariadbData(rows, [])
|
|
140
148
|
}
|
|
141
149
|
|
|
142
|
-
private async _exec(sql:string):Promise<
|
|
143
|
-
let connection:mariadb.
|
|
150
|
+
private async _exec(sql:string):Promise<OINODbDataSet> {
|
|
151
|
+
let connection:mariadb.PoolConnection|null = null
|
|
152
|
+
let rows:OINODataRow[] = OINODB_EMPTY_ROWS
|
|
144
153
|
try {
|
|
145
|
-
connection = await this._pool.getConnection()
|
|
146
|
-
const
|
|
147
|
-
// console.log(result)
|
|
148
|
-
|
|
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
|
+
}
|
|
149
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
|
+
|
|
150
166
|
} finally {
|
|
151
167
|
if (connection) {
|
|
152
|
-
|
|
168
|
+
connection.release()
|
|
153
169
|
}
|
|
154
170
|
}
|
|
171
|
+
return new OINOMariadbData(rows, [])
|
|
155
172
|
}
|
|
156
173
|
|
|
157
174
|
/**
|
|
@@ -271,8 +288,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
271
288
|
*
|
|
272
289
|
*/
|
|
273
290
|
async connect(): Promise<OINOResult> {
|
|
274
|
-
|
|
275
|
-
|
|
291
|
+
let result:OINOResult = new OINOResult()
|
|
292
|
+
if (this.isConnected) {
|
|
293
|
+
return result
|
|
294
|
+
}
|
|
295
|
+
let connection:mariadb.PoolConnection|null = null
|
|
276
296
|
try {
|
|
277
297
|
// make sure that any items are correctly URL encoded in the connection string
|
|
278
298
|
connection = await this._pool.getConnection()
|
|
@@ -284,7 +304,7 @@ export class OINODbMariadb extends OINODb {
|
|
|
284
304
|
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", "exception in connect", {message:e.message, stack:e.stack})
|
|
285
305
|
} finally {
|
|
286
306
|
if (connection) {
|
|
287
|
-
await connection.
|
|
307
|
+
await connection.release()
|
|
288
308
|
}
|
|
289
309
|
}
|
|
290
310
|
|
|
@@ -300,7 +320,7 @@ export class OINODbMariadb extends OINODb {
|
|
|
300
320
|
let result:OINOResult = new OINOResult()
|
|
301
321
|
try {
|
|
302
322
|
const sql = this._getValidateSql(this._params.database)
|
|
303
|
-
const sql_res:OINODbDataSet = await this.
|
|
323
|
+
const sql_res:OINODbDataSet = await this._query(sql)
|
|
304
324
|
if (sql_res.isEmpty()) {
|
|
305
325
|
result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate")
|
|
306
326
|
|
|
@@ -321,6 +341,18 @@ export class OINODbMariadb extends OINODb {
|
|
|
321
341
|
return result
|
|
322
342
|
}
|
|
323
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
|
+
|
|
324
356
|
/**
|
|
325
357
|
* Execute a select operation.
|
|
326
358
|
*
|
|
@@ -328,16 +360,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
328
360
|
*
|
|
329
361
|
*/
|
|
330
362
|
async sqlSelect(sql:string): Promise<OINODbDataSet> {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
try {
|
|
334
|
-
const rows:OINODataRow[] = await this._query(sql)
|
|
335
|
-
result = new OINOMariadbData(rows, [])
|
|
336
|
-
|
|
337
|
-
} catch (e:any) {
|
|
338
|
-
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "exception in SQL select", {message:e.message, stack:e.stack})
|
|
339
|
-
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!")
|
|
340
365
|
}
|
|
366
|
+
OINOBenchmark.startMetric("OINODb", "sqlSelect")
|
|
367
|
+
let result:OINODbDataSet = await this._query(sql)
|
|
341
368
|
OINOBenchmark.endMetric("OINODb", "sqlSelect")
|
|
342
369
|
return result
|
|
343
370
|
}
|
|
@@ -349,17 +376,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
349
376
|
*
|
|
350
377
|
*/
|
|
351
378
|
async sqlExec(sql:string): Promise<OINODbDataSet> {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
try {
|
|
355
|
-
const sql_res:OINODataRow[] = await this._exec(sql)
|
|
356
|
-
result = new OINOMariadbData(sql_res, [])
|
|
357
|
-
|
|
358
|
-
} catch (e:any) {
|
|
359
|
-
const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
|
|
360
|
-
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "exception in SQL exec", {message:msg_parts[2], stack:e.stack})
|
|
361
|
-
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!")
|
|
362
381
|
}
|
|
382
|
+
OINOBenchmark.startMetric("OINODb", "sqlExec")
|
|
383
|
+
let result:OINODbDataSet = await this._exec(sql)
|
|
363
384
|
OINOBenchmark.endMetric("OINODb", "sqlExec")
|
|
364
385
|
return result
|
|
365
386
|
}
|
|
@@ -401,7 +422,7 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`
|
|
|
401
422
|
*/
|
|
402
423
|
async initializeApiDatamodel(api:OINODbApi): Promise<void> {
|
|
403
424
|
|
|
404
|
-
const schema_res:OINODbDataSet = await this.
|
|
425
|
+
const schema_res:OINODbDataSet = await this._query(this._getSchemaSql(this._params.database, api.params.tableName))
|
|
405
426
|
while (!schema_res.isEof()) {
|
|
406
427
|
const row:OINODataRow = schema_res.getRow()
|
|
407
428
|
// console.log("OINODbMariadb.initializeApiDatamodel row", row)
|