@oino-ts/db-mariadb 0.5.1 → 0.6.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.
@@ -250,7 +250,7 @@ class OINODbMariadb extends db_1.OINODb {
250
250
  else if (sqlValue === undefined) {
251
251
  return undefined;
252
252
  }
253
- else if (((sqlType == "date")) && (typeof (sqlValue) == "string")) {
253
+ else if (((sqlType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
254
254
  return new Date(sqlValue);
255
255
  }
256
256
  else if ((sqlType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
@@ -282,6 +282,37 @@ class OINODbMariadb extends db_1.OINODb {
282
282
  throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
283
283
  }
284
284
  }
285
+ /**
286
+ * Validate connection to database is working.
287
+ *
288
+ */
289
+ async validate() {
290
+ db_1.OINOBenchmark.start("OINODb", "validate");
291
+ let result = new db_1.OINOResult();
292
+ try {
293
+ const sql = this._getValidateSql(this._params.database);
294
+ // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
295
+ const sql_res = await this.sqlSelect(sql);
296
+ db_1.OINOLog.debug("OINODbMariadb.validate", { sql_res: sql_res });
297
+ if (sql_res.isEmpty()) {
298
+ result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
299
+ }
300
+ else if (sql_res.getRow().length == 0) {
301
+ result.setError(400, "DB returned no values for database!", "OINODbMariadb.validate");
302
+ }
303
+ else if (sql_res.getRow()[0] == "0") {
304
+ result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate");
305
+ }
306
+ else {
307
+ // connection is working
308
+ }
309
+ }
310
+ catch (e) {
311
+ result.setError(500, db_1.OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate");
312
+ }
313
+ db_1.OINOBenchmark.end("OINODb", "validate");
314
+ return result;
315
+ }
285
316
  /**
286
317
  * Execute a select operation.
287
318
  *
@@ -337,6 +368,14 @@ WHERE C.TABLE_SCHEMA = '${dbName}' AND C.TABLE_NAME = '${tableName}'
337
368
  ORDER BY C.ORDINAL_POSITION;`;
338
369
  return sql;
339
370
  }
371
+ _getValidateSql(dbName) {
372
+ const sql = `SELECT
373
+ Count(c.COLUMN_NAME) AS COLUMN_COUNT
374
+ FROM information_schema.COLUMNS C
375
+ LEFT JOIN information_schema.KEY_COLUMN_USAGE KCU ON KCU.TABLE_SCHEMA = C.TABLE_SCHEMA AND KCU.TABLE_NAME = C.TABLE_NAME AND C.COLUMN_NAME = KCU.COLUMN_NAME and KCU.REFERENCED_TABLE_NAME IS NOT NULL
376
+ WHERE C.TABLE_SCHEMA = '${dbName}';`;
377
+ return sql;
378
+ }
340
379
  /**
341
380
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
342
381
  * the model.
@@ -3,7 +3,7 @@
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 } from "@oino-ts/db";
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";
7
7
  import mariadb from "mariadb";
8
8
  /**
9
9
  * Implmentation of OINODbDataSet for MariaDb.
@@ -247,7 +247,7 @@ export class OINODbMariadb extends OINODb {
247
247
  else if (sqlValue === undefined) {
248
248
  return undefined;
249
249
  }
250
- else if (((sqlType == "date")) && (typeof (sqlValue) == "string")) {
250
+ else if (((sqlType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
251
251
  return new Date(sqlValue);
252
252
  }
253
253
  else if ((sqlType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
@@ -279,6 +279,37 @@ export class OINODbMariadb extends OINODb {
279
279
  throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
280
280
  }
281
281
  }
282
+ /**
283
+ * Validate connection to database is working.
284
+ *
285
+ */
286
+ async validate() {
287
+ OINOBenchmark.start("OINODb", "validate");
288
+ let result = new OINOResult();
289
+ try {
290
+ const sql = this._getValidateSql(this._params.database);
291
+ // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
292
+ const sql_res = await this.sqlSelect(sql);
293
+ OINOLog.debug("OINODbMariadb.validate", { sql_res: sql_res });
294
+ if (sql_res.isEmpty()) {
295
+ result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
296
+ }
297
+ else if (sql_res.getRow().length == 0) {
298
+ result.setError(400, "DB returned no values for database!", "OINODbMariadb.validate");
299
+ }
300
+ else if (sql_res.getRow()[0] == "0") {
301
+ result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate");
302
+ }
303
+ else {
304
+ // connection is working
305
+ }
306
+ }
307
+ catch (e) {
308
+ result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate");
309
+ }
310
+ OINOBenchmark.end("OINODb", "validate");
311
+ return result;
312
+ }
282
313
  /**
283
314
  * Execute a select operation.
284
315
  *
@@ -334,6 +365,14 @@ WHERE C.TABLE_SCHEMA = '${dbName}' AND C.TABLE_NAME = '${tableName}'
334
365
  ORDER BY C.ORDINAL_POSITION;`;
335
366
  return sql;
336
367
  }
368
+ _getValidateSql(dbName) {
369
+ const sql = `SELECT
370
+ Count(c.COLUMN_NAME) AS COLUMN_COUNT
371
+ FROM information_schema.COLUMNS C
372
+ LEFT JOIN information_schema.KEY_COLUMN_USAGE KCU ON KCU.TABLE_SCHEMA = C.TABLE_SCHEMA AND KCU.TABLE_NAME = C.TABLE_NAME AND C.COLUMN_NAME = KCU.COLUMN_NAME and KCU.REFERENCED_TABLE_NAME IS NOT NULL
373
+ WHERE C.TABLE_SCHEMA = '${dbName}';`;
374
+ return sql;
375
+ }
337
376
  /**
338
377
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
339
378
  * the model.
@@ -1,4 +1,4 @@
1
- import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
1
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell, OINOResult } from "@oino-ts/db";
2
2
  /**
3
3
  * Implementation of MariaDb/MySql-database.
4
4
  *
@@ -59,6 +59,11 @@ export declare class OINODbMariadb extends OINODb {
59
59
  *
60
60
  */
61
61
  connect(): Promise<boolean>;
62
+ /**
63
+ * Validate connection to database is working.
64
+ *
65
+ */
66
+ validate(): Promise<OINOResult>;
62
67
  /**
63
68
  * Execute a select operation.
64
69
  *
@@ -74,6 +79,7 @@ export declare class OINODbMariadb extends OINODb {
74
79
  */
75
80
  sqlExec(sql: string): Promise<OINODbDataSet>;
76
81
  private _getSchemaSql;
82
+ private _getValidateSql;
77
83
  /**
78
84
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
79
85
  * the model.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-mariadb",
3
- "version": "0.5.1",
3
+ "version": "0.6.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,7 @@
21
21
  "module": "./dist/esm/index.js",
22
22
  "types": "./dist/types/index.d.ts",
23
23
  "dependencies": {
24
- "@oino-ts/db": "^0.5.1",
24
+ "@oino-ts/db": "^0.6.0",
25
25
  "mariadb": "^3.2.3"
26
26
  },
27
27
  "devDependencies": {
@@ -4,7 +4,7 @@
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 } from "@oino-ts/db";
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";
8
8
 
9
9
  import mariadb from "mariadb";
10
10
 
@@ -264,7 +264,7 @@ export class OINODbMariadb extends OINODb {
264
264
  } else if (sqlValue === undefined) {
265
265
  return undefined
266
266
 
267
- } else if (((sqlType == "date")) && (typeof(sqlValue) == "string")) {
267
+ } else if (((sqlType == "date")) && (typeof(sqlValue) == "string") && (sqlValue != "")) {
268
268
  return new Date(sqlValue)
269
269
 
270
270
  } else if ((sqlType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
@@ -298,6 +298,37 @@ export class OINODbMariadb extends OINODb {
298
298
  }
299
299
  }
300
300
 
301
+ /**
302
+ * Validate connection to database is working.
303
+ *
304
+ */
305
+ async validate(): Promise<OINOResult> {
306
+ OINOBenchmark.start("OINODb", "validate")
307
+ let result:OINOResult = new OINOResult()
308
+ try {
309
+ const sql = this._getValidateSql(this._params.database)
310
+ // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
311
+ const sql_res:OINODbDataSet = await this.sqlSelect(sql)
312
+ OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
313
+ if (sql_res.isEmpty()) {
314
+ result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate")
315
+
316
+ } else if (sql_res.getRow().length == 0) {
317
+ result.setError(400, "DB returned no values for database!", "OINODbMariadb.validate")
318
+
319
+ } else if (sql_res.getRow()[0] == "0") {
320
+ result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate")
321
+
322
+ } else {
323
+ // connection is working
324
+ }
325
+ } catch (e:any) {
326
+ result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate")
327
+ }
328
+ OINOBenchmark.end("OINODb", "validate")
329
+ return result
330
+ }
331
+
301
332
  /**
302
333
  * Execute a select operation.
303
334
  *
@@ -357,6 +388,17 @@ ORDER BY C.ORDINAL_POSITION;`
357
388
  return sql
358
389
  }
359
390
 
391
+ private _getValidateSql(dbName:string):string {
392
+ const sql =
393
+ `SELECT
394
+ Count(c.COLUMN_NAME) AS COLUMN_COUNT
395
+ FROM information_schema.COLUMNS C
396
+ LEFT JOIN information_schema.KEY_COLUMN_USAGE KCU ON KCU.TABLE_SCHEMA = C.TABLE_SCHEMA AND KCU.TABLE_NAME = C.TABLE_NAME AND C.COLUMN_NAME = KCU.COLUMN_NAME and KCU.REFERENCED_TABLE_NAME IS NOT NULL
397
+ WHERE C.TABLE_SCHEMA = '${dbName}';`
398
+ return sql
399
+ }
400
+
401
+
360
402
  /**
361
403
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
362
404
  * the model.