All files / src/model source-option-builder.ts

97.36% Statements 37/38
92.85% Branches 39/42
91.66% Functions 11/12
100% Lines 34/34

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                    131x 131x 131x   218x               131x             131x 131x                       258x 138x   120x 92x   28x                 27x                 138x 138x 138x 1x   139x 1x   138x       92x 92x 92x 1x   91x                           25x           30x         30x 2x   28x 28x 30x 1x   27x                
import { SqlExpression } from '../expressions/methods.ts';
import { SqlLiteral } from '../ddl/sql-literal.ts';
import { SqlAst, type ExprNode, type FromItem, type JsonTableColumn } from '../sql/ast.ts';
import { WhereCompiler } from '../where/where.ts';
import { validCastType } from '../where/operator-allowlist.ts';
import type { AttributeColumns } from './attribute-columns.ts';
import type { ModelRegistry } from './model-registry.ts';
import type { JsonTableColumnOptions, JsonTableOptions } from './source-options.ts';
import { TableSampleBuilder } from './table-sample-builder.ts';
 
const ast = new SqlAst();
const literals = new SqlLiteral();
const values = new WhereCompiler({ resolve: () => undefined });
 
const expr = (value: unknown): ExprNode => values.toExpr(value);
 
type Plain = Readonly<Record<string, unknown>>;
 
/** Converts the object forms of a `from` entry (`values`, `function`, `jsonTable`, `lateral`, `model` + `sample`). */
export class SourceOptionBuilder {
  readonly #registry: ModelRegistry;
  readonly #columns: AttributeColumns;
  readonly #samples = new TableSampleBuilder();
 
  /**
   * @param registry - Model definitions.
   * @param columns - Table references of models.
   */
  constructor(registry: ModelRegistry, columns: AttributeColumns) {
    this.#registry = registry;
    this.#columns = columns;
  }
 
  /**
   * Builds the FROM item of an object-form source.
   *
   * @param alias - Alias of the relation.
   * @param source - Source option.
   * @returns FROM item.
   * @throws Error for inconsistent options.
   */
  build(alias: string, source: Plain): FromItem {
    if ('values' in source) {
      return SourceOptionBuilder.#values(alias, source['values'] as Plain);
    }
    if ('function' in source) {
      return SourceOptionBuilder.#function(alias, source);
    }
    return 'jsonTable' in source || 'lateral' in source
      ? SourceOptionBuilder.#nested(alias, source)
      : {
          ...this.#columns.table(this.#registry.definition(source['model'] as never), alias),
          sample: this.#samples.build(source['sample'] as never),
        };
  }
 
  static #nested(alias: string, source: Plain): FromItem {
    return 'jsonTable' in source
      ? {
          jsonTable: SourceOptionBuilder.#jsonTable(source['jsonTable'] as JsonTableOptions),
          alias,
        }
      : { subquery: (source['lateral'] as { build(): never }).build(), alias, lateral: true };
  }
 
  static #values(alias: string, spec: Plain): FromItem {
    const columns = spec['columns'] as readonly string[];
    const rows = spec['rows'] as readonly (readonly unknown[])[];
    if (rows.length === 0 || columns.length === 0) {
      throw new Error('values needs at least one column and one row');
    }
    if (rows.some((row) => row.length !== columns.length)) {
      throw new Error('every row of values must have one cell per column');
    }
    return { values: rows.map((row) => row.map(expr)), alias, columns };
  }
 
  static #function(alias: string, source: Plain): FromItem {
    const ordinality = source['ordinality'] as string | undefined;
    const columns = source['columns'] as readonly string[] | undefined;
    if (ordinality !== undefined && columns === undefined) {
      throw new Error('function with ordinality needs columns');
    }
    return {
      function: String(source['function']).split('.'),
      args: ((source['args'] as readonly unknown[] | undefined) ?? []).map(expr),
      alias,
      ...(columns === undefined
        ? {}
        : { columns: ordinality === undefined ? columns : [...columns, ordinality] }),
      ...(ordinality === undefined ? {} : { ordinality }),
    };
  }
 
  static #jsonTable(
    options: JsonTableOptions,
  ): Extract<FromItem, { jsonTable: unknown }>['jsonTable'] {
    return {
      doc:
        options.doc instanceof SqlExpression
          ? expr(options.doc)
          : { kind: 'cast', expr: ast.param(JSON.stringify(options.doc)), type: 'jsonb' },
      path: literals.escape(options.path),
      columns: options.columns.map((column) => SourceOptionBuilder.#jsonColumn(column)),
    };
  }
 
  static #jsonColumn(column: JsonTableColumnOptions): JsonTableColumn {
    if (column.ordinality === true) {
      return { name: column.name, kind: 'ordinality' };
    }
    const exists = column.exists === true;
    const type = column.type ?? (exists ? 'boolean' : undefined);
    if (type === undefined) {
      throw new Error(`JSON_TABLE column "${column.name}" needs a type`);
    }
    return {
      name: column.name,
      kind: exists ? 'exists' : 'value',
      type: validCastType(type),
      ...(column.path === undefined ? {} : { path: literals.escape(column.path) }),
    };
  }
}