namespace Khaemhat.Data.Schema;
/// A column of a table, in engine-neutral form.
public sealed class ColumnDefinition
{
/// Creates a column definition.
/// The column name (snake_case).
/// The logical type.
public ColumnDefinition(string name, ColumnType type)
{
SqlNames.EnsureIdentifier(name);
Name = name;
Type = type;
}
/// The column name.
public string Name { get; }
/// The logical type.
public ColumnType Type { get; }
/// The maximum length, for .
public int? Length { get; init; }
/// The scale, for (default 6).
public int? Scale { get; init; }
/// Whether the column accepts NULL.
public bool IsNullable { get; init; } = true;
/// Whether the column is an identity primary key (id).
public bool IsIdentity { get; init; }
/// A constant default value written as an SQL literal, or .
public object? DefaultValue { get; init; }
/// Whether the default is the current UTC timestamp.
public bool DefaultsToCurrentTimestamp { get; init; }
/// The logical type as documented, for example String(20).
public string TypeDescription => ColumnTypes.Describe(Type, Length, Scale);
}
/// An index.
/// The index name (for example ix_parties_name or ux_parties_code).
/// The table.
/// The indexed columns, in order.
/// Whether the index is unique.
public sealed record IndexDefinition(string Name, string Table, IReadOnlyList Columns, bool IsUnique);
/// What happens to referencing rows when the referenced row is deleted.
public enum ReferentialAction
{
/// The delete is refused while referencing rows exist (default).
Restrict = 0,
/// Referencing rows are deleted too.
Cascade = 1,
/// The referencing column is set to NULL.
SetNull = 2,
}
/// A foreign key.
/// The constraint name (for example fk_party_addresses_party).
/// The referencing columns.
/// The referenced table.
/// The referenced columns (usually id).
/// The action on delete.
public sealed record ForeignKeyDefinition(
string Name, IReadOnlyList Columns, string ReferencedTable, IReadOnlyList ReferencedColumns,
ReferentialAction OnDelete = ReferentialAction.Restrict);
/// A table, in engine-neutral form.
public sealed class TableDefinition
{
private readonly List _columns = [];
private readonly List _indexes = [];
private readonly List _foreignKeys = [];
/// Creates a table definition.
/// The table name (snake_case).
public TableDefinition(string name)
{
SqlNames.EnsureIdentifier(name);
Name = name;
}
/// The table name.
public string Name { get; }
/// The columns in order.
public IReadOnlyList Columns => _columns;
/// The primary key columns (usually id).
public IReadOnlyList PrimaryKey { get; internal set; } = [];
/// The indexes.
public IReadOnlyList Indexes => _indexes;
/// The foreign keys.
public IReadOnlyList ForeignKeys => _foreignKeys;
///
/// The date column by whose year PostgreSQL may partition the table (SPEC ยง5.2, large tables); recorded now,
/// applied by a later migration with PostgresPartitioning.
///
public string? PartitionByYearColumn { get; internal set; }
/// Finds a column by name.
/// The column name.
/// The column, or .
public ColumnDefinition? Column(string name) => _columns.Find(c => c.Name == name);
internal void AddColumn(ColumnDefinition column)
{
if (Column(column.Name) is not null)
{
throw new InvalidOperationException($"Column {column.Name} is defined twice in table {Name}.");
}
_columns.Add(column);
}
internal void ReplaceColumn(ColumnDefinition column)
{
var index = _columns.FindIndex(c => c.Name == column.Name);
_columns[index] = column;
}
internal void AddIndex(IndexDefinition index) => _indexes.Add(index);
internal void RemoveIndex(string name) => _indexes.RemoveAll(i => i.Name == name);
internal void AddForeignKey(ForeignKeyDefinition foreignKey) => _foreignKeys.Add(foreignKey);
}