Skip to content

Commit

Permalink
#3 Oracle sequence implementation of HiLoIdGenerator added
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwittke committed Sep 19, 2017
1 parent 1c165a9 commit c81e759
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/Backend.Fx.EfCorePersistence/OracleSequenceHiLoIdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace Backend.Fx.EfCorePersistence
{
using System;
using System.Data.Common;
using Backend.Fx.Logging;
using Backend.Fx.Patterns.IdGeneration;
using Microsoft.EntityFrameworkCore;

public abstract class OracleSequenceHiLoIdGenerator : HiLoIdGenerator
{
private static readonly ILogger Logger = LogManager.Create<OracleSequenceHiLoIdGenerator>();
private readonly DbContextOptions dbContextOptions;
private readonly string sequenceName;

protected OracleSequenceHiLoIdGenerator(DbContextOptions dbContextOptions, string sequenceName)
{
this.dbContextOptions = dbContextOptions;
this.sequenceName = sequenceName.ToUpperInvariant();
}

protected override int GetNextBlockStart()
{
using (DbContext dbContext = new DbContext(dbContextOptions))
{
using (DbConnection dbConnection = dbContext.Database.GetDbConnection())
{
dbConnection.Open();
int nextBlockStart;
using (DbCommand command = dbConnection.CreateCommand())
{
command.CommandText = $"SELECT {sequenceName}.NEXTVAL FROM dual";
nextBlockStart = Convert.ToInt32(command.ExecuteScalar());
Logger.Debug($"{sequenceName} served {nextBlockStart} as next value");
}
return nextBlockStart;
}
}
}

public void EnsureSqlSequenceExistence()
{
using (DbContext dbContext = new DbContext(dbContextOptions))
{
using (DbConnection dbConnection = dbContext.Database.GetDbConnection())
{
dbConnection.Open();
bool sequenceExists;
using (DbCommand command = dbConnection.CreateCommand())
{
command.CommandText = $"SELECT count(*) FROM user_sequences WHERE sequence_name = '{sequenceName}'";
sequenceExists = (decimal)command.ExecuteScalar() == 1;
}
if (!sequenceExists)
{
using (DbCommand command = dbConnection.CreateCommand())
{
command.CommandText = $"CREATE SEQUENCE {sequenceName} START WITH 1 INCREMENT BY {Increment}";
command.ExecuteNonQuery();
}
}
}
}
}
}
}

0 comments on commit c81e759

Please sign in to comment.