Skip to content

Sequences

The Sequence class in Weasel.Oracle manages Oracle sequences with safe creation using PL/SQL exception handling.

Defining a Sequence

cs
// Simple sequence starting at 1
var seq = new Sequence("WEASEL.order_seq");

// Sequence with a custom start value
var seq2 = new Sequence(
    DbObjectName.Parse(OracleProvider.Instance, "WEASEL.invoice_seq"),
    startWith: 1000
);

snippet source | anchor

Using Sequences with Table Columns

Set a column's default to the sequence's NEXTVAL:

cs
var table = new Table("WEASEL.orders");
var seq = new Sequence("WEASEL.order_seq");

table.AddColumn<long>("id").AsPrimaryKey()
    .DefaultValueFromSequence(seq);

snippet source | anchor

This generates a default expression of WEASEL.order_seq.NEXTVAL.

Generating DDL

The create statement uses PL/SQL exception handling to safely skip creation if the sequence already exists (ORA-00955):

cs
var migrator = new OracleMigrator();
var writer = new StringWriter();
seq.WriteCreateStatement(migrator, writer);

snippet source | anchor

The drop statement checks all_sequences before dropping:

cs
seq.WriteDropStatement(migrator, writer);

snippet source | anchor

Delta Detection

Sequences are checked for existence in all_sequences:

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

var delta = await seq.FindDeltaAsync(conn);
// delta.Difference: None or Create

snippet source | anchor

Released under the MIT License.