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
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
var orders = new Table("WEASEL.orders");
orders.AddColumn<int>("id").AsPrimaryKey();
orders.AddColumn<int>("user_id").NotNull()
.ForeignKeyTo("WEASEL.users", "id", onDelete: CascadeAction.Cascade);Index Direction
IndexDefinition.SortOrder = SortOrder.Desc appends one trailing DESC, which Oracle applies to the last key column only. For an index that mixes directions, name the descending columns instead:
var index = new IndexDefinition("idx_triggers_acquire")
{
Columns = ["sched_name", "trigger_state", "next_fire_time", "priority", "misfire_instr"]
};
index.DescendingColumns.Add("priority");
// CREATE INDEX WEASEL.idx_triggers_acquire ON ... (sched_name, trigger_state, next_fire_time, priority DESC, misfire_instr)Direction is read back per column and compared, so an index whose direction differs from the model is rebuilt. When DescendingColumns names any column, SortOrder is ignored. A name that is not a key column throws, as does any entry on an index built from FunctionExpression.
Partitioning
Oracle tables support Range, Hash, and List partitioning:
table.PartitionByRange("created_at");
table.PartitionByHash("id");
table.PartitionByList("region");Delta Detection
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:
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.
