From e15d8921ed89e796245fc78070e668a5665bce7b Mon Sep 17 00:00:00 2001 From: Mircea MATEI Date: Thu, 8 Sep 2022 15:10:23 +0300 Subject: [PATCH] #5. Fixing dependencies on SqlLockStatementProvider --- .../CustomSqlLockStatementFormatter.cs | 30 +++++++++++++++++++ .../CustomSqlLockStatementProvider.cs | 4 +-- .../Integration/BatchStateMachineTests.cs | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/SampleBatch.Components/CustomSqlLockStatementFormatter.cs diff --git a/src/SampleBatch.Components/CustomSqlLockStatementFormatter.cs b/src/SampleBatch.Components/CustomSqlLockStatementFormatter.cs new file mode 100644 index 0000000..97931f0 --- /dev/null +++ b/src/SampleBatch.Components/CustomSqlLockStatementFormatter.cs @@ -0,0 +1,30 @@ +using MassTransit.EntityFrameworkCoreIntegration; +using System.Text; + +namespace SampleBatch.Components +{ + internal class CustomSqlLockStatementFormatter : ILockStatementFormatter + { + public void Create(StringBuilder sb, string schema, string table) + { + sb.AppendFormat("SELECT * FROM [{0}].{1} WITH (UPDLOCK, ROWLOCK, SERIALIZABLE) WHERE ", schema, table); + } + + public void AppendColumn(StringBuilder sb, int index, string columnName) + { + if (index == 0) + sb.AppendFormat("{0} = @p0", columnName); + else + sb.AppendFormat(" AND {0} = @p{1}", columnName, index); + } + + public void Complete(StringBuilder sb) + { + } + + public void CreateOutboxStatement(StringBuilder sb, string schema, string table, string columnName) + { + sb.AppendFormat(@"SELECT TOP 1 * FROM [{0}].{1} WITH (UPDLOCK, ROWLOCK, READPAST) ORDER BY {2}", schema, table, columnName); + } + } +} \ No newline at end of file diff --git a/src/SampleBatch.Components/CustomSqlLockStatementProvider.cs b/src/SampleBatch.Components/CustomSqlLockStatementProvider.cs index e134081..ca00578 100644 --- a/src/SampleBatch.Components/CustomSqlLockStatementProvider.cs +++ b/src/SampleBatch.Components/CustomSqlLockStatementProvider.cs @@ -7,8 +7,8 @@ public class CustomSqlLockStatementProvider : { const string DefaultSchemaName = "dbo"; - public CustomSqlLockStatementProvider(string lockStatement) - : base(DefaultSchemaName, lockStatement) + public CustomSqlLockStatementProvider() + : base(DefaultSchemaName, new CustomSqlLockStatementFormatter()) { } } diff --git a/src/SampleBatch.Tests/Integration/BatchStateMachineTests.cs b/src/SampleBatch.Tests/Integration/BatchStateMachineTests.cs index 6349716..6325457 100644 --- a/src/SampleBatch.Tests/Integration/BatchStateMachineTests.cs +++ b/src/SampleBatch.Tests/Integration/BatchStateMachineTests.cs @@ -47,7 +47,7 @@ public BatchStateMachineTests() db.Database.EnsureCreated(); } - _sagaRepository = EntityFrameworkSagaRepository.CreatePessimistic(_dbContextFactory, new CustomSqlLockStatementProvider("select * from {0}.{1} WITH (UPDLOCK, ROWLOCK) WHERE BatchId = @p0")); + _sagaRepository = EntityFrameworkSagaRepository.CreatePessimistic(_dbContextFactory, new CustomSqlLockStatementProvider()); _stateMachine = new BatchStateMachine(); _inMemoryTestHarness = new InMemoryTestHarness();