deno.land / x / pg_mem@2.8.1 / transforms / alias.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
import { TransformBase, FilterBase } from './transform-base.ts';import { _Transaction, IValue, _Explainer, _ISelection, _SelectExplanation, QueryError, Stats, nil, _IAlias } from '../interfaces-private.ts';import { Evaluator } from '../evaluator.ts';import { Types, RecordCol } from '../datatypes/index.ts';import { ExprRef } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { asSingleName, colToStr } from '../utils.ts';import { ColumnNotFound } from '../interfaces.ts';import { RecordType } from '../datatypes/t-record.ts';
export function buildAlias(on: _ISelection, alias?: string): _ISelection { if (!alias) { return on as any; } if (on instanceof Alias && on.name === alias) { return on; } return new Alias(on, alias);}
export class Alias<T> extends TransformBase<T> implements _IAlias {
private oldToThis = new Map<IValue, IValue>(); private thisToOld = new Map<IValue, IValue>(); private _columns: IValue<any>[] | null = null; private asRecord!: IValue;
get isExecutionWithNoResult(): boolean { return this.base.isExecutionWithNoResult; }
constructor(sel: _ISelection, public name: string) { super(sel); }
*listSelectableIdentities(): Iterable<IValue> { this.init(); yield* super.listSelectableIdentities(); yield this.asRecord; }

rebuild() { this._columns = null; this.oldToThis.clear(); this.thisToOld.clear(); }
selectAlias(alias: string): _IAlias | nil { if (this.name === alias) { return this; } return null; }
listColumns(): Iterable<IValue> { return this.columns; }

get debugId() { return this.base.debugId; }
get columns(): ReadonlyArray<IValue<any>> { this.init(); return this._columns!; } init() { if (this._columns) { return; } this._columns = this.base.columns.map(x => { const ret = x.setOrigin(this); this.oldToThis.set(x, ret); this.thisToOld.set(ret, x); return ret; });
// how to build a record out of this alias? this.asRecord = new Evaluator( RecordType.from(this) , this.name , Math.random().toString() , this._columns , v => ({ ...v }) , { forceNotConstant: true }); }
stats(t: _Transaction): Stats | null { return this.base.stats(t); }
enumerate(t: _Transaction): Iterable<T> { return this.base.enumerate(t); }
hasItem(value: T, t: _Transaction): boolean { return this.base.hasItem(value, t); }
getColumn(column: string | ExprRef): IValue; getColumn(column: string | ExprRef, nullIfNotFound?: boolean): IValue | nil; getColumn(column: string | ExprRef, nullIfNotFound?: boolean): IValue | nil { const col = this._getColumn(column); if (col) { return col; }
if (asSingleName(column) === this.name) { return this.asRecord; }
if (nullIfNotFound) { return null; } throw new ColumnNotFound(colToStr(column)); }
private _getColumn(column: string | ExprRef): IValue | nil { if (typeof column !== 'string' && column.table) { if (!column.table.schema && column.table.name !== this.name) { return null; } column = column.name; } const got = this.base.getColumn(column, true); if (!got) { return got; } this.init(); const ret = this.oldToThis.get(got); if (!ret) { throw new Error('Corrupted alias'); } return ret; }
explain(e: _Explainer): _SelectExplanation { // no need to explain an alias... it does nothing. return this.base.explain(e); // return { // id: e.idFor(this), // type: 'alias', // alias: this.as, // of: this.base.explain(e), // }; }
getIndex(...forValue: IValue[]) { return this.base.getIndex(...forValue.map(v => this.thisToOld.get(v) ?? v)); }
}
pg_mem

Version Info

Tagged at
4 months ago