deno.land / x / pg_mem@2.8.1 / transforms / transform-base.ts

transform-base.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
// <== THERE MUST BE NO ACTUAL IMPORTS OTHER THAN IMPORT TYPES (dependency loop)// ... use 'kind-of' dependency injection belowimport type { _ISelection, IValue, _IIndex, _ISchema, _IDb, _Transaction, _SelectExplanation, _Explainer, Stats, nil, _IAlias } from '../interfaces-private.ts';import type { buildSelection } from './selection.ts';import type { buildAlias } from './alias.ts';import type { buildFilter } from './build-filter.ts';import type { buildGroupBy } from './aggregation.ts';import type { buildLimit } from './limit.ts';import type { buildUnion } from './union.ts';import type { buildOrderBy } from './order-by.ts';import type { buildDistinct } from './distinct.ts';
import { Expr, SelectedColumn, SelectStatement, LimitStatement, OrderByStatement, ExprRef } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { RestrictiveIndex } from './restrictive-index.ts';
interface Fns { buildSelection: typeof buildSelection; buildAlias: typeof buildAlias; buildLimit: typeof buildLimit; buildUnion: typeof buildUnion; buildFilter: typeof buildFilter; buildGroupBy: typeof buildGroupBy; buildOrderBy: typeof buildOrderBy; buildDistinct: typeof buildDistinct;}let fns: Fns;export function initialize(init: Fns) { fns = init;}
export abstract class DataSourceBase<T> implements _ISelection<T> { abstract enumerate(t: _Transaction): Iterable<T>; abstract entropy(t: _Transaction): number; abstract readonly columns: ReadonlyArray<IValue<any>>; abstract getColumn(column: string, nullIfNotFound?: boolean): IValue<any>; abstract hasItem(value: T, t: _Transaction): boolean; abstract getIndex(forValue: IValue): _IIndex<any> | null | undefined; abstract explain(e: _Explainer): _SelectExplanation; abstract isOriginOf(a: IValue<any>): boolean; abstract stats(t: _Transaction): Stats | null; abstract get isExecutionWithNoResult(): boolean isAggregation(): boolean { return false; } // abstract get name(): string;

get db() { return this.ownerSchema.db; }
constructor(readonly ownerSchema: _ISchema) { }
listColumns(): Iterable<IValue> { return this.columns; }
listSelectableIdentities(): Iterable<IValue> { return this.columns; }
select(select: (string | SelectedColumn)[] | nil): _ISelection<any> { let sel: SelectedColumn[] | nil; if (select?.some(v => typeof v === 'string')) { sel = select.map<SelectedColumn>(v => typeof v !== 'string' ? v : { expr: { type: 'ref', name: v }, }) } else { sel = select as SelectedColumn[] | nil; } return fns.buildSelection(this, sel); }

selectAlias(alias: string): _IAlias | nil { return null; }

filter(filter: Expr | undefined | null): _ISelection { if (!filter) { return this; } const plan = fns.buildFilter(this, filter, 'WHERE'); return plan; }
groupBy(grouping: Expr[] | nil): _ISelection { if (!grouping?.length) { return this; } const plan = fns.buildGroupBy(this, grouping); return plan; }

setAlias(alias?: string): _ISelection<any> { return fns.buildAlias(this, alias); }
limit(limit: LimitStatement): _ISelection { if (!limit?.limit && !limit?.offset) { return this; } return fns.buildLimit(this, limit) }
orderBy(orderBy: OrderByStatement[] | nil): _ISelection<any> { if (!orderBy?.length) { return this; } return fns.buildOrderBy(this, orderBy); }
distinct(exprs?: Expr[]): _ISelection<any> { return fns.buildDistinct(this, exprs); }
union(right: _ISelection<any>): _ISelection<any> { return fns.buildUnion(this, right); }
}

export abstract class TransformBase<T> extends DataSourceBase<T> {

constructor(readonly base: _ISelection) { super(base.ownerSchema); }
get isExecutionWithNoResult(): boolean { return false; }
entropy(t: _Transaction): number { return this.base.entropy(t); }
isOriginOf(a: IValue<any>): boolean { return a.origin === this || this.base?.isOriginOf(a); }}
export abstract class FilterBase<T> extends TransformBase<T> {
isAggregation(): boolean { return false; }
constructor(_base: _ISelection<T>) { super(_base); }
get columns(): ReadonlyArray<IValue<any>> { return this.base.columns; }
selectAlias(alias: string): nil | _IAlias { // this is a filter... that the alias returned by the unfiltered datasource will // be valid for the filtered datasource (aliases are only listing columns) return this.base.selectAlias(alias); }
/** private _columns: IValue[]; private _columnMappings: Map<IValue, IValue>; get columns(): ReadonlyArray<IValue<any>> { this.initCols(); return this._columns; // return this.base.columns; }
private initCols() { if (this._columns) { return; } this._columns = []; this._columnMappings = new Map(); for (const c of this.base.columns) { const nc = c.setOrigin(this); this._columns.push(nc); this._columnMappings.set(c, nc); } }
getColumn(column: string, nullIfNotFound?: boolean): IValue<any> { if (!this.base) { // istanbul ignore next throw new Error('Should not call .getColumn() on join'); } if (!('columns' in this.base)) { // istanbul ignore next throw new Error('Should not call getColumn() on table'); } this.initCols(); const col = this.base.getColumn(column, nullIfNotFound); return col && this._columnMappings.get(col); } */
getColumn(column: string | ExprRef): IValue; getColumn(column: string | ExprRef, nullIfNotFound?: boolean): IValue | nil; getColumn(column: string | ExprRef, nullIfNotFound?: boolean): IValue<any> | nil { if (!this.base) { // istanbul ignore next throw new Error('Should not call .getColumn() on join'); } if (!('columns' in this.base)) { // istanbul ignore next throw new Error('Should not call getColumn() on table'); } return this.base.getColumn(column, nullIfNotFound); }
getIndex(...forValue: IValue<any>[]): _IIndex<any> | null | undefined { const index = this.base.getIndex(...forValue); if (!index) { return null; } return new RestrictiveIndex(index, this); }}
pg_mem

Version Info

Tagged at
4 months ago