-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
limit.ts
68 lines (56 loc) · 1.86 KB
/
limit.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
import { IValue, _ISelection, _Transaction, _Explainer, _SelectExplanation, Stats, nil } from '../interfaces-private.ts';
import { FilterBase } from './transform-base.ts';
import { LimitStatement } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';
import { buildValue } from '../parser/expression-builder.ts';
import { withSelection } from '../parser/context.ts';
export function buildLimit(on: _ISelection, limit: LimitStatement) {
return withSelection(on, () => {
const l = limit.limit && buildValue(limit.limit);
const o = limit.offset && buildValue(limit.offset);
return new LimitFilter(on, l, o);
});
}
class LimitFilter<T = any> extends FilterBase<T> {
get index() {
return null;
}
entropy(t: _Transaction) {
return this.selection.entropy(t);
}
hasItem(raw: T, t: _Transaction): boolean {
return this.base.hasItem(raw, t);
}
constructor(private selection: _ISelection<T>, private take: IValue | nil, private skip: IValue | nil) {
super(selection);
}
stats(t: _Transaction): Stats | null {
return null;
}
*enumerate(t: _Transaction): Iterable<T> {
let skip = this.skip?.get(null, t) ?? 0;
let take = this.take?.get(null, t) ?? Number.MAX_SAFE_INTEGER;
if (take <= 0) {
return;
}
for (const raw of this.selection.enumerate(t)) {
if (skip > 0) {
skip--;
continue;
}
yield raw;
take--;
if (!take) {
return;
}
}
}
explain(e: _Explainer): _SelectExplanation {
return {
id: e.idFor(this),
_: 'limit',
take: this.take?.explain(e),
skip: this.skip?.explain(e),
on: this.selection.explain(e),
};
}
}