This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
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.
Merge pull request #54 from marcwittke/feature/forelab
Improvements for Oracle Sequences and Id Generation
- Loading branch information
Showing
15 changed files
with
168 additions
and
156 deletions.
There are no files selected for viewing
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
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
5 changes: 3 additions & 2 deletions
5
src/implementations/Backend.Fx.EfCorePersistence/DatabaseManagerWithMigration.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
3 changes: 2 additions & 1 deletion
3
src/implementations/Backend.Fx.EfCorePersistence/DatabaseManagerWithoutMigration.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
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
10 changes: 5 additions & 5 deletions
10
src/implementations/Backend.Fx.EfCorePersistence/ISequence.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
namespace Backend.Fx.EfCorePersistence | ||
{ | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Data; | ||
|
||
namespace Backend.Fx.EfCorePersistence | ||
{ | ||
public interface ISequence | ||
{ | ||
void EnsureSequence(DbContext dbContext); | ||
int GetNextValue(DbContext dbContext); | ||
void EnsureSequence(IDbConnection dbConnection); | ||
int GetNextValue(IDbConnection dbConnection); | ||
int Increment { get; } | ||
} | ||
} |
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
64 changes: 31 additions & 33 deletions
64
src/implementations/Backend.Fx.EfCorePersistence/Mssql/MsSqlSequence.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
84 changes: 50 additions & 34 deletions
84
src/implementations/Backend.Fx.EfCorePersistence/Oracle/OracleSequence.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 |
---|---|---|
@@ -1,60 +1,76 @@ | ||
namespace Backend.Fx.EfCorePersistence.Oracle | ||
{ | ||
using System; | ||
using System.Data.Common; | ||
using System.Data; | ||
using Logging; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public abstract class OracleSequence : ISequence | ||
{ | ||
private static readonly ILogger Logger = LogManager.Create<OracleSequence>(); | ||
public void EnsureSequence(DbContext dbContext) | ||
|
||
public void EnsureSequence(IDbConnection dbConnection) | ||
{ | ||
Logger.Info($"Ensuring existence of oracle sequence {SequenceName}"); | ||
using (DbConnection dbConnection = dbContext.Database.GetDbConnection()) | ||
Logger.Info($"Ensuring existence of oracle sequence {SchemaPrefix}{SequenceName}"); | ||
|
||
if (dbConnection.State == ConnectionState.Closed) | ||
{ | ||
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) | ||
{ | ||
Logger.Info($"Sequence {SequenceName} exists"); | ||
} | ||
else | ||
} | ||
|
||
bool sequenceExists; | ||
using (IDbCommand command = dbConnection.CreateCommand()) | ||
{ | ||
command.CommandText = $"SELECT count(*) FROM user_sequences WHERE sequence_name = '{SequenceName}'"; | ||
sequenceExists = (decimal)command.ExecuteScalar() == 1; | ||
} | ||
if (sequenceExists) | ||
{ | ||
Logger.Info($"Sequence {SchemaPrefix}{SequenceName} exists"); | ||
} | ||
else | ||
{ | ||
Logger.Info($"Sequence {SchemaPrefix}{SequenceName} does not exist yet and will be created now"); | ||
using (var cmd = dbConnection.CreateCommand()) | ||
{ | ||
Logger.Info($"Sequence {SequenceName} does not exist yet and will be created now"); | ||
using (var cmd = dbConnection.CreateCommand()) | ||
{ | ||
cmd.CommandText = $"CREATE SEQUENCE {SequenceName} START WITH 1 INCREMENT BY {Increment}"; | ||
cmd.ExecuteNonQuery(); | ||
Logger.Info($"Sequence {SequenceName} created"); | ||
} | ||
cmd.CommandText = $"CREATE SEQUENCE {SchemaPrefix}{SequenceName} START WITH 1 INCREMENT BY {Increment}"; | ||
cmd.ExecuteNonQuery(); | ||
Logger.Info($"Sequence {SchemaPrefix}{SequenceName} created"); | ||
} | ||
} | ||
} | ||
|
||
public int GetNextValue(DbContext dbContext) | ||
public int GetNextValue(IDbConnection dbConnection) | ||
{ | ||
using (DbConnection dbConnection = dbContext.Database.GetDbConnection()) | ||
if (dbConnection.State == ConnectionState.Closed) | ||
{ | ||
dbConnection.Open(); | ||
int nextValue; | ||
using (DbCommand command = dbConnection.CreateCommand()) | ||
{ | ||
command.CommandText = $"SELECT {SequenceName}.NEXTVAL FROM dual"; | ||
nextValue = Convert.ToInt32(command.ExecuteScalar()); | ||
Logger.Debug($"{SequenceName} served {nextValue} as next value"); | ||
} | ||
return nextValue; | ||
} | ||
|
||
int nextValue; | ||
using (IDbCommand command = dbConnection.CreateCommand()) | ||
{ | ||
command.CommandText = $"SELECT {SchemaPrefix}{SequenceName}.NEXTVAL FROM dual"; | ||
nextValue = Convert.ToInt32(command.ExecuteScalar()); | ||
Logger.Debug($"{SchemaPrefix}{SequenceName} served {nextValue} as next value"); | ||
} | ||
return nextValue; | ||
} | ||
|
||
public abstract int Increment { get; } | ||
protected abstract string SequenceName { get; } | ||
protected abstract string SchemaName { get; } | ||
|
||
private string SchemaPrefix | ||
{ | ||
get | ||
{ | ||
if (string.IsNullOrEmpty(SchemaName)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
return SchemaName + "."; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.