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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 134x 920x 920x 920x 920x 920x 920x 1x 2812x 8x 5x 2x 2x 41x 38x 915x 38x 29x 60x 1x 463x 572x 151x | import type { AttributeDefinition } from '../model/model-definition.ts';
import { ColumnDdl } from './column-ddl.ts';
import { ConstraintDdl } from './constraint-ddl.ts';
import { DdlGuards } from './ddl-guards.ts';
import type {
ColumnDefinition,
ConstraintOptions,
CreateTableOptions,
DeferrableValue,
IndexField,
IndexOptions,
} from './ddl-types.ts';
import { EnumDdl } from './enum-ddl.ts';
import { IndexDdl } from './index-ddl.ts';
import {
PartitionDdl,
type CreatePartitionOptions,
type DetachPartitionOptions,
} from './partition-ddl.ts';
import type { PartitionBoundSpec } from './partition-types.ts';
import type { TableName } from './sql-literal.ts';
import { TableDdl } from './table-ddl.ts';
export {
Deferrable,
type ColumnDefinition,
type ConstraintOptions,
type DeferrableValue,
type ExcludeElement,
type IndexField,
type IndexOptions,
} from './ddl-types.ts';
const guards = new DdlGuards();
/**
* Generates DDL statements (tables, columns, enum types, indexes, constraints, comments,
* references) for PostgreSQL. Values are inlined as escaped literals.
*
* @internal
*/
export class DdlGenerator {
readonly #enums = new EnumDdl();
readonly #columns = new ColumnDdl(this.#enums);
readonly #tables = new TableDdl(this.#columns, this.#enums);
readonly #indexes = new IndexDdl();
readonly #constraints = new ConstraintDdl();
readonly #partitions = new PartitionDdl();
/**
* SQL of a `Deferrable` value; anything that is not one of them is rejected.
*
* @internal
*/
static deferrableSql(value: DeferrableValue): string {
return guards.deferrableSql(value);
}
/**
* Converts a normalized attribute to a column definition.
*
* @param attribute - Normalized attribute.
* @returns Column definition.
*/
columnOf(attribute: AttributeDefinition): ColumnDefinition {
return this.#columns.columnOf(attribute);
}
/**
* Name of the PostgreSQL enum type of a column (`enum_<table>_<column>`, v6).
*
* @param table - Table name or `{ tableName, schema }`.
* @param field - Column name.
* @returns Unquoted enum type name.
*/
enumTypeName(table: TableName, field: string): string {
return this.#enums.typeName(table, field);
}
/**
* SQL type of a column (SERIAL for auto-increment integers, enum type names).
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column definition.
* @returns SQL type.
*/
columnType(table: TableName, column: ColumnDefinition): string {
return this.#columns.columnType(table, column);
}
/**
* `DEFAULT ...` clause value.
*
* @param column - Column definition.
* @returns SQL of the default, or `undefined` for client-side defaults.
*/
defaultSql(column: ColumnDefinition): string | undefined {
return this.#columns.defaultSql(column);
}
/**
* `REFERENCES ...` clause.
*
* @param column - Column definition.
* @param defaultSchema - Schema of the referenced table when it has none.
* @returns SQL of the clause, or `undefined` without references.
*/
referencesSql(column: ColumnDefinition, defaultSchema?: string): string | undefined {
return this.#columns.referencesSql(column, defaultSchema);
}
/**
* One column of `CREATE TABLE` / `ADD COLUMN`.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column definition.
* @param options - `inlinePrimaryKey` and the default schema of references.
* @returns Column SQL.
*/
columnSql(
table: TableName,
column: ColumnDefinition,
options: Readonly<{ inlinePrimaryKey?: boolean; defaultSchema?: string }> = {},
): string {
return this.#columns.columnSql(table, column, options);
}
/**
* `CREATE TYPE ... AS ENUM` wrapped so an existing type is not an error (v6).
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column definition.
* @returns Statement, or `undefined` for non-enum columns.
*/
createEnumSql(table: TableName, column: ColumnDefinition): string | undefined {
return this.#enums.createSql(table, column);
}
/**
* 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, persistence (`unlogged`, `temporary`), default schema and `ifNotExists` (default true).
* @returns Statements in execution order.
*/
createTableSql(
table: TableName,
columns: readonly ColumnDefinition[],
options: CreateTableOptions = {},
): readonly string[] {
return this.#tables.createTableSql(table, columns, options);
}
/**
* `COMMENT ON COLUMN` for a column with a comment.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name and comment.
* @returns The statement, or nothing without a comment.
*/
columnCommentSql(
table: TableName,
column: Readonly<{ field: string; comment?: string }>,
): readonly string[] {
return this.#columns.commentSql(table, column);
}
/**
* `ALTER TYPE ... ADD VALUE IF NOT EXISTS` for every enum value of a column.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column definition.
* @returns Statements; empty for non-enum columns.
*/
addEnumValuesSql(table: TableName, column: ColumnDefinition): readonly string[] {
return this.#enums.addValuesSql(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[] {
return this.#tables.changeColumnSql(table, column, defaultSchema);
}
/**
* Default index name (v6 `Utils.nameIndex`).
*
* @param table - Table name or `{ tableName, schema }`.
* @param fields - Indexed fields.
* @returns Index name.
*/
indexName(table: TableName, fields: readonly IndexField[]): string {
return this.#indexes.indexName(table, fields);
}
/**
* `CREATE [UNIQUE] INDEX` statement.
*
* @param table - Table name or `{ tableName, schema }`.
* @param options - Index options.
* @param defaultSchema - Schema used when the table reference has none.
* @returns Statement.
* @throws Error without fields or with an invalid method / operator class.
*/
createIndexSql(table: TableName, options: IndexOptions, defaultSchema?: string): string {
return this.#indexes.createIndexSql(table, options, defaultSchema);
}
/**
* `ALTER TABLE ... ADD CONSTRAINT` statement.
*
* @param table - Table name or `{ tableName, schema }`.
* @param options - Constraint definition.
* @param defaultSchema - Schema used when a table reference has none.
* @returns Statement.
* @throws Error for an unknown constraint type or a foreign key without references.
*/
addConstraintSql(table: TableName, options: ConstraintOptions, defaultSchema?: string): string {
return this.#constraints.addConstraintSql(table, options, defaultSchema);
}
/**
* `ALTER TABLE ... VALIDATE CONSTRAINT` statement.
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Constraint name.
* @param defaultSchema - Schema used when the table reference has none.
* @returns Statement.
*/
validateConstraintSql(table: TableName, name: string, defaultSchema?: string): string {
return this.#constraints.validateConstraintSql(table, name, defaultSchema);
}
/**
* `CREATE TABLE ... PARTITION OF` statement.
*
* @param parent - Partitioned table.
* @param partition - New partition name.
* @param bound - Bounds.
* @param options - Sub-partitioning and `ifNotExists`.
* @returns Statement.
*/
createPartitionSql(
parent: TableName,
partition: TableName,
bound: PartitionBoundSpec,
options?: CreatePartitionOptions,
): string {
return this.#partitions.createPartitionSql(parent, partition, bound, options);
}
/**
* `ALTER TABLE ... ATTACH PARTITION` statement.
*
* @param parent - Partitioned table.
* @param partition - Existing table to attach.
* @param bound - Bounds.
* @returns Statement.
*/
attachPartitionSql(parent: TableName, partition: TableName, bound: PartitionBoundSpec): string {
return this.#partitions.attachPartitionSql(parent, partition, bound);
}
/**
* `ALTER TABLE ... DETACH PARTITION` statement.
*
* @param parent - Partitioned table.
* @param partition - Partition to detach.
* @param options - `concurrently` or `finalize`.
* @returns Statement.
*/
detachPartitionSql(
parent: TableName,
partition: TableName,
options?: DetachPartitionOptions,
): string {
return this.#partitions.detachPartitionSql(parent, partition, options);
}
}
|