All files / src/sql query-compiler.ts

93.93% Statements 31/33
100% Branches 74/74
94.44% Functions 17/18
96.55% Lines 28/29

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 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                                        4x 174x     551x 551x 551x 551x                   7890x                   391x                 551x                   8355x 250x           8105x 137x   7968x 96x   7872x                   4x       7890x 7890x                   287x         7890x                                         174x 174x                       16x                   277x                   554x           554x       267x          
import type {
  FromItem,
  JoinNode,
  LockNode,
  QueryNode,
  SelectNode,
  SetOperationNode,
} from './ast.ts';
import { WithCompiler, type CteBodyCompiler } from './with-compiler.ts';
import { FromCompiler } from './from-compiler.ts';
import { ExpressionCompiler } from './expression-compiler.ts';
import { ChunkBuilder, type Chunks } from './sql-chunks.ts';
 
/** Compiles SELECT statements with their FROM, JOIN, WITH and locking clauses. */
export class QueryCompiler extends ChunkBuilder {
  /** Expression compiler that compiles subqueries through this compiler. */
  readonly expr: ExpressionCompiler;
  readonly #sources: FromCompiler;
  readonly #with: WithCompiler;
  #modifying: CteBodyCompiler = (node) => this.unexpected(node as never);
  readonly #fromItem = (from: FromItem): Chunks => this.from(from);
  readonly #joinClause = (node: JoinNode): Chunks => this.#join(node);
 
  constructor() {
    super();
    this.expr = new ExpressionCompiler(this);
    this.#sources = new FromCompiler(this.expr);
    this.#with = new WithCompiler((node) => this.#cteBody(node));
  }
 
  /**
   * Compiles a SELECT statement.
   *
   * @param node - SELECT node.
   * @returns Chunks.
   */
  select(node: SelectNode): Chunks {
    return [...this.#selectHead(node), ...this.#selectTail(node)];
  }
 
  /**
   * Compiles a query expression: a SELECT or a set operation.
   *
   * @param node - Query node.
   * @returns Chunks.
   */
  query(node: QueryNode): Chunks {
    return node.kind === 'setop' ? this.#setOperation(node) : this.select(node);
  }
 
  /**
   * Sets the compiler of data-modifying CTE bodies (which live in the DML compiler).
   *
   * @param compile - Compiler of INSERT / UPDATE / DELETE statements.
   */
  useModifying(compile: CteBodyCompiler): void {
    this.#modifying = compile;
  }
 
  /**
   * One FROM source: a table or an aliased subquery.
   *
   * @param from - FROM source.
   * @returns Chunks.
   */
  from(from: FromItem): Chunks {
    if ('subquery' in from) {
      return [
        from.lateral === true ? 'LATERAL ' : '',
        ...this.wrap(this.query(from.subquery)),
        ` AS ${this.quote(from.alias)}`,
      ];
    }
    if ('values' in from) {
      return this.#sources.values(from);
    }
    if ('function' in from) {
      return this.#sources.function(from);
    }
    return 'jsonTable' in from ? this.#sources.jsonTable(from) : this.#sources.table(from);
  }
 
  /**
   * Comma-separated FROM sources.
   *
   * @param items - FROM sources.
   * @returns Chunks.
   */
  fromList(items: readonly FromItem[]): Chunks {
    return this.joinAll(items.map(this.#fromItem), ', ');
  }
 
  #selectHead(node: SelectNode): Chunks {
    const distinctOn = node.distinctOn ?? [];
    return [
      ...(node.with === undefined ? [] : this.#with.with(node.with)),
      node.distinct === true || distinctOn.length > 0 ? 'SELECT DISTINCT ' : 'SELECT ',
      ...(distinctOn.length === 0
        ? []
        : ['ON ', ...this.wrap(this.expr.exprList(distinctOn)), ' ']),
      ...this.expr.exprList(node.columns),
      ' FROM ',
      ...this.from(node.from),
      ...(node.joins ?? []).flatMap(this.#joinClause),
      ...(node.extraFrom ?? []).flatMap((item) => [', ', ...this.from(item)]),
    ];
  }
 
  #selectTail(node: SelectNode): Chunks {
    return [
      ...this.expr.where(node.where),
      ...(node.groupBy === undefined || node.groupBy.length === 0
        ? []
        : [' GROUP BY ', ...this.expr.exprList(node.groupBy)]),
      ...(node.having === undefined ? [] : [' HAVING ', ...this.expr.expression(node.having)]),
      ...(node.orderBy === undefined || node.orderBy.length === 0
        ? []
        : [' ORDER BY ', ...this.expr.orderList(node.orderBy)]),
      ...(node.limit === undefined || node.withTies === true
        ? []
        : [' LIMIT ', { param: node.limit }]),
      ...(node.offset === undefined ? [] : [' OFFSET ', { param: node.offset }]),
      ...(node.limit === undefined || node.withTies !== true
        ? []
        : [' FETCH FIRST ', { param: node.limit }, ' ROWS WITH TIES']),
      ...(node.lock === undefined ? [] : [this.#lock(node.lock)]),
    ];
  }
 
  #join(node: JoinNode): Chunks {
    const nested = node.nested ?? [];
    return nested.length === 0
      ? [` ${node.type} JOIN `, ...this.from(node.table), ' ON ', ...this.expr.expression(node.on)]
      : [
          ` ${node.type} JOIN ( `,
          ...this.from(node.table),
          ...nested.flatMap(this.#joinClause),
          ' ) ON ',
          ...this.expr.expression(node.on),
        ];
  }
 
  #lock(node: LockNode): string {
    return [
      ` FOR ${node.strength}`,
      node.of === undefined || node.of.length === 0
        ? ''
        : ` OF ${node.of.map(this.quote).join(', ')}`,
      node.wait === undefined ? '' : ` ${node.wait}`,
    ].join('');
  }
 
  #setOperation(node: SetOperationNode): Chunks {
    return [
      ...this.#operand(node.left),
      ` ${node.op}${node.all ? ' ALL' : ''} `,
      ...this.#operand(node.right),
    ];
  }
 
  // Parentheses only where the grammar needs them, so plain operands keep their text.
  #operand(node: QueryNode): Chunks {
    const needsParens =
      node.kind === 'setop' ||
      (node.orderBy?.length ?? 0) > 0 ||
      node.limit !== undefined ||
      node.offset !== undefined ||
      node.lock !== undefined ||
      node.with !== undefined;
    return needsParens ? this.wrap(this.query(node)) : this.select(node);
  }
 
  #cteBody(node: Parameters<CteBodyCompiler>[0]): Chunks {
    return node.kind === 'select' || node.kind === 'setop'
      ? this.query(node)
      : this.#modifying(node);
  }
}