@oino-ts/common 0.21.1 → 1.0.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.
Files changed (72) hide show
  1. package/README.md +183 -0
  2. package/dist/cjs/OINOApi.js +322 -0
  3. package/dist/cjs/OINOBenchmark.js +3 -4
  4. package/dist/cjs/OINOConfig.js +104 -0
  5. package/dist/cjs/OINOConstants.js +42 -0
  6. package/dist/cjs/OINODataField.js +346 -0
  7. package/dist/cjs/OINODataModel.js +182 -0
  8. package/dist/cjs/OINODataSource.js +165 -0
  9. package/dist/cjs/OINOFormatter.js +6 -5
  10. package/dist/cjs/OINOHtmlTemplate.js +21 -18
  11. package/dist/cjs/OINOModelSet.js +333 -0
  12. package/dist/cjs/OINOParser.js +448 -0
  13. package/dist/cjs/OINOQueryParams.js +434 -0
  14. package/dist/cjs/OINORequest.js +21 -13
  15. package/dist/cjs/OINOResult.js +13 -12
  16. package/dist/cjs/OINOStr.js +11 -11
  17. package/dist/cjs/OINOSwagger.js +205 -0
  18. package/dist/cjs/index.js +57 -39
  19. package/dist/esm/OINOApi.js +315 -0
  20. package/dist/esm/OINOBenchmark.js +3 -4
  21. package/dist/esm/OINOConfig.js +100 -0
  22. package/dist/esm/OINOConstants.js +39 -0
  23. package/dist/esm/OINODataField.js +337 -0
  24. package/dist/esm/OINODataModel.js +178 -0
  25. package/dist/esm/OINODataSource.js +159 -0
  26. package/dist/esm/OINOFormatter.js +2 -1
  27. package/dist/esm/OINOHtmlTemplate.js +4 -1
  28. package/dist/esm/OINOModelSet.js +329 -0
  29. package/dist/esm/OINOParser.js +444 -0
  30. package/dist/esm/OINOQueryParams.js +426 -0
  31. package/dist/esm/OINORequest.js +9 -1
  32. package/dist/esm/OINOResult.js +2 -1
  33. package/dist/esm/OINOStr.js +1 -1
  34. package/dist/esm/OINOSwagger.js +201 -0
  35. package/dist/esm/index.js +14 -32
  36. package/dist/types/OINOApi.d.ts +191 -0
  37. package/dist/types/OINOBenchmark.d.ts +1 -1
  38. package/dist/types/OINOConfig.d.ts +63 -0
  39. package/dist/types/OINOConstants.d.ts +51 -0
  40. package/dist/types/OINODataField.d.ts +209 -0
  41. package/dist/types/OINODataModel.d.ts +78 -0
  42. package/dist/types/OINODataSource.d.ts +184 -0
  43. package/dist/types/OINOHtmlTemplate.d.ts +1 -1
  44. package/dist/types/OINOModelSet.d.ts +64 -0
  45. package/dist/types/OINOParser.d.ts +42 -0
  46. package/dist/types/OINOQueryParams.d.ts +270 -0
  47. package/dist/types/OINORequest.d.ts +4 -1
  48. package/dist/types/OINOResult.d.ts +1 -1
  49. package/dist/types/OINOStr.d.ts +1 -1
  50. package/dist/types/OINOSwagger.d.ts +25 -0
  51. package/dist/types/index.d.ts +14 -31
  52. package/package.json +32 -32
  53. package/src/OINOApi.ts +429 -0
  54. package/src/OINOBenchmark.ts +323 -324
  55. package/src/OINOConfig.ts +113 -0
  56. package/src/OINOConstants.ts +59 -0
  57. package/src/OINODataField.ts +371 -0
  58. package/src/OINODataModel.ts +187 -0
  59. package/src/OINODataSource.ts +280 -0
  60. package/src/OINOFormatter.ts +166 -165
  61. package/src/OINOHeaders.ts +51 -51
  62. package/src/OINOHtmlTemplate.test.ts +114 -114
  63. package/src/OINOHtmlTemplate.ts +225 -222
  64. package/src/OINOLog.ts +292 -292
  65. package/src/OINOModelSet.ts +359 -0
  66. package/src/OINOParser.ts +441 -0
  67. package/src/OINOQueryParams.ts +449 -0
  68. package/src/OINORequest.ts +204 -196
  69. package/src/OINOResult.ts +331 -330
  70. package/src/OINOStr.ts +254 -254
  71. package/src/OINOSwagger.ts +213 -0
  72. package/src/index.ts +18 -38
@@ -0,0 +1,346 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINOStringDataField = exports.OINODataField = void 0;
9
+ const node_buffer_1 = require("node:buffer");
10
+ const OINOConstants_js_1 = require("./OINOConstants.js");
11
+ const OINOLog_js_1 = require("./OINOLog.js");
12
+ /**
13
+ * Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
14
+ *
15
+ */
16
+ class OINODataField {
17
+ /** OINO data source reference*/
18
+ datasource;
19
+ /** Name of the field */
20
+ name;
21
+ /** Internal type of field*/
22
+ type;
23
+ /** SQL type of the field */
24
+ nativeType;
25
+ /** Maximum length of the field (or 0) */
26
+ maxLength;
27
+ /** Parameters for the field */
28
+ fieldParams;
29
+ /**
30
+ * Constructor for a data field
31
+ *
32
+ * @param datasource OINO data source reference
33
+ * @param name name of the field
34
+ * @param type internal type of the field
35
+ * @param nativeType column type in database
36
+ * @param fieldParams parameters of the field
37
+ * @param maxLength maximum length of the field (or 0)
38
+ *
39
+ */
40
+ constructor(datasource, name, type, nativeType, fieldParams, maxLength = 0) {
41
+ this.datasource = datasource;
42
+ this.name = name;
43
+ this.type = type;
44
+ this.maxLength = maxLength;
45
+ this.nativeType = nativeType;
46
+ this.fieldParams = fieldParams;
47
+ }
48
+ /**
49
+ * Serialize cell value in the given content format.
50
+ *
51
+ * @param cellVal cell value
52
+ *
53
+ */
54
+ serializeCell(cellVal) {
55
+ cellVal = this.datasource.parseValueAsCell(cellVal, this.nativeType);
56
+ if ((cellVal === null) || (cellVal === undefined)) {
57
+ return cellVal; // let content type encoder worry what to do with the value (so not force it to string)
58
+ }
59
+ else {
60
+ return cellVal.toString();
61
+ }
62
+ }
63
+ /**
64
+ * Parce cell value from string using field type specific formatting rules.
65
+ *
66
+ * @param value string value
67
+ *
68
+ */
69
+ deserializeCell(value) {
70
+ return value;
71
+ }
72
+ /**
73
+ * Print data cell (from deserialization) as SQL-string.
74
+ *
75
+ * @param cellVal cell value
76
+ *
77
+ */
78
+ printCellAsValue(cellVal) {
79
+ return this.datasource.printCellAsValue(cellVal, this.nativeType);
80
+ }
81
+ /**
82
+ * Print name of column as SQL.
83
+ *
84
+ */
85
+ printColumnName() {
86
+ return this.datasource.printColumnName(this.name);
87
+ }
88
+ }
89
+ exports.OINODataField = OINODataField;
90
+ /**
91
+ * Specialised class for a string column.
92
+ *
93
+ */
94
+ class OINOStringDataField extends OINODataField {
95
+ /**
96
+ * Constructor for a string data field
97
+ *
98
+ * @param datasource OINO data source reference
99
+ * @param name name of the field
100
+ * @param nativeType column type in database
101
+ * @param fieldParams parameters of the field
102
+ * @param maxLength maximum length of the field (or 0)
103
+ *
104
+ */
105
+ constructor(datasource, name, nativeType, fieldParams, maxLength) {
106
+ super(datasource, name, "string", nativeType, fieldParams, maxLength);
107
+ }
108
+ }
109
+ exports.OINOStringDataField = OINOStringDataField;
110
+ /**
111
+ * Specialised class for a boolean column.
112
+ *
113
+ */
114
+ class OINOBooleanDataField extends OINODataField {
115
+ /**
116
+ * Constructor for a boolean data field
117
+ *
118
+ * @param datasource OINO data source reference
119
+ * @param name name of the field
120
+ * @param nativeType column type in database
121
+ * @param fieldParams parameters of the field
122
+ *
123
+ */
124
+ constructor(datasource, name, nativeType, fieldParams) {
125
+ super(datasource, name, "boolean", nativeType, fieldParams);
126
+ }
127
+ /**
128
+ * Serialize cell value in the given content format.
129
+ *
130
+ * @param cellVal cell value
131
+ *
132
+ */
133
+ serializeCell(cellVal) {
134
+ const parsed_value = (this.datasource.parseValueAsCell(cellVal, this.nativeType) || "").toString();
135
+ let result;
136
+ // console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
137
+ if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
138
+ result = "false";
139
+ }
140
+ else {
141
+ result = "true";
142
+ }
143
+ return result;
144
+ }
145
+ /**
146
+ * Parce cell value from string using field type specific formatting rules.
147
+ *
148
+ * @param value string value
149
+ *
150
+ */
151
+ deserializeCell(value) {
152
+ if (value == null || value == "" || value.toString().toLowerCase() == "false" || value == "0") { // TODO: testaa poistaa .toString()
153
+ return false;
154
+ }
155
+ else {
156
+ return true;
157
+ }
158
+ }
159
+ }
160
+ exports.OINOBooleanDataField = OINOBooleanDataField;
161
+ /**
162
+ * Specialised class for a number column.
163
+ *
164
+ */
165
+ class OINONumberDataField extends OINODataField {
166
+ /**
167
+ * Constructor for a string data field
168
+ *
169
+ * @param datasource OINO data source reference
170
+ * @param name name of the field
171
+ * @param nativeType column type in database
172
+ * @param fieldParams parameters of the field
173
+ *
174
+ */
175
+ constructor(datasource, name, nativeType, fieldParams) {
176
+ super(datasource, name, "number", nativeType, fieldParams);
177
+ }
178
+ /**
179
+ * Serialize cell value in the given content format.
180
+ *
181
+ * @param cellVal cell value
182
+ *
183
+ */
184
+ serializeCell(cellVal) {
185
+ let result;
186
+ if ((cellVal === null) || (cellVal === undefined) || (cellVal === "")) {
187
+ result = null;
188
+ }
189
+ else {
190
+ result = cellVal.toString();
191
+ }
192
+ return result;
193
+ }
194
+ /**
195
+ * Parce cell value from string using field type specific formatting rules.
196
+ *
197
+ * @param value string value
198
+ *
199
+ */
200
+ deserializeCell(value) {
201
+ if (value === undefined) {
202
+ return undefined;
203
+ }
204
+ else if ((value === "") || (value === null)) {
205
+ return null;
206
+ }
207
+ else {
208
+ const result = parseFloat(value);
209
+ if (isNaN(result)) {
210
+ OINOLog_js_1.OINOLog.error("@oino-ts/db", "OINONumberDataField", "toSql", "Invalid value!", { value: value });
211
+ throw new Error(OINOConstants_js_1.OINO_ERROR_PREFIX + ": OINONumberDataField.deserializeCell - Invalid value '" + value + "'"); // incorrectly formatted data could be a security risk, abort processing
212
+ }
213
+ return result;
214
+ }
215
+ }
216
+ }
217
+ exports.OINONumberDataField = OINONumberDataField;
218
+ /**
219
+ * Specialised class for a blob column.
220
+ *
221
+ */
222
+ class OINOBlobDataField extends OINODataField {
223
+ /**
224
+ * Constructor for a blob data field
225
+ *
226
+ * @param datasource OINO data source reference
227
+ * @param name name of the field
228
+ * @param nativeType column type in database
229
+ * @param fieldParams parameters of the field
230
+ * @param maxLength maximum length of the field (or 0)
231
+ *
232
+ */
233
+ constructor(datasource, name, nativeType, fieldParams, maxLength) {
234
+ super(datasource, name, "blob", nativeType, fieldParams, maxLength);
235
+ }
236
+ /**
237
+ * Serialize cell value in the given content format.
238
+ *
239
+ * @param cellVal cell value
240
+ *
241
+ */
242
+ serializeCell(cellVal) {
243
+ // console.log("OINOBlobDataField.serializeCell: cellVal", cellVal, typeof(cellVal))
244
+ if ((cellVal === null) || (cellVal === undefined)) {
245
+ return cellVal;
246
+ }
247
+ else if (cellVal instanceof node_buffer_1.Buffer) {
248
+ return cellVal.toString('base64');
249
+ }
250
+ else if (cellVal instanceof Uint8Array) {
251
+ return node_buffer_1.Buffer.from(cellVal).toString('base64');
252
+ }
253
+ else {
254
+ return this.datasource.parseValueAsCell(cellVal, this.nativeType)?.toString();
255
+ }
256
+ }
257
+ /**
258
+ * Parce cell value from string using field type specific formatting rules.
259
+ *
260
+ * @param value string value
261
+ *
262
+ */
263
+ deserializeCell(value) {
264
+ if (value == null) {
265
+ return node_buffer_1.Buffer.alloc(0);
266
+ }
267
+ else {
268
+ return node_buffer_1.Buffer.from(value, 'base64'); // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
269
+ }
270
+ }
271
+ }
272
+ exports.OINOBlobDataField = OINOBlobDataField;
273
+ /**
274
+ * Specialised class for a datetime column.
275
+ *
276
+ */
277
+ class OINODatetimeDataField extends OINODataField {
278
+ /**
279
+ * Constructor for a string data field
280
+ *
281
+ * @param datasource OINO data source reference
282
+ * @param name name of the field
283
+ * @param nativeType column type in database
284
+ * @param fieldParams parameters of the field
285
+ *
286
+ */
287
+ constructor(datasource, name, nativeType, fieldParams) {
288
+ super(datasource, name, "datetime", nativeType, fieldParams);
289
+ }
290
+ /**
291
+ * Serialize cell value in the given content format.
292
+ *
293
+ * @param cellVal cell value
294
+ *
295
+ */
296
+ serializeCell(cellVal) {
297
+ if (typeof (cellVal) == "string") {
298
+ cellVal = this.datasource.parseValueAsCell(cellVal, this.nativeType);
299
+ }
300
+ if ((cellVal === null) || (cellVal === undefined)) {
301
+ return cellVal;
302
+ }
303
+ else if (cellVal instanceof Date) {
304
+ return cellVal.toISOString();
305
+ }
306
+ else {
307
+ return cellVal.toString();
308
+ }
309
+ }
310
+ /**
311
+ * Serialize cell value in the given content format.
312
+ *
313
+ * @param cellVal cell value
314
+ * @param locale locale-object to format datetimes with
315
+ *
316
+ */
317
+ serializeCellWithLocale(cellVal, locale) {
318
+ if (typeof (cellVal) == "string") {
319
+ cellVal = this.datasource.parseValueAsCell(cellVal, this.nativeType);
320
+ }
321
+ if ((cellVal === null) || (cellVal === undefined)) {
322
+ return cellVal;
323
+ }
324
+ else if (cellVal instanceof Date) {
325
+ return locale.format(cellVal);
326
+ }
327
+ else {
328
+ return cellVal.toString();
329
+ }
330
+ }
331
+ /**
332
+ * Parce cell value from string using field type specific formatting rules.
333
+ *
334
+ * @param value string value
335
+ *
336
+ */
337
+ deserializeCell(value) {
338
+ if ((value === null) || (value === undefined)) {
339
+ return value;
340
+ }
341
+ else {
342
+ return new Date(value);
343
+ }
344
+ }
345
+ }
346
+ exports.OINODatetimeDataField = OINODatetimeDataField;
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.OINODataModel = void 0;
9
+ const OINODataField_js_1 = require("./OINODataField.js");
10
+ /**
11
+ * OINO Datamodel object for representing one database table and it's columns.
12
+ *
13
+ */
14
+ class OINODataModel {
15
+ _fieldIndexLookup;
16
+ /** Database refererence of the table */
17
+ api;
18
+ /** Field refererences of the API */
19
+ fields;
20
+ /**
21
+ * Constructor of the data model.
22
+ * NOTE! OINODbDataModel.initialize must be called after constructor to populate fields.
23
+ *
24
+ * @param api api of the data model
25
+ *
26
+ */
27
+ constructor(api) {
28
+ this._fieldIndexLookup = {};
29
+ this.api = api;
30
+ this.fields = [];
31
+ }
32
+ /**
33
+ * Add a field to the datamodel.
34
+ *
35
+ * @param field dataset field
36
+ *
37
+ */
38
+ addField(field) {
39
+ this.fields.push(field);
40
+ this._fieldIndexLookup[field.name] = this.fields.length - 1;
41
+ }
42
+ /**
43
+ * Find a field of a given name if any.
44
+ *
45
+ * @param name name of the field to find
46
+ *
47
+ */
48
+ findFieldByName(name) {
49
+ const i = this._fieldIndexLookup[name];
50
+ if (i >= 0) {
51
+ return this.fields[i];
52
+ }
53
+ else {
54
+ return null;
55
+ }
56
+ }
57
+ /**
58
+ * Find index of a field of a given name if any.
59
+ *
60
+ * @param name name of the field to find
61
+ *
62
+ */
63
+ findFieldIndexByName(name) {
64
+ const i = this._fieldIndexLookup[name];
65
+ if (i >= 0) {
66
+ return i;
67
+ }
68
+ else {
69
+ return -1;
70
+ }
71
+ }
72
+ /**
73
+ * Find all fields based of given filter callback criteria (e.g. fields of certain data type, primary keys etc.)
74
+ *
75
+ * @param filter callback called for each field to include or not
76
+ *
77
+ */
78
+ filterFields(filter) {
79
+ let result = [];
80
+ for (let f of this.fields) {
81
+ if (filter(f)) {
82
+ result.push(f);
83
+ }
84
+ }
85
+ return result;
86
+ }
87
+ /**
88
+ * Return the primary key values of one row in order of the data model
89
+ *
90
+ * @param row data row
91
+ * @param hashidValues apply hashid when applicable
92
+ *
93
+ */
94
+ getRowPrimarykeyValues(row, hashidValues = false) {
95
+ let values = [];
96
+ for (let i = 0; i < this.fields.length; i++) {
97
+ const f = this.fields[i];
98
+ if (f.fieldParams.isPrimaryKey) {
99
+ const value = row[i]?.toString() || "";
100
+ if (hashidValues && value && (f instanceof OINODataField_js_1.OINONumberDataField) && this.api.hashid) {
101
+ values.push(this.api.hashid.encode(value));
102
+ }
103
+ else {
104
+ values.push(value);
105
+ }
106
+ }
107
+ }
108
+ return values;
109
+ }
110
+ /**
111
+ * Pring debug information for the field
112
+ *
113
+ * @param length length of the debug output (or 0 for as long as needed)
114
+ *
115
+ */
116
+ printColumnDebug(field, length = 0) {
117
+ let params = "";
118
+ if (field.fieldParams.isPrimaryKey) {
119
+ params += "PK ";
120
+ }
121
+ if (field.fieldParams.isForeignKey) {
122
+ params += "FK ";
123
+ }
124
+ if (field.fieldParams.isAutoInc) {
125
+ params += "AUTOINC ";
126
+ }
127
+ if (field.fieldParams.isNotNull) {
128
+ params += "NOTNUL ";
129
+ }
130
+ if (params != "") {
131
+ params = "{" + params.trim() + "}";
132
+ }
133
+ if (field.maxLength > 0) {
134
+ params = field.nativeType + "(" + field.maxLength + ")" + params;
135
+ }
136
+ else {
137
+ params = field.nativeType + params;
138
+ }
139
+ const name_length = length - 2 - 1 - params.length;
140
+ let result = field.name;
141
+ if (length > 0) {
142
+ if (result.length > name_length) {
143
+ result = result.substring(0, name_length - 2) + "..";
144
+ }
145
+ result = (result + ":" + params).padEnd(length - 2, " ");
146
+ }
147
+ else {
148
+ result = field.type + ":" + result + ":" + params;
149
+ }
150
+ return "[" + result + "]";
151
+ }
152
+ /**
153
+ * Print debug information about the fields.
154
+ *
155
+ * @param separator string to separate field prints
156
+ *
157
+ */
158
+ printDebug(separator = "") {
159
+ let result = this.api.params.tableName + ":" + separator;
160
+ for (let f of this.fields) {
161
+ result += this.printColumnDebug(f) + separator;
162
+ }
163
+ return result;
164
+ }
165
+ /**
166
+ * Print all public properties (db, table name, fields) of the datamodel. Used
167
+ * in automated testing validate schema has stayed the same.
168
+ *
169
+ */
170
+ printFieldPublicPropertiesJson() {
171
+ const result = JSON.stringify(this.fields, (key, value) => {
172
+ if (key.startsWith("_")) {
173
+ return undefined;
174
+ }
175
+ else {
176
+ return value;
177
+ }
178
+ });
179
+ return result;
180
+ }
181
+ }
182
+ exports.OINODataModel = OINODataModel;
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OINOMemoryDataset = exports.OINODataSet = exports.OINODataSource = void 0;
4
+ const OINOConstants_js_1 = require("./OINOConstants.js");
5
+ const OINOResult_js_1 = require("./OINOResult.js");
6
+ /**
7
+ * Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
8
+ * between SQL and serialization formats.
9
+ *
10
+ */
11
+ class OINODataSource {
12
+ isConnected = false;
13
+ isValidated = false;
14
+ }
15
+ exports.OINODataSource = OINODataSource;
16
+ /**
17
+ * Base class for SQL results that can be asynchronously iterated (but
18
+ * not necessarity rewinded). Idea is to handle database specific mechanisms
19
+ * for returning and formatting conventions in the database specific
20
+ * implementation. Data might be in memory or streamed in chunks and
21
+ * `OINODataSet` will serve it out consistently.
22
+ *
23
+ */
24
+ class OINODataSet extends OINOResult_js_1.OINOResult {
25
+ _data;
26
+ /** Error messages */
27
+ messages;
28
+ /**
29
+ * Constructor for `OINODataSet`.
30
+ *
31
+ * @param data internal database specific data type (constructor will throw if invalid)
32
+ * @param messages error messages from SQL-query
33
+ *
34
+ */
35
+ constructor(data, messages = []) {
36
+ super();
37
+ this._data = data;
38
+ this.messages = messages;
39
+ }
40
+ /**
41
+ * Checks if the messages contain errors.
42
+ *
43
+ */
44
+ hasErrors() {
45
+ for (let i = 0; i < this.messages.length; i++) {
46
+ if (this.messages[i].startsWith(OINOConstants_js_1.OINO_ERROR_PREFIX)) {
47
+ return true;
48
+ }
49
+ }
50
+ return false;
51
+ }
52
+ /**
53
+ * Finds the first error message that occured
54
+ *
55
+ */
56
+ getFirstError() {
57
+ for (let i = this.messages.length - 1; i >= 0; i--) {
58
+ if (this.messages[i].startsWith(OINOConstants_js_1.OINO_ERROR_PREFIX)) {
59
+ return this.messages[i];
60
+ }
61
+ }
62
+ return "";
63
+ }
64
+ /**
65
+ * Finds the last error message that occured
66
+ *
67
+ */
68
+ getLastError() {
69
+ for (let i = 0; i < this.messages.length; i++) {
70
+ if (this.messages[i].startsWith(OINOConstants_js_1.OINO_ERROR_PREFIX)) {
71
+ return this.messages[i];
72
+ }
73
+ }
74
+ return "";
75
+ }
76
+ }
77
+ exports.OINODataSet = OINODataSet;
78
+ /**
79
+ * Generic in memory implementation of a data set where data is an array of rows. Used
80
+ * by BunSqlite and automated testing. Can be rewinded.
81
+ *
82
+ */
83
+ class OINOMemoryDataset extends OINODataSet {
84
+ _rows;
85
+ _currentRow;
86
+ _eof;
87
+ /**
88
+ * Constructor of `OINOMemoryDataset`.
89
+ *
90
+ * @param data data as OINODataRow[] (constructor will throw if invalid)
91
+ * @param errors error messages from SQL-query
92
+ *
93
+ */
94
+ constructor(data, errors = []) {
95
+ super(data, errors);
96
+ if ((data == null) || !(Array.isArray(data))) {
97
+ throw new Error(OINOConstants_js_1.OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
98
+ }
99
+ this._rows = data;
100
+ if (this.isEmpty()) {
101
+ this._currentRow = -1;
102
+ this._eof = true;
103
+ }
104
+ else {
105
+ this._currentRow = 0;
106
+ this._eof = false;
107
+ }
108
+ }
109
+ /**
110
+ * Is data set empty.
111
+ *
112
+ */
113
+ isEmpty() {
114
+ return (this._rows.length == 0);
115
+ }
116
+ /**
117
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
118
+ *
119
+ */
120
+ isEof() {
121
+ return (this._eof);
122
+ }
123
+ /**
124
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
125
+ *
126
+ */
127
+ async next() {
128
+ if (this._currentRow < this._rows.length - 1) {
129
+ this._currentRow = this._currentRow + 1;
130
+ }
131
+ else {
132
+ this._eof = true;
133
+ }
134
+ return Promise.resolve(!this._eof);
135
+ }
136
+ /**
137
+ * Gets current row of data.
138
+ *
139
+ */
140
+ getRow() {
141
+ if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
142
+ return this._rows[this._currentRow];
143
+ }
144
+ else {
145
+ return OINOConstants_js_1.OINO_EMPTY_ROW;
146
+ }
147
+ }
148
+ /**
149
+ * Gets all rows of data.
150
+ *
151
+ */
152
+ async getAllRows() {
153
+ return this._rows; // at the moment theres no result streaming, so we can just return the rows
154
+ }
155
+ /**
156
+ * Rewinds data set to the first row, returns !isEof().
157
+ *
158
+ */
159
+ first() {
160
+ this._currentRow = 0;
161
+ this._eof = this._rows.length == 0;
162
+ return !this._eof;
163
+ }
164
+ }
165
+ exports.OINOMemoryDataset = OINOMemoryDataset;