-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 Oracle sequence implementation of HiLoIdGenerator added
- Loading branch information
1 parent
1c165a9
commit c81e759
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/Backend.Fx.EfCorePersistence/OracleSequenceHiLoIdGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |