@oino-ts/db-bunsqlite 0.18.1 → 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.
@@ -6,6 +6,7 @@
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINODbBunSqlite = void 0;
9
+ const common_1 = require("@oino-ts/common");
9
10
  const db_1 = require("@oino-ts/db");
10
11
  const bun_sqlite_1 = require("bun:sqlite");
11
12
  /**
@@ -35,10 +36,10 @@ class OINODbBunSqlite extends db_1.OINODb {
35
36
  super(params);
36
37
  this._db = null;
37
38
  if (!this._params.url.startsWith("file://")) {
38
- throw new Error(db_1.OINO_ERROR_PREFIX + ": OINODbBunSqlite url must be a file://-url!");
39
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": OINODbBunSqlite url must be a file://-url!");
39
40
  }
40
41
  if (this._params.type !== "OINODbBunSqlite") {
41
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbBunSqlite-type: " + this._params.type);
42
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbBunSqlite-type: " + this._params.type);
42
43
  }
43
44
  }
44
45
  _parseDbFieldParams(fieldStr) {
@@ -151,8 +152,11 @@ class OINODbBunSqlite extends db_1.OINODb {
151
152
  *
152
153
  */
153
154
  async connect() {
154
- db_1.OINOBenchmark.startMetric("OINODb", "connect");
155
- let result = new db_1.OINOResult();
155
+ common_1.OINOBenchmark.startMetric("OINODb", "connect");
156
+ let result = new common_1.OINOResult();
157
+ if (this.isConnected) {
158
+ return result;
159
+ }
156
160
  const filepath = this._params.url.substring(7);
157
161
  try {
158
162
  this._db = bun_sqlite_1.Database.open(filepath, { create: true, readonly: false, readwrite: true });
@@ -160,9 +164,9 @@ class OINODbBunSqlite extends db_1.OINODb {
160
164
  }
161
165
  catch (e) {
162
166
  result.setError(500, "Exception connecting to database: " + e.message, "OINODbBunSqlite.connect");
163
- db_1.OINOLog.exception("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "connect", "exception in connect", { message: e.message, stack: e.stack });
167
+ common_1.OINOLog.exception("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "connect", "exception in connect", { message: e.message, stack: e.stack });
164
168
  }
165
- db_1.OINOBenchmark.endMetric("OINODb", "connect");
169
+ common_1.OINOBenchmark.endMetric("OINODb", "connect");
166
170
  return result;
167
171
  }
168
172
  /**
@@ -170,11 +174,15 @@ class OINODbBunSqlite extends db_1.OINODb {
170
174
  *
171
175
  */
172
176
  async validate() {
173
- db_1.OINOBenchmark.startMetric("OINODb", "validate");
174
- let result = new db_1.OINOResult();
177
+ if (!this.isConnected) {
178
+ return new common_1.OINOResult().setError(400, "Database not connected!", "OINODbBunSqlite.validate");
179
+ }
180
+ common_1.OINOBenchmark.startMetric("OINODb", "validate");
181
+ let result = new common_1.OINOResult();
175
182
  try {
183
+ this.isValidated = false;
176
184
  const sql = this._getValidateSql(this._params.database);
177
- const sql_res = await this.sqlSelect(sql);
185
+ const sql_res = await this._query(sql);
178
186
  if (sql_res.isEmpty()) {
179
187
  result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
180
188
  }
@@ -189,28 +197,67 @@ class OINODbBunSqlite extends db_1.OINODb {
189
197
  }
190
198
  }
191
199
  catch (e) {
192
- result.setError(500, db_1.OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
200
+ result.setError(500, common_1.OINO_ERROR_PREFIX + " (OINODbBunSqlite.validate): Exception in db query: " + e.message, "OINODbBunSqlite.validate");
193
201
  }
194
- db_1.OINOBenchmark.endMetric("OINODb", "validate");
202
+ common_1.OINOBenchmark.endMetric("OINODb", "validate");
195
203
  return result;
196
204
  }
197
205
  /**
198
- * Execute a select operation.
199
- *
200
- * @param sql SQL statement.
206
+ * Connect to database.
201
207
  *
202
208
  */
203
- async sqlSelect(sql) {
204
- db_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
209
+ async disconnect() {
210
+ this.isConnected = false;
211
+ this.isValidated = false;
212
+ }
213
+ async _query(sql) {
205
214
  let result;
206
215
  try {
207
- result = new OINOBunSqliteDataset(this._db?.query(sql).values(), []);
216
+ const sql_res = this._db?.query(sql).values();
217
+ if (sql_res) {
218
+ // console.log("OINODbBunSqlite._query: res", sql_res)
219
+ result = new OINOBunSqliteDataset(sql_res, []);
220
+ }
221
+ else {
222
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []);
223
+ }
208
224
  }
209
225
  catch (e) {
210
- result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message]);
226
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + " (OINODbBunSqlite._query): Exception in db query: " + e.message, "OINODbBunSqlite._query");
211
227
  }
212
- db_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
213
- return Promise.resolve(result);
228
+ return result;
229
+ }
230
+ async _exec(sql) {
231
+ let result;
232
+ try {
233
+ const sql_res = this._db?.query(sql).values();
234
+ if (sql_res) {
235
+ // console.log("OINODbBunSqlite._exec: res", sql_res)
236
+ result = new OINOBunSqliteDataset(sql_res, []);
237
+ }
238
+ else {
239
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []);
240
+ }
241
+ }
242
+ catch (e) {
243
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbBunSqlite._exec");
244
+ }
245
+ return result;
246
+ }
247
+ /**
248
+ * Execute a select operation.
249
+ *
250
+ * @param sql SQL statement.
251
+ *
252
+ */
253
+ async sqlSelect(sql) {
254
+ if (!this.isValidated) {
255
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
256
+ }
257
+ common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
258
+ let result = await this._query(sql);
259
+ common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
260
+ return result;
214
261
  }
215
262
  /**
216
263
  * Execute other sql operations.
@@ -219,17 +266,13 @@ class OINODbBunSqlite extends db_1.OINODb {
219
266
  *
220
267
  */
221
268
  async sqlExec(sql) {
222
- db_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
223
- let result;
224
- try {
225
- this._db?.exec(sql);
226
- result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []);
227
- }
228
- catch (e) {
229
- result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"]);
269
+ if (!this.isValidated) {
270
+ return new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (OINODbBunSqlite.sqlExec): Database connection not validated!"]);
230
271
  }
231
- db_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
232
- return Promise.resolve(result);
272
+ common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
273
+ let result = await this._exec(sql);
274
+ common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
275
+ return result;
233
276
  }
234
277
  _getSchemaSql(dbName, tableName) {
235
278
  const sql = "SELECT sql from sqlite_schema WHERE name='" + tableName + "'";
@@ -248,7 +291,7 @@ class OINODbBunSqlite extends db_1.OINODb {
248
291
  */
249
292
  async initializeApiDatamodel(api) {
250
293
  const schema_sql = this._getSchemaSql(this._params.database, api.params.tableName);
251
- const res = await this.sqlSelect(schema_sql);
294
+ const res = await this._query(schema_sql);
252
295
  const sql_desc = (res?.getRow()[0]);
253
296
  const excluded_fields = [];
254
297
  let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc);
@@ -256,7 +299,7 @@ class OINODbBunSqlite extends db_1.OINODb {
256
299
  throw new Error("Table " + api.params.tableName + " not recognized as a valid Sqlite table!");
257
300
  }
258
301
  else {
259
- let field_strings = db_1.OINOStr.splitExcludingBrackets(table_matches[1], ',', '(', ')');
302
+ let field_strings = common_1.OINOStr.splitExcludingBrackets(table_matches[1], ',', '(', ')');
260
303
  for (let field_str of field_strings) {
261
304
  field_str = field_str.trim();
262
305
  let field_params = this._parseDbFieldParams(field_str);
@@ -270,7 +313,7 @@ class OINODbBunSqlite extends db_1.OINODb {
270
313
  for (let i = 0; i < primary_keys.length; i++) {
271
314
  const pk = primary_keys[i].trim(); //..the trim
272
315
  if (excluded_fields.indexOf(pk) >= 0) {
273
- throw new Error(db_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + pk);
316
+ throw new Error(common_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + pk);
274
317
  }
275
318
  for (let j = 0; j < api.datamodel.fields.length; j++) {
276
319
  if (api.datamodel.fields[j].name == pk) {
@@ -288,7 +331,7 @@ class OINODbBunSqlite extends db_1.OINODb {
288
331
  }
289
332
  }
290
333
  else {
291
- db_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "Unsupported field definition skipped.", { field: field_str });
334
+ common_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "Unsupported field definition skipped.", { field: field_str });
292
335
  }
293
336
  }
294
337
  else {
@@ -298,7 +341,7 @@ class OINODbBunSqlite extends db_1.OINODb {
298
341
  const field_length = parseInt(field_match[4]) || 0;
299
342
  if (api.isFieldIncluded(field_name) == false) {
300
343
  excluded_fields.push(field_name);
301
- db_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "Field excluded in API parameters.", { field: field_name });
344
+ common_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "Field excluded in API parameters.", { field: field_name });
302
345
  }
303
346
  else {
304
347
  if ((sql_type == "INTEGER") || (sql_type == "REAL") || (sql_type == "DOUBLE") || (sql_type == "NUMERIC") || (sql_type == "DECIMAL")) {
@@ -322,14 +365,14 @@ class OINODbBunSqlite extends db_1.OINODb {
322
365
  api.datamodel.addField(new db_1.OINOBooleanDataField(this, field_name, sql_type, field_params));
323
366
  }
324
367
  else {
325
- db_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
368
+ common_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
326
369
  api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
327
370
  }
328
371
  }
329
372
  }
330
373
  }
331
374
  ;
332
- db_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
375
+ common_1.OINOLog.info("@oino-ts/db-bunsqlite", "OINODbBunSqlite", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
333
376
  return Promise.resolve();
334
377
  }
335
378
  }
@@ -3,7 +3,8 @@
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, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINODbMemoryDataSet, OINOBenchmark, OINOBlobDataField, OINODatetimeDataField, OINOStr, OINOLog, OINOResult, OINODB_EMPTY_ROWS } from "@oino-ts/db";
6
+ import { OINO_ERROR_PREFIX, OINOBenchmark, OINOStr, OINOLog, OINOResult } from "@oino-ts/common";
7
+ import { OINODb, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbMemoryDataSet, OINOBlobDataField, OINODatetimeDataField, OINODB_EMPTY_ROWS } from "@oino-ts/db";
7
8
  import { Database as BunSqliteDb } from "bun:sqlite";
8
9
  /**
9
10
  * Implmentation of OINODbDataSet for BunSqlite.
@@ -150,6 +151,9 @@ export class OINODbBunSqlite extends OINODb {
150
151
  async connect() {
151
152
  OINOBenchmark.startMetric("OINODb", "connect");
152
153
  let result = new OINOResult();
154
+ if (this.isConnected) {
155
+ return result;
156
+ }
153
157
  const filepath = this._params.url.substring(7);
154
158
  try {
155
159
  this._db = BunSqliteDb.open(filepath, { create: true, readonly: false, readwrite: true });
@@ -167,11 +171,15 @@ export class OINODbBunSqlite extends OINODb {
167
171
  *
168
172
  */
169
173
  async validate() {
174
+ if (!this.isConnected) {
175
+ return new OINOResult().setError(400, "Database not connected!", "OINODbBunSqlite.validate");
176
+ }
170
177
  OINOBenchmark.startMetric("OINODb", "validate");
171
178
  let result = new OINOResult();
172
179
  try {
180
+ this.isValidated = false;
173
181
  const sql = this._getValidateSql(this._params.database);
174
- const sql_res = await this.sqlSelect(sql);
182
+ const sql_res = await this._query(sql);
175
183
  if (sql_res.isEmpty()) {
176
184
  result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
177
185
  }
@@ -186,28 +194,67 @@ export class OINODbBunSqlite extends OINODb {
186
194
  }
187
195
  }
188
196
  catch (e) {
189
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
197
+ result.setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite.validate): Exception in db query: " + e.message, "OINODbBunSqlite.validate");
190
198
  }
191
199
  OINOBenchmark.endMetric("OINODb", "validate");
192
200
  return result;
193
201
  }
194
202
  /**
195
- * Execute a select operation.
196
- *
197
- * @param sql SQL statement.
203
+ * Connect to database.
198
204
  *
199
205
  */
200
- async sqlSelect(sql) {
201
- OINOBenchmark.startMetric("OINODb", "sqlSelect");
206
+ async disconnect() {
207
+ this.isConnected = false;
208
+ this.isValidated = false;
209
+ }
210
+ async _query(sql) {
202
211
  let result;
203
212
  try {
204
- result = new OINOBunSqliteDataset(this._db?.query(sql).values(), []);
213
+ const sql_res = this._db?.query(sql).values();
214
+ if (sql_res) {
215
+ // console.log("OINODbBunSqlite._query: res", sql_res)
216
+ result = new OINOBunSqliteDataset(sql_res, []);
217
+ }
218
+ else {
219
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []);
220
+ }
205
221
  }
206
222
  catch (e) {
207
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message]);
223
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite._query): Exception in db query: " + e.message, "OINODbBunSqlite._query");
224
+ }
225
+ return result;
226
+ }
227
+ async _exec(sql) {
228
+ let result;
229
+ try {
230
+ const sql_res = this._db?.query(sql).values();
231
+ if (sql_res) {
232
+ // console.log("OINODbBunSqlite._exec: res", sql_res)
233
+ result = new OINOBunSqliteDataset(sql_res, []);
234
+ }
235
+ else {
236
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []);
237
+ }
208
238
  }
239
+ catch (e) {
240
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbBunSqlite._exec");
241
+ }
242
+ return result;
243
+ }
244
+ /**
245
+ * Execute a select operation.
246
+ *
247
+ * @param sql SQL statement.
248
+ *
249
+ */
250
+ async sqlSelect(sql) {
251
+ if (!this.isValidated) {
252
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
253
+ }
254
+ OINOBenchmark.startMetric("OINODb", "sqlSelect");
255
+ let result = await this._query(sql);
209
256
  OINOBenchmark.endMetric("OINODb", "sqlSelect");
210
- return Promise.resolve(result);
257
+ return result;
211
258
  }
212
259
  /**
213
260
  * Execute other sql operations.
@@ -216,17 +263,13 @@ export class OINODbBunSqlite extends OINODb {
216
263
  *
217
264
  */
218
265
  async sqlExec(sql) {
219
- OINOBenchmark.startMetric("OINODb", "sqlExec");
220
- let result;
221
- try {
222
- this._db?.exec(sql);
223
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []);
224
- }
225
- catch (e) {
226
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"]);
266
+ if (!this.isValidated) {
267
+ return new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (OINODbBunSqlite.sqlExec): Database connection not validated!"]);
227
268
  }
269
+ OINOBenchmark.startMetric("OINODb", "sqlExec");
270
+ let result = await this._exec(sql);
228
271
  OINOBenchmark.endMetric("OINODb", "sqlExec");
229
- return Promise.resolve(result);
272
+ return result;
230
273
  }
231
274
  _getSchemaSql(dbName, tableName) {
232
275
  const sql = "SELECT sql from sqlite_schema WHERE name='" + tableName + "'";
@@ -245,7 +288,7 @@ export class OINODbBunSqlite extends OINODb {
245
288
  */
246
289
  async initializeApiDatamodel(api) {
247
290
  const schema_sql = this._getSchemaSql(this._params.database, api.params.tableName);
248
- const res = await this.sqlSelect(schema_sql);
291
+ const res = await this._query(schema_sql);
249
292
  const sql_desc = (res?.getRow()[0]);
250
293
  const excluded_fields = [];
251
294
  let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc);
@@ -1,4 +1,5 @@
1
- import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell, OINOResult } from "@oino-ts/db";
1
+ import { OINOResult } from "@oino-ts/common";
2
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
2
3
  /**
3
4
  * Implementation of BunSqlite-database.
4
5
  *
@@ -64,6 +65,13 @@ export declare class OINODbBunSqlite extends OINODb {
64
65
  *
65
66
  */
66
67
  validate(): Promise<OINOResult>;
68
+ /**
69
+ * Connect to database.
70
+ *
71
+ */
72
+ disconnect(): Promise<void>;
73
+ private _query;
74
+ private _exec;
67
75
  /**
68
76
  * Execute a select operation.
69
77
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-bunsqlite",
3
- "version": "0.18.1",
3
+ "version": "0.20.0",
4
4
  "description": "OINO TS package for using Bun Sqlite databases.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -20,7 +20,8 @@
20
20
  "module": "./dist/esm/index.js",
21
21
  "types": "./dist/types/index.d.ts",
22
22
  "dependencies": {
23
- "@oino-ts/db": "^0.18.1"
23
+ "@oino-ts/common": "^0.20.0",
24
+ "@oino-ts/db": "^0.20.0"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/bun": "latest",
@@ -4,7 +4,8 @@
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, OINODbMemoryDataSet, OINODataCell, OINOBenchmark, OINOBlobDataField, OINODatetimeDataField, OINOStr, OINOLog, OINOResult, OINODB_EMPTY_ROWS } from "@oino-ts/db";
7
+ import { OINO_ERROR_PREFIX, OINOBenchmark, OINOStr, OINOLog, OINOResult } from "@oino-ts/common";
8
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINODbMemoryDataSet, OINODataCell, OINOBlobDataField, OINODatetimeDataField, OINODB_EMPTY_ROWS } from "@oino-ts/db";
8
9
 
9
10
  import { Database as BunSqliteDb } from "bun:sqlite";
10
11
 
@@ -162,6 +163,9 @@ export class OINODbBunSqlite extends OINODb {
162
163
  async connect(): Promise<OINOResult> {
163
164
  OINOBenchmark.startMetric("OINODb", "connect")
164
165
  let result:OINOResult = new OINOResult()
166
+ if (this.isConnected) {
167
+ return result
168
+ }
165
169
  const filepath:string = this._params.url.substring(7)
166
170
  try {
167
171
  this._db = BunSqliteDb.open(filepath, { create: true, readonly: false, readwrite: true })
@@ -179,11 +183,15 @@ export class OINODbBunSqlite extends OINODb {
179
183
  *
180
184
  */
181
185
  async validate(): Promise<OINOResult> {
186
+ if (!this.isConnected) {
187
+ return new OINOResult().setError(400, "Database not connected!", "OINODbBunSqlite.validate")
188
+ }
182
189
  OINOBenchmark.startMetric("OINODb", "validate")
183
190
  let result:OINOResult = new OINOResult()
184
191
  try {
192
+ this.isValidated = false
185
193
  const sql = this._getValidateSql(this._params.database)
186
- const sql_res:OINODbDataSet = await this.sqlSelect(sql)
194
+ const sql_res:OINODbDataSet = await this._query(sql)
187
195
  if (sql_res.isEmpty()) {
188
196
  result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate")
189
197
 
@@ -197,29 +205,69 @@ export class OINODbBunSqlite extends OINODb {
197
205
  this.isValidated = true
198
206
  }
199
207
  } catch (e:any) {
200
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate")
208
+ result.setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite.validate): Exception in db query: " + e.message, "OINODbBunSqlite.validate")
201
209
  }
202
210
  OINOBenchmark.endMetric("OINODb", "validate")
203
211
  return result
204
212
  }
205
213
 
206
214
  /**
207
- * Execute a select operation.
208
- *
209
- * @param sql SQL statement.
215
+ * Connect to database.
210
216
  *
211
217
  */
212
- async sqlSelect(sql:string): Promise<OINODbDataSet> {
213
- OINOBenchmark.startMetric("OINODb", "sqlSelect")
218
+ async disconnect(): Promise<void> {
219
+ this.isConnected = false
220
+ this.isValidated = false
221
+ }
222
+
223
+
224
+ private async _query(sql:string): Promise<OINODbDataSet> {
225
+ let result:OINODbDataSet
226
+ try {
227
+ const sql_res = this._db?.query(sql).values()
228
+ if (sql_res) {
229
+ // console.log("OINODbBunSqlite._query: res", sql_res)
230
+ result = new OINOBunSqliteDataset(sql_res, [])
231
+ } else {
232
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [])
233
+ }
234
+
235
+ } catch (e:any) {
236
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite._query): Exception in db query: " + e.message, "OINODbBunSqlite._query") as OINOBunSqliteDataset
237
+ }
238
+ return result
239
+ }
240
+ private async _exec(sql:string): Promise<OINODbDataSet> {
214
241
  let result:OINODbDataSet
215
242
  try {
216
- result = new OINOBunSqliteDataset(this._db?.query(sql).values(), [])
243
+ const sql_res = this._db?.query(sql).values()
244
+ if (sql_res) {
245
+ // console.log("OINODbBunSqlite._exec: res", sql_res)
246
+ result = new OINOBunSqliteDataset(sql_res, [])
247
+ } else {
248
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [])
249
+ }
217
250
 
218
251
  } catch (e:any) {
219
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message])
252
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbBunSqlite._exec") as OINOBunSqliteDataset
220
253
  }
254
+ return result
255
+ }
256
+
257
+ /**
258
+ * Execute a select operation.
259
+ *
260
+ * @param sql SQL statement.
261
+ *
262
+ */
263
+ async sqlSelect(sql:string): Promise<OINODbDataSet> {
264
+ if (!this.isValidated) {
265
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
266
+ }
267
+ OINOBenchmark.startMetric("OINODb", "sqlSelect")
268
+ let result:OINODbDataSet = await this._query(sql)
221
269
  OINOBenchmark.endMetric("OINODb", "sqlSelect")
222
- return Promise.resolve(result)
270
+ return result
223
271
  }
224
272
 
225
273
  /**
@@ -229,17 +277,13 @@ export class OINODbBunSqlite extends OINODb {
229
277
  *
230
278
  */
231
279
  async sqlExec(sql:string): Promise<OINODbDataSet> {
232
- OINOBenchmark.startMetric("OINODb", "sqlExec")
233
- let result:OINODbDataSet
234
- try {
235
- this._db?.exec(sql)
236
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [])
237
-
238
- } catch (e:any) {
239
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"])
280
+ if (!this.isValidated) {
281
+ return new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (OINODbBunSqlite.sqlExec): Database connection not validated!"])
240
282
  }
283
+ OINOBenchmark.startMetric("OINODb", "sqlExec")
284
+ let result:OINODbDataSet = await this._exec(sql)
241
285
  OINOBenchmark.endMetric("OINODb", "sqlExec")
242
- return Promise.resolve(result)
286
+ return result
243
287
  }
244
288
 
245
289
  private _getSchemaSql(dbName:string, tableName:string):string {
@@ -261,7 +305,7 @@ export class OINODbBunSqlite extends OINODb {
261
305
  */
262
306
  async initializeApiDatamodel(api:OINODbApi): Promise<void> {
263
307
  const schema_sql:string = this._getSchemaSql(this._params.database, api.params.tableName)
264
- const res:OINODbDataSet|null = await this.sqlSelect(schema_sql)
308
+ const res:OINODbDataSet|null = await this._query(schema_sql)
265
309
  const sql_desc:string = (res?.getRow()[0]) as string
266
310
  const excluded_fields:string[] = []
267
311
  let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc)