From 46aec1851f54c8fb4cacd8380b51e80b7baf769b Mon Sep 17 00:00:00 2001 From: Ninja Date: Tue, 24 Sep 2024 23:35:56 +0100 Subject: [PATCH] - Add Visitor to query execution --- Schemio.sln | 15 +++++--- src/Schemio.EF/BaseSQLQuery.cs | 4 +-- src/Schemio.EF/EFQueryEngine.cs | 35 ++++++++----------- src/Schemio.EF/IDbContext.cs | 2 -- src/Schemio.EF/ISQLQuery.cs | 4 +-- src/Schemio.SQL/ISQLQuery.cs | 3 -- src/Schemio.SQL/SQLEngine.cs | 22 +++--------- src/Schemio/BaseQuery.cs | 11 ++++++ src/Schemio/IQuery.cs | 2 ++ src/Schemio/IQueryEngine.cs | 4 ++- src/Schemio/Impl/QueryExecutor.cs | 8 +++-- .../DataProvider.Tests/QueryExecutorTests.cs | 8 ++--- 12 files changed, 57 insertions(+), 61 deletions(-) diff --git a/Schemio.sln b/Schemio.sln index 7897b1c..dd138b3 100644 --- a/Schemio.sln +++ b/Schemio.sln @@ -12,13 +12,8 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution", "solution", "{AF995FEF-BB94-48D0-B02B-6671DA73056B}" ProjectSection(SolutionItems) = preProject .gitignore = .gitignore - .github\workflows\Build-Master.yml = .github\workflows\Build-Master.yml - GitVersion.yml = GitVersion.yml LICENSE.md = LICENSE.md - .github\workflows\Master-CodeQL.yml = .github\workflows\Master-CodeQL.yml README.md = README.md - .github\workflows\PullRequest-CodeQL.yml = .github\workflows\PullRequest-CodeQL.yml - .github\workflows\PullRequest-CI.yml = .github\workflows\PullRequest-CI.yml EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Schemio.SQL", "src\Schemio.SQL\Schemio.SQL.csproj", "{1A0CB973-23C9-4A17-905E-59510CD18932}" @@ -29,6 +24,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Schemio.Tests", "tests\Sche EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Schemio.SQL.Tests", "tests\Schemio.SQL.Tests\Schemio.SQL.Tests.csproj", "{1E319404-8EF0-40A1-A9D7-A404A71A98C4}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "github", "github", "{39FD806A-D320-43C9-800F-E9D9BCC90623}" + ProjectSection(SolutionItems) = preProject + .github\workflows\Build-Master.yml = .github\workflows\Build-Master.yml + GitVersion.yml = GitVersion.yml + .github\workflows\Master-CodeQL.yml = .github\workflows\Master-CodeQL.yml + .github\workflows\PullRequest-CI.yml = .github\workflows\PullRequest-CI.yml + .github\workflows\PullRequest-CodeQL.yml = .github\workflows\PullRequest-CodeQL.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -65,6 +69,7 @@ Global {6B92CC17-B7DB-446F-BF2F-A93696D48B5D} = {F41DA3D8-A0E9-4A05-8A35-78313C0F5804} {B09236CB-BBD2-4DCF-A698-74CCCAB29FFB} = {07BAE427-96CF-4F9B-80A9-48CFB0A89CF3} {1E319404-8EF0-40A1-A9D7-A404A71A98C4} = {07BAE427-96CF-4F9B-80A9-48CFB0A89CF3} + {39FD806A-D320-43C9-800F-E9D9BCC90623} = {AF995FEF-BB94-48D0-B02B-6671DA73056B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C0FF62D6-1374-4939-A546-432862338528} diff --git a/src/Schemio.EF/BaseSQLQuery.cs b/src/Schemio.EF/BaseSQLQuery.cs index 3f29e66..d25df65 100644 --- a/src/Schemio.EF/BaseSQLQuery.cs +++ b/src/Schemio.EF/BaseSQLQuery.cs @@ -1,5 +1,3 @@ -using Microsoft.EntityFrameworkCore; - namespace Schemio.EF { public abstract class BaseSQLQuery @@ -12,6 +10,6 @@ public abstract class BaseSQLQuery /// Delegate returns a collection from db. /// /// Func> - public abstract Func> GetQuery(); + public abstract Func> GetQuery(); } } \ No newline at end of file diff --git a/src/Schemio.EF/EFQueryEngine.cs b/src/Schemio.EF/EFQueryEngine.cs index 5e094bd..73cfa89 100644 --- a/src/Schemio.EF/EFQueryEngine.cs +++ b/src/Schemio.EF/EFQueryEngine.cs @@ -9,32 +9,25 @@ public class EFQueryEngine : IQueryEngine where T : DbContext public EFQueryEngine(IDbContextFactory _dbContextFactory) => this._dbContextFactory = _dbContextFactory; - public IQueryResult[] Run(IQueryList queryList, IDataContext context) + public IQueryResult[] Execute(IQuery query, IDataContext context) { - if (queryList?.Queries == null) - return Array.Empty(); - - var queries = queryList.Queries.Cast(); - - if (!queries.Any()) - return Array.Empty(); - var output = new List(); + if (query == null || !(query is ISQLQuery)) + return output.ToArray(); + using (var dbcontext = _dbContextFactory.CreateDbContext()) { - foreach (var query in queries) - { - var queryDelegate = query.GetQuery(); - if (queryDelegate == null) - continue; - - var results = queryDelegate(dbcontext); - if (results == null) - continue; - - output.AddRange(results); - } + var queryDelegate = ((ISQLQuery)query).GetQuery(); + if (queryDelegate == null) + return output.ToArray(); + + var results = queryDelegate((IDbContext)dbcontext); + if (results == null) + return output.ToArray(); + + output.AddRange(results); + return output.ToArray(); } } diff --git a/src/Schemio.EF/IDbContext.cs b/src/Schemio.EF/IDbContext.cs index 2ef695a..a26364c 100644 --- a/src/Schemio.EF/IDbContext.cs +++ b/src/Schemio.EF/IDbContext.cs @@ -1,6 +1,4 @@ -using System.Diagnostics.CodeAnalysis; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata; namespace Schemio.EF { diff --git a/src/Schemio.EF/ISQLQuery.cs b/src/Schemio.EF/ISQLQuery.cs index 9430899..58009dd 100644 --- a/src/Schemio.EF/ISQLQuery.cs +++ b/src/Schemio.EF/ISQLQuery.cs @@ -1,5 +1,3 @@ -using Microsoft.EntityFrameworkCore; - namespace Schemio.EF { public interface ISQLQuery @@ -9,6 +7,6 @@ public interface ISQLQuery /// Delegate returns a collection from db. /// /// Func> - Func> GetQuery(); + Func> GetQuery(); } } \ No newline at end of file diff --git a/src/Schemio.SQL/ISQLQuery.cs b/src/Schemio.SQL/ISQLQuery.cs index 50f585a..3445994 100644 --- a/src/Schemio.SQL/ISQLQuery.cs +++ b/src/Schemio.SQL/ISQLQuery.cs @@ -1,12 +1,9 @@ using System.Data; -using Dapper; namespace Schemio.SQL { public interface ISQLQuery : IQuery { - CommandDefinition GetCommandDefinition(); - IEnumerable Run(IDbConnection conn); } } \ No newline at end of file diff --git a/src/Schemio.SQL/SQLEngine.cs b/src/Schemio.SQL/SQLEngine.cs index 8301f08..ac2310f 100644 --- a/src/Schemio.SQL/SQLEngine.cs +++ b/src/Schemio.SQL/SQLEngine.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; using System.Data.Common; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Schemio.SQL { @@ -16,12 +11,11 @@ public SQLEngine(SqlConfiguration sqlConfiguration) this.sqlConfiguration = sqlConfiguration; } - public IQueryResult[] Run(IQueryList list, IDataContext context) + public IQueryResult[] Execute(IQuery query, IDataContext context) { var output = new List(); - var queries = list.Queries.Cast(); - if (!queries.Any()) + if (query == null || query is not ISQLQuery) return output.ToArray(); var factory = DbProviderFactories.GetFactory(sqlConfiguration.ConnectionSettings.ProviderName) @@ -36,16 +30,10 @@ public IQueryResult[] Run(IQueryList list, IDataContext context) connection.ConnectionString = sqlConfiguration.ConnectionSettings.ConnectionString; - foreach (var query in queries) - { - if (query != null) - { - var results = query.Run(connection); + var results = ((ISQLQuery)query).Run(connection); - if (results != null && results.Any()) - output.AddRange(results); - } - } + if (results != null && results.Any()) + output.AddRange(results); } return output.ToArray(); diff --git a/src/Schemio/BaseQuery.cs b/src/Schemio/BaseQuery.cs index ae25f6f..5fe5bd9 100644 --- a/src/Schemio/BaseQuery.cs +++ b/src/Schemio/BaseQuery.cs @@ -49,5 +49,16 @@ public virtual void ResolveParameterInChildMode(IDataContext context, IQueryResu public virtual void ResolveParameterInParentMode(IDataContext context) { } + + /// + /// Runs this query with query engine instance. + /// + /// + /// + /// + public IQueryResult[] Run(IQueryEngine engine, IDataContext context) + { + return engine.Execute(this, context); + } } } \ No newline at end of file diff --git a/src/Schemio/IQuery.cs b/src/Schemio/IQuery.cs index 04838b2..9effe5c 100644 --- a/src/Schemio/IQuery.cs +++ b/src/Schemio/IQuery.cs @@ -14,6 +14,8 @@ public interface IQuery void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult); bool IsContextResolved(); + + IQueryResult[] Run(IQueryEngine engine, IDataContext context); } // public interface IQuery diff --git a/src/Schemio/IQueryEngine.cs b/src/Schemio/IQueryEngine.cs index 71f7ffb..bf150dc 100644 --- a/src/Schemio/IQueryEngine.cs +++ b/src/Schemio/IQueryEngine.cs @@ -2,6 +2,8 @@ namespace Schemio { public interface IQueryEngine { - IQueryResult[] Run(IQueryList queries, IDataContext context); + //IQueryResult[] Run(IQueryList queries, IDataContext context); + + IQueryResult[] Execute(IQuery query, IDataContext context); } } \ No newline at end of file diff --git a/src/Schemio/Impl/QueryExecutor.cs b/src/Schemio/Impl/QueryExecutor.cs index 1b7d1f0..55c2527 100644 --- a/src/Schemio/Impl/QueryExecutor.cs +++ b/src/Schemio/Impl/QueryExecutor.cs @@ -48,8 +48,12 @@ private List Run(IQueryList queries, IDataContext context) foreach (var engine in queryEngines) { - var results = engine.Run(queries, context); - output.AddRange(results); + foreach (var query in queries.Queries) + { + var results = query.Run(engine, context); + if (results != null) + output.AddRange(results); + } } return output; diff --git a/tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs b/tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs index 2498b72..8ec3afe 100644 --- a/tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs +++ b/tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs @@ -26,7 +26,7 @@ public void TestQueryExecutorToReturnWhenNoQueries() { _queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList()); - _queryEngine.Verify(x => x.Run(It.IsAny(), It.IsAny()), Times.Never()); + _queryEngine.Verify(x => x.Execute(It.IsAny(), It.IsAny()), Times.Never()); } [Test] @@ -34,10 +34,10 @@ public void TestQueryExecutorToCallEngineWhenQueriesExistForExecution() { _queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList(new[] { new CustomerQuery() }) { }); - _queryEngine.Verify(x => x.Run(It.IsAny(), It.IsAny()), Times.Once()); + _queryEngine.Verify(x => x.Execute(It.IsAny(), It.IsAny()), Times.Once()); } - [Test] // TODO - + [Test] // TODO - All sequence assertions public void TestQueryExecutorToExecuteConfiguredQueriesInCorrectOrder() { var querList = new QueryBuilder(new CustomerSchema(), new XPathMatcher()) @@ -45,7 +45,7 @@ public void TestQueryExecutorToExecuteConfiguredQueriesInCorrectOrder() _queryExecutor.Execute(new DataContext(new EntityContext()), querList); - _queryEngine.Verify(x => x.Run(It.IsAny(), It.IsAny()), Times.Once()); + _queryEngine.Verify(x => x.Execute(It.IsAny(), It.IsAny()), Times.Once()); } } } \ No newline at end of file