Repository navigation
Expand file tree
/
Copy pathrender-tracker.ts
More file actions
82 lines (73 loc) · 2.99 KB
/
Copy pathrender-tracker.ts
File metadata and controls
82 lines (73 loc) · 2.99 KB
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
// What a clause turned out to contain, learned while rendering it.
//
// Fragments are opaque until they render, so the only place to learn whether a
// SELECT expression aggregates, or which columns it reads outside an aggregate,
// is the render itself. `compile` renders each clause under `track` and reads
// the result; column identifiers and the builder's own functions report into
// it as they render.
//
// Only what the builder can vouch for is counted. A fragment it did not build
// (a `rawExpr`, a `sql` template, a function a caller declared with `makeExpr`)
// renders as opaque: nothing inside it is counted, so an unknown function is
// never mistaken for a scalar one wrapping a bare column. The checks built on
// this can miss an error inside opaque SQL, never invent one.
//
// The track is kept in the rendering fiber's context (see `fiber-local.ts`),
// and every entry point saves and restores, so a nested compile (a subquery)
// and a callback that throws leave no state behind.
import { Context } from "effect"
import { fiberLocal } from "./fiber-local"
/** What one tracked render found. */
export interface RenderTrack {
/** An aggregate function was rendered outside any window. */
aggregate: boolean
/** Columns read outside any aggregate, window or opaque fragment, as
* `qualifier.name` (or `name`). */
readonly columns: Set<string>
}
interface State {
readonly track: RenderTrack
aggregateDepth: number
/** Inside a window or an opaque fragment: nothing is counted. */
hiddenDepth: number
}
const current = fiberLocal(
Context.Reference<State | undefined>("@maple-dev/effect-orm/internal/RenderTrack", { defaultValue: () => undefined }),
)
/** Render `body` with a fresh track, returning both. */
export function track<A>(body: () => A): [A, RenderTrack] {
const state: State = { track: { aggregate: false, columns: new Set() }, aggregateDepth: 0, hiddenDepth: 0 }
return [current.with(state, body), state.track]
}
/** Render `body` with no track: a nested query's columns are its own. */
export function untracked<A>(body: () => A): A {
return current.with(undefined, body)
}
/** A column identifier rendered. */
export function noteColumn(name: string): void {
const state = current.get()
if (state !== undefined && state.aggregateDepth === 0 && state.hiddenDepth === 0) state.track.columns.add(name)
}
/** Render an aggregate call: it marks the clause, and its arguments are not bare columns. */
export function inAggregate(render: () => string): string {
const state = current.get()
if (state === undefined) return render()
if (state.hiddenDepth === 0) state.track.aggregate = true
state.aggregateDepth++
try {
return render()
} finally {
state.aggregateDepth--
}
}
/** Render a window (`… OVER (…)`) or SQL the builder did not write: nothing inside is counted. */
export function hidden(render: () => string): string {
const state = current.get()
if (state === undefined) return render()
state.hiddenDepth++
try {
return render()
} finally {
state.hiddenDepth--
}
}