@oino-ts/db-mariadb 0.7.2 → 0.8.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.
- package/dist/cjs/OINODbMariadb.js +19 -55
- package/dist/esm/OINODbMariadb.js +19 -55
- package/package.json +2 -2
- package/src/OINODbMariadb.ts +19 -54
|
@@ -26,7 +26,6 @@ class OINOMariadbData extends db_1.OINODbDataSet {
|
|
|
26
26
|
else if (Array.isArray(data)) {
|
|
27
27
|
this._rows = data;
|
|
28
28
|
}
|
|
29
|
-
// OINOLog.debug("OINOMariadbData.constructor", {_rows:this._rows})
|
|
30
29
|
if (this.isEmpty()) {
|
|
31
30
|
this._currentRow = -1;
|
|
32
31
|
this._eof = true;
|
|
@@ -57,7 +56,6 @@ class OINOMariadbData extends db_1.OINODbDataSet {
|
|
|
57
56
|
*
|
|
58
57
|
*/
|
|
59
58
|
async next() {
|
|
60
|
-
// OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
|
|
61
59
|
if (this._currentRow < this._rows.length - 1) {
|
|
62
60
|
this._currentRow = this._currentRow + 1;
|
|
63
61
|
}
|
|
@@ -101,24 +99,11 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
101
99
|
*/
|
|
102
100
|
constructor(params) {
|
|
103
101
|
super(params);
|
|
104
|
-
// OINOLog.debug("OINODbMariadb.constructor", {params:params})
|
|
105
102
|
if (this._params.type !== "OINODbMariadb") {
|
|
106
103
|
throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
|
|
107
104
|
}
|
|
108
105
|
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 });
|
|
109
106
|
delete this._params.password; // do not store password in db object
|
|
110
|
-
// this._pool.on("acquire", (conn: mariadb.Connection) => {
|
|
111
|
-
// OINOLog.info("OINODbMariadb acquire", {conn:conn})
|
|
112
|
-
// })
|
|
113
|
-
// this._pool.on("connection", (conn: mariadb.Connection) => {
|
|
114
|
-
// OINOLog.info("OINODbMariadb connection", {conn:conn})
|
|
115
|
-
// })
|
|
116
|
-
// this._pool.on("release", (conn: mariadb.Connection) => {
|
|
117
|
-
// OINOLog.info("OINODbMariadb release", {conn:conn})
|
|
118
|
-
// })
|
|
119
|
-
// this._pool.on("enqueue", () => {
|
|
120
|
-
// OINOLog.info("OINODbMariadb enqueue", {})
|
|
121
|
-
// })
|
|
122
107
|
}
|
|
123
108
|
_parseFieldLength(fieldLengthStr) {
|
|
124
109
|
let result = parseInt(fieldLengthStr);
|
|
@@ -128,27 +113,19 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
128
113
|
return result;
|
|
129
114
|
}
|
|
130
115
|
async _query(sql) {
|
|
131
|
-
// OINOLog.debug("OINODbMariadb._query", {sql:sql})
|
|
132
116
|
let connection = null;
|
|
133
117
|
try {
|
|
134
118
|
connection = await this._pool.getConnection();
|
|
135
119
|
const result = await connection.query(sql);
|
|
136
|
-
// console.log("OINODbMariadb._query rows="+result)
|
|
137
120
|
return Promise.resolve(result);
|
|
138
121
|
}
|
|
139
|
-
catch (err) {
|
|
140
|
-
// console.log("OINODbMariadb._query err=" + err);
|
|
141
|
-
throw err;
|
|
142
|
-
}
|
|
143
122
|
finally {
|
|
144
123
|
if (connection) {
|
|
145
124
|
await connection.end();
|
|
146
125
|
}
|
|
147
126
|
}
|
|
148
|
-
// OINOLog.debug("OINODbMariadb._query", {result:query_result})
|
|
149
127
|
}
|
|
150
128
|
async _exec(sql) {
|
|
151
|
-
// OINOLog.debug("OINODbMariadb._exec", {sql:sql})
|
|
152
129
|
let connection = null;
|
|
153
130
|
try {
|
|
154
131
|
connection = await this._pool.getConnection();
|
|
@@ -156,17 +133,11 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
156
133
|
// console.log(result);
|
|
157
134
|
return Promise.resolve(result);
|
|
158
135
|
}
|
|
159
|
-
catch (err) {
|
|
160
|
-
const msg_parts = err.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
|
|
161
|
-
// OINOLog.debug("OINODbMariadb._exec exception", {connection: msg_parts[1], message:msg_parts[2], sql:msg_parts[3]}) // print connection info just to log so tests don't break on runtime output
|
|
162
|
-
throw new Error(msg_parts[2]);
|
|
163
|
-
}
|
|
164
136
|
finally {
|
|
165
137
|
if (connection) {
|
|
166
138
|
await connection.end();
|
|
167
139
|
}
|
|
168
140
|
}
|
|
169
|
-
// OINOLog.debug("OINODbMariadb._query", {result:query_result})
|
|
170
141
|
}
|
|
171
142
|
/**
|
|
172
143
|
* Print a table name using database specific SQL escaping.
|
|
@@ -195,7 +166,6 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
195
166
|
*
|
|
196
167
|
*/
|
|
197
168
|
printCellAsSqlValue(cellValue, sqlType) {
|
|
198
|
-
// OINOLog.debug("OINODbMariadb.printCellAsSqlValue", {cellValue:cellValue, sqlType:sqlType})
|
|
199
169
|
if (cellValue === null) {
|
|
200
170
|
return "NULL";
|
|
201
171
|
}
|
|
@@ -252,7 +222,6 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
252
222
|
*
|
|
253
223
|
*/
|
|
254
224
|
parseSqlValueAsCell(sqlValue, sqlType) {
|
|
255
|
-
// OINOLog.debug("OINODbMariadb.parseSqlValueAsCell", {sqlValue:sqlValue, sqlType:sqlType})
|
|
256
225
|
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
257
226
|
return null;
|
|
258
227
|
}
|
|
@@ -283,14 +252,13 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
283
252
|
let connection = null;
|
|
284
253
|
try {
|
|
285
254
|
// make sure that any items are correctly URL encoded in the connection string
|
|
286
|
-
// OINOLog.debug("OINODbMariadb.connect")
|
|
287
255
|
connection = await this._pool.getConnection();
|
|
288
256
|
this.isConnected = true;
|
|
289
257
|
}
|
|
290
|
-
catch (
|
|
291
|
-
const msg_parts =
|
|
258
|
+
catch (e) {
|
|
259
|
+
const msg_parts = e.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
|
|
292
260
|
result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
|
|
293
|
-
db_1.OINOLog.
|
|
261
|
+
db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", result.statusMessage, { message: e.message, stack: e.stack });
|
|
294
262
|
}
|
|
295
263
|
finally {
|
|
296
264
|
if (connection) {
|
|
@@ -308,9 +276,7 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
308
276
|
let result = new db_1.OINOResult();
|
|
309
277
|
try {
|
|
310
278
|
const sql = this._getValidateSql(this._params.database);
|
|
311
|
-
// OINOLog.debug("OINODbMariadb.validate", {sql:sql})
|
|
312
279
|
const sql_res = await this.sqlSelect(sql);
|
|
313
|
-
// OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
|
|
314
280
|
if (sql_res.isEmpty()) {
|
|
315
281
|
result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
|
|
316
282
|
}
|
|
@@ -324,9 +290,9 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
324
290
|
this.isValidated = true;
|
|
325
291
|
}
|
|
326
292
|
}
|
|
327
|
-
catch (
|
|
328
|
-
result.setError(500, "Exception validating connection: " +
|
|
329
|
-
db_1.OINOLog.
|
|
293
|
+
catch (e) {
|
|
294
|
+
result.setError(500, "Exception validating connection: " + e.message, "OINODbMariadb.validate");
|
|
295
|
+
db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "validate", result.statusMessage, { message: e, stack: e.stack });
|
|
330
296
|
}
|
|
331
297
|
db_1.OINOBenchmark.end("OINODb", "validate");
|
|
332
298
|
return result;
|
|
@@ -341,11 +307,11 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
341
307
|
db_1.OINOBenchmark.start("OINODb", "sqlSelect");
|
|
342
308
|
let result;
|
|
343
309
|
try {
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
result = new OINOMariadbData(sql_res, []);
|
|
310
|
+
const rows = await this._query(sql);
|
|
311
|
+
result = new OINOMariadbData(rows, []);
|
|
347
312
|
}
|
|
348
313
|
catch (e) {
|
|
314
|
+
db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "SQL select exception", { message: e.message, stack: e.stack });
|
|
349
315
|
result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
|
|
350
316
|
}
|
|
351
317
|
db_1.OINOBenchmark.end("OINODb", "sqlSelect");
|
|
@@ -362,11 +328,12 @@ class OINODbMariadb extends db_1.OINODb {
|
|
|
362
328
|
let result;
|
|
363
329
|
try {
|
|
364
330
|
const sql_res = await this._exec(sql);
|
|
365
|
-
// OINOLog.debug("OINODbMariadb.sqlExec", {sql_res:sql_res})
|
|
366
331
|
result = new OINOMariadbData(sql_res, []);
|
|
367
332
|
}
|
|
368
333
|
catch (e) {
|
|
369
|
-
|
|
334
|
+
const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
|
|
335
|
+
db_1.OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "SQL exec exception", { message: msg_parts[2], stack: e.stack });
|
|
336
|
+
result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + msg_parts[2] + "]"]);
|
|
370
337
|
}
|
|
371
338
|
db_1.OINOBenchmark.end("OINODb", "sqlExec");
|
|
372
339
|
return result;
|
|
@@ -402,13 +369,11 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
402
369
|
*
|
|
403
370
|
*/
|
|
404
371
|
async initializeApiDatamodel(api) {
|
|
405
|
-
const
|
|
406
|
-
while (!
|
|
407
|
-
const row =
|
|
408
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { description:row })
|
|
372
|
+
const schema_res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName));
|
|
373
|
+
while (!schema_res.isEof()) {
|
|
374
|
+
const row = schema_res.getRow();
|
|
409
375
|
const field_name = row[0]?.toString() || "";
|
|
410
376
|
const field_matches = OINODbMariadb._fieldLengthRegex.exec(row[1]?.toString() || "") || [];
|
|
411
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { field_matches:field_matches })
|
|
412
377
|
const sql_type = field_matches[1] || "";
|
|
413
378
|
const field_length1 = this._parseFieldLength(field_matches[3] || "0");
|
|
414
379
|
const field_length2 = this._parseFieldLength(field_matches[4] || "0");
|
|
@@ -420,13 +385,12 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
420
385
|
isNotNull: row[2] == "NO"
|
|
421
386
|
};
|
|
422
387
|
if (api.isFieldIncluded(field_name) == false) {
|
|
423
|
-
db_1.OINOLog.info("OINODbMariadb.initializeApiDatamodel
|
|
388
|
+
db_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", ".initializeApiDatamodel", "Field excluded in API parameters", { field: field_name });
|
|
424
389
|
if (field_params.isPrimaryKey) {
|
|
425
390
|
throw new Error(db_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
|
|
426
391
|
}
|
|
427
392
|
}
|
|
428
393
|
else {
|
|
429
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel: next field ", {field_name: field_name, sql_type:sql_type, field_length1:field_length1, field_length2:field_length2, field_params:field_params })
|
|
430
394
|
if ((sql_type == "int") || (sql_type == "smallint") || (sql_type == "float") || (sql_type == "double")) {
|
|
431
395
|
api.datamodel.addField(new db_1.OINONumberDataField(this, field_name, sql_type, field_params));
|
|
432
396
|
}
|
|
@@ -456,13 +420,13 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
456
420
|
}
|
|
457
421
|
}
|
|
458
422
|
else {
|
|
459
|
-
db_1.OINOLog.info("OINODbMariadb
|
|
423
|
+
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 });
|
|
460
424
|
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
461
425
|
}
|
|
462
426
|
}
|
|
463
|
-
await
|
|
427
|
+
await schema_res.next();
|
|
464
428
|
}
|
|
465
|
-
db_1.OINOLog.
|
|
429
|
+
db_1.OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
|
|
466
430
|
return Promise.resolve();
|
|
467
431
|
}
|
|
468
432
|
}
|
|
@@ -23,7 +23,6 @@ class OINOMariadbData extends OINODbDataSet {
|
|
|
23
23
|
else if (Array.isArray(data)) {
|
|
24
24
|
this._rows = data;
|
|
25
25
|
}
|
|
26
|
-
// OINOLog.debug("OINOMariadbData.constructor", {_rows:this._rows})
|
|
27
26
|
if (this.isEmpty()) {
|
|
28
27
|
this._currentRow = -1;
|
|
29
28
|
this._eof = true;
|
|
@@ -54,7 +53,6 @@ class OINOMariadbData extends OINODbDataSet {
|
|
|
54
53
|
*
|
|
55
54
|
*/
|
|
56
55
|
async next() {
|
|
57
|
-
// OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
|
|
58
56
|
if (this._currentRow < this._rows.length - 1) {
|
|
59
57
|
this._currentRow = this._currentRow + 1;
|
|
60
58
|
}
|
|
@@ -98,24 +96,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
98
96
|
*/
|
|
99
97
|
constructor(params) {
|
|
100
98
|
super(params);
|
|
101
|
-
// OINOLog.debug("OINODbMariadb.constructor", {params:params})
|
|
102
99
|
if (this._params.type !== "OINODbMariadb") {
|
|
103
100
|
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
|
|
104
101
|
}
|
|
105
102
|
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 });
|
|
106
103
|
delete this._params.password; // do not store password in db object
|
|
107
|
-
// this._pool.on("acquire", (conn: mariadb.Connection) => {
|
|
108
|
-
// OINOLog.info("OINODbMariadb acquire", {conn:conn})
|
|
109
|
-
// })
|
|
110
|
-
// this._pool.on("connection", (conn: mariadb.Connection) => {
|
|
111
|
-
// OINOLog.info("OINODbMariadb connection", {conn:conn})
|
|
112
|
-
// })
|
|
113
|
-
// this._pool.on("release", (conn: mariadb.Connection) => {
|
|
114
|
-
// OINOLog.info("OINODbMariadb release", {conn:conn})
|
|
115
|
-
// })
|
|
116
|
-
// this._pool.on("enqueue", () => {
|
|
117
|
-
// OINOLog.info("OINODbMariadb enqueue", {})
|
|
118
|
-
// })
|
|
119
104
|
}
|
|
120
105
|
_parseFieldLength(fieldLengthStr) {
|
|
121
106
|
let result = parseInt(fieldLengthStr);
|
|
@@ -125,27 +110,19 @@ export class OINODbMariadb extends OINODb {
|
|
|
125
110
|
return result;
|
|
126
111
|
}
|
|
127
112
|
async _query(sql) {
|
|
128
|
-
// OINOLog.debug("OINODbMariadb._query", {sql:sql})
|
|
129
113
|
let connection = null;
|
|
130
114
|
try {
|
|
131
115
|
connection = await this._pool.getConnection();
|
|
132
116
|
const result = await connection.query(sql);
|
|
133
|
-
// console.log("OINODbMariadb._query rows="+result)
|
|
134
117
|
return Promise.resolve(result);
|
|
135
118
|
}
|
|
136
|
-
catch (err) {
|
|
137
|
-
// console.log("OINODbMariadb._query err=" + err);
|
|
138
|
-
throw err;
|
|
139
|
-
}
|
|
140
119
|
finally {
|
|
141
120
|
if (connection) {
|
|
142
121
|
await connection.end();
|
|
143
122
|
}
|
|
144
123
|
}
|
|
145
|
-
// OINOLog.debug("OINODbMariadb._query", {result:query_result})
|
|
146
124
|
}
|
|
147
125
|
async _exec(sql) {
|
|
148
|
-
// OINOLog.debug("OINODbMariadb._exec", {sql:sql})
|
|
149
126
|
let connection = null;
|
|
150
127
|
try {
|
|
151
128
|
connection = await this._pool.getConnection();
|
|
@@ -153,17 +130,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
153
130
|
// console.log(result);
|
|
154
131
|
return Promise.resolve(result);
|
|
155
132
|
}
|
|
156
|
-
catch (err) {
|
|
157
|
-
const msg_parts = err.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
|
|
158
|
-
// OINOLog.debug("OINODbMariadb._exec exception", {connection: msg_parts[1], message:msg_parts[2], sql:msg_parts[3]}) // print connection info just to log so tests don't break on runtime output
|
|
159
|
-
throw new Error(msg_parts[2]);
|
|
160
|
-
}
|
|
161
133
|
finally {
|
|
162
134
|
if (connection) {
|
|
163
135
|
await connection.end();
|
|
164
136
|
}
|
|
165
137
|
}
|
|
166
|
-
// OINOLog.debug("OINODbMariadb._query", {result:query_result})
|
|
167
138
|
}
|
|
168
139
|
/**
|
|
169
140
|
* Print a table name using database specific SQL escaping.
|
|
@@ -192,7 +163,6 @@ export class OINODbMariadb extends OINODb {
|
|
|
192
163
|
*
|
|
193
164
|
*/
|
|
194
165
|
printCellAsSqlValue(cellValue, sqlType) {
|
|
195
|
-
// OINOLog.debug("OINODbMariadb.printCellAsSqlValue", {cellValue:cellValue, sqlType:sqlType})
|
|
196
166
|
if (cellValue === null) {
|
|
197
167
|
return "NULL";
|
|
198
168
|
}
|
|
@@ -249,7 +219,6 @@ export class OINODbMariadb extends OINODb {
|
|
|
249
219
|
*
|
|
250
220
|
*/
|
|
251
221
|
parseSqlValueAsCell(sqlValue, sqlType) {
|
|
252
|
-
// OINOLog.debug("OINODbMariadb.parseSqlValueAsCell", {sqlValue:sqlValue, sqlType:sqlType})
|
|
253
222
|
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
254
223
|
return null;
|
|
255
224
|
}
|
|
@@ -280,14 +249,13 @@ export class OINODbMariadb extends OINODb {
|
|
|
280
249
|
let connection = null;
|
|
281
250
|
try {
|
|
282
251
|
// make sure that any items are correctly URL encoded in the connection string
|
|
283
|
-
// OINOLog.debug("OINODbMariadb.connect")
|
|
284
252
|
connection = await this._pool.getConnection();
|
|
285
253
|
this.isConnected = true;
|
|
286
254
|
}
|
|
287
|
-
catch (
|
|
288
|
-
const msg_parts =
|
|
255
|
+
catch (e) {
|
|
256
|
+
const msg_parts = e.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
|
|
289
257
|
result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
|
|
290
|
-
OINOLog.
|
|
258
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", result.statusMessage, { message: e.message, stack: e.stack });
|
|
291
259
|
}
|
|
292
260
|
finally {
|
|
293
261
|
if (connection) {
|
|
@@ -305,9 +273,7 @@ export class OINODbMariadb extends OINODb {
|
|
|
305
273
|
let result = new OINOResult();
|
|
306
274
|
try {
|
|
307
275
|
const sql = this._getValidateSql(this._params.database);
|
|
308
|
-
// OINOLog.debug("OINODbMariadb.validate", {sql:sql})
|
|
309
276
|
const sql_res = await this.sqlSelect(sql);
|
|
310
|
-
// OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
|
|
311
277
|
if (sql_res.isEmpty()) {
|
|
312
278
|
result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
|
|
313
279
|
}
|
|
@@ -321,9 +287,9 @@ export class OINODbMariadb extends OINODb {
|
|
|
321
287
|
this.isValidated = true;
|
|
322
288
|
}
|
|
323
289
|
}
|
|
324
|
-
catch (
|
|
325
|
-
result.setError(500, "Exception validating connection: " +
|
|
326
|
-
OINOLog.
|
|
290
|
+
catch (e) {
|
|
291
|
+
result.setError(500, "Exception validating connection: " + e.message, "OINODbMariadb.validate");
|
|
292
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "validate", result.statusMessage, { message: e, stack: e.stack });
|
|
327
293
|
}
|
|
328
294
|
OINOBenchmark.end("OINODb", "validate");
|
|
329
295
|
return result;
|
|
@@ -338,11 +304,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
338
304
|
OINOBenchmark.start("OINODb", "sqlSelect");
|
|
339
305
|
let result;
|
|
340
306
|
try {
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
result = new OINOMariadbData(sql_res, []);
|
|
307
|
+
const rows = await this._query(sql);
|
|
308
|
+
result = new OINOMariadbData(rows, []);
|
|
344
309
|
}
|
|
345
310
|
catch (e) {
|
|
311
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "SQL select exception", { message: e.message, stack: e.stack });
|
|
346
312
|
result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
|
|
347
313
|
}
|
|
348
314
|
OINOBenchmark.end("OINODb", "sqlSelect");
|
|
@@ -359,11 +325,12 @@ export class OINODbMariadb extends OINODb {
|
|
|
359
325
|
let result;
|
|
360
326
|
try {
|
|
361
327
|
const sql_res = await this._exec(sql);
|
|
362
|
-
// OINOLog.debug("OINODbMariadb.sqlExec", {sql_res:sql_res})
|
|
363
328
|
result = new OINOMariadbData(sql_res, []);
|
|
364
329
|
}
|
|
365
330
|
catch (e) {
|
|
366
|
-
|
|
331
|
+
const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
|
|
332
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "SQL exec exception", { message: msg_parts[2], stack: e.stack });
|
|
333
|
+
result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + msg_parts[2] + "]"]);
|
|
367
334
|
}
|
|
368
335
|
OINOBenchmark.end("OINODb", "sqlExec");
|
|
369
336
|
return result;
|
|
@@ -399,13 +366,11 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
399
366
|
*
|
|
400
367
|
*/
|
|
401
368
|
async initializeApiDatamodel(api) {
|
|
402
|
-
const
|
|
403
|
-
while (!
|
|
404
|
-
const row =
|
|
405
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { description:row })
|
|
369
|
+
const schema_res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName));
|
|
370
|
+
while (!schema_res.isEof()) {
|
|
371
|
+
const row = schema_res.getRow();
|
|
406
372
|
const field_name = row[0]?.toString() || "";
|
|
407
373
|
const field_matches = OINODbMariadb._fieldLengthRegex.exec(row[1]?.toString() || "") || [];
|
|
408
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { field_matches:field_matches })
|
|
409
374
|
const sql_type = field_matches[1] || "";
|
|
410
375
|
const field_length1 = this._parseFieldLength(field_matches[3] || "0");
|
|
411
376
|
const field_length2 = this._parseFieldLength(field_matches[4] || "0");
|
|
@@ -417,13 +382,12 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
417
382
|
isNotNull: row[2] == "NO"
|
|
418
383
|
};
|
|
419
384
|
if (api.isFieldIncluded(field_name) == false) {
|
|
420
|
-
OINOLog.info("OINODbMariadb.initializeApiDatamodel
|
|
385
|
+
OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", ".initializeApiDatamodel", "Field excluded in API parameters", { field: field_name });
|
|
421
386
|
if (field_params.isPrimaryKey) {
|
|
422
387
|
throw new Error(OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
|
|
423
388
|
}
|
|
424
389
|
}
|
|
425
390
|
else {
|
|
426
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel: next field ", {field_name: field_name, sql_type:sql_type, field_length1:field_length1, field_length2:field_length2, field_params:field_params })
|
|
427
391
|
if ((sql_type == "int") || (sql_type == "smallint") || (sql_type == "float") || (sql_type == "double")) {
|
|
428
392
|
api.datamodel.addField(new OINONumberDataField(this, field_name, sql_type, field_params));
|
|
429
393
|
}
|
|
@@ -453,13 +417,13 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`;
|
|
|
453
417
|
}
|
|
454
418
|
}
|
|
455
419
|
else {
|
|
456
|
-
OINOLog.info("OINODbMariadb
|
|
420
|
+
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 });
|
|
457
421
|
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
458
422
|
}
|
|
459
423
|
}
|
|
460
|
-
await
|
|
424
|
+
await schema_res.next();
|
|
461
425
|
}
|
|
462
|
-
OINOLog.
|
|
426
|
+
OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
|
|
463
427
|
return Promise.resolve();
|
|
464
428
|
}
|
|
465
429
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-mariadb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
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.
|
|
24
|
+
"@oino-ts/db": "^0.8.1",
|
|
25
25
|
"mariadb": "^3.2.3"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
package/src/OINODbMariadb.ts
CHANGED
|
@@ -29,7 +29,6 @@ class OINOMariadbData extends OINODbDataSet {
|
|
|
29
29
|
this._rows = data as OINODataRow[]
|
|
30
30
|
|
|
31
31
|
}
|
|
32
|
-
// OINOLog.debug("OINOMariadbData.constructor", {_rows:this._rows})
|
|
33
32
|
if (this.isEmpty()) {
|
|
34
33
|
this._currentRow = -1
|
|
35
34
|
this._eof = true
|
|
@@ -62,7 +61,6 @@ class OINOMariadbData extends OINODbDataSet {
|
|
|
62
61
|
*
|
|
63
62
|
*/
|
|
64
63
|
async next():Promise<boolean> {
|
|
65
|
-
// OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
|
|
66
64
|
if (this._currentRow < this._rows.length-1) {
|
|
67
65
|
this._currentRow = this._currentRow + 1
|
|
68
66
|
} else {
|
|
@@ -111,25 +109,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
111
109
|
constructor(params:OINODbParams) {
|
|
112
110
|
super(params)
|
|
113
111
|
|
|
114
|
-
// OINOLog.debug("OINODbMariadb.constructor", {params:params})
|
|
115
112
|
if (this._params.type !== "OINODbMariadb") {
|
|
116
113
|
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type)
|
|
117
114
|
}
|
|
118
115
|
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 })
|
|
119
116
|
delete this._params.password // do not store password in db object
|
|
120
|
-
|
|
121
|
-
// this._pool.on("acquire", (conn: mariadb.Connection) => {
|
|
122
|
-
// OINOLog.info("OINODbMariadb acquire", {conn:conn})
|
|
123
|
-
// })
|
|
124
|
-
// this._pool.on("connection", (conn: mariadb.Connection) => {
|
|
125
|
-
// OINOLog.info("OINODbMariadb connection", {conn:conn})
|
|
126
|
-
// })
|
|
127
|
-
// this._pool.on("release", (conn: mariadb.Connection) => {
|
|
128
|
-
// OINOLog.info("OINODbMariadb release", {conn:conn})
|
|
129
|
-
// })
|
|
130
|
-
// this._pool.on("enqueue", () => {
|
|
131
|
-
// OINOLog.info("OINODbMariadb enqueue", {})
|
|
132
|
-
// })
|
|
133
117
|
}
|
|
134
118
|
|
|
135
119
|
private _parseFieldLength(fieldLengthStr:string):number {
|
|
@@ -141,27 +125,20 @@ export class OINODbMariadb extends OINODb {
|
|
|
141
125
|
}
|
|
142
126
|
|
|
143
127
|
private async _query(sql:string):Promise<OINODataRow[]> {
|
|
144
|
-
// OINOLog.debug("OINODbMariadb._query", {sql:sql})
|
|
145
128
|
let connection:mariadb.Connection|null = null
|
|
146
129
|
try {
|
|
147
130
|
connection = await this._pool.getConnection();
|
|
148
131
|
const result = await connection.query(sql);
|
|
149
|
-
// console.log("OINODbMariadb._query rows="+result)
|
|
150
132
|
return Promise.resolve(result)
|
|
151
133
|
|
|
152
|
-
} catch (err) {
|
|
153
|
-
// console.log("OINODbMariadb._query err=" + err);
|
|
154
|
-
throw err;
|
|
155
134
|
} finally {
|
|
156
135
|
if (connection) {
|
|
157
136
|
await connection.end()
|
|
158
137
|
}
|
|
159
138
|
}
|
|
160
|
-
// OINOLog.debug("OINODbMariadb._query", {result:query_result})
|
|
161
139
|
}
|
|
162
140
|
|
|
163
141
|
private async _exec(sql:string):Promise<any> {
|
|
164
|
-
// OINOLog.debug("OINODbMariadb._exec", {sql:sql})
|
|
165
142
|
let connection:mariadb.Connection|null = null
|
|
166
143
|
try {
|
|
167
144
|
connection = await this._pool.getConnection();
|
|
@@ -169,16 +146,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
169
146
|
// console.log(result);
|
|
170
147
|
return Promise.resolve(result)
|
|
171
148
|
|
|
172
|
-
} catch (err) {
|
|
173
|
-
const msg_parts = (err as Error).message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
|
|
174
|
-
// OINOLog.debug("OINODbMariadb._exec exception", {connection: msg_parts[1], message:msg_parts[2], sql:msg_parts[3]}) // print connection info just to log so tests don't break on runtime output
|
|
175
|
-
throw new Error(msg_parts[2]);
|
|
176
149
|
} finally {
|
|
177
150
|
if (connection) {
|
|
178
151
|
await connection.end()
|
|
179
152
|
}
|
|
180
153
|
}
|
|
181
|
-
// OINOLog.debug("OINODbMariadb._query", {result:query_result})
|
|
182
154
|
}
|
|
183
155
|
|
|
184
156
|
/**
|
|
@@ -211,7 +183,6 @@ export class OINODbMariadb extends OINODb {
|
|
|
211
183
|
*
|
|
212
184
|
*/
|
|
213
185
|
printCellAsSqlValue(cellValue:OINODataCell, sqlType: string): string {
|
|
214
|
-
// OINOLog.debug("OINODbMariadb.printCellAsSqlValue", {cellValue:cellValue, sqlType:sqlType})
|
|
215
186
|
if (cellValue === null) {
|
|
216
187
|
return "NULL"
|
|
217
188
|
|
|
@@ -267,7 +238,6 @@ export class OINODbMariadb extends OINODb {
|
|
|
267
238
|
*
|
|
268
239
|
*/
|
|
269
240
|
parseSqlValueAsCell(sqlValue:OINODataCell, sqlType: string): OINODataCell {
|
|
270
|
-
// OINOLog.debug("OINODbMariadb.parseSqlValueAsCell", {sqlValue:sqlValue, sqlType:sqlType})
|
|
271
241
|
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
272
242
|
return null
|
|
273
243
|
|
|
@@ -300,14 +270,13 @@ export class OINODbMariadb extends OINODb {
|
|
|
300
270
|
let connection:mariadb.Connection|null = null
|
|
301
271
|
try {
|
|
302
272
|
// make sure that any items are correctly URL encoded in the connection string
|
|
303
|
-
// OINOLog.debug("OINODbMariadb.connect")
|
|
304
273
|
connection = await this._pool.getConnection()
|
|
305
274
|
this.isConnected = true
|
|
306
275
|
|
|
307
|
-
} catch (
|
|
308
|
-
const msg_parts = (
|
|
276
|
+
} catch (e:any) {
|
|
277
|
+
const msg_parts = (e as Error).message.match(OINODbMariadb._connectionExceptionMessageRegex) || []
|
|
309
278
|
result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect")
|
|
310
|
-
OINOLog.
|
|
279
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "connect", result.statusMessage, {message:e.message, stack:e.stack})
|
|
311
280
|
} finally {
|
|
312
281
|
if (connection) {
|
|
313
282
|
await connection.end()
|
|
@@ -326,9 +295,7 @@ export class OINODbMariadb extends OINODb {
|
|
|
326
295
|
let result:OINOResult = new OINOResult()
|
|
327
296
|
try {
|
|
328
297
|
const sql = this._getValidateSql(this._params.database)
|
|
329
|
-
// OINOLog.debug("OINODbMariadb.validate", {sql:sql})
|
|
330
298
|
const sql_res:OINODbDataSet = await this.sqlSelect(sql)
|
|
331
|
-
// OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
|
|
332
299
|
if (sql_res.isEmpty()) {
|
|
333
300
|
result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate")
|
|
334
301
|
|
|
@@ -341,9 +308,9 @@ export class OINODbMariadb extends OINODb {
|
|
|
341
308
|
} else {
|
|
342
309
|
this.isValidated = true
|
|
343
310
|
}
|
|
344
|
-
} catch (
|
|
345
|
-
result.setError(500, "Exception validating connection: " +
|
|
346
|
-
OINOLog.
|
|
311
|
+
} catch (e:any) {
|
|
312
|
+
result.setError(500, "Exception validating connection: " + e.message, "OINODbMariadb.validate")
|
|
313
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "validate", result.statusMessage, {message:e, stack:e.stack})
|
|
347
314
|
}
|
|
348
315
|
OINOBenchmark.end("OINODb", "validate")
|
|
349
316
|
return result
|
|
@@ -359,11 +326,11 @@ export class OINODbMariadb extends OINODb {
|
|
|
359
326
|
OINOBenchmark.start("OINODb", "sqlSelect")
|
|
360
327
|
let result:OINODbDataSet
|
|
361
328
|
try {
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
result = new OINOMariadbData(sql_res, [])
|
|
329
|
+
const rows:OINODataRow[] = await this._query(sql)
|
|
330
|
+
result = new OINOMariadbData(rows, [])
|
|
365
331
|
|
|
366
332
|
} catch (e:any) {
|
|
333
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlSelect", "SQL select exception", {message:e.message, stack:e.stack})
|
|
367
334
|
result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message])
|
|
368
335
|
}
|
|
369
336
|
OINOBenchmark.end("OINODb", "sqlSelect")
|
|
@@ -381,11 +348,12 @@ export class OINODbMariadb extends OINODb {
|
|
|
381
348
|
let result:OINODbDataSet
|
|
382
349
|
try {
|
|
383
350
|
const sql_res:OINODataRow[] = await this._exec(sql)
|
|
384
|
-
// OINOLog.debug("OINODbMariadb.sqlExec", {sql_res:sql_res})
|
|
385
351
|
result = new OINOMariadbData(sql_res, [])
|
|
386
352
|
|
|
387
353
|
} catch (e:any) {
|
|
388
|
-
|
|
354
|
+
const msg_parts = e.message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
|
|
355
|
+
OINOLog.exception("@oino-ts/db-mariadb", "OINODbMariadb", "sqlExec", "SQL exec exception", {message:msg_parts[2], stack:e.stack})
|
|
356
|
+
result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + msg_parts[2] + "]"])
|
|
389
357
|
}
|
|
390
358
|
OINOBenchmark.end("OINODb", "sqlExec")
|
|
391
359
|
return result
|
|
@@ -428,13 +396,11 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`
|
|
|
428
396
|
*/
|
|
429
397
|
async initializeApiDatamodel(api:OINODbApi): Promise<void> {
|
|
430
398
|
|
|
431
|
-
const
|
|
432
|
-
while (!
|
|
433
|
-
const row:OINODataRow =
|
|
434
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { description:row })
|
|
399
|
+
const schema_res:OINODbDataSet = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName))
|
|
400
|
+
while (!schema_res.isEof()) {
|
|
401
|
+
const row:OINODataRow = schema_res.getRow()
|
|
435
402
|
const field_name:string = row[0]?.toString() || ""
|
|
436
403
|
const field_matches = OINODbMariadb._fieldLengthRegex.exec(row[1]?.toString() || "") || []
|
|
437
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { field_matches:field_matches })
|
|
438
404
|
const sql_type:string = field_matches[1] || ""
|
|
439
405
|
const field_length1:number = this._parseFieldLength(field_matches[3] || "0")
|
|
440
406
|
const field_length2:number = this._parseFieldLength(field_matches[4] || "0")
|
|
@@ -446,13 +412,12 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`
|
|
|
446
412
|
isNotNull: row[2] == "NO"
|
|
447
413
|
}
|
|
448
414
|
if (api.isFieldIncluded(field_name)==false) {
|
|
449
|
-
OINOLog.info("OINODbMariadb.initializeApiDatamodel
|
|
415
|
+
OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", ".initializeApiDatamodel", "Field excluded in API parameters", {field:field_name})
|
|
450
416
|
if (field_params.isPrimaryKey) {
|
|
451
417
|
throw new Error(OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name)
|
|
452
418
|
}
|
|
453
419
|
|
|
454
420
|
} else {
|
|
455
|
-
// OINOLog.debug("OINODbMariadb.initializeApiDatamodel: next field ", {field_name: field_name, sql_type:sql_type, field_length1:field_length1, field_length2:field_length2, field_params:field_params })
|
|
456
421
|
if ((sql_type == "int") || (sql_type == "smallint") || (sql_type == "float") || (sql_type == "double")) {
|
|
457
422
|
api.datamodel.addField(new OINONumberDataField(this, field_name, sql_type, field_params ))
|
|
458
423
|
|
|
@@ -480,13 +445,13 @@ WHERE C.TABLE_SCHEMA = '${dbName}';`
|
|
|
480
445
|
}
|
|
481
446
|
|
|
482
447
|
} else {
|
|
483
|
-
OINOLog.info("OINODbMariadb
|
|
448
|
+
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 })
|
|
484
449
|
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0))
|
|
485
450
|
}
|
|
486
451
|
}
|
|
487
|
-
await
|
|
452
|
+
await schema_res.next()
|
|
488
453
|
}
|
|
489
|
-
OINOLog.
|
|
454
|
+
OINOLog.info("@oino-ts/db-mariadb", "OINODbMariadb", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"))
|
|
490
455
|
return Promise.resolve()
|
|
491
456
|
}
|
|
492
457
|
}
|