Tables
The Table class in Weasel.Oracle.Tables provides a fluent API for defining Oracle tables with columns, primary keys, foreign keys, indexes, and partitioning.
Defining a Table
cs
var table = new Table("WEASEL.users");
table.AddColumn<int>("id").AsPrimaryKey();
table.AddColumn<string>("name").NotNull();
table.AddColumn<string>("email").NotNull().AddIndex(idx => idx.IsUnique = true);
table.AddColumn<DateTime>("created_at");Column Configuration
The AddColumn method returns a ColumnExpression with these options:
AsPrimaryKey()-- marks the column as part of the primary keyAutoNumber()-- marks the column as auto-incrementingNotNull()/AllowNulls()-- controls nullabilityDefaultValue(value)-- sets a default valueDefaultValueByExpression(expr)-- sets a default using a SQL expressionDefaultValueFromSequence(sequence)-- sets default tosequence.NEXTVALAddIndex(configure?)-- adds an index on this columnForeignKeyTo(table, column)-- adds a foreign key constraint
Foreign Keys
cs
var orders = new Table("WEASEL.orders");
orders.AddColumn<int>("id").AsPrimaryKey();
orders.AddColumn<int>("user_id").NotNull()
.ForeignKeyTo("WEASEL.users", "id", onDelete: CascadeAction.Cascade);Partitioning
Oracle tables support Range, Hash, and List partitioning:
cs
table.PartitionByRange("created_at");
table.PartitionByHash("id");
table.PartitionByList("region");Delta Detection
cs
await using var conn = new OracleConnection(connectionString);
await conn.OpenAsync();
var delta = await table.FindDeltaAsync(conn);Generating DDL
Oracle DDL uses PL/SQL blocks for safe CREATE IF NOT EXISTS semantics:
cs
var migrator = new OracleMigrator();
var writer = new StringWriter();
table.WriteCreateStatement(migrator, writer);The generated DDL checks all_tables before creating the table, wrapping the logic in a PL/SQL anonymous block.
