All files / src/ddl function-ddl.ts

100% Statements 27/27
100% Branches 18/18
100% Functions 14/14
100% Lines 24/24

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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192      132x   132x                                                                                                                                                                                             619x 619x         9x                                 94x                       75x       640x 1x       639x   236x         39x             1855x 1x                 618x       248x 248x 248x         76x 76x       248x 248x 2x   248x      
import { RoutineGuards } from './routine-guards.ts';
import { SqlLiteral } from './sql-literal.ts';
 
const guards = new RoutineGuards();
 
const sqlLiteral = new SqlLiteral();
 
/** Function reference: a plain name (`'touch'`, `'app.touch'`) or `{ name, schema }` (quoted exactly). */
export type FunctionName =
  | string
  | Readonly<{
      /** Function name, quoted exactly. */
      name: string;
      /** Schema name, quoted exactly. */
      schema?: string;
    }>;
 
/** Parameter of a function. */
export type FunctionParam = Readonly<{
  /** SQL type, e.g. `'integer'`, `'varchar(10)'`, `'text[]'`. */
  type: string;
  /** Parameter name (identifier word). */
  name?: string;
  /** `IN`, `OUT`, `INOUT` or `VARIADIC`. */
  direction?: string;
}>;
 
/** Local variable declared before `BEGIN`. */
export type FunctionVariable = Readonly<{
  /** Variable name (identifier word). */
  name: string;
  /** SQL type. */
  type: string;
  /** Initial value, rendered as a literal. */
  default?: unknown;
}>;
 
/**
 * Options of `createFunction`.
 *
 * @example
 * ```ts
 * const options: CreateFunctionOptions = {
 *   force: true,
 *   variables: [{ name: 'limit_rows', type: 'integer', default: 100 }],
 * };
 * await queryInterface.createFunction('f', [], 'integer', 'plpgsql', 'RETURN limit_rows;', [], options);
 * ```
 */
export type CreateFunctionOptions = Readonly<{
  /** Local variables (`DECLARE name type := default;`). */
  variables?: readonly FunctionVariable[];
  /** `CREATE OR REPLACE FUNCTION`. */
  force?: boolean;
}>;
 
/**
 * Options of `dropFunction`.
 *
 * @example
 * ```ts
 * const options: DropFunctionOptions = { ifExists: true, cascade: true };
 * await queryInterface.dropFunction('f', [], options);
 * ```
 */
export type DropFunctionOptions = Readonly<{
  /** Adds `IF EXISTS`. */
  ifExists?: boolean;
  /** `CASCADE` instead of the default `RESTRICT`. */
  cascade?: boolean;
}>;
 
/**
 * `CREATE`, `DROP` and `ALTER ... RENAME` statements for functions (Sequelize v6 shapes).
 *
 * @internal
 */
export class FunctionDdl {
  /**
   * `CREATE [OR REPLACE] FUNCTION` statement; the body is wrapped in `BEGIN ... END;` like v6.
   *
   * @param name - Function name.
   * @param params - Parameters.
   * @param returnType - Return type.
   * @param language - Language word.
   * @param body - Trusted SQL / PL/pgSQL text.
   * @param optionsArray - Characteristic keywords.
   * @param options - Statement options.
   * @returns Statement.
   * @throws Error for missing parts or values outside the guards.
   */
  createSql(
    name: FunctionName,
    params: readonly FunctionParam[],
    returnType: string,
    language: string,
    body: string,
    optionsArray: readonly string[],
    options: CreateFunctionOptions,
  ): string {
    this.#require([returnType, language, body]);
    return [
      this.#head(name, params, options),
      ` RETURNS ${guards.typeName(returnType, 'return type')}`,
      this.#body(body, options.variables ?? []),
      ` LANGUAGE ${guards.word(language, 'language')}`,
      ...guards.functionOptions(optionsArray).map((option) => ` ${option}`),
    ].join('');
  }
 
  /**
   * `DROP FUNCTION` statement (`RESTRICT` unless `cascade`).
   *
   * @param name - Function name.
   * @param params - Parameters identifying the overload.
   * @param options - Statement options.
   * @returns Statement.
   */
  dropSql(
    name: FunctionName,
    params: readonly FunctionParam[],
    options: DropFunctionOptions,
  ): string {
    return `DROP FUNCTION ${options.ifExists === true ? 'IF EXISTS ' : ''}${guards.functionName(name)}(${this.#params(params)}) ${options.cascade === true ? 'CASCADE' : 'RESTRICT'}`;
  }
 
  /**
   * `ALTER FUNCTION ... RENAME TO` statement.
   *
   * @param name - Current name.
   * @param params - Parameters identifying the overload.
   * @param newName - New unqualified name.
   * @returns Statement.
   */
  renameSql(name: FunctionName, params: readonly FunctionParam[], newName: string): string {
    return `ALTER FUNCTION ${guards.functionName(name)}(${this.#params(params)}) RENAME TO ${guards.word(newName, 'function name')}`;
  }
 
  #params(params: readonly FunctionParam[]): string {
    if (!Array.isArray(params)) {
      throw new Error(
        'function parameters array required, including an empty one for no arguments',
      );
    }
    return params
      .map((param) =>
        [
          param.direction === undefined ? '' : guards.direction(param.direction),
          param.name === undefined ? '' : guards.word(param.name, 'parameter name'),
          guards.typeName(param.type, 'parameter type'),
        ]
          .filter((part) => part !== '')
          .join(' '),
      )
      .join(', ');
  }
 
  #require(parts: readonly string[]): void {
    if (parts.some((part) => part === '')) {
      throw new Error('createFunction requires functionName, returnType, language and body');
    }
  }
 
  #head(
    name: FunctionName,
    params: readonly FunctionParam[],
    options: CreateFunctionOptions,
  ): string {
    return `CREATE ${options.force === true ? 'OR REPLACE ' : ''}FUNCTION ${guards.functionName(name)}(${this.#params(params)})`;
  }
 
  #body(body: string, variables: readonly FunctionVariable[]): string {
    const tag = this.#tag(body);
    const declared = variables.map((variable) => this.#variable(variable)).join(' ');
    return ` AS ${tag} ${declared} BEGIN ${body} END; ${tag}`;
  }
 
  #variable(variable: FunctionVariable): string {
    const initial =
      variable.default === undefined ? '' : ` := ${sqlLiteral.escape(variable.default)}`;
    return `DECLARE ${guards.word(variable.name, 'variable name')} ${guards.typeName(variable.type, 'variable type')}${initial};`;
  }
 
  #tag(body: string): string {
    let tag = '$func$';
    for (let index = 1; body.includes(tag); index += 1) {
      tag = `$func${index}$`;
    }
    return tag;
  }
}