From af36b6758862ac59234c58f1a41ceea12440cf13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Thierry=20K=C3=A9chichian?= Date: Thu, 12 Oct 2023 00:08:17 +0200 Subject: [PATCH] Add Schema to IndexCommand CompoundKey (#501) Co-authored-by: Sebastien Ros --- .github/workflows/build.yml | 18 ++--- .github/workflows/pr.yml | 18 ++--- samples/YesSql.Bench/YesSql.Bench.csproj | 2 +- .../YesSql.Samples.FullText.csproj | 2 +- .../YesSql.Samples.Hi.csproj | 2 +- .../YesSql.Samples.Performance.csproj | 2 +- .../YesSql.Samples.Web.csproj | 4 +- src/Directory.Build.props | 2 +- src/YesSql.Core/Commands/IndexCommand.cs | 76 +++++-------------- src/YesSql.Core/Provider/BaseDialect.cs | 4 - src/YesSql.Core/Session.cs | 7 +- test/YesSql.Tests/CoreTests.cs | 6 +- test/YesSql.Tests/Indexes/AttachmentByDay.cs | 2 +- test/YesSql.Tests/YesSql.Tests.csproj | 2 +- 14 files changed, 43 insertions(+), 104 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7ff0aee7..69d41db8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -48,12 +48,12 @@ jobs: steps: - uses: actions/checkout@v3 - # as of 12/2022, 3.1 and 5.0 are not installed by default on the images - - uses: actions/setup-dotnet@v3 - with: - dotnet-version: | - 3.1.x - 5.0.x + # Required if the .NET version is not available on the GH Actions images + # - uses: actions/setup-dotnet@v3 + # with: + # dotnet-version: | + # 6.0.x + # 7.0.x - name: Build run: dotnet build --configuration Release @@ -64,12 +64,6 @@ jobs: - name: Test - Sqlite .NET 6.0 run: dotnet test --configuration Release --filter YesSql.Tests.SqliteTests --no-restore --no-build --framework net6.0 - - name: Test - Sqlite .NET 5.0 - run: dotnet test --configuration Release --filter YesSql.Tests.SqliteTests --no-restore --no-build --framework net5.0 - - - name: Test - Sqlite .NET 3.1 - run: dotnet test --configuration Release --filter YesSql.Tests.SqliteTests --no-restore --no-build --framework netcoreapp3.1 - - name: Test - PostgresQL .NET 7.0 run: dotnet test --configuration Release --filter YesSql.Tests.PostgreSqlTests --no-restore --no-build --framework net7.0 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 296a2dc6..688f99eb 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -49,12 +49,12 @@ jobs: steps: - uses: actions/checkout@v3 - # as of 12/2022, 3.1 and 5.0 are not installed by default on the images - - uses: actions/setup-dotnet@v3 - with: - dotnet-version: | - 3.1.x - 5.0.x + # Required if the .NET version is not available on the GH Actions images + # - uses: actions/setup-dotnet@v3 + # with: + # dotnet-version: | + # 6.0.x + # 7.0.x - name: Build run: dotnet build --configuration Release @@ -65,12 +65,6 @@ jobs: - name: Test - Sqlite .NET 6.0 run: dotnet test --configuration Release --filter YesSql.Tests.SqliteTests --no-restore --no-build --framework net6.0 - - name: Test - Sqlite .NET 5.0 - run: dotnet test --configuration Release --filter YesSql.Tests.SqliteTests --no-restore --no-build --framework net5.0 - - - name: Test - Sqlite .NET 3.1 - run: dotnet test --configuration Release --filter YesSql.Tests.SqliteTests --no-restore --no-build --framework netcoreapp3.1 - - name: Test - PostgresQL .NET 7.0 run: dotnet test --configuration Release --filter YesSql.Tests.PostgreSqlTests --no-restore --no-build --framework net7.0 diff --git a/samples/YesSql.Bench/YesSql.Bench.csproj b/samples/YesSql.Bench/YesSql.Bench.csproj index 21d3e066..185f02a2 100644 --- a/samples/YesSql.Bench/YesSql.Bench.csproj +++ b/samples/YesSql.Bench/YesSql.Bench.csproj @@ -1,6 +1,6 @@ - netcoreapp3.1 + net7.0 portable true YesSql.Bench diff --git a/samples/YesSql.Samples.FullText/YesSql.Samples.FullText.csproj b/samples/YesSql.Samples.FullText/YesSql.Samples.FullText.csproj index 0a47143b..b1cf4d3b 100644 --- a/samples/YesSql.Samples.FullText/YesSql.Samples.FullText.csproj +++ b/samples/YesSql.Samples.FullText/YesSql.Samples.FullText.csproj @@ -1,6 +1,6 @@ - netcoreapp3.1 + net7.0 portable true YesSql.Samples.FullText diff --git a/samples/YesSql.Samples.Hi/YesSql.Samples.Hi.csproj b/samples/YesSql.Samples.Hi/YesSql.Samples.Hi.csproj index 648d6714..555531bc 100644 --- a/samples/YesSql.Samples.Hi/YesSql.Samples.Hi.csproj +++ b/samples/YesSql.Samples.Hi/YesSql.Samples.Hi.csproj @@ -1,6 +1,6 @@ - netcoreapp3.1 + net7.0 portable true YesSql.Samples.Hi diff --git a/samples/YesSql.Samples.Performance/YesSql.Samples.Performance.csproj b/samples/YesSql.Samples.Performance/YesSql.Samples.Performance.csproj index 569e5974..a1a8ca19 100644 --- a/samples/YesSql.Samples.Performance/YesSql.Samples.Performance.csproj +++ b/samples/YesSql.Samples.Performance/YesSql.Samples.Performance.csproj @@ -1,6 +1,6 @@ - net5.0 + net7.0 YesSql.Samples.Performance Exe false diff --git a/samples/YesSql.Samples.Web/YesSql.Samples.Web.csproj b/samples/YesSql.Samples.Web/YesSql.Samples.Web.csproj index 2c4b618b..9f4c3c80 100644 --- a/samples/YesSql.Samples.Web/YesSql.Samples.Web.csproj +++ b/samples/YesSql.Samples.Web/YesSql.Samples.Web.csproj @@ -1,7 +1,7 @@ - + - net5.0 + net7.0 false diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 66fed2a1..b88c7309 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -3,7 +3,7 @@ Sebastien Ros Sebastien Ros - netcoreapp3.1;net5.0;net6.0;net7.0 + net6.0 true portable https://github.com/sebastienros/yessql diff --git a/src/YesSql.Core/Commands/IndexCommand.cs b/src/YesSql.Core/Commands/IndexCommand.cs index 0b84bc24..fbc1aec6 100644 --- a/src/YesSql.Core/Commands/IndexCommand.cs +++ b/src/YesSql.Core/Commands/IndexCommand.cs @@ -79,7 +79,12 @@ protected static PropertyInfo[] TypePropertiesCache(Type type) protected string Inserts(Type type, ISqlDialect dialect) { - var key = new CompoundKey(dialect.Name, type.FullName, _store.Configuration.TablePrefix, Collection); + var key = new CompoundKey( + dialect.Name, + type.FullName, + _store.Configuration.Schema, + _store.Configuration.TablePrefix, + Collection); if (!InsertsList.TryGetValue(key, out var result)) { @@ -91,22 +96,22 @@ protected string Inserts(Type type, ISqlDialect dialect) { var sbColumnList = new StringBuilder(null); - for (var i = 0; i < allProperties.Count(); i++) + for (var i = 0; i < allProperties.Length; i++) { var property = allProperties.ElementAt(i); sbColumnList.Append(dialect.QuoteForColumnName(property.Name)); - if (i < allProperties.Count() - 1) + if (i < allProperties.Length - 1) { sbColumnList.Append(", "); } } var sbParameterList = new StringBuilder(null); - for (var i = 0; i < allProperties.Count(); i++) + for (var i = 0; i < allProperties.Length; i++) { var property = allProperties.ElementAt(i); sbParameterList.Append("@").Append(property.Name).Append(ParameterSuffix); - if (i < allProperties.Count() - 1) + if (i < allProperties.Length - 1) { sbParameterList.Append(", "); } @@ -136,12 +141,17 @@ protected string Inserts(Type type, ISqlDialect dialect) InsertsList[key] = result = $"insert into {dialect.QuoteForTableName(_store.Configuration.TablePrefix + _store.Configuration.TableNameConvention.GetIndexTable(type, Collection), _store.Configuration.Schema)} {values} {dialect.IdentitySelectString} {dialect.QuoteForColumnName("Id")};"; } - return result; + return result; } protected string Updates(Type type, ISqlDialect dialect) { - var key = new CompoundKey(dialect.Name, type.FullName, _store.Configuration.TablePrefix, Collection); + var key = new CompoundKey( + dialect.Name, + type.FullName, + _store.Configuration.Schema, + _store.Configuration.TablePrefix, + Collection); if (!UpdatesList.TryGetValue(key, out var result)) { @@ -176,56 +186,6 @@ private static bool IsWriteable(PropertyInfo pi) public abstract bool AddToBatch(ISqlDialect dialect, List queries, DbCommand batchCommand, List> actions, int index); - public struct CompoundKey : IEquatable - { - private readonly string _key1; - private readonly string _key2; - private readonly string _key3; - private readonly string _key4; - - public CompoundKey(string key1, string key2, string key3, string key4) - { - _key1 = key1; - _key2 = key2; - _key3 = key3; - _key4 = key4; - } - - /// - public override bool Equals(object obj) - { - if (obj is CompoundKey other) - { - return Equals(other); - } - - return false; - } - - /// - public bool Equals(CompoundKey other) - { - return String.Equals(_key1, other._key1) - && String.Equals(_key2, other._key2) - && String.Equals(_key3, other._key3) - && String.Equals(_key4, other._key4) - ; - } - - /// - public override int GetHashCode() - { - unchecked - { - var hashCode = 13; - hashCode = (hashCode * 397) ^ (!string.IsNullOrEmpty(_key1) ? _key1.GetHashCode() : 0); - hashCode = (hashCode * 397) ^ (!string.IsNullOrEmpty(_key2) ? _key2.GetHashCode() : 0); - hashCode = (hashCode * 397) ^ (!string.IsNullOrEmpty(_key3) ? _key3.GetHashCode() : 0); - hashCode = (hashCode * 397) ^ (!string.IsNullOrEmpty(_key4) ? _key4.GetHashCode() : 0); - - return hashCode; - } - } - } + private record CompoundKey(string Dialect, string Type, string Schema, string Prefix, string Collection); } } diff --git a/src/YesSql.Core/Provider/BaseDialect.cs b/src/YesSql.Core/Provider/BaseDialect.cs index 8f9466d6..65fc863a 100644 --- a/src/YesSql.Core/Provider/BaseDialect.cs +++ b/src/YesSql.Core/Provider/BaseDialect.cs @@ -138,11 +138,7 @@ public virtual string GetAddForeignKeyConstraintString(string name, string[] src res.Append(" constraint ") .Append(name) .Append(" foreign key (") -#if NETSTANDARD2_1 .AppendJoin(", ", srcColumns) -#else - .Append(String.Join(", ", srcColumns)) -#endif .Append(") references ") .Append(destQuotedTable); diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index c1756ac9..50190288 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -1375,14 +1375,9 @@ public async Task BeginTransactionAsync(IsolationLevel isolationL { await CreateConnectionAsync(); - // In the case of shared connections (InMemory) this can throw as the transation + // In the case of shared connections (InMemory) this can throw as the transaction // might already be set by a concurrent thread on the same shared connection. -#if SUPPORTS_ASYNC_TRANSACTIONS _transaction = await _connection.BeginTransactionAsync(isolationLevel); -#else - _transaction = _connection.BeginTransaction(isolationLevel); -#endif - } return _transaction; diff --git a/test/YesSql.Tests/CoreTests.cs b/test/YesSql.Tests/CoreTests.cs index b875b58f..dc9b7590 100644 --- a/test/YesSql.Tests/CoreTests.cs +++ b/test/YesSql.Tests/CoreTests.cs @@ -1307,7 +1307,7 @@ public async Task ShouldSupportAsyncEnumerable() results.Add(person); } - Assert.Equal(2, results.Count()); + Assert.Equal(2, results.Count); } } @@ -3825,7 +3825,7 @@ public virtual async Task ShouldReadCommittedRecords() * session2 1 index found (session1 statements isolated) * session2 save and commit person * session2 2 index found (session2 statements flushed) - * session2 disposed (session2 transation committed) + * session2 disposed (session2 transaction committed) * session2 2 index found * session1 2 index found */ @@ -3924,7 +3924,7 @@ public virtual async Task ShouldReadUncommittedRecords() * session2 1 index found (session1 statements isolated) * session2 save and commit person * session2 2 index found (session2 statements flushed) - * session2 disposed (session2 transation committed) + * session2 disposed (session2 transaction committed) * session2 2 index found * session1 disposed * session1 2 index found diff --git a/test/YesSql.Tests/Indexes/AttachmentByDay.cs b/test/YesSql.Tests/Indexes/AttachmentByDay.cs index 89764762..c3133017 100644 --- a/test/YesSql.Tests/Indexes/AttachmentByDay.cs +++ b/test/YesSql.Tests/Indexes/AttachmentByDay.cs @@ -28,7 +28,7 @@ public override void Describe(DescribeContext context) .Map(email => new AttachmentByDay { Date = email.Date.DayOfYear, - Count = email.Attachments.Count() + Count = email.Attachments.Count }) .Group(email => email.Date) .Reduce(group => new AttachmentByDay diff --git a/test/YesSql.Tests/YesSql.Tests.csproj b/test/YesSql.Tests/YesSql.Tests.csproj index d1c956b9..f4ff4db4 100644 --- a/test/YesSql.Tests/YesSql.Tests.csproj +++ b/test/YesSql.Tests/YesSql.Tests.csproj @@ -1,6 +1,6 @@ - netcoreapp3.1;net5.0;net6.0;net7.0 + net6.0;net7.0 latest YesSql.Tests YesSql.Tests