All files / src/sql sql-chunks.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 7/7
100% Lines 14/14

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                       100438x                   60773x 60773x 249829x 189083x   249829x 618877x     60773x                   41728x                   4399x                   13276x   26552x                               2x      
import type { TableRef } from './ast.ts';
import { IdentifierQuoter } from './identifier-quoter.ts';
 
const quoter = new IdentifierQuoter();
 
/** Bound value inside a chunk list; the same chunk object maps to the same `$n`. */
export type ParamChunk = Readonly<{ param: unknown }>;
/** SQL text or a bound value. */
export type Chunk = string | ParamChunk;
/** Chunk list rendered by `SqlCompiler.finalize`. */
export type Chunks = readonly Chunk[];
 
/** Shared chunk helpers of the SQL compilers: quoting, wrapping, joining and table references. */
export class ChunkBuilder {
  /** Quotes an identifier: `"name"`, doubling embedded quotes. */
  protected readonly quote = (name: string): string => quoter.quote(name);
 
  /**
   * Joins chunk lists with a separator.
   *
   * @param parts - Chunk lists.
   * @param separator - SQL text between the lists.
   * @returns Joined chunks.
   */
  protected joinAll(parts: readonly Chunks[], separator: string): Chunks {
    const joined: Chunk[] = [];
    for (const [index, part] of parts.entries()) {
      if (index > 0) {
        joined.push(separator);
      }
      for (const chunk of part) {
        joined.push(chunk);
      }
    }
    return joined;
  }
 
  /**
   * Wraps chunks in parentheses.
   *
   * @param chunks - Chunks.
   * @returns `(chunks)`.
   */
  protected wrap(chunks: Chunks): Chunks {
    return ['(', ...chunks, ')'];
  }
 
  /**
   * Parenthesized quoted column list.
   *
   * @param columns - Column names.
   * @returns `("a", "b")`.
   */
  protected columnList(columns: readonly string[]): Chunks {
    return this.wrap([columns.map(this.quote).join(', ')]);
  }
 
  /**
   * Quoted table reference with an optional alias.
   *
   * @param table - Table reference.
   * @returns `"schema"."name" AS "alias"`.
   */
  protected tableRef(table: TableRef): Chunks {
    return [
      [table.schema, table.name]
        .filter((part): part is string => part !== undefined)
        .map(this.quote)
        .join('.'),
      ...(table.alias === undefined || table.alias === table.name
        ? []
        : [` AS ${this.quote(table.alias)}`]),
    ];
  }
 
  /**
   * Exhaustiveness guard of node switches.
   *
   * @param value - Unhandled node.
   * @throws Error always.
   */
  protected unexpected(value: never): never {
    throw new Error(`Unexpected AST node: ${JSON.stringify(value)}`);
  }
}