@oino-ts/db 0.17.1 → 0.17.2
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/OINODbApi.js +9 -0
- package/dist/esm/OINODbApi.js +9 -0
- package/dist/types/OINODbApi.d.ts +1 -0
- package/package.json +37 -37
- package/src/OINODb.ts +321 -321
- package/src/OINODbApi.test.ts +492 -492
- package/src/OINODbApi.ts +611 -602
- package/src/OINODbConfig.ts +98 -98
- package/src/OINODbDataField.ts +403 -403
- package/src/OINODbDataModel.ts +291 -291
- package/src/OINODbFactory.ts +68 -68
- package/src/OINODbModelSet.ts +351 -351
- package/src/OINODbParser.ts +447 -447
- package/src/OINODbSqlParams.ts +592 -592
- package/src/OINODbSwagger.ts +208 -208
- package/src/index.ts +120 -120
package/src/OINODbDataField.ts
CHANGED
|
@@ -1,403 +1,403 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { OINODbDataFieldParams, OINODataCell, OINODb, OINOLog, OINO_ERROR_PREFIX } from "./index.js";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
export class OINODbDataField {
|
|
14
|
-
|
|
15
|
-
/** OINODB reference*/
|
|
16
|
-
readonly db:OINODb;
|
|
17
|
-
|
|
18
|
-
/** Name of the field */
|
|
19
|
-
readonly name: string;
|
|
20
|
-
|
|
21
|
-
/** Internal type of field*/
|
|
22
|
-
readonly type: string;
|
|
23
|
-
|
|
24
|
-
/** SQL type of the field */
|
|
25
|
-
readonly sqlType: string;
|
|
26
|
-
|
|
27
|
-
/** Maximum length of the field (or 0) */
|
|
28
|
-
readonly maxLength: number;
|
|
29
|
-
|
|
30
|
-
/** Parameters for the field */
|
|
31
|
-
readonly fieldParams: OINODbDataFieldParams;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Constructor for a data field
|
|
35
|
-
*
|
|
36
|
-
* @param db OINODb reference
|
|
37
|
-
* @param name name of the field
|
|
38
|
-
* @param type internal type of the field
|
|
39
|
-
* @param sqlType column type in database
|
|
40
|
-
* @param fieldParams parameters of the field
|
|
41
|
-
* @param maxLength maximum length of the field (or 0)
|
|
42
|
-
*
|
|
43
|
-
*/
|
|
44
|
-
constructor(db:OINODb, name: string, type:string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number = 0) {
|
|
45
|
-
this.db = db
|
|
46
|
-
this.name = name
|
|
47
|
-
this.type = type
|
|
48
|
-
this.maxLength = maxLength
|
|
49
|
-
this.sqlType = sqlType
|
|
50
|
-
this.fieldParams = fieldParams
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Pring debug information for the field
|
|
55
|
-
*
|
|
56
|
-
* @param length length of the debug output (or 0 for as long as needed)
|
|
57
|
-
*
|
|
58
|
-
*/
|
|
59
|
-
printColumnDebug(length:number = 0): string {
|
|
60
|
-
let params: string = "";
|
|
61
|
-
if (this.fieldParams.isPrimaryKey) {
|
|
62
|
-
params += "PK ";
|
|
63
|
-
}
|
|
64
|
-
if (this.fieldParams.isForeignKey) {
|
|
65
|
-
params += "FK ";
|
|
66
|
-
}
|
|
67
|
-
if (this.fieldParams.isAutoInc) {
|
|
68
|
-
params += "AUTOINC ";
|
|
69
|
-
}
|
|
70
|
-
if (this.fieldParams.isNotNull) {
|
|
71
|
-
params += "NOTNUL ";
|
|
72
|
-
}
|
|
73
|
-
if (params != "") {
|
|
74
|
-
params = "{" + params.trim() + "}";
|
|
75
|
-
}
|
|
76
|
-
if (this.maxLength > 0) {
|
|
77
|
-
params = this.sqlType + "(" + this.maxLength + ")" + params
|
|
78
|
-
} else {
|
|
79
|
-
params = this.sqlType + params
|
|
80
|
-
}
|
|
81
|
-
const name_length:number = length - 2 - 1 - params.length
|
|
82
|
-
let result:string = this.name
|
|
83
|
-
if (length > 0) {
|
|
84
|
-
if (result.length > name_length) {
|
|
85
|
-
result = result.substring(0, name_length-2)+".."
|
|
86
|
-
}
|
|
87
|
-
result = (result + ":" + params).padEnd(length-2, " ")
|
|
88
|
-
} else {
|
|
89
|
-
result = this.type + ":" + result + ":" + params
|
|
90
|
-
}
|
|
91
|
-
return "[" + result + "]";
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Serialize cell value in the given content format.
|
|
96
|
-
*
|
|
97
|
-
* @param cellVal cell value
|
|
98
|
-
*
|
|
99
|
-
*/
|
|
100
|
-
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
101
|
-
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
|
|
102
|
-
if ((cellVal === null) || (cellVal === undefined)) {
|
|
103
|
-
return cellVal // let content type encoder worry what to do with the value (so not force it to string)
|
|
104
|
-
} else {
|
|
105
|
-
return cellVal.toString()
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Parce cell value from string using field type specific formatting rules.
|
|
111
|
-
*
|
|
112
|
-
* @param value string value
|
|
113
|
-
*
|
|
114
|
-
*/
|
|
115
|
-
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
116
|
-
return value
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Print data cell (from deserialization) as SQL-string.
|
|
121
|
-
*
|
|
122
|
-
* @param cellVal cell value
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
|
-
printCellAsSqlValue(cellVal: OINODataCell):string {
|
|
126
|
-
return this.db.printCellAsSqlValue(cellVal, this.sqlType);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Print name of column as SQL.
|
|
131
|
-
*
|
|
132
|
-
*/
|
|
133
|
-
printSqlColumnName():string {
|
|
134
|
-
return this.db.printSqlColumnname(this.name)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Specialised class for a string column.
|
|
140
|
-
*
|
|
141
|
-
*/
|
|
142
|
-
export class OINOStringDataField extends OINODbDataField {
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Constructor for a string data field
|
|
146
|
-
*
|
|
147
|
-
* @param db OINODb reference
|
|
148
|
-
* @param name name of the field
|
|
149
|
-
* @param sqlType column type in database
|
|
150
|
-
* @param fieldParams parameters of the field
|
|
151
|
-
* @param maxLength maximum length of the field (or 0)
|
|
152
|
-
*
|
|
153
|
-
*/
|
|
154
|
-
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength: number) {
|
|
155
|
-
super(db, name, "string", sqlType, fieldParams, maxLength)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Specialised class for a boolean column.
|
|
162
|
-
*
|
|
163
|
-
*/
|
|
164
|
-
export class OINOBooleanDataField extends OINODbDataField {
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Constructor for a boolean data field
|
|
168
|
-
*
|
|
169
|
-
* @param db OINODb reference
|
|
170
|
-
* @param name name of the field
|
|
171
|
-
* @param sqlType column type in database
|
|
172
|
-
* @param fieldParams parameters of the field
|
|
173
|
-
*
|
|
174
|
-
*/
|
|
175
|
-
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
|
|
176
|
-
super(db, name, "boolean", sqlType, fieldParams)
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Serialize cell value in the given content format.
|
|
180
|
-
*
|
|
181
|
-
* @param cellVal cell value
|
|
182
|
-
*
|
|
183
|
-
*/
|
|
184
|
-
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
185
|
-
const parsed_value:string = (this.db.parseSqlValueAsCell(cellVal, this.sqlType) || "").toString()
|
|
186
|
-
let result:string
|
|
187
|
-
// console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
|
|
188
|
-
if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
|
|
189
|
-
result = "false"
|
|
190
|
-
} else {
|
|
191
|
-
result = "true"
|
|
192
|
-
}
|
|
193
|
-
return result
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Parce cell value from string using field type specific formatting rules.
|
|
198
|
-
*
|
|
199
|
-
* @param value string value
|
|
200
|
-
*
|
|
201
|
-
*/
|
|
202
|
-
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
203
|
-
if (value == null || value == "" || value.toString().toLowerCase() == "false" || value == "0") { // TODO: testaa poistaa .toString()
|
|
204
|
-
return false
|
|
205
|
-
} else {
|
|
206
|
-
return true
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Specialised class for a number column.
|
|
213
|
-
*
|
|
214
|
-
*/
|
|
215
|
-
export class OINONumberDataField extends OINODbDataField {
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Constructor for a string data field
|
|
219
|
-
*
|
|
220
|
-
* @param db OINODb reference
|
|
221
|
-
* @param name name of the field
|
|
222
|
-
* @param sqlType column type in database
|
|
223
|
-
* @param fieldParams parameters of the field
|
|
224
|
-
*
|
|
225
|
-
*/
|
|
226
|
-
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
|
|
227
|
-
super(db, name, "number", sqlType, fieldParams)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Serialize cell value in the given content format.
|
|
232
|
-
*
|
|
233
|
-
* @param cellVal cell value
|
|
234
|
-
*
|
|
235
|
-
*/
|
|
236
|
-
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
237
|
-
let result:string|null
|
|
238
|
-
if ((cellVal === null) || (cellVal === undefined) || (cellVal === "")) {
|
|
239
|
-
result = null
|
|
240
|
-
} else {
|
|
241
|
-
result = cellVal.toString()
|
|
242
|
-
}
|
|
243
|
-
return result
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Parce cell value from string using field type specific formatting rules.
|
|
248
|
-
*
|
|
249
|
-
* @param value string value
|
|
250
|
-
*
|
|
251
|
-
*/
|
|
252
|
-
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
253
|
-
if (value === undefined) {
|
|
254
|
-
return undefined
|
|
255
|
-
} else if ((value === "") || (value === null)) {
|
|
256
|
-
return null
|
|
257
|
-
} else {
|
|
258
|
-
const result:number = parseFloat(value)
|
|
259
|
-
if (isNaN(result)) {
|
|
260
|
-
OINOLog.error("@oino-ts/db", "OINONumberDataField", "toSql", "Invalid value!", {value:value})
|
|
261
|
-
throw new Error(OINO_ERROR_PREFIX + ": OINONumberDataField.deserializeCell - Invalid value '" + value + "'") // incorrectly formatted data could be a security risk, abort processing
|
|
262
|
-
}
|
|
263
|
-
return result
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Specialised class for a blob column.
|
|
270
|
-
*
|
|
271
|
-
*/
|
|
272
|
-
export class OINOBlobDataField extends OINODbDataField {
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Constructor for a blob data field
|
|
276
|
-
*
|
|
277
|
-
* @param db OINODb reference
|
|
278
|
-
* @param name name of the field
|
|
279
|
-
* @param sqlType column type in database
|
|
280
|
-
* @param fieldParams parameters of the field
|
|
281
|
-
* @param maxLength maximum length of the field (or 0)
|
|
282
|
-
*
|
|
283
|
-
*/
|
|
284
|
-
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number) {
|
|
285
|
-
super(db, name, "blob", sqlType, fieldParams, maxLength)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Serialize cell value in the given content format.
|
|
290
|
-
*
|
|
291
|
-
* @param cellVal cell value
|
|
292
|
-
*
|
|
293
|
-
*/
|
|
294
|
-
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
295
|
-
// console.log("OINOBlobDataField.serializeCell: cellVal", cellVal, typeof(cellVal))
|
|
296
|
-
if ((cellVal === null) || (cellVal === undefined)) {
|
|
297
|
-
return cellVal
|
|
298
|
-
|
|
299
|
-
} else if (cellVal instanceof Buffer) {
|
|
300
|
-
return cellVal.toString('base64')
|
|
301
|
-
|
|
302
|
-
} else if (cellVal instanceof Uint8Array) {
|
|
303
|
-
return Buffer.from(cellVal).toString('base64')
|
|
304
|
-
|
|
305
|
-
} else {
|
|
306
|
-
return this.db.parseSqlValueAsCell(cellVal, this.sqlType)?.toString()
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Parce cell value from string using field type specific formatting rules.
|
|
312
|
-
*
|
|
313
|
-
* @param value string value
|
|
314
|
-
*
|
|
315
|
-
*/
|
|
316
|
-
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
317
|
-
if (value == null) {
|
|
318
|
-
return Buffer.alloc(0)
|
|
319
|
-
|
|
320
|
-
} else {
|
|
321
|
-
return Buffer.from(value, 'base64') // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Specialised class for a datetime column.
|
|
329
|
-
*
|
|
330
|
-
*/
|
|
331
|
-
export class OINODatetimeDataField extends OINODbDataField {
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Constructor for a string data field
|
|
335
|
-
*
|
|
336
|
-
* @param db OINODb reference
|
|
337
|
-
* @param name name of the field
|
|
338
|
-
* @param sqlType column type in database
|
|
339
|
-
* @param fieldParams parameters of the field
|
|
340
|
-
*
|
|
341
|
-
*/
|
|
342
|
-
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
|
|
343
|
-
super(db, name, "datetime", sqlType, fieldParams)
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Serialize cell value in the given content format.
|
|
348
|
-
*
|
|
349
|
-
* @param cellVal cell value
|
|
350
|
-
*
|
|
351
|
-
*/
|
|
352
|
-
serializeCell(cellVal: OINODataCell): string|null|undefined {
|
|
353
|
-
if (typeof(cellVal) == "string") {
|
|
354
|
-
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
|
|
355
|
-
}
|
|
356
|
-
if ((cellVal === null) || (cellVal === undefined)) {
|
|
357
|
-
return cellVal
|
|
358
|
-
|
|
359
|
-
} else if (cellVal instanceof Date) {
|
|
360
|
-
return cellVal.toISOString()
|
|
361
|
-
|
|
362
|
-
} else {
|
|
363
|
-
return cellVal.toString()
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Serialize cell value in the given content format.
|
|
369
|
-
*
|
|
370
|
-
* @param cellVal cell value
|
|
371
|
-
* @param locale locale-object to format datetimes with
|
|
372
|
-
*
|
|
373
|
-
*/
|
|
374
|
-
serializeCellWithLocale(cellVal: OINODataCell, locale:Intl.DateTimeFormat): string|null|undefined {
|
|
375
|
-
if (typeof(cellVal) == "string") {
|
|
376
|
-
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
|
|
377
|
-
}
|
|
378
|
-
if ((cellVal === null) || (cellVal === undefined)) {
|
|
379
|
-
return cellVal
|
|
380
|
-
|
|
381
|
-
} else if (cellVal instanceof Date) {
|
|
382
|
-
return locale.format(cellVal)
|
|
383
|
-
|
|
384
|
-
} else {
|
|
385
|
-
return cellVal.toString()
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Parce cell value from string using field type specific formatting rules.
|
|
391
|
-
*
|
|
392
|
-
* @param value string value
|
|
393
|
-
*
|
|
394
|
-
*/
|
|
395
|
-
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
396
|
-
if ((value === null) || (value === undefined)) {
|
|
397
|
-
return value
|
|
398
|
-
} else {
|
|
399
|
-
return new Date(value)
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { OINODbDataFieldParams, OINODataCell, OINODb, OINOLog, OINO_ERROR_PREFIX } from "./index.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export class OINODbDataField {
|
|
14
|
+
|
|
15
|
+
/** OINODB reference*/
|
|
16
|
+
readonly db:OINODb;
|
|
17
|
+
|
|
18
|
+
/** Name of the field */
|
|
19
|
+
readonly name: string;
|
|
20
|
+
|
|
21
|
+
/** Internal type of field*/
|
|
22
|
+
readonly type: string;
|
|
23
|
+
|
|
24
|
+
/** SQL type of the field */
|
|
25
|
+
readonly sqlType: string;
|
|
26
|
+
|
|
27
|
+
/** Maximum length of the field (or 0) */
|
|
28
|
+
readonly maxLength: number;
|
|
29
|
+
|
|
30
|
+
/** Parameters for the field */
|
|
31
|
+
readonly fieldParams: OINODbDataFieldParams;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Constructor for a data field
|
|
35
|
+
*
|
|
36
|
+
* @param db OINODb reference
|
|
37
|
+
* @param name name of the field
|
|
38
|
+
* @param type internal type of the field
|
|
39
|
+
* @param sqlType column type in database
|
|
40
|
+
* @param fieldParams parameters of the field
|
|
41
|
+
* @param maxLength maximum length of the field (or 0)
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
constructor(db:OINODb, name: string, type:string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number = 0) {
|
|
45
|
+
this.db = db
|
|
46
|
+
this.name = name
|
|
47
|
+
this.type = type
|
|
48
|
+
this.maxLength = maxLength
|
|
49
|
+
this.sqlType = sqlType
|
|
50
|
+
this.fieldParams = fieldParams
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Pring debug information for the field
|
|
55
|
+
*
|
|
56
|
+
* @param length length of the debug output (or 0 for as long as needed)
|
|
57
|
+
*
|
|
58
|
+
*/
|
|
59
|
+
printColumnDebug(length:number = 0): string {
|
|
60
|
+
let params: string = "";
|
|
61
|
+
if (this.fieldParams.isPrimaryKey) {
|
|
62
|
+
params += "PK ";
|
|
63
|
+
}
|
|
64
|
+
if (this.fieldParams.isForeignKey) {
|
|
65
|
+
params += "FK ";
|
|
66
|
+
}
|
|
67
|
+
if (this.fieldParams.isAutoInc) {
|
|
68
|
+
params += "AUTOINC ";
|
|
69
|
+
}
|
|
70
|
+
if (this.fieldParams.isNotNull) {
|
|
71
|
+
params += "NOTNUL ";
|
|
72
|
+
}
|
|
73
|
+
if (params != "") {
|
|
74
|
+
params = "{" + params.trim() + "}";
|
|
75
|
+
}
|
|
76
|
+
if (this.maxLength > 0) {
|
|
77
|
+
params = this.sqlType + "(" + this.maxLength + ")" + params
|
|
78
|
+
} else {
|
|
79
|
+
params = this.sqlType + params
|
|
80
|
+
}
|
|
81
|
+
const name_length:number = length - 2 - 1 - params.length
|
|
82
|
+
let result:string = this.name
|
|
83
|
+
if (length > 0) {
|
|
84
|
+
if (result.length > name_length) {
|
|
85
|
+
result = result.substring(0, name_length-2)+".."
|
|
86
|
+
}
|
|
87
|
+
result = (result + ":" + params).padEnd(length-2, " ")
|
|
88
|
+
} else {
|
|
89
|
+
result = this.type + ":" + result + ":" + params
|
|
90
|
+
}
|
|
91
|
+
return "[" + result + "]";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Serialize cell value in the given content format.
|
|
96
|
+
*
|
|
97
|
+
* @param cellVal cell value
|
|
98
|
+
*
|
|
99
|
+
*/
|
|
100
|
+
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
101
|
+
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
|
|
102
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
103
|
+
return cellVal // let content type encoder worry what to do with the value (so not force it to string)
|
|
104
|
+
} else {
|
|
105
|
+
return cellVal.toString()
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
111
|
+
*
|
|
112
|
+
* @param value string value
|
|
113
|
+
*
|
|
114
|
+
*/
|
|
115
|
+
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
116
|
+
return value
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Print data cell (from deserialization) as SQL-string.
|
|
121
|
+
*
|
|
122
|
+
* @param cellVal cell value
|
|
123
|
+
*
|
|
124
|
+
*/
|
|
125
|
+
printCellAsSqlValue(cellVal: OINODataCell):string {
|
|
126
|
+
return this.db.printCellAsSqlValue(cellVal, this.sqlType);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Print name of column as SQL.
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
printSqlColumnName():string {
|
|
134
|
+
return this.db.printSqlColumnname(this.name)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Specialised class for a string column.
|
|
140
|
+
*
|
|
141
|
+
*/
|
|
142
|
+
export class OINOStringDataField extends OINODbDataField {
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Constructor for a string data field
|
|
146
|
+
*
|
|
147
|
+
* @param db OINODb reference
|
|
148
|
+
* @param name name of the field
|
|
149
|
+
* @param sqlType column type in database
|
|
150
|
+
* @param fieldParams parameters of the field
|
|
151
|
+
* @param maxLength maximum length of the field (or 0)
|
|
152
|
+
*
|
|
153
|
+
*/
|
|
154
|
+
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength: number) {
|
|
155
|
+
super(db, name, "string", sqlType, fieldParams, maxLength)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Specialised class for a boolean column.
|
|
162
|
+
*
|
|
163
|
+
*/
|
|
164
|
+
export class OINOBooleanDataField extends OINODbDataField {
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Constructor for a boolean data field
|
|
168
|
+
*
|
|
169
|
+
* @param db OINODb reference
|
|
170
|
+
* @param name name of the field
|
|
171
|
+
* @param sqlType column type in database
|
|
172
|
+
* @param fieldParams parameters of the field
|
|
173
|
+
*
|
|
174
|
+
*/
|
|
175
|
+
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
|
|
176
|
+
super(db, name, "boolean", sqlType, fieldParams)
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Serialize cell value in the given content format.
|
|
180
|
+
*
|
|
181
|
+
* @param cellVal cell value
|
|
182
|
+
*
|
|
183
|
+
*/
|
|
184
|
+
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
185
|
+
const parsed_value:string = (this.db.parseSqlValueAsCell(cellVal, this.sqlType) || "").toString()
|
|
186
|
+
let result:string
|
|
187
|
+
// console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
|
|
188
|
+
if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
|
|
189
|
+
result = "false"
|
|
190
|
+
} else {
|
|
191
|
+
result = "true"
|
|
192
|
+
}
|
|
193
|
+
return result
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
198
|
+
*
|
|
199
|
+
* @param value string value
|
|
200
|
+
*
|
|
201
|
+
*/
|
|
202
|
+
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
203
|
+
if (value == null || value == "" || value.toString().toLowerCase() == "false" || value == "0") { // TODO: testaa poistaa .toString()
|
|
204
|
+
return false
|
|
205
|
+
} else {
|
|
206
|
+
return true
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Specialised class for a number column.
|
|
213
|
+
*
|
|
214
|
+
*/
|
|
215
|
+
export class OINONumberDataField extends OINODbDataField {
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Constructor for a string data field
|
|
219
|
+
*
|
|
220
|
+
* @param db OINODb reference
|
|
221
|
+
* @param name name of the field
|
|
222
|
+
* @param sqlType column type in database
|
|
223
|
+
* @param fieldParams parameters of the field
|
|
224
|
+
*
|
|
225
|
+
*/
|
|
226
|
+
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
|
|
227
|
+
super(db, name, "number", sqlType, fieldParams)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Serialize cell value in the given content format.
|
|
232
|
+
*
|
|
233
|
+
* @param cellVal cell value
|
|
234
|
+
*
|
|
235
|
+
*/
|
|
236
|
+
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
237
|
+
let result:string|null
|
|
238
|
+
if ((cellVal === null) || (cellVal === undefined) || (cellVal === "")) {
|
|
239
|
+
result = null
|
|
240
|
+
} else {
|
|
241
|
+
result = cellVal.toString()
|
|
242
|
+
}
|
|
243
|
+
return result
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
248
|
+
*
|
|
249
|
+
* @param value string value
|
|
250
|
+
*
|
|
251
|
+
*/
|
|
252
|
+
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
253
|
+
if (value === undefined) {
|
|
254
|
+
return undefined
|
|
255
|
+
} else if ((value === "") || (value === null)) {
|
|
256
|
+
return null
|
|
257
|
+
} else {
|
|
258
|
+
const result:number = parseFloat(value)
|
|
259
|
+
if (isNaN(result)) {
|
|
260
|
+
OINOLog.error("@oino-ts/db", "OINONumberDataField", "toSql", "Invalid value!", {value:value})
|
|
261
|
+
throw new Error(OINO_ERROR_PREFIX + ": OINONumberDataField.deserializeCell - Invalid value '" + value + "'") // incorrectly formatted data could be a security risk, abort processing
|
|
262
|
+
}
|
|
263
|
+
return result
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Specialised class for a blob column.
|
|
270
|
+
*
|
|
271
|
+
*/
|
|
272
|
+
export class OINOBlobDataField extends OINODbDataField {
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Constructor for a blob data field
|
|
276
|
+
*
|
|
277
|
+
* @param db OINODb reference
|
|
278
|
+
* @param name name of the field
|
|
279
|
+
* @param sqlType column type in database
|
|
280
|
+
* @param fieldParams parameters of the field
|
|
281
|
+
* @param maxLength maximum length of the field (or 0)
|
|
282
|
+
*
|
|
283
|
+
*/
|
|
284
|
+
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number) {
|
|
285
|
+
super(db, name, "blob", sqlType, fieldParams, maxLength)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Serialize cell value in the given content format.
|
|
290
|
+
*
|
|
291
|
+
* @param cellVal cell value
|
|
292
|
+
*
|
|
293
|
+
*/
|
|
294
|
+
serializeCell(cellVal: OINODataCell):string|null|undefined {
|
|
295
|
+
// console.log("OINOBlobDataField.serializeCell: cellVal", cellVal, typeof(cellVal))
|
|
296
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
297
|
+
return cellVal
|
|
298
|
+
|
|
299
|
+
} else if (cellVal instanceof Buffer) {
|
|
300
|
+
return cellVal.toString('base64')
|
|
301
|
+
|
|
302
|
+
} else if (cellVal instanceof Uint8Array) {
|
|
303
|
+
return Buffer.from(cellVal).toString('base64')
|
|
304
|
+
|
|
305
|
+
} else {
|
|
306
|
+
return this.db.parseSqlValueAsCell(cellVal, this.sqlType)?.toString()
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
312
|
+
*
|
|
313
|
+
* @param value string value
|
|
314
|
+
*
|
|
315
|
+
*/
|
|
316
|
+
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
317
|
+
if (value == null) {
|
|
318
|
+
return Buffer.alloc(0)
|
|
319
|
+
|
|
320
|
+
} else {
|
|
321
|
+
return Buffer.from(value, 'base64') // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Specialised class for a datetime column.
|
|
329
|
+
*
|
|
330
|
+
*/
|
|
331
|
+
export class OINODatetimeDataField extends OINODbDataField {
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Constructor for a string data field
|
|
335
|
+
*
|
|
336
|
+
* @param db OINODb reference
|
|
337
|
+
* @param name name of the field
|
|
338
|
+
* @param sqlType column type in database
|
|
339
|
+
* @param fieldParams parameters of the field
|
|
340
|
+
*
|
|
341
|
+
*/
|
|
342
|
+
constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
|
|
343
|
+
super(db, name, "datetime", sqlType, fieldParams)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Serialize cell value in the given content format.
|
|
348
|
+
*
|
|
349
|
+
* @param cellVal cell value
|
|
350
|
+
*
|
|
351
|
+
*/
|
|
352
|
+
serializeCell(cellVal: OINODataCell): string|null|undefined {
|
|
353
|
+
if (typeof(cellVal) == "string") {
|
|
354
|
+
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
|
|
355
|
+
}
|
|
356
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
357
|
+
return cellVal
|
|
358
|
+
|
|
359
|
+
} else if (cellVal instanceof Date) {
|
|
360
|
+
return cellVal.toISOString()
|
|
361
|
+
|
|
362
|
+
} else {
|
|
363
|
+
return cellVal.toString()
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Serialize cell value in the given content format.
|
|
369
|
+
*
|
|
370
|
+
* @param cellVal cell value
|
|
371
|
+
* @param locale locale-object to format datetimes with
|
|
372
|
+
*
|
|
373
|
+
*/
|
|
374
|
+
serializeCellWithLocale(cellVal: OINODataCell, locale:Intl.DateTimeFormat): string|null|undefined {
|
|
375
|
+
if (typeof(cellVal) == "string") {
|
|
376
|
+
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
|
|
377
|
+
}
|
|
378
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
379
|
+
return cellVal
|
|
380
|
+
|
|
381
|
+
} else if (cellVal instanceof Date) {
|
|
382
|
+
return locale.format(cellVal)
|
|
383
|
+
|
|
384
|
+
} else {
|
|
385
|
+
return cellVal.toString()
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
391
|
+
*
|
|
392
|
+
* @param value string value
|
|
393
|
+
*
|
|
394
|
+
*/
|
|
395
|
+
deserializeCell(value: string|null|undefined): OINODataCell {
|
|
396
|
+
if ((value === null) || (value === undefined)) {
|
|
397
|
+
return value
|
|
398
|
+
} else {
|
|
399
|
+
return new Date(value)
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
}
|