Skip to content

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");

snippet source | anchor

Column Configuration

The AddColumn method returns a ColumnExpression with these options:

  • AsPrimaryKey() -- marks the column as part of the primary key
  • AutoNumber() -- marks the column as auto-incrementing
  • NotNull() / AllowNulls() -- controls nullability
  • DefaultValue(value) -- sets a default value
  • DefaultValueByExpression(expr) -- sets a default using a SQL expression
  • DefaultValueFromSequence(sequence) -- sets default to sequence.NEXTVAL
  • AddIndex(configure?) -- adds an index on this column
  • ForeignKeyTo(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);

snippet source | anchor

Partitioning

Oracle tables support Range, Hash, and List partitioning:

cs
table.PartitionByRange("created_at");
table.PartitionByHash("id");
table.PartitionByList("region");

snippet source | anchor

Delta Detection

cs
await using var conn = new OracleConnection(connectionString);
await conn.OpenAsync();

var delta = await table.FindDeltaAsync(conn);

snippet source | anchor

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);

snippet source | anchor

The generated DDL checks all_tables before creating the table, wrapping the logic in a PL/SQL anonymous block.

Released under the MIT License.