All files / src/sql merge-compiler.ts

100% Statements 16/16
100% Branches 11/11
100% Functions 7/7
100% Lines 15/15

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                      170x             551x 551x 551x 551x                   86x                         86x                   170x                     170x   82x   4x   2x   81x   1x         81x                    
import type { MergeAction, MergeNode, MergeWhen } from './ast.ts';
import type { DmlCompiler } from './dml-compiler.ts';
import type { ExpressionCompiler } from './expression-compiler.ts';
import type { QueryCompiler } from './query-compiler.ts';
import { ChunkBuilder, type Chunks } from './sql-chunks.ts';
 
/** Compiles MERGE statements. */
export class MergeCompiler extends ChunkBuilder {
  readonly #query: QueryCompiler;
  readonly #expr: ExpressionCompiler;
  readonly #dml: DmlCompiler;
  readonly #whenClause = (clause: MergeWhen): Chunks => this.#when(clause);
 
  /**
   * @param query - SELECT compiler; its expression compiler is shared.
   * @param dml - SET and RETURNING clauses.
   */
  constructor(query: QueryCompiler, dml: DmlCompiler) {
    super();
    this.#query = query;
    this.#expr = query.expr;
    this.#dml = dml;
  }
 
  /**
   * Compiles a MERGE statement.
   *
   * @param node - MERGE node.
   * @returns Chunks.
   */
  merge(node: MergeNode): Chunks {
    return [
      'MERGE INTO ',
      ...this.tableRef(node.table),
      ' USING ',
      ...this.#source(node.source),
      ' ON ',
      ...this.#expr.expression(node.on),
      ...node.when.flatMap(this.#whenClause),
      ...this.#dml.returning(node.returning, node.returningOld),
    ];
  }
 
  #source(source: MergeNode['source']): Chunks {
    return source.kind === 'query'
      ? [...this.wrap(this.#query.query(source.query)), ` AS ${this.quote(source.alias)}`]
      : [
          ...this.wrap(['VALUES ', ...this.#expr.rows(source.rows)]),
          ` AS ${this.quote(source.alias)} `,
          ...this.columnList(source.columns),
        ];
  }
 
  #when(clause: MergeWhen): Chunks {
    return [
      ` WHEN ${clause.match}`,
      ...(clause.condition === undefined
        ? []
        : [' AND ', ...this.#expr.expression(clause.condition)]),
      ' THEN ',
      ...this.#action(clause.action),
    ];
  }
 
  #action(action: MergeAction): Chunks {
    switch (action.kind) {
      case 'update':
        return ['UPDATE SET ', ...this.#dml.setList(action.set)];
      case 'delete':
        return ['DELETE'];
      case 'nothing':
        return ['DO NOTHING'];
      case 'insert':
        return this.#insert(action);
      default:
        return this.unexpected(action);
    }
  }
 
  #insert(action: Extract<MergeAction, Readonly<{ kind: 'insert' }>>): Chunks {
    return action.columns.length === 0
      ? ['INSERT DEFAULT VALUES']
      : [
          'INSERT ',
          ...this.columnList(action.columns),
          ' VALUES ',
          ...this.wrap(this.#expr.exprList(action.values)),
        ];
  }
}