deno.land / x / pg_mem@2.8.1 / evaluator.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import { IValue, _IIndex, _ISelection, _IType, _Transaction, _Explainer, _ExprExplanation, _ISchema, Parameter } from './interfaces-private.ts';import { DataType, QueryError, CastError, nil, ISchema, IType, ArgDefDetails } from './interfaces.ts';import hash from 'https://deno.land/x/object_hash@2.0.3.1/mod.ts';import { Types, ArrayType, isNumeric, reconciliateTypes } from './datatypes/index.ts';import { buildCall } from './parser/function-call.ts';import { nullIsh, executionCtx } from './utils.ts';import { QName } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';

export class Evaluator<T = any> implements IValue<T> {
readonly isConstantLiteral: boolean; readonly usedColumns = new Set<IValue>(); readonly forceNotConstant?: boolean;
get index(): _IIndex | nil { return this.origin?.getIndex(this); }
get isConstant(): boolean { return !this.usedColumns.size && !this.forceNotConstant; }
get isConstantReal(): boolean { return typeof this.val !== 'function'; }
origin: _ISelection | nil;
get isAny(): boolean { return this.opts?.isAny ?? false; }
constructor(readonly type: _IType<T> , readonly id: string | nil , readonly hash: string , dependencies: IValue | IValue[] | nil , public val: nil | Object | number | string | Date | ((raw: any, transaction: _Transaction | nil) => any) , private opts?: { isAny?: boolean; isColumnOf?: _ISelection; forceNotConstant?: boolean; unpure?: boolean; }) { this.isConstantLiteral = typeof val !== 'function'; if (opts?.forceNotConstant) { this.forceNotConstant = true; }
// fetch columns to depend on let depArray: IValue[] | undefined = undefined; let hasNotConstant = false; if (dependencies) { if (!Array.isArray(dependencies)) { depArray = [dependencies]; this.usedColumns = dependencies.usedColumns as Set<IValue>; hasNotConstant = !dependencies.isConstant; this.origin = dependencies.origin; } else { this.usedColumns = new Set(); for (const d of dependencies) { if (d.origin) { if (this.origin && d.origin && this.origin !== d.origin) { throw new Error('You cannot evaluate an expression which is coming from multiple origins'); } this.origin = d.origin; } if (!d.isConstant) { hasNotConstant = true; } for (const u of d.usedColumns) { this.usedColumns.add(u); } } } }
if (opts?.isColumnOf) { this.usedColumns.add(this); this.origin = opts.isColumnOf; delete opts.isColumnOf; } if (hasNotConstant && !this.usedColumns.size) { this.forceNotConstant = true; }
if (!this.usedColumns.size // no used columns && !this.origin && !this.opts?.unpure && !this.forceNotConstant && !depArray?.some(x => !x.isConstantReal) // all real constant dependencies ) { // no dependency => this is a real constant => evaluate it. if (typeof this.val === 'function') { this.val = this.val(null, null); } } }
setType(type: _IType) { if (this.type === type) { return this; } return new Evaluator<T>(type , this.id , this.hash , this , this.val , this.opts ); }


setConversion(converter: (val: T, t: _Transaction | nil) => any , hashConv: (hash: string) => any) { return new Evaluator<T>(this.type , this.id , hash(hashConv(this.hash)) , this , (raw, t) => { let got = this.get(raw, t); if (nullIsh(got)) { return null; } if (!this.isAny) { return converter(got, t); } if (!Array.isArray(got)) { throw new QueryError('Unexpected use of ANY()'); } return (got as any[]).map(x => converter(x, t)); } , this.opts ); }
setOrigin(origin: _ISelection): IValue<T> { const ret = this.clone(); ret.origin = origin; return ret; }
clone(): Evaluator<T> { return new Evaluator<T>( this.type , this.id , this.hash , this , this.val , this.opts ); }
map<TNew>(mapper: (val: T) => TNew, newType?: _IType<TNew>): IValue<TNew> { if (this.isAny) { throw new QueryError('Unexpected use of ANY()'); } const ret = new Evaluator<TNew>( (newType ?? this.type) as _IType , this.id , this.hash , this , (raw, t) => { const got = this.get(raw, t); if (nullIsh(got)) { return null; } return mapper(got); } , this.opts ); ret.origin = this.origin; return ret; }
setWrapper<TNew>(newOrigin: _ISelection, unwrap: (val: T) => TNew, newType?: _IType<TNew>): IValue<TNew> { if (this.isAny) { throw new QueryError('Unexpected use of ANY()'); } const ret = new Evaluator<TNew>( (newType ?? this.type) as _IType , this.id , this.hash , this , (raw, t) => { const got = unwrap(raw) if (nullIsh(got)) { return null; } return this.get(got, t); } , this.opts ); ret.origin = newOrigin; return ret; }
setId(newId: string): IValue { if (this.id === newId) { return this; } return new Evaluator<T>( this.type , newId , this.hash , this , this.val , this.opts ); }
get(): T; get(raw: any, t: _Transaction | nil): T; get(raw?: any, t?: _Transaction): T { if ((nullIsh(raw) || !t) && !this.isConstant) { throw new Error('Cannot be evaluated as constant'); } if (typeof this.val !== 'function') { return this.val as any; } return this.val(raw, t) }
canCast(to: _IType<T>): boolean { return !!this.type.canCast(to); }
cast<T = any>(to: _IType<T>): IValue<T> { return this.type.cast(this, to); }
convertImplicit<T = any>(to: _IType<T>): IValue<T> { return this.type.convertImplicit(this, to); }

explain(e: _Explainer): _ExprExplanation { if (!this.origin) { return { constant: true, } } return { on: e.idFor(this.origin), col: this.id ?? '<complex expression>', }; }}
// export class ArrayEvaluator<T> implements IValue {
// constructor(// readonly type: _IType<T>// , readonly id: string// , readonly sql: string// , readonly hash: string// , readonly selection: _ISelection// , public val: T | ((raw: any) => T)) {// }
// get index() {// return this.selection?.getIndex(this);// }
// get isConstant(): boolean {// return typeof this.val !== 'function';// }
// get(raw: any): T {// if (typeof this.val !== 'function') {// return this.val;// }// return (this.val as ((raw: any) => T))(raw);// }
// asConstant(perform = true) {// if (!perform || typeof this.val !== 'function') {// return this;// }// return new Evaluator(this.type// , this.id// , this.sql// , this.hash// , this.selection// , this.get(null));// }

// setId(newId: string): IValue {// if (this.id === newId) {// return this;// }// return new Evaluator<T>(// this.type// , newId// , this.sql// , this.hash// , this.selection// , this.val// );// }
// canCast(to: DataType | _IType<T>): boolean {// return this.type.canCast(to);// }
// cast<T = any>(to: DataType | _IType<T>): IValue<T> {// return this.type.cast(this, to);// }// }

export const Value = { null(ofType?: _IType): IValue { return new Evaluator(ofType ?? Types.null, null, 'null', null, null, undefined); }, text(value: string, length: number | nil = null): IValue { return new Evaluator( Types.text(length) , null , value , null , value); }, number(value: number, type = Types.float): IValue { return new Evaluator( type , null , value.toString(10) , null , value); }, function(value: string | QName, args: IValue[]): IValue { return buildCall(value, args); }, bool(value: boolean): IValue { const str = value ? 'true' : 'false'; return new Evaluator( Types.bool , null , str , null , value); }, /** @deprecated Use with care */ constant(_type: _IType, value: any): IValue { const type = nullIsh(value) ? Types.null : _type; return new Evaluator( type , null , (null as any) , null , value); }, /** @deprecated Use with care */ converter(type: _IType, to: _IType): (val: any, t: _Transaction | nil) => any { let last: any = null; const evaluator = new Evaluator( type , null , (null as any) , null , () => last , { forceNotConstant: true }) .cast(to); return (val: any, t) => { if (nullIsh(val)) { return null; } last = val; const ret = evaluator.get(val, t); last = null; return ret; }; }, in(value: IValue, array: IValue, inclusive: boolean): IValue { if (!value) { throw new Error('Argument null'); } if (array.type.primary !== DataType.list && array.type) { array = Value.list([array]); } const of = (array.type as ArrayType).of;
// cast both sides (see #castArrayIn in uts) const type = reconciliateTypes([value, Value.null(of)]) value = value.cast(type); array = array.cast(type.asArray());
return new Evaluator( Types.bool , null , hash({ val: value.hash, in: array.hash }) , [value, array] , (raw, t) => { const rawValue = value.get(raw, t); const rawArray = array.get(raw, t); if (!Array.isArray(rawArray)) { return false; } const has = rawArray.some(x => of.equals(rawValue, x)); return inclusive ? has : !has; }); }, isNull(leftValue: IValue, expectNull: boolean): IValue { return new Evaluator( Types.bool , null , hash({ isNull: leftValue.hash, expectNull }) , leftValue , (raw, t) => { const left = leftValue.get(raw, t); // check that result is null (will never return NULL) const ret = nullIsh(left); return expectNull ? ret : !ret; }) }, isTrue(leftValue: IValue, expectTrue: boolean): IValue { leftValue = leftValue.cast(Types.bool); return new Evaluator( Types.bool , null , hash({ isTrue: leftValue.hash, expectTrue }) , leftValue , expectTrue ? ((raw, t) => { const left = leftValue.get(raw, t); return left === true; // never returns null }) : ((raw, t) => { const left = leftValue.get(raw, t); return !(left === true); // never returns null })); }, isFalse(leftValue: IValue, expectFalse: boolean): IValue { leftValue = leftValue.cast(Types.bool); return new Evaluator( Types.bool , null , hash({ isFalse: leftValue.hash, expectFalse }) , leftValue , expectFalse ? ((raw, t) => { const left = leftValue.get(raw, t); return left === false; // never returns null }) : ((raw, t) => { const left = leftValue.get(raw, t); return !(left === false); // never returns null })); }, negate(value: IValue): IValue { if (value.type === Types.bool) { return (value as Evaluator) .setConversion(x => !x, x => ({ not: x })); } if (!isNumeric(value.type)) { throw new QueryError('Can only apply "-" unary operator to numeric types'); } return (value as Evaluator) .setConversion(x => -x, x => ({ neg: x })); }, array(values: IValue[]): IValue { return arrayOrList(values, false); }, list(values: IValue[]): IValue { return arrayOrList(values, true); }} as const;

function arrayOrList(values: IValue[], list: boolean) { const type = values.reduce((t, v) => { if (v.canCast(t)) { return t; } if (!t.canCast(v.type)) { throw new CastError(t.primary, v.type.primary); } return v.type; }, Types.null); // const sel = values.find(x => !!x.selection)?.selection; const converted = values.map(x => x.cast(type)); return new Evaluator( list ? type.asList() : type.asArray() , null , hash(converted.map(x => x.hash)) , converted , (raw, t) => { const arr = values.map(x => x.get(raw, t)); return arr; });}

export function buildParameterList(statementName: string | null, args: ArgDefDetails[]) { return args.map<Parameter>(({ type, name }, i) => { const value = new Evaluator( type as _IType , name , hash({ param: name }) , null , () => { const { parametersValues } = executionCtx(); if (!parametersValues || parametersValues.length <= i) { throw new QueryError(`bind message supplies ${parametersValues?.length ?? 0} parameters, but prepared statement "${statementName}" requires ${args.length}`, '08P01'); } return parametersValues[i]; }, { forceNotConstant: true, }); return { value, index: i }; })}
pg_mem

Version Info

Tagged at
3 months ago