@oino-ts/db-mariadb 0.21.2 → 1.0.1

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.
@@ -7,14 +7,15 @@
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINODbMariadb = void 0;
9
9
  const common_1 = require("@oino-ts/common");
10
+ const common_2 = require("@oino-ts/common");
10
11
  const db_1 = require("@oino-ts/db");
11
12
  const mariadb_1 = require("mariadb");
12
13
  /**
13
- * Implmentation of OINODbDataSet for MariaDb.
14
+ * Implmentation of OINODataSet for MariaDb.
14
15
  *
15
16
  */
16
- class OINOMariadbData extends db_1.OINODbDataSet {
17
- _rows = db_1.OINODB_EMPTY_ROWS;
17
+ class OINOMariadbData extends common_2.OINODataSet {
18
+ _rows = common_2.OINO_EMPTY_ROWS;
18
19
  /**
19
20
  * OINOMariadbData constructor
20
21
  * @param params database parameters
@@ -74,7 +75,7 @@ class OINOMariadbData extends db_1.OINODbDataSet {
74
75
  return this._rows[this._currentRow];
75
76
  }
76
77
  else {
77
- return db_1.OINODB_EMPTY_ROW;
78
+ return common_2.OINO_EMPTY_ROW;
78
79
  }
79
80
  }
80
81
  /**
@@ -100,11 +101,11 @@ class OINODbMariadb extends db_1.OINODb {
100
101
  */
101
102
  constructor(params) {
102
103
  super(params);
103
- if (this._params.type !== "OINODbMariadb") {
104
- throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
104
+ if (this.dbParams.type !== "OINODbMariadb") {
105
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this.dbParams.type);
105
106
  }
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 });
107
- delete this._params.password; // do not store password in db object
107
+ this._pool = mariadb_1.default.createPool({ host: this.dbParams.url, database: this.dbParams.database, port: this.dbParams.port, user: this.dbParams.user, password: this.dbParams.password, acquireTimeout: 2000, debug: false, rowsAsArray: true, multipleStatements: true });
108
+ delete this.dbParams.password; // do not store password in db object
108
109
  }
109
110
  _parseFieldLength(fieldLengthStr) {
110
111
  let result = parseInt(fieldLengthStr);
@@ -115,7 +116,7 @@ class OINODbMariadb extends db_1.OINODb {
115
116
  }
116
117
  async _query(sql) {
117
118
  let connection = null;
118
- let rows = db_1.OINODB_EMPTY_ROWS;
119
+ let rows = common_2.OINO_EMPTY_ROWS;
119
120
  try {
120
121
  connection = await this._pool.getConnection();
121
122
  const sql_res = await connection.query(sql);
@@ -126,7 +127,7 @@ class OINODbMariadb extends db_1.OINODb {
126
127
  }
127
128
  catch (e) {
128
129
  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");
130
+ return new OINOMariadbData(common_2.OINO_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMariadb._query");
130
131
  }
131
132
  finally {
132
133
  if (connection) {
@@ -137,7 +138,7 @@ class OINODbMariadb extends db_1.OINODb {
137
138
  }
138
139
  async _exec(sql) {
139
140
  let connection = null;
140
- let rows = db_1.OINODB_EMPTY_ROWS;
141
+ let rows = common_2.OINO_EMPTY_ROWS;
141
142
  try {
142
143
  connection = await this._pool.getConnection();
143
144
  const sql_res = await connection.query(sql);
@@ -149,7 +150,7 @@ class OINODbMariadb extends db_1.OINODb {
149
150
  catch (e) {
150
151
  const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
151
152
  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");
153
+ return new OINOMariadbData(common_2.OINO_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec [" + msg_parts[2] + "]", "OINODbMariadb._exec");
153
154
  }
154
155
  finally {
155
156
  if (connection) {
@@ -164,7 +165,7 @@ class OINODbMariadb extends db_1.OINODb {
164
165
  * @param sqlTable name of the table
165
166
  *
166
167
  */
167
- printSqlTablename(sqlTable) {
168
+ printTableName(sqlTable) {
168
169
  return "`" + sqlTable + "`";
169
170
  }
170
171
  /**
@@ -173,7 +174,7 @@ class OINODbMariadb extends db_1.OINODb {
173
174
  * @param sqlColumn name of the column
174
175
  *
175
176
  */
176
- printSqlColumnname(sqlColumn) {
177
+ printColumnName(sqlColumn) {
177
178
  return "`" + sqlColumn + "`";
178
179
  }
179
180
  /**
@@ -181,20 +182,20 @@ class OINODbMariadb extends db_1.OINODb {
181
182
  * type with the correct SQL escaping.
182
183
  *
183
184
  * @param cellValue data from sql results
184
- * @param sqlType native type name for table column
185
+ * @param nativeType native type name for table column
185
186
  *
186
187
  */
187
- printCellAsSqlValue(cellValue, sqlType) {
188
+ printCellAsValue(cellValue, nativeType) {
188
189
  if (cellValue === null) {
189
190
  return "NULL";
190
191
  }
191
192
  else if (cellValue === undefined) {
192
193
  return "UNDEFINED";
193
194
  }
194
- else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float") || (sqlType == "double")) {
195
+ else if ((nativeType == "int") || (nativeType == "smallint") || (nativeType == "float") || (nativeType == "double")) {
195
196
  return cellValue.toString();
196
197
  }
197
- else if ((sqlType == "longblob") || (sqlType == "binary") || (sqlType == "varbinary")) {
198
+ else if ((nativeType == "longblob") || (nativeType == "binary") || (nativeType == "varbinary")) {
198
199
  if (cellValue instanceof Buffer) {
199
200
  return "x'" + cellValue.toString("hex") + "'";
200
201
  }
@@ -205,10 +206,10 @@ class OINODbMariadb extends db_1.OINODb {
205
206
  return "\"" + cellValue?.toString() + "\"";
206
207
  }
207
208
  }
208
- else if (((sqlType == "date") || (sqlType == "datetime") || (sqlType == "timestamp")) && (cellValue instanceof Date)) {
209
+ else if (((nativeType == "date") || (nativeType == "datetime") || (nativeType == "timestamp")) && (cellValue instanceof Date)) {
209
210
  return "\"" + cellValue.toISOString().replace('T', ' ').substring(0, 23) + "\"";
210
211
  }
211
- else if ((sqlType == "bit")) {
212
+ else if ((nativeType == "bit")) {
212
213
  if ((cellValue === false) || (cellValue == null) || (cellValue == "") || (cellValue.toString().toLowerCase() == "false") || (cellValue == "0")) {
213
214
  return "b'0'";
214
215
  }
@@ -220,7 +221,7 @@ class OINODbMariadb extends db_1.OINODb {
220
221
  }
221
222
  }
222
223
  else {
223
- return this.printSqlString(cellValue?.toString());
224
+ return this.printStringValue(cellValue?.toString());
224
225
  }
225
226
  }
226
227
  /**
@@ -229,7 +230,7 @@ class OINODbMariadb extends db_1.OINODb {
229
230
  * @param sqlString string value
230
231
  *
231
232
  */
232
- printSqlString(sqlString) {
233
+ printStringValue(sqlString) {
233
234
  return "\"" + sqlString.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"").replaceAll("\r", "\\r").replaceAll("\n", "\\n").replaceAll("\t", "\\t") + "\"";
234
235
  }
235
236
  /**
@@ -237,20 +238,20 @@ class OINODbMariadb extends db_1.OINODb {
237
238
  * type.
238
239
  *
239
240
  * @param sqlValue data from serialization
240
- * @param sqlType native type name for table column
241
+ * @param nativeType native type name for table column
241
242
  *
242
243
  */
243
- parseSqlValueAsCell(sqlValue, sqlType) {
244
+ parseValueAsCell(sqlValue, nativeType) {
244
245
  if ((sqlValue === null) || (sqlValue == "NULL")) {
245
246
  return null;
246
247
  }
247
248
  else if (sqlValue === undefined) {
248
249
  return undefined;
249
250
  }
250
- else if (((sqlType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
251
+ else if (((nativeType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
251
252
  return new Date(sqlValue);
252
253
  }
253
- else if ((sqlType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
254
+ else if ((nativeType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
254
255
  const buf = sqlValue;
255
256
  if (buf.length == 1) {
256
257
  return buf.readUInt8(0) === 1;
@@ -303,7 +304,7 @@ class OINODbMariadb extends db_1.OINODb {
303
304
  common_1.OINOBenchmark.startMetric("OINODb", "validate");
304
305
  let result = new common_1.OINOResult();
305
306
  try {
306
- const sql = this._getValidateSql(this._params.database);
307
+ const sql = this._getValidateSql(this.dbParams.database);
307
308
  const sql_res = await this._query(sql);
308
309
  if (sql_res.isEmpty()) {
309
310
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
@@ -367,37 +368,38 @@ class OINODbMariadb extends db_1.OINODb {
367
368
  return result;
368
369
  }
369
370
  _getSchemaSql(dbName, tableName) {
370
- const sql = `SELECT
371
- c.COLUMN_NAME,
372
- c.COLUMN_TYPE,
373
- c.IS_NULLABLE,
374
- c.COLUMN_KEY,
375
- c.COLUMN_DEFAULT,
376
- c.EXTRA,
377
- KCU.CONSTRAINT_NAME AS ForeignKeyName
378
- FROM information_schema.COLUMNS C
379
- 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
380
- WHERE C.TABLE_SCHEMA = '${dbName}' AND C.TABLE_NAME = '${tableName}'
371
+ const sql = `SELECT
372
+ c.COLUMN_NAME,
373
+ c.COLUMN_TYPE,
374
+ c.IS_NULLABLE,
375
+ c.COLUMN_KEY,
376
+ c.COLUMN_DEFAULT,
377
+ c.EXTRA,
378
+ KCU.CONSTRAINT_NAME AS ForeignKeyName
379
+ FROM information_schema.COLUMNS C
380
+ 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
381
+ WHERE C.TABLE_SCHEMA = '${dbName}' AND C.TABLE_NAME = '${tableName}'
381
382
  ORDER BY C.ORDINAL_POSITION;`;
382
383
  return sql;
383
384
  }
384
385
  _getValidateSql(dbName) {
385
- const sql = `SELECT
386
- Count(c.COLUMN_NAME) AS COLUMN_COUNT
387
- FROM information_schema.COLUMNS C
388
- 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
386
+ const sql = `SELECT
387
+ Count(c.COLUMN_NAME) AS COLUMN_COUNT
388
+ FROM information_schema.COLUMNS C
389
+ 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
389
390
  WHERE C.TABLE_SCHEMA = '${dbName}';`;
390
391
  return sql;
391
392
  }
392
393
  /**
393
- * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
394
+ * Initialize a data model by getting the SQL schema and populating OINODataFields of
394
395
  * the model.
395
396
  *
396
397
  * @param api api which data model to initialize.
397
398
  *
398
399
  */
399
400
  async initializeApiDatamodel(api) {
400
- const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName));
401
+ api.initializeDatamodel(new db_1.OINODbDataModel(api));
402
+ const schema_res = await this._query(this._getSchemaSql(this.dbParams.database, api.params.tableName));
401
403
  while (!schema_res.isEof()) {
402
404
  const row = schema_res.getRow();
403
405
  // console.log("OINODbMariadb.initializeApiDatamodel row", row)
@@ -421,36 +423,36 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
421
423
  }
422
424
  else {
423
425
  if ((sql_type == "int") || (sql_type == "smallint") || (sql_type == "float") || (sql_type == "double")) {
424
- api.datamodel.addField(new db_1.OINONumberDataField(this, field_name, sql_type, field_params));
426
+ api.datamodel.addField(new common_2.OINONumberDataField(this, field_name, sql_type, field_params));
425
427
  }
426
428
  else if ((sql_type == "date") || (sql_type == "datetime") || (sql_type == "timestamp")) {
427
429
  if (api.params.useDatesAsString) {
428
- api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
430
+ api.datamodel.addField(new common_2.OINOStringDataField(this, field_name, sql_type, field_params, 0));
429
431
  }
430
432
  else {
431
- api.datamodel.addField(new db_1.OINODatetimeDataField(this, field_name, sql_type, field_params));
433
+ api.datamodel.addField(new common_2.OINODatetimeDataField(this, field_name, sql_type, field_params));
432
434
  }
433
435
  }
434
436
  else if ((sql_type == "char") || (sql_type == "varchar") || (sql_type == "tinytext") || (sql_type == "tinytext") || (sql_type == "mediumtext") || (sql_type == "longtext")) {
435
- api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, field_length1));
437
+ api.datamodel.addField(new common_2.OINOStringDataField(this, field_name, sql_type, field_params, field_length1));
436
438
  }
437
439
  else if ((sql_type == "longblob") || (sql_type == "binary") || (sql_type == "varbinary")) {
438
- api.datamodel.addField(new db_1.OINOBlobDataField(this, field_name, sql_type, field_params, field_length1));
440
+ api.datamodel.addField(new common_2.OINOBlobDataField(this, field_name, sql_type, field_params, field_length1));
439
441
  }
440
442
  else if ((sql_type == "decimal")) {
441
- api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, field_length1 + field_length2 + 1));
443
+ api.datamodel.addField(new common_2.OINOStringDataField(this, field_name, sql_type, field_params, field_length1 + field_length2 + 1));
442
444
  }
443
445
  else if ((sql_type == "bit")) {
444
446
  if (field_length1 == 1) {
445
- api.datamodel.addField(new db_1.OINOBooleanDataField(this, field_name, sql_type, field_params));
447
+ api.datamodel.addField(new common_2.OINOBooleanDataField(this, field_name, sql_type, field_params));
446
448
  }
447
449
  else {
448
- api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, field_length1 * 8));
450
+ api.datamodel.addField(new common_2.OINOStringDataField(this, field_name, sql_type, field_params, field_length1 * 8));
449
451
  }
450
452
  }
451
453
  else {
452
454
  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 });
453
- api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
455
+ api.datamodel.addField(new common_2.OINOStringDataField(this, field_name, sql_type, field_params, 0));
454
456
  }
455
457
  }
456
458
  await schema_res.next();
@@ -4,14 +4,15 @@
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
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
+ import { OINODataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODatetimeDataField, OINOBlobDataField, OINO_EMPTY_ROW, OINO_EMPTY_ROWS } from "@oino-ts/common";
8
+ import { OINODb, OINODbDataModel } from "@oino-ts/db";
8
9
  import mariadb from "mariadb";
9
10
  /**
10
- * Implmentation of OINODbDataSet for MariaDb.
11
+ * Implmentation of OINODataSet for MariaDb.
11
12
  *
12
13
  */
13
- class OINOMariadbData extends OINODbDataSet {
14
- _rows = OINODB_EMPTY_ROWS;
14
+ class OINOMariadbData extends OINODataSet {
15
+ _rows = OINO_EMPTY_ROWS;
15
16
  /**
16
17
  * OINOMariadbData constructor
17
18
  * @param params database parameters
@@ -71,7 +72,7 @@ class OINOMariadbData extends OINODbDataSet {
71
72
  return this._rows[this._currentRow];
72
73
  }
73
74
  else {
74
- return OINODB_EMPTY_ROW;
75
+ return OINO_EMPTY_ROW;
75
76
  }
76
77
  }
77
78
  /**
@@ -97,11 +98,11 @@ export class OINODbMariadb extends OINODb {
97
98
  */
98
99
  constructor(params) {
99
100
  super(params);
100
- if (this._params.type !== "OINODbMariadb") {
101
- throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
101
+ if (this.dbParams.type !== "OINODbMariadb") {
102
+ throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this.dbParams.type);
102
103
  }
103
- this._pool = mariadb.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 });
104
- delete this._params.password; // do not store password in db object
104
+ this._pool = mariadb.createPool({ host: this.dbParams.url, database: this.dbParams.database, port: this.dbParams.port, user: this.dbParams.user, password: this.dbParams.password, acquireTimeout: 2000, debug: false, rowsAsArray: true, multipleStatements: true });
105
+ delete this.dbParams.password; // do not store password in db object
105
106
  }
106
107
  _parseFieldLength(fieldLengthStr) {
107
108
  let result = parseInt(fieldLengthStr);
@@ -112,7 +113,7 @@ export class OINODbMariadb extends OINODb {
112
113
  }
113
114
  async _query(sql) {
114
115
  let connection = null;
115
- let rows = OINODB_EMPTY_ROWS;
116
+ let rows = OINO_EMPTY_ROWS;
116
117
  try {
117
118
  connection = await this._pool.getConnection();
118
119
  const sql_res = await connection.query(sql);
@@ -123,7 +124,7 @@ export class OINODbMariadb extends OINODb {
123
124
  }
124
125
  catch (e) {
125
126
  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");
127
+ return new OINOMariadbData(OINO_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMariadb._query");
127
128
  }
128
129
  finally {
129
130
  if (connection) {
@@ -134,7 +135,7 @@ export class OINODbMariadb extends OINODb {
134
135
  }
135
136
  async _exec(sql) {
136
137
  let connection = null;
137
- let rows = OINODB_EMPTY_ROWS;
138
+ let rows = OINO_EMPTY_ROWS;
138
139
  try {
139
140
  connection = await this._pool.getConnection();
140
141
  const sql_res = await connection.query(sql);
@@ -146,7 +147,7 @@ export class OINODbMariadb extends OINODb {
146
147
  catch (e) {
147
148
  const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
148
149
  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");
150
+ return new OINOMariadbData(OINO_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec [" + msg_parts[2] + "]", "OINODbMariadb._exec");
150
151
  }
151
152
  finally {
152
153
  if (connection) {
@@ -161,7 +162,7 @@ export class OINODbMariadb extends OINODb {
161
162
  * @param sqlTable name of the table
162
163
  *
163
164
  */
164
- printSqlTablename(sqlTable) {
165
+ printTableName(sqlTable) {
165
166
  return "`" + sqlTable + "`";
166
167
  }
167
168
  /**
@@ -170,7 +171,7 @@ export class OINODbMariadb extends OINODb {
170
171
  * @param sqlColumn name of the column
171
172
  *
172
173
  */
173
- printSqlColumnname(sqlColumn) {
174
+ printColumnName(sqlColumn) {
174
175
  return "`" + sqlColumn + "`";
175
176
  }
176
177
  /**
@@ -178,20 +179,20 @@ export class OINODbMariadb extends OINODb {
178
179
  * type with the correct SQL escaping.
179
180
  *
180
181
  * @param cellValue data from sql results
181
- * @param sqlType native type name for table column
182
+ * @param nativeType native type name for table column
182
183
  *
183
184
  */
184
- printCellAsSqlValue(cellValue, sqlType) {
185
+ printCellAsValue(cellValue, nativeType) {
185
186
  if (cellValue === null) {
186
187
  return "NULL";
187
188
  }
188
189
  else if (cellValue === undefined) {
189
190
  return "UNDEFINED";
190
191
  }
191
- else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float") || (sqlType == "double")) {
192
+ else if ((nativeType == "int") || (nativeType == "smallint") || (nativeType == "float") || (nativeType == "double")) {
192
193
  return cellValue.toString();
193
194
  }
194
- else if ((sqlType == "longblob") || (sqlType == "binary") || (sqlType == "varbinary")) {
195
+ else if ((nativeType == "longblob") || (nativeType == "binary") || (nativeType == "varbinary")) {
195
196
  if (cellValue instanceof Buffer) {
196
197
  return "x'" + cellValue.toString("hex") + "'";
197
198
  }
@@ -202,10 +203,10 @@ export class OINODbMariadb extends OINODb {
202
203
  return "\"" + cellValue?.toString() + "\"";
203
204
  }
204
205
  }
205
- else if (((sqlType == "date") || (sqlType == "datetime") || (sqlType == "timestamp")) && (cellValue instanceof Date)) {
206
+ else if (((nativeType == "date") || (nativeType == "datetime") || (nativeType == "timestamp")) && (cellValue instanceof Date)) {
206
207
  return "\"" + cellValue.toISOString().replace('T', ' ').substring(0, 23) + "\"";
207
208
  }
208
- else if ((sqlType == "bit")) {
209
+ else if ((nativeType == "bit")) {
209
210
  if ((cellValue === false) || (cellValue == null) || (cellValue == "") || (cellValue.toString().toLowerCase() == "false") || (cellValue == "0")) {
210
211
  return "b'0'";
211
212
  }
@@ -217,7 +218,7 @@ export class OINODbMariadb extends OINODb {
217
218
  }
218
219
  }
219
220
  else {
220
- return this.printSqlString(cellValue?.toString());
221
+ return this.printStringValue(cellValue?.toString());
221
222
  }
222
223
  }
223
224
  /**
@@ -226,7 +227,7 @@ export class OINODbMariadb extends OINODb {
226
227
  * @param sqlString string value
227
228
  *
228
229
  */
229
- printSqlString(sqlString) {
230
+ printStringValue(sqlString) {
230
231
  return "\"" + sqlString.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"").replaceAll("\r", "\\r").replaceAll("\n", "\\n").replaceAll("\t", "\\t") + "\"";
231
232
  }
232
233
  /**
@@ -234,20 +235,20 @@ export class OINODbMariadb extends OINODb {
234
235
  * type.
235
236
  *
236
237
  * @param sqlValue data from serialization
237
- * @param sqlType native type name for table column
238
+ * @param nativeType native type name for table column
238
239
  *
239
240
  */
240
- parseSqlValueAsCell(sqlValue, sqlType) {
241
+ parseValueAsCell(sqlValue, nativeType) {
241
242
  if ((sqlValue === null) || (sqlValue == "NULL")) {
242
243
  return null;
243
244
  }
244
245
  else if (sqlValue === undefined) {
245
246
  return undefined;
246
247
  }
247
- else if (((sqlType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
248
+ else if (((nativeType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
248
249
  return new Date(sqlValue);
249
250
  }
250
- else if ((sqlType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
251
+ else if ((nativeType == "bit") && (sqlValue instanceof Buffer)) { // mariadb returns a buffer for bit-fields
251
252
  const buf = sqlValue;
252
253
  if (buf.length == 1) {
253
254
  return buf.readUInt8(0) === 1;
@@ -300,7 +301,7 @@ export class OINODbMariadb extends OINODb {
300
301
  OINOBenchmark.startMetric("OINODb", "validate");
301
302
  let result = new OINOResult();
302
303
  try {
303
- const sql = this._getValidateSql(this._params.database);
304
+ const sql = this._getValidateSql(this.dbParams.database);
304
305
  const sql_res = await this._query(sql);
305
306
  if (sql_res.isEmpty()) {
306
307
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
@@ -364,37 +365,38 @@ export class OINODbMariadb extends OINODb {
364
365
  return result;
365
366
  }
366
367
  _getSchemaSql(dbName, tableName) {
367
- const sql = `SELECT
368
- c.COLUMN_NAME,
369
- c.COLUMN_TYPE,
370
- c.IS_NULLABLE,
371
- c.COLUMN_KEY,
372
- c.COLUMN_DEFAULT,
373
- c.EXTRA,
374
- KCU.CONSTRAINT_NAME AS ForeignKeyName
375
- FROM information_schema.COLUMNS C
376
- 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
377
- WHERE C.TABLE_SCHEMA = '${dbName}' AND C.TABLE_NAME = '${tableName}'
368
+ const sql = `SELECT
369
+ c.COLUMN_NAME,
370
+ c.COLUMN_TYPE,
371
+ c.IS_NULLABLE,
372
+ c.COLUMN_KEY,
373
+ c.COLUMN_DEFAULT,
374
+ c.EXTRA,
375
+ KCU.CONSTRAINT_NAME AS ForeignKeyName
376
+ FROM information_schema.COLUMNS C
377
+ 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
378
+ WHERE C.TABLE_SCHEMA = '${dbName}' AND C.TABLE_NAME = '${tableName}'
378
379
  ORDER BY C.ORDINAL_POSITION;`;
379
380
  return sql;
380
381
  }
381
382
  _getValidateSql(dbName) {
382
- const sql = `SELECT
383
- Count(c.COLUMN_NAME) AS COLUMN_COUNT
384
- FROM information_schema.COLUMNS C
385
- 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
383
+ const sql = `SELECT
384
+ Count(c.COLUMN_NAME) AS COLUMN_COUNT
385
+ FROM information_schema.COLUMNS C
386
+ 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
386
387
  WHERE C.TABLE_SCHEMA = '${dbName}';`;
387
388
  return sql;
388
389
  }
389
390
  /**
390
- * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
391
+ * Initialize a data model by getting the SQL schema and populating OINODataFields of
391
392
  * the model.
392
393
  *
393
394
  * @param api api which data model to initialize.
394
395
  *
395
396
  */
396
397
  async initializeApiDatamodel(api) {
397
- const schema_res = await this._query(this._getSchemaSql(this._params.database, api.params.tableName));
398
+ api.initializeDatamodel(new OINODbDataModel(api));
399
+ const schema_res = await this._query(this._getSchemaSql(this.dbParams.database, api.params.tableName));
398
400
  while (!schema_res.isEof()) {
399
401
  const row = schema_res.getRow();
400
402
  // console.log("OINODbMariadb.initializeApiDatamodel row", row)
@@ -1,5 +1,6 @@
1
1
  import { OINOResult } from "@oino-ts/common";
2
- import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
2
+ import { OINODataSet, OINODataCell } from "@oino-ts/common";
3
+ import { OINODb, OINODbApi, OINODbParams } from "@oino-ts/db";
3
4
  /**
4
5
  * Implementation of MariaDb/MySql-database.
5
6
  *
@@ -23,39 +24,39 @@ export declare class OINODbMariadb extends OINODb {
23
24
  * @param sqlTable name of the table
24
25
  *
25
26
  */
26
- printSqlTablename(sqlTable: string): string;
27
+ printTableName(sqlTable: string): string;
27
28
  /**
28
29
  * Print a column name with correct SQL escaping.
29
30
  *
30
31
  * @param sqlColumn name of the column
31
32
  *
32
33
  */
33
- printSqlColumnname(sqlColumn: string): string;
34
+ printColumnName(sqlColumn: string): string;
34
35
  /**
35
36
  * Print a single data value from serialization using the context of the native data
36
37
  * type with the correct SQL escaping.
37
38
  *
38
39
  * @param cellValue data from sql results
39
- * @param sqlType native type name for table column
40
+ * @param nativeType native type name for table column
40
41
  *
41
42
  */
42
- printCellAsSqlValue(cellValue: OINODataCell, sqlType: string): string;
43
+ printCellAsValue(cellValue: OINODataCell, nativeType: string): string;
43
44
  /**
44
45
  * Print a single string value as valid sql literal
45
46
  *
46
47
  * @param sqlString string value
47
48
  *
48
49
  */
49
- printSqlString(sqlString: string): string;
50
+ printStringValue(sqlString: string): string;
50
51
  /**
51
52
  * Parse a single SQL result value for serialization using the context of the native data
52
53
  * type.
53
54
  *
54
55
  * @param sqlValue data from serialization
55
- * @param sqlType native type name for table column
56
+ * @param nativeType native type name for table column
56
57
  *
57
58
  */
58
- parseSqlValueAsCell(sqlValue: OINODataCell, sqlType: string): OINODataCell;
59
+ parseValueAsCell(sqlValue: OINODataCell, nativeType: string): OINODataCell;
59
60
  /**
60
61
  * Connect to database.
61
62
  *
@@ -77,18 +78,18 @@ export declare class OINODbMariadb extends OINODb {
77
78
  * @param sql SQL statement.
78
79
  *
79
80
  */
80
- sqlSelect(sql: string): Promise<OINODbDataSet>;
81
+ sqlSelect(sql: string): Promise<OINODataSet>;
81
82
  /**
82
83
  * Execute other sql operations.
83
84
  *
84
85
  * @param sql SQL statement.
85
86
  *
86
87
  */
87
- sqlExec(sql: string): Promise<OINODbDataSet>;
88
+ sqlExec(sql: string): Promise<OINODataSet>;
88
89
  private _getSchemaSql;
89
90
  private _getValidateSql;
90
91
  /**
91
- * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
92
+ * Initialize a data model by getting the SQL schema and populating OINODataFields of
92
93
  * the model.
93
94
  *
94
95
  * @param api api which data model to initialize.