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 | 131x 131x 387x 34x 34x 34x 31x 31x 31x 65x | import type { DataTypeInput } from '../data-types/data-types.ts';
import { DdlGenerator, type ColumnDefinition } from '../ddl/ddl-generator.ts';
import { SqlLiteral, type TableName } from '../ddl/sql-literal.ts';
import { ModelDefinition, type AttributeOptions } from '../model/model-definition.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();
/**
* Column DDL of `QueryInterface`: add, remove, change and rename. Names are quoted identifiers.
*
* @internal
*/
export class ColumnCommands {
readonly #runner: StatementRunner;
/**
* @param runner - Statement runner.
*/
constructor(runner: StatementRunner) {
this.#runner = runner;
}
/**
* Adds a column and its enum type and comment.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name.
* @param attribute - Column definition.
* @param options - Query options.
*/
async addColumn(
table: TableName,
column: string,
attribute: DataTypeInput | AttributeOptions,
options?: QueryOptionsBase,
): Promise<void> {
const definition = this.#column(column, attribute);
const enumSql = ddl.createEnumSql(table, definition);
await this.#runner.runAll(
[
...(enumSql === undefined ? [] : [enumSql]),
`ALTER TABLE ${sqlLiteral.quoteTable(table)} ADD COLUMN ${ddl.columnSql(table, definition, { inlinePrimaryKey: true })}`,
...ddl.columnCommentSql(table, definition),
],
options,
);
}
/**
* Removes a column.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name.
* @param options - Query options.
*/
async removeColumn(table: TableName, column: string, options?: QueryOptionsBase): Promise<void> {
await this.#runner.run(
`ALTER TABLE ${sqlLiteral.quoteTable(table)} DROP COLUMN ${SqlCompiler.quoteIdent(column)}`,
options,
);
}
/**
* Changes type, nullability, default and comment of a column.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name.
* @param attribute - New column definition.
* @param options - Query options.
*/
async changeColumn(
table: TableName,
column: string,
attribute: DataTypeInput | AttributeOptions,
options?: QueryOptionsBase,
): Promise<void> {
await this.#runner.runAll(ddl.changeColumnSql(table, this.#column(column, attribute)), options);
}
/**
* Renames a column.
*
* @param table - Table name or `{ tableName, schema }`.
* @param before - Current column name.
* @param after - New column name.
* @param options - Query options.
*/
async renameColumn(
table: TableName,
before: string,
after: string,
options?: QueryOptionsBase,
): Promise<void> {
await this.#runner.run(
`ALTER TABLE ${sqlLiteral.quoteTable(table)} RENAME COLUMN ${SqlCompiler.quoteIdent(before)} TO ${SqlCompiler.quoteIdent(after)}`,
options,
);
}
#column(name: string, attribute: DataTypeInput | AttributeOptions): ColumnDefinition {
return ddl.columnOf(ModelDefinition.attribute(name, attribute, false));
}
}
|