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 | 131x 131x 387x 411x 411x 76x 76x 76x 76x 563x 298x 151x | import {
DdlGenerator,
type ConstraintOptions,
type IndexField,
type IndexOptions,
} from '../ddl/ddl-generator.ts';
import { SqlLiteral, type TableName } from '../ddl/sql-literal.ts';
import type { QueryOptionsBase } from '../model/runtime.ts';
import { SqlCompiler } from '../sql/compile.ts';
import type { StatementRunner } from './statement-runner.ts';
const ddl = new DdlGenerator();
const sqlLiteral = new SqlLiteral();
/**
* Index and constraint DDL of `QueryInterface`. Names are quoted identifiers.
*
* @internal
*/
export class IndexCommands {
readonly #runner: StatementRunner;
/**
* @param runner - Statement runner.
*/
constructor(runner: StatementRunner) {
this.#runner = runner;
}
/**
* Creates an index.
*
* @param table - Table name or `{ tableName, schema }`.
* @param fieldsOrOptions - Indexed fields or index options.
* @param options - Index options when fields are given separately.
*/
async addIndex(
table: TableName,
fieldsOrOptions: readonly IndexField[] | IndexOptions,
options: IndexOptions & QueryOptionsBase,
): Promise<void> {
const index: IndexOptions = Array.isArray(fieldsOrOptions)
? { ...options, fields: fieldsOrOptions as readonly IndexField[] }
: { ...(fieldsOrOptions as IndexOptions), ...options };
await this.#runner.run(ddl.createIndexSql(table, index), options);
}
/**
* Drops an index by name or by its fields (default name).
*
* @param table - Table name or `{ tableName, schema }`.
* @param nameOrFields - Index name or indexed fields.
* @param options - `concurrently` and query options.
*/
async removeIndex(
table: TableName,
nameOrFields: string | readonly IndexField[],
options: QueryOptionsBase & Readonly<{ concurrently?: boolean }>,
): Promise<void> {
const { schema } = sqlLiteral.tableParts(table);
const name =
typeof nameOrFields === 'string' ? nameOrFields : ddl.indexName(table, nameOrFields);
const qualified =
schema === undefined
? SqlCompiler.quoteIdent(name)
: `${SqlCompiler.quoteIdent(schema)}.${SqlCompiler.quoteIdent(name)}`;
await this.#runner.run(
`DROP INDEX ${options.concurrently === true ? 'CONCURRENTLY ' : ''}IF EXISTS ${qualified}`,
options,
);
}
/**
* Adds a constraint.
*
* @param table - Table name or `{ tableName, schema }`.
* @param options - Constraint definition.
*/
async addConstraint(
table: TableName,
options: ConstraintOptions & QueryOptionsBase,
): Promise<void> {
await this.#runner.run(ddl.addConstraintSql(table, options), options);
}
/**
* Drops a constraint.
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Constraint name.
* @param options - Query options.
*/
async removeConstraint(
table: TableName,
name: string,
options?: QueryOptionsBase,
): Promise<void> {
await this.#runner.run(
`ALTER TABLE ${sqlLiteral.quoteTable(table)} DROP CONSTRAINT ${SqlCompiler.quoteIdent(name)}`,
options,
);
}
/**
* Validates a `NOT VALID` constraint.
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Constraint name.
* @param options - Query options.
*/
async validateConstraint(
table: TableName,
name: string,
options?: QueryOptionsBase,
): Promise<void> {
await this.#runner.run(ddl.validateConstraintSql(table, name), options);
}
}
|