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 | 134x 134x 920x 920x 915x 915x 915x 915x 915x 2705x 2705x 2631x 60x 60x 60x 60x 60x 60x 912x 915x 2x 913x 1x 912x 912x 912x 2705x 912x 2705x 748x 912x 912x 3x 912x 2705x 2705x 2700x 5x 5x 2705x 2705x | import { SqlCompiler } from '../sql/compile.ts';
import type { ColumnDdl } from './column-ddl.ts';
import type { ColumnDefinition, CreateTableOptions } from './ddl-types.ts';
import type { EnumDdl } from './enum-ddl.ts';
import { PartitionDdl } from './partition-ddl.ts';
import { SqlLiteral, type TableName } from './sql-literal.ts';
const sqlLiteral = new SqlLiteral();
const partitions = new PartitionDdl();
type NamedUniqueKey = Readonly<{ fields: readonly string[]; nullsNotDistinct: boolean }>;
/**
* `CREATE TABLE` (with enum types, keys and comments) and `ALTER COLUMN` statements.
*
* @internal
*/
export class TableDdl {
readonly #columns: ColumnDdl;
readonly #enums: EnumDdl;
/**
* @param columns - Column parts.
* @param enums - Enum types.
*/
constructor(columns: ColumnDdl, enums: EnumDdl) {
this.#columns = columns;
this.#enums = enums;
}
/**
* Statements creating a table: enum types, `CREATE TABLE IF NOT EXISTS`, comments.
*
* @param table - Table name or `{ tableName, schema }`.
* @param columns - Column definitions.
* @param options - Table comment, default schema and `ifNotExists` (default true).
* @returns Statements in execution order.
*/
createTableSql(
table: TableName,
columns: readonly ColumnDefinition[],
options: CreateTableOptions = {},
): readonly string[] {
this.#checkPersistence(table, options);
const defaultSchema = options.temporary === true ? undefined : options.defaultSchema;
const body = this.#tableBody(table, columns, defaultSchema);
const qualified = sqlLiteral.quoteTable(table, defaultSchema);
return [
...columns
.map((column) => this.#enums.createSql(table, column))
.filter((sql): sql is string => sql !== undefined),
`CREATE ${this.#persistence(options)}TABLE ${options.ifNotExists === false ? '' : 'IF NOT EXISTS '}${qualified} (${body})${options.partitionBy === undefined ? '' : partitions.partitionBySql(options.partitionBy)}`,
...(options.comment === undefined
? []
: [`COMMENT ON TABLE ${qualified} IS ${sqlLiteral.escape(options.comment)}`]),
...columns.flatMap((column) => this.#columns.commentSql(table, column)),
];
}
/**
* `ALTER TABLE ... ALTER COLUMN` statements bringing a column to its definition (v6 changeColumn).
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column definition.
* @param defaultSchema - Schema used when the table reference has none.
* @returns Statements in execution order.
*/
changeColumnSql(
table: TableName,
column: ColumnDefinition,
defaultSchema?: string,
): readonly string[] {
const qualified = sqlLiteral.quoteTable(table, defaultSchema);
const target = `ALTER TABLE ${qualified} ALTER COLUMN ${SqlCompiler.quoteIdent(column.field)}`;
const value = this.#columns.defaultSql(column);
const enumSql = this.#enums.createSql(table, column);
const type = this.#columns.columnType(table, { ...column, autoIncrement: false });
return [
`${target} ${column.allowNull ? 'DROP NOT NULL' : 'SET NOT NULL'}`,
...(column.autoIncrement === true && value === undefined
? []
: [value === undefined ? `${target} DROP DEFAULT` : `${target} SET DEFAULT ${value}`]),
...(enumSql === undefined ? [] : [enumSql]),
`${target} TYPE ${type}${column.type.key === 'ENUM' ? ` USING (${SqlCompiler.quoteIdent(column.field)}::${type})` : ''}`,
...this.#columns.commentSql(table, column),
];
}
#persistence(options: CreateTableOptions): string {
return options.temporary === true ? 'TEMPORARY ' : options.unlogged === true ? 'UNLOGGED ' : '';
}
#checkPersistence(table: TableName, options: CreateTableOptions): void {
if (options.temporary === true && options.unlogged === true) {
throw new Error('A table cannot be both temporary and unlogged');
}
if (options.temporary === true && sqlLiteral.tableParts(table).schema !== undefined) {
throw new Error('A temporary table lives in the session schema and cannot have a schema');
}
}
#tableBody(
table: TableName,
columns: readonly ColumnDefinition[],
defaultSchema: string | undefined,
): string {
const primaryKey = this.#primaryKeyConstraint(columns);
const uniqueKeys = this.#uniqueKeyConstraints(columns);
return [
...columns.map((column) =>
this.#columns.columnSql(
table,
column,
defaultSchema === undefined ? {} : { defaultSchema },
),
),
...uniqueKeys,
...primaryKey,
].join(', ');
}
#primaryKeyConstraint(columns: readonly ColumnDefinition[]): readonly string[] {
const primaryKeys = columns
.filter((column) => column.primaryKey)
.map((column) => SqlCompiler.quoteIdent(column.field));
return primaryKeys.length === 0 ? [] : [`PRIMARY KEY (${primaryKeys.join(', ')})`];
}
#uniqueKeyConstraints(columns: readonly ColumnDefinition[]): readonly string[] {
return [...this.#namedUniqueKeys(columns)].map(
([name, key]) =>
`CONSTRAINT ${SqlCompiler.quoteIdent(name)} UNIQUE${key.nullsNotDistinct ? ' NULLS NOT DISTINCT' : ''} (${key.fields.map(SqlCompiler.quoteIdent).join(', ')})`,
);
}
/** Named unique keys: name → fields, with the NULLS NOT DISTINCT flag. */
#namedUniqueKeys(columns: readonly ColumnDefinition[]): ReadonlyMap<string, NamedUniqueKey> {
return columns.reduce((keys, column) => {
const name = this.#uniqueName(column.unique);
if (name === undefined) {
return keys;
}
const existing = keys.get(name);
const nullsNotDistinct =
(existing?.nullsNotDistinct ?? false) || this.#columns.nullsNotDistinct(column.unique);
return new Map([
...keys,
[name, { fields: [...(existing?.fields ?? []), column.field], nullsNotDistinct }],
]);
}, new Map<string, NamedUniqueKey>());
}
#uniqueName(unique: ColumnDefinition['unique']): string | undefined {
return typeof unique === 'string'
? unique
: typeof unique === 'object' && unique !== null
? unique.name
: undefined;
}
}
|