All files / src/ddl enum-ddl.ts

100% Statements 14/14
100% Branches 10/10
100% Functions 7/7
100% Lines 13/13

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        134x                               183x                     175x 175x 175x                     2803x 2803x 2718x   172x 85x                     29x   5x         2832x              
import { SqlCompiler } from '../sql/compile.ts';
import type { ColumnDefinition } from './ddl-types.ts';
import { SqlLiteral, type TableName } from './sql-literal.ts';
 
const sqlLiteral = new SqlLiteral();
 
/**
 * PostgreSQL enum types of `ENUM` and `ARRAY(ENUM)` columns (`enum_<table>_<column>`, v6).
 *
 * @internal
 */
export class EnumDdl {
  /**
   * Name of the PostgreSQL enum type of a column (`enum_<table>_<column>`, v6).
   *
   * @param table - Table name or `{ tableName, schema }`.
   * @param field - Column name.
   * @returns Unquoted enum type name.
   */
  typeName(table: TableName, field: string): string {
    return `enum_${sqlLiteral.tableParts(table).name}_${field}`;
  }
 
  /**
   * Quoted, schema-qualified enum type of a column.
   *
   * @param table - Table name or `{ tableName, schema }`.
   * @param field - Column name.
   * @returns Quoted type name.
   */
  quotedType(table: TableName, field: string): string {
    const { schema } = sqlLiteral.tableParts(table);
    const name = SqlCompiler.quoteIdent(this.typeName(table, field));
    return schema === undefined ? name : `${SqlCompiler.quoteIdent(schema)}.${name}`;
  }
 
  /**
   * `CREATE TYPE ... AS ENUM` wrapped so an existing type is not an error (v6).
   *
   * @param table - Table name or `{ tableName, schema }`.
   * @param column - Column definition.
   * @returns Statement, or `undefined` for non-enum columns.
   */
  createSql(table: TableName, column: ColumnDefinition): string | undefined {
    const values = this.#values(column);
    if (values === undefined) {
      return undefined;
    }
    const body = `CREATE TYPE ${this.quotedType(table, column.field)} AS ENUM(${values.map((value) => sqlLiteral.escape(value)).join(', ')}); EXCEPTION WHEN duplicate_object THEN null;`;
    return `DO ${sqlLiteral.escape(`BEGIN ${body} END`)}`;
  }
 
  /**
   * `ALTER TYPE ... ADD VALUE IF NOT EXISTS` for every enum value of a column.
   *
   * @param table - Table name or `{ tableName, schema }`.
   * @param column - Column definition.
   * @returns Statements; empty for non-enum columns.
   */
  addValuesSql(table: TableName, column: ColumnDefinition): readonly string[] {
    return (this.#values(column) ?? []).map(
      (value) =>
        `ALTER TYPE ${this.quotedType(table, column.field)} ADD VALUE IF NOT EXISTS ${sqlLiteral.escape(value)}`,
    );
  }
 
  #values(column: ColumnDefinition): readonly string[] | undefined {
    return column.type.key === 'ENUM'
      ? column.type.values
      : column.type.subtype?.key === 'ENUM'
        ? column.type.subtype.values
        : undefined;
  }
}