All files / src/query-interface object-commands.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 8/8
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 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                                131x   131x   131x                             387x                         227x                         77x                         243x                         92x                         93x                     101x       26x                                   92x             18x      
import {
  ExtensionDdl,
  type CreateExtensionOptions,
  type DropExtensionOptions,
} from '../ddl/extension-ddl.ts';
import {
  SequenceDdl,
  type AlterSequenceOptions,
  type CreateSequenceOptions,
  type DropSequenceOptions,
  type SequenceNumber,
} from '../ddl/sequence-ddl.ts';
import { SqlLiteral, type TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase } from '../model/runtime.ts';
import type { StatementRunner } from './statement-runner.ts';
 
const extensions = new ExtensionDdl();
 
const sequences = new SequenceDdl();
 
const sqlLiteral = new SqlLiteral();
 
/**
 * Extension and sequence DDL of `QueryInterface`. Names are quoted identifiers; sequence numbers
 * are validated integers; `nextval` / `setval` bind everything.
 *
 * @internal
 */
export class ObjectCommands {
  readonly #runner: StatementRunner;
 
  /**
   * @param runner - Statement runner.
   */
  constructor(runner: StatementRunner) {
    this.#runner = runner;
  }
 
  /**
   * Runs `CREATE EXTENSION`.
   *
   * @param name - Extension name.
   * @param options - Statement and query options.
   */
  async createExtension(
    name: string,
    options: CreateExtensionOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(extensions.createSql(name, options), options);
  }
 
  /**
   * Runs `DROP EXTENSION`.
   *
   * @param name - Extension name.
   * @param options - Statement and query options.
   */
  async dropExtension(
    name: string,
    options: DropExtensionOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(extensions.dropSql(name, options), options);
  }
 
  /**
   * Runs `CREATE SEQUENCE`.
   *
   * @param name - Sequence name.
   * @param options - Sequence and query options.
   */
  async createSequence(
    name: TableName,
    options: CreateSequenceOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(sequences.createSql(name, options), options);
  }
 
  /**
   * Runs `ALTER SEQUENCE`.
   *
   * @param name - Sequence name.
   * @param options - Properties to change and query options.
   */
  async alterSequence(
    name: TableName,
    options: AlterSequenceOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(sequences.alterSql(name, options), options);
  }
 
  /**
   * Runs `DROP SEQUENCE`.
   *
   * @param name - Sequence name.
   * @param options - Statement and query options.
   */
  async dropSequence(
    name: TableName,
    options: DropSequenceOptions & QueryOptionsBase,
  ): Promise<void> {
    await this.#runner.run(sequences.dropSql(name, options), options);
  }
 
  /**
   * Advances a sequence and returns the new value.
   *
   * @param name - Sequence name.
   * @param options - Query options.
   * @returns Value as text (bigint safe).
   */
  async nextval(name: TableName, options: QueryOptionsBase): Promise<string> {
    const result = await this.#runner.execute(
      { text: 'SELECT nextval($1::regclass) AS value', values: [sqlLiteral.quoteTable(name)] },
      options,
    );
    return String(result.rows[0]?.['value']);
  }
 
  /**
   * Sets the current value of a sequence.
   *
   * @param name - Sequence name.
   * @param value - New value.
   * @param isCalled - `false` makes the next `nextval` return `value` itself.
   * @param options - Query options.
   * @returns The value set, as text.
   */
  async setval(
    name: TableName,
    value: SequenceNumber,
    isCalled: boolean,
    options: QueryOptionsBase,
  ): Promise<string> {
    const result = await this.#runner.execute(
      {
        text: 'SELECT setval($1::regclass, $2::bigint, $3::boolean) AS value',
        values: [sqlLiteral.quoteTable(name), value.toString(), isCalled],
      },
      options,
    );
    return String(result.rows[0]?.['value']);
  }
}