@oino-ts/db-mssql 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/OINODbMsSql.js +68 -26
- package/dist/esm/OINODbMsSql.js +68 -26
- package/dist/types/OINODbMsSql.d.ts +15 -0
- package/package.json +2 -2
- package/src/OINODbMsSql.ts +70 -27
package/dist/cjs/OINODbMsSql.js
CHANGED
|
@@ -14,7 +14,7 @@ const mssql_1 = require("mssql");
|
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
class OINOMsSqlData extends db_1.OINODbDataSet {
|
|
17
|
-
_recordsets
|
|
17
|
+
_recordsets;
|
|
18
18
|
_rows = db_1.OINODB_EMPTY_ROWS;
|
|
19
19
|
_currentRecordset;
|
|
20
20
|
_currentRow;
|
|
@@ -28,7 +28,7 @@ class OINOMsSqlData extends db_1.OINODbDataSet {
|
|
|
28
28
|
if (data == null) {
|
|
29
29
|
this.messages.push(common_1.OINO_INFO_PREFIX + "SQL result is empty");
|
|
30
30
|
}
|
|
31
|
-
else if (!(Array.isArray(data) && (data.length > 0)
|
|
31
|
+
else if (!(Array.isArray(data)) && (data.length > 0)) {
|
|
32
32
|
throw new Error(common_1.OINO_ERROR_PREFIX + ": OINOMsSqlData constructor: invalid data!");
|
|
33
33
|
}
|
|
34
34
|
else {
|
|
@@ -51,7 +51,7 @@ class OINOMsSqlData extends db_1.OINODbDataSet {
|
|
|
51
51
|
*
|
|
52
52
|
*/
|
|
53
53
|
isEmpty() {
|
|
54
|
-
return (this._rows.length == 0);
|
|
54
|
+
return (this._recordsets.length == 0) || (this._rows == undefined) || (this._rows.length == 0);
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
57
|
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
@@ -133,14 +133,28 @@ class OINODbMsSql extends db_1.OINODb {
|
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
135
|
async _query(sql) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
try {
|
|
137
|
+
const request = this._pool.request(); // this does not need to be released but the pool will handle it
|
|
138
|
+
const sql_res = await request.query(sql);
|
|
139
|
+
// console.log("_query: result=", sql_res.recordsets, sql_res.recordsets?.length) // TODO: remove
|
|
140
|
+
return new OINOMsSqlData(sql_res.recordsets, []);
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
common_1.OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "_query", "exception in SQL query", { message: e.message, stack: e.stack, sql: sql });
|
|
144
|
+
return new OINOMsSqlData(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMsSql._query");
|
|
145
|
+
}
|
|
140
146
|
}
|
|
141
147
|
async _exec(sql) {
|
|
142
|
-
|
|
143
|
-
|
|
148
|
+
try {
|
|
149
|
+
const request = this._pool.request(); // this does not need to be released but the pool will handle it
|
|
150
|
+
const sql_res = await request.query(sql);
|
|
151
|
+
// console.log("_exec: result=", sql_res.recordsets, sql_res.recordsets?.length) // TODO: remove
|
|
152
|
+
return new OINOMsSqlData(sql_res.recordsets, []);
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
common_1.OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "_exec", "exception in SQL exec", { message: e.message, stack: e.stack, sql: sql });
|
|
156
|
+
return new OINOMsSqlData(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbMsSql._exec");
|
|
157
|
+
}
|
|
144
158
|
}
|
|
145
159
|
/**
|
|
146
160
|
* Print a table name using database specific SQL escaping.
|
|
@@ -270,12 +284,32 @@ class OINODbMsSql extends db_1.OINODb {
|
|
|
270
284
|
common_1.OINOLog.debug("@oino-ts/db-mssql", "OINODbMsSql", "printSqlSelect", "Result", { sql: result });
|
|
271
285
|
return result;
|
|
272
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Print SQL select statement with DB specific formatting.
|
|
289
|
+
*
|
|
290
|
+
* @param tableName - The name of the table to select from.
|
|
291
|
+
* @param columns - The columns to be selected.
|
|
292
|
+
* @param values - The values to be inserted.
|
|
293
|
+
* @param returnIdFields - the id fields to return if returnIds is true (if supported by the database)
|
|
294
|
+
*
|
|
295
|
+
*/
|
|
296
|
+
printSqlInsert(tableName, columns, values, returnIdFields) {
|
|
297
|
+
let result = "INSERT INTO " + tableName + " (" + columns + ")";
|
|
298
|
+
if (returnIdFields) {
|
|
299
|
+
result += " OUTPUT " + returnIdFields.map(f => "INSERTED." + f).join(", ");
|
|
300
|
+
}
|
|
301
|
+
result += " VALUES (" + values + ");";
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
273
304
|
/**
|
|
274
305
|
* Connect to database.
|
|
275
306
|
*
|
|
276
307
|
*/
|
|
277
308
|
async connect() {
|
|
278
309
|
let result = new common_1.OINOResult();
|
|
310
|
+
if (this.isConnected) {
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
279
313
|
try {
|
|
280
314
|
// make sure that any items are correctly URL encoded in the connection string
|
|
281
315
|
await this._pool.connect();
|
|
@@ -301,7 +335,7 @@ class OINODbMsSql extends db_1.OINODb {
|
|
|
301
335
|
common_1.OINOBenchmark.startMetric("OINODb", "validate");
|
|
302
336
|
try {
|
|
303
337
|
const sql = this._getValidateSql(this._params.database);
|
|
304
|
-
const sql_res = await this.
|
|
338
|
+
const sql_res = await this._query(sql);
|
|
305
339
|
if (sql_res.isEmpty()) {
|
|
306
340
|
result.setError(400, "DB returned no rows for select!", "OINODbMsSql.validate");
|
|
307
341
|
}
|
|
@@ -323,6 +357,22 @@ class OINODbMsSql extends db_1.OINODb {
|
|
|
323
357
|
common_1.OINOBenchmark.endMetric("OINODb", "validate");
|
|
324
358
|
return result;
|
|
325
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Disconnect from database.
|
|
362
|
+
*
|
|
363
|
+
*/
|
|
364
|
+
async disconnect() {
|
|
365
|
+
if (this._pool) {
|
|
366
|
+
try {
|
|
367
|
+
await this._pool.close();
|
|
368
|
+
}
|
|
369
|
+
catch (e) {
|
|
370
|
+
common_1.OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "disconnect", "exception in disconnect", { message: e.message, stack: e.stack });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
this.isConnected = false;
|
|
374
|
+
this.isValidated = false;
|
|
375
|
+
}
|
|
326
376
|
/**
|
|
327
377
|
* Execute a select operation.
|
|
328
378
|
*
|
|
@@ -330,15 +380,11 @@ class OINODbMsSql extends db_1.OINODb {
|
|
|
330
380
|
*
|
|
331
381
|
*/
|
|
332
382
|
async sqlSelect(sql) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
try {
|
|
336
|
-
result = await this._query(sql);
|
|
337
|
-
}
|
|
338
|
-
catch (e) {
|
|
339
|
-
common_1.OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "sqlSelect", "exception in SQL select", { message: e.message, stack: e.stack });
|
|
340
|
-
result = new OINOMsSqlData(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (sqlSelect): OINODbMsSql.sqlSelect exception in _db.query: " + e.message]);
|
|
383
|
+
if (!this.isValidated) {
|
|
384
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
341
385
|
}
|
|
386
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
387
|
+
let result = await this._query(sql);
|
|
342
388
|
common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
343
389
|
return result;
|
|
344
390
|
}
|
|
@@ -349,15 +395,11 @@ class OINODbMsSql extends db_1.OINODb {
|
|
|
349
395
|
*
|
|
350
396
|
*/
|
|
351
397
|
async sqlExec(sql) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
try {
|
|
355
|
-
result = await this._exec(sql);
|
|
356
|
-
}
|
|
357
|
-
catch (e) {
|
|
358
|
-
common_1.OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "sqlExec", "exception in SQL exec", { message: e.message, stack: e.stack });
|
|
359
|
-
result = new OINOMsSqlData(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
398
|
+
if (!this.isValidated) {
|
|
399
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
360
400
|
}
|
|
401
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
402
|
+
let result = await this._exec(sql);
|
|
361
403
|
common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
362
404
|
return result;
|
|
363
405
|
}
|
package/dist/esm/OINODbMsSql.js
CHANGED
|
@@ -11,7 +11,7 @@ import { ConnectionPool } from "mssql";
|
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
13
13
|
class OINOMsSqlData extends OINODbDataSet {
|
|
14
|
-
_recordsets
|
|
14
|
+
_recordsets;
|
|
15
15
|
_rows = OINODB_EMPTY_ROWS;
|
|
16
16
|
_currentRecordset;
|
|
17
17
|
_currentRow;
|
|
@@ -25,7 +25,7 @@ class OINOMsSqlData extends OINODbDataSet {
|
|
|
25
25
|
if (data == null) {
|
|
26
26
|
this.messages.push(OINO_INFO_PREFIX + "SQL result is empty");
|
|
27
27
|
}
|
|
28
|
-
else if (!(Array.isArray(data) && (data.length > 0)
|
|
28
|
+
else if (!(Array.isArray(data)) && (data.length > 0)) {
|
|
29
29
|
throw new Error(OINO_ERROR_PREFIX + ": OINOMsSqlData constructor: invalid data!");
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
@@ -48,7 +48,7 @@ class OINOMsSqlData extends OINODbDataSet {
|
|
|
48
48
|
*
|
|
49
49
|
*/
|
|
50
50
|
isEmpty() {
|
|
51
|
-
return (this._rows.length == 0);
|
|
51
|
+
return (this._recordsets.length == 0) || (this._rows == undefined) || (this._rows.length == 0);
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
54
|
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
@@ -130,14 +130,28 @@ export class OINODbMsSql extends OINODb {
|
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
132
|
async _query(sql) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
try {
|
|
134
|
+
const request = this._pool.request(); // this does not need to be released but the pool will handle it
|
|
135
|
+
const sql_res = await request.query(sql);
|
|
136
|
+
// console.log("_query: result=", sql_res.recordsets, sql_res.recordsets?.length) // TODO: remove
|
|
137
|
+
return new OINOMsSqlData(sql_res.recordsets, []);
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "_query", "exception in SQL query", { message: e.message, stack: e.stack, sql: sql });
|
|
141
|
+
return new OINOMsSqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMsSql._query");
|
|
142
|
+
}
|
|
137
143
|
}
|
|
138
144
|
async _exec(sql) {
|
|
139
|
-
|
|
140
|
-
|
|
145
|
+
try {
|
|
146
|
+
const request = this._pool.request(); // this does not need to be released but the pool will handle it
|
|
147
|
+
const sql_res = await request.query(sql);
|
|
148
|
+
// console.log("_exec: result=", sql_res.recordsets, sql_res.recordsets?.length) // TODO: remove
|
|
149
|
+
return new OINOMsSqlData(sql_res.recordsets, []);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "_exec", "exception in SQL exec", { message: e.message, stack: e.stack, sql: sql });
|
|
153
|
+
return new OINOMsSqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbMsSql._exec");
|
|
154
|
+
}
|
|
141
155
|
}
|
|
142
156
|
/**
|
|
143
157
|
* Print a table name using database specific SQL escaping.
|
|
@@ -267,12 +281,32 @@ export class OINODbMsSql extends OINODb {
|
|
|
267
281
|
OINOLog.debug("@oino-ts/db-mssql", "OINODbMsSql", "printSqlSelect", "Result", { sql: result });
|
|
268
282
|
return result;
|
|
269
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Print SQL select statement with DB specific formatting.
|
|
286
|
+
*
|
|
287
|
+
* @param tableName - The name of the table to select from.
|
|
288
|
+
* @param columns - The columns to be selected.
|
|
289
|
+
* @param values - The values to be inserted.
|
|
290
|
+
* @param returnIdFields - the id fields to return if returnIds is true (if supported by the database)
|
|
291
|
+
*
|
|
292
|
+
*/
|
|
293
|
+
printSqlInsert(tableName, columns, values, returnIdFields) {
|
|
294
|
+
let result = "INSERT INTO " + tableName + " (" + columns + ")";
|
|
295
|
+
if (returnIdFields) {
|
|
296
|
+
result += " OUTPUT " + returnIdFields.map(f => "INSERTED." + f).join(", ");
|
|
297
|
+
}
|
|
298
|
+
result += " VALUES (" + values + ");";
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
270
301
|
/**
|
|
271
302
|
* Connect to database.
|
|
272
303
|
*
|
|
273
304
|
*/
|
|
274
305
|
async connect() {
|
|
275
306
|
let result = new OINOResult();
|
|
307
|
+
if (this.isConnected) {
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
276
310
|
try {
|
|
277
311
|
// make sure that any items are correctly URL encoded in the connection string
|
|
278
312
|
await this._pool.connect();
|
|
@@ -298,7 +332,7 @@ export class OINODbMsSql extends OINODb {
|
|
|
298
332
|
OINOBenchmark.startMetric("OINODb", "validate");
|
|
299
333
|
try {
|
|
300
334
|
const sql = this._getValidateSql(this._params.database);
|
|
301
|
-
const sql_res = await this.
|
|
335
|
+
const sql_res = await this._query(sql);
|
|
302
336
|
if (sql_res.isEmpty()) {
|
|
303
337
|
result.setError(400, "DB returned no rows for select!", "OINODbMsSql.validate");
|
|
304
338
|
}
|
|
@@ -320,6 +354,22 @@ export class OINODbMsSql extends OINODb {
|
|
|
320
354
|
OINOBenchmark.endMetric("OINODb", "validate");
|
|
321
355
|
return result;
|
|
322
356
|
}
|
|
357
|
+
/**
|
|
358
|
+
* Disconnect from database.
|
|
359
|
+
*
|
|
360
|
+
*/
|
|
361
|
+
async disconnect() {
|
|
362
|
+
if (this._pool) {
|
|
363
|
+
try {
|
|
364
|
+
await this._pool.close();
|
|
365
|
+
}
|
|
366
|
+
catch (e) {
|
|
367
|
+
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "disconnect", "exception in disconnect", { message: e.message, stack: e.stack });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
this.isConnected = false;
|
|
371
|
+
this.isValidated = false;
|
|
372
|
+
}
|
|
323
373
|
/**
|
|
324
374
|
* Execute a select operation.
|
|
325
375
|
*
|
|
@@ -327,15 +377,11 @@ export class OINODbMsSql extends OINODb {
|
|
|
327
377
|
*
|
|
328
378
|
*/
|
|
329
379
|
async sqlSelect(sql) {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
try {
|
|
333
|
-
result = await this._query(sql);
|
|
334
|
-
}
|
|
335
|
-
catch (e) {
|
|
336
|
-
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "sqlSelect", "exception in SQL select", { message: e.message, stack: e.stack });
|
|
337
|
-
result = new OINOMsSqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMsSql.sqlSelect exception in _db.query: " + e.message]);
|
|
380
|
+
if (!this.isValidated) {
|
|
381
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
338
382
|
}
|
|
383
|
+
OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
384
|
+
let result = await this._query(sql);
|
|
339
385
|
OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
340
386
|
return result;
|
|
341
387
|
}
|
|
@@ -346,15 +392,11 @@ export class OINODbMsSql extends OINODb {
|
|
|
346
392
|
*
|
|
347
393
|
*/
|
|
348
394
|
async sqlExec(sql) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
try {
|
|
352
|
-
result = await this._exec(sql);
|
|
353
|
-
}
|
|
354
|
-
catch (e) {
|
|
355
|
-
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "sqlExec", "exception in SQL exec", { message: e.message, stack: e.stack });
|
|
356
|
-
result = new OINOMsSqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
395
|
+
if (!this.isValidated) {
|
|
396
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
|
|
357
397
|
}
|
|
398
|
+
OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
399
|
+
let result = await this._exec(sql);
|
|
358
400
|
OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
359
401
|
return result;
|
|
360
402
|
}
|
|
@@ -64,6 +64,16 @@ export declare class OINODbMsSql extends OINODb {
|
|
|
64
64
|
*
|
|
65
65
|
*/
|
|
66
66
|
printSqlSelect(tableName: string, columnNames: string, whereCondition: string, orderCondition: string, limitCondition: string, groupByCondition: string): string;
|
|
67
|
+
/**
|
|
68
|
+
* Print SQL select statement with DB specific formatting.
|
|
69
|
+
*
|
|
70
|
+
* @param tableName - The name of the table to select from.
|
|
71
|
+
* @param columns - The columns to be selected.
|
|
72
|
+
* @param values - The values to be inserted.
|
|
73
|
+
* @param returnIdFields - the id fields to return if returnIds is true (if supported by the database)
|
|
74
|
+
*
|
|
75
|
+
*/
|
|
76
|
+
printSqlInsert(tableName: string, columns: string, values: string, returnIdFields?: string[]): string;
|
|
67
77
|
/**
|
|
68
78
|
* Connect to database.
|
|
69
79
|
*
|
|
@@ -74,6 +84,11 @@ export declare class OINODbMsSql extends OINODb {
|
|
|
74
84
|
*
|
|
75
85
|
*/
|
|
76
86
|
validate(): Promise<OINOResult>;
|
|
87
|
+
/**
|
|
88
|
+
* Disconnect from database.
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
disconnect(): Promise<void>;
|
|
77
92
|
/**
|
|
78
93
|
* Execute a select operation.
|
|
79
94
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-mssql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "OINO TS package for using Microsoft Sql databases.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"module": "./dist/esm/index.js",
|
|
23
23
|
"types": "./dist/types/index.d.ts",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@oino-ts/db": "0.
|
|
25
|
+
"@oino-ts/db": "0.20.0",
|
|
26
26
|
"mssql": "^11.0.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
package/src/OINODbMsSql.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {ConnectionPool, config} from "mssql";
|
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
class OINOMsSqlData extends OINODbDataSet {
|
|
17
|
-
private _recordsets:
|
|
17
|
+
private _recordsets:any
|
|
18
18
|
private _rows:OINODataRow[] = OINODB_EMPTY_ROWS
|
|
19
19
|
|
|
20
20
|
private _currentRecordset: number
|
|
@@ -30,11 +30,11 @@ class OINOMsSqlData extends OINODbDataSet {
|
|
|
30
30
|
if (data == null) {
|
|
31
31
|
this.messages.push(OINO_INFO_PREFIX + "SQL result is empty")
|
|
32
32
|
|
|
33
|
-
} else if (!(Array.isArray(data) && (data.length>
|
|
33
|
+
} else if (!(Array.isArray(data)) && (data.length > 0)) {
|
|
34
34
|
throw new Error(OINO_ERROR_PREFIX + ": OINOMsSqlData constructor: invalid data!")
|
|
35
35
|
|
|
36
36
|
} else {
|
|
37
|
-
this._recordsets = data
|
|
37
|
+
this._recordsets = data
|
|
38
38
|
this._rows = this._recordsets[0]
|
|
39
39
|
}
|
|
40
40
|
if (this.isEmpty()) {
|
|
@@ -53,7 +53,7 @@ class OINOMsSqlData extends OINODbDataSet {
|
|
|
53
53
|
*
|
|
54
54
|
*/
|
|
55
55
|
isEmpty():boolean {
|
|
56
|
-
return (this._rows.length == 0)
|
|
56
|
+
return (this._recordsets.length == 0) || (this._rows == undefined) || (this._rows.length == 0)
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
@@ -145,15 +145,27 @@ export class OINODbMsSql extends OINODb {
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
private async _query(sql:string):Promise<OINOMsSqlData> {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
148
|
+
try {
|
|
149
|
+
const request = this._pool.request() // this does not need to be released but the pool will handle it
|
|
150
|
+
const sql_res = await request.query(sql)
|
|
151
|
+
// console.log("_query: result=", sql_res.recordsets, sql_res.recordsets?.length) // TODO: remove
|
|
152
|
+
return new OINOMsSqlData(sql_res.recordsets, [])
|
|
153
|
+
} catch (e:any) {
|
|
154
|
+
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "_query", "exception in SQL query", {message:e.message, stack:e.stack, sql:sql})
|
|
155
|
+
return new OINOMsSqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbMsSql._query") as OINOMsSqlData
|
|
156
|
+
}
|
|
152
157
|
}
|
|
153
158
|
|
|
154
159
|
private async _exec(sql:string):Promise<OINOMsSqlData> {
|
|
155
|
-
|
|
156
|
-
|
|
160
|
+
try {
|
|
161
|
+
const request = this._pool.request() // this does not need to be released but the pool will handle it
|
|
162
|
+
const sql_res = await request.query(sql)
|
|
163
|
+
// console.log("_exec: result=", sql_res.recordsets, sql_res.recordsets?.length) // TODO: remove
|
|
164
|
+
return new OINOMsSqlData(sql_res.recordsets, [])
|
|
165
|
+
} catch (e:any) {
|
|
166
|
+
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "_exec", "exception in SQL exec", {message:e.message, stack:e.stack, sql:sql})
|
|
167
|
+
return new OINOMsSqlData(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbMsSql._exec") as OINOMsSqlData
|
|
168
|
+
}
|
|
157
169
|
}
|
|
158
170
|
|
|
159
171
|
/**
|
|
@@ -289,12 +301,34 @@ export class OINODbMsSql extends OINODb {
|
|
|
289
301
|
return result;
|
|
290
302
|
}
|
|
291
303
|
|
|
304
|
+
/**
|
|
305
|
+
* Print SQL select statement with DB specific formatting.
|
|
306
|
+
*
|
|
307
|
+
* @param tableName - The name of the table to select from.
|
|
308
|
+
* @param columns - The columns to be selected.
|
|
309
|
+
* @param values - The values to be inserted.
|
|
310
|
+
* @param returnIdFields - the id fields to return if returnIds is true (if supported by the database)
|
|
311
|
+
*
|
|
312
|
+
*/
|
|
313
|
+
printSqlInsert(tableName:string, columns:string, values:string, returnIdFields?:string[]): string {
|
|
314
|
+
let result = "INSERT INTO " + tableName + " (" + columns + ")"
|
|
315
|
+
if (returnIdFields) {
|
|
316
|
+
result += " OUTPUT " + returnIdFields.map(f => "INSERTED."+f ).join(", ")
|
|
317
|
+
}
|
|
318
|
+
result += " VALUES (" + values + ");"
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
|
|
292
323
|
/**
|
|
293
324
|
* Connect to database.
|
|
294
325
|
*
|
|
295
326
|
*/
|
|
296
327
|
async connect(): Promise<OINOResult> {
|
|
297
328
|
let result:OINOResult = new OINOResult()
|
|
329
|
+
if (this.isConnected) {
|
|
330
|
+
return result
|
|
331
|
+
}
|
|
298
332
|
try {
|
|
299
333
|
// make sure that any items are correctly URL encoded in the connection string
|
|
300
334
|
await this._pool.connect()
|
|
@@ -321,7 +355,7 @@ export class OINODbMsSql extends OINODb {
|
|
|
321
355
|
OINOBenchmark.startMetric("OINODb", "validate")
|
|
322
356
|
try {
|
|
323
357
|
const sql = this._getValidateSql(this._params.database)
|
|
324
|
-
const sql_res:OINODbDataSet = await this.
|
|
358
|
+
const sql_res:OINODbDataSet = await this._query(sql)
|
|
325
359
|
if (sql_res.isEmpty()) {
|
|
326
360
|
result.setError(400, "DB returned no rows for select!", "OINODbMsSql.validate")
|
|
327
361
|
|
|
@@ -343,6 +377,23 @@ export class OINODbMsSql extends OINODb {
|
|
|
343
377
|
return result
|
|
344
378
|
}
|
|
345
379
|
|
|
380
|
+
/**
|
|
381
|
+
* Disconnect from database.
|
|
382
|
+
*
|
|
383
|
+
*/
|
|
384
|
+
async disconnect(): Promise<void> {
|
|
385
|
+
if (this._pool) {
|
|
386
|
+
try {
|
|
387
|
+
await this._pool.close()
|
|
388
|
+
|
|
389
|
+
} catch (e:any) {
|
|
390
|
+
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "disconnect", "exception in disconnect", {message:e.message, stack:e.stack})
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
this.isConnected = false
|
|
394
|
+
this.isValidated = false
|
|
395
|
+
}
|
|
396
|
+
|
|
346
397
|
/**
|
|
347
398
|
* Execute a select operation.
|
|
348
399
|
*
|
|
@@ -350,15 +401,11 @@ export class OINODbMsSql extends OINODb {
|
|
|
350
401
|
*
|
|
351
402
|
*/
|
|
352
403
|
async sqlSelect(sql:string): Promise<OINODbDataSet> {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
try {
|
|
356
|
-
result = await this._query(sql)
|
|
357
|
-
|
|
358
|
-
} catch (e:any) {
|
|
359
|
-
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "sqlSelect", "exception in SQL select", {message:e.message, stack:e.stack})
|
|
360
|
-
result = new OINOMsSqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMsSql.sqlSelect exception in _db.query: " + e.message])
|
|
404
|
+
if (!this.isValidated) {
|
|
405
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
|
|
361
406
|
}
|
|
407
|
+
OINOBenchmark.startMetric("OINODb", "sqlSelect")
|
|
408
|
+
let result:OINODbDataSet = await this._query(sql)
|
|
362
409
|
OINOBenchmark.endMetric("OINODb", "sqlSelect")
|
|
363
410
|
return result
|
|
364
411
|
}
|
|
@@ -370,15 +417,11 @@ export class OINODbMsSql extends OINODb {
|
|
|
370
417
|
*
|
|
371
418
|
*/
|
|
372
419
|
async sqlExec(sql:string): Promise<OINODbDataSet> {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
try {
|
|
376
|
-
result = await this._exec(sql)
|
|
377
|
-
|
|
378
|
-
} catch (e:any) {
|
|
379
|
-
OINOLog.exception("@oino-ts/db-mssql", "OINODbMsSql", "sqlExec", "exception in SQL exec", {message:e.message, stack:e.stack})
|
|
380
|
-
result = new OINOMsSqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
|
|
420
|
+
if (!this.isValidated) {
|
|
421
|
+
throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
|
|
381
422
|
}
|
|
423
|
+
OINOBenchmark.startMetric("OINODb", "sqlExec")
|
|
424
|
+
let result:OINODbDataSet = await this._exec(sql)
|
|
382
425
|
OINOBenchmark.endMetric("OINODb", "sqlExec")
|
|
383
426
|
return result
|
|
384
427
|
}
|