Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 144x 8x 5x 9x 14x 9x 7x 7x 2x | import type { Over } from '../expressions/methods.ts';
import type { ExprNode, OrderNode } from '../sql/ast.ts';
import { OrderDirection } from '../sql/order.ts';
import type { OperandResolver } from './operand-resolver.ts';
import type { ExprConverter } from './where-support.ts';
/** Compiles window function calls (`over()`): `PARTITION BY` and `ORDER BY` items. */
export class WindowCompiler {
readonly #operands: OperandResolver;
/**
* @param operands - Resolves attribute names.
*/
constructor(operands: OperandResolver) {
this.#operands = operands;
}
/** `fn OVER (PARTITION BY ... ORDER BY ...)`. */
over(compiler: ExprConverter, value: Over): ExprNode {
return {
kind: 'over',
expr: compiler.toExpr(value.fn),
partitionBy: value.partitionBy.map((item) => this.#target(compiler, item)),
orderBy: value.orderBy.map((item) => this.#order(compiler, item)),
};
}
#target(compiler: ExprConverter, item: unknown): ExprNode {
return typeof item === 'string'
? this.#operands.operand(compiler.context, item).node
: compiler.toExpr(item);
}
#order(compiler: ExprConverter, item: unknown): OrderNode {
if (Array.isArray(item)) {
const [target, direction]: readonly unknown[] = item;
return {
expr: this.#target(compiler, target),
...(direction === undefined ? {} : OrderDirection.parse(direction)),
};
}
return { expr: this.#target(compiler, item) };
}
}
|