All files / src/sql compile.ts

100% Statements 34/34
100% Branches 12/12
100% Functions 8/8
100% Lines 33/33

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                        154x   154x 1x         551x 551x 551x     551x         15239x         12432x         425x         14040x 14040x 14040x 14040x 625292x 451512x 451512x   173780x 173780x 173778x 173778x 173778x   173780x   14040x       12661x   7225x   8x   4006x   296x   178x   861x   86x   1x        
import type { ExprNode, StatementNode } from './ast.ts';
import { DmlCompiler } from './dml-compiler.ts';
import { IdentifierQuoter } from './identifier-quoter.ts';
import { MergeCompiler } from './merge-compiler.ts';
import { QueryCompiler } from './query-compiler.ts';
import type { Chunks, ParamChunk } from './sql-chunks.ts';
 
export type { Chunk, Chunks, ParamChunk } from './sql-chunks.ts';
 
/** Parameterized SQL text with its bound values. */
export type CompiledQuery = Readonly<{ text: string; values: readonly unknown[] }>;
 
const quoter = new IdentifierQuoter();
 
const assertNever = (value: never): never => {
  throw new Error(`Unexpected AST node: ${JSON.stringify(value)}`);
};
 
/** Compiles SQL AST nodes into parameterized text with `$n` placeholders. Stateless. */
export class SqlCompiler {
  readonly #query = new QueryCompiler();
  readonly #dml = new DmlCompiler(this.#query);
  readonly #merge = new MergeCompiler(this.#query, this.#dml);
 
  constructor() {
    this.#query.useModifying((node) => this.#statement(node));
  }
 
  /** Quotes an identifier: `"name"`, doubling embedded quotes. */
  static quoteIdent(name: string): string {
    return quoter.quote(name);
  }
 
  /** Compiles a statement. */
  compile(node: StatementNode): CompiledQuery {
    return this.finalize(this.#statement(node));
  }
 
  /** Compiles a standalone expression. */
  compileExpr(node: ExprNode): CompiledQuery {
    return this.finalize(this.#query.expr.expression(node));
  }
 
  /** Joins chunks into text, numbering distinct parameter chunks `$1`, `$2`, ... */
  finalize(chunks: Chunks): CompiledQuery {
    const positions = new Map<ParamChunk, number>();
    const values: unknown[] = [];
    let text = '';
    for (const chunk of chunks) {
      if (typeof chunk === 'string') {
        text += chunk;
        continue;
      }
      let position = positions.get(chunk);
      if (position === undefined) {
        values.push(chunk.param);
        position = values.length;
        positions.set(chunk, position);
      }
      text += `$${position}`;
    }
    return { text, values };
  }
 
  #statement(node: StatementNode): Chunks {
    switch (node.kind) {
      case 'select':
        return this.#query.select(node);
      case 'setop':
        return this.#query.query(node);
      case 'insert':
        return this.#dml.insert(node);
      case 'update':
        return this.#dml.update(node);
      case 'delete':
        return this.#dml.delete(node);
      case 'truncate':
        return this.#dml.truncate(node);
      case 'merge':
        return this.#merge.merge(node);
      default:
        return assertNever(node);
    }
  }
}