From 497c761d1f64c06d7ea27fe2093f113b887a1582 Mon Sep 17 00:00:00 2001 From: Ninja Date: Wed, 25 Sep 2024 00:24:12 +0100 Subject: [PATCH] - fix query executor to take collection of queries --- src/Schemio.EF/BaseSQLQuery.cs | 2 +- src/Schemio.EF/EFQueryEngine.cs | 21 ++++++------ src/Schemio.EF/ISQLQuery.cs | 2 +- src/Schemio.SQL/SQLEngine.cs | 33 ++++++++++--------- src/Schemio/BaseQuery.cs | 11 ------- src/Schemio/IQuery.cs | 2 -- src/Schemio/IQueryEngine.cs | 4 +-- src/Schemio/Impl/QueryExecutor.cs | 14 ++++---- .../DataProvider.Tests/QueryExecutorTests.cs | 8 +++-- 9 files changed, 44 insertions(+), 53 deletions(-) diff --git a/src/Schemio.EF/BaseSQLQuery.cs b/src/Schemio.EF/BaseSQLQuery.cs index d25df65..c49104f 100644 --- a/src/Schemio.EF/BaseSQLQuery.cs +++ b/src/Schemio.EF/BaseSQLQuery.cs @@ -10,6 +10,6 @@ public abstract class BaseSQLQuery /// Delegate returns a collection from db. /// /// Func> - public abstract Func> GetQuery(); + public abstract IEnumerable Run(IDbContext dbContext); } } \ No newline at end of file diff --git a/src/Schemio.EF/EFQueryEngine.cs b/src/Schemio.EF/EFQueryEngine.cs index 73cfa89..af919df 100644 --- a/src/Schemio.EF/EFQueryEngine.cs +++ b/src/Schemio.EF/EFQueryEngine.cs @@ -9,24 +9,23 @@ public class EFQueryEngine : IQueryEngine where T : DbContext public EFQueryEngine(IDbContextFactory _dbContextFactory) => this._dbContextFactory = _dbContextFactory; - public IQueryResult[] Execute(IQuery query, IDataContext context) + public bool CanExecute(IQuery query) => query != null && query is ISQLQuery; + + public IEnumerable Execute(IEnumerable queries, IDataContext context) { var output = new List(); - if (query == null || !(query is ISQLQuery)) - return output.ToArray(); - using (var dbcontext = _dbContextFactory.CreateDbContext()) { - var queryDelegate = ((ISQLQuery)query).GetQuery(); - if (queryDelegate == null) - return output.ToArray(); + foreach (var query in queries) + { + var results = ((ISQLQuery)query).Run((IDbContext)dbcontext); - var results = queryDelegate((IDbContext)dbcontext); - if (results == null) - return output.ToArray(); + if (results == null) + continue; - output.AddRange(results); + output.AddRange(results); + } return output.ToArray(); } diff --git a/src/Schemio.EF/ISQLQuery.cs b/src/Schemio.EF/ISQLQuery.cs index 58009dd..726479b 100644 --- a/src/Schemio.EF/ISQLQuery.cs +++ b/src/Schemio.EF/ISQLQuery.cs @@ -7,6 +7,6 @@ public interface ISQLQuery /// Delegate returns a collection from db. /// /// Func> - Func> GetQuery(); + IEnumerable Run(IDbContext dbContext); } } \ No newline at end of file diff --git a/src/Schemio.SQL/SQLEngine.cs b/src/Schemio.SQL/SQLEngine.cs index ac2310f..d89134c 100644 --- a/src/Schemio.SQL/SQLEngine.cs +++ b/src/Schemio.SQL/SQLEngine.cs @@ -11,30 +11,33 @@ public SQLEngine(SqlConfiguration sqlConfiguration) this.sqlConfiguration = sqlConfiguration; } - public IQueryResult[] Execute(IQuery query, IDataContext context) + public bool CanExecute(IQuery query) => query != null && query is ISQLQuery; + + public IEnumerable Execute(IEnumerable queries, IDataContext context) { var output = new List(); - if (query == null || query is not ISQLQuery) - return output.ToArray(); - var factory = DbProviderFactories.GetFactory(sqlConfiguration.ConnectionSettings.ProviderName) ?? throw new InvalidOperationException($"Provider: {sqlConfiguration.ConnectionSettings.ProviderName} is not supported. Please register entry in DbProviderFactories "); - //var batches = queries.Chunk(sqlConfiguration.QuerySettings.QueryBatchSize); - //foreach (var batch in batches) - using (var connection = factory.CreateConnection()) - { - if (connection == null) - throw new Exception($"Failed to create connection with Provider: {sqlConfiguration.ConnectionSettings.ProviderName}. Please check the connection settings."); + var batches = queries.Chunk(sqlConfiguration.QuerySettings.QueryBatchSize); + + foreach (var batch in batches) + using (var connection = factory.CreateConnection()) + { + if (connection == null) + throw new Exception($"Failed to create connection with Provider: {sqlConfiguration.ConnectionSettings.ProviderName}. Please check the connection settings."); - connection.ConnectionString = sqlConfiguration.ConnectionSettings.ConnectionString; + connection.ConnectionString = sqlConfiguration.ConnectionSettings.ConnectionString; - var results = ((ISQLQuery)query).Run(connection); + foreach (var query in batch) + { + 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 5fe5bd9..ae25f6f 100644 --- a/src/Schemio/BaseQuery.cs +++ b/src/Schemio/BaseQuery.cs @@ -49,16 +49,5 @@ 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 9effe5c..04838b2 100644 --- a/src/Schemio/IQuery.cs +++ b/src/Schemio/IQuery.cs @@ -14,8 +14,6 @@ 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 bf150dc..7f1b63d 100644 --- a/src/Schemio/IQueryEngine.cs +++ b/src/Schemio/IQueryEngine.cs @@ -2,8 +2,8 @@ namespace Schemio { public interface IQueryEngine { - //IQueryResult[] Run(IQueryList queries, IDataContext context); + bool CanExecute(IQuery query); - IQueryResult[] Execute(IQuery query, IDataContext context); + IEnumerable Execute(IEnumerable queries, IDataContext context); } } \ No newline at end of file diff --git a/src/Schemio/Impl/QueryExecutor.cs b/src/Schemio/Impl/QueryExecutor.cs index 55c2527..3557fef 100644 --- a/src/Schemio/Impl/QueryExecutor.cs +++ b/src/Schemio/Impl/QueryExecutor.cs @@ -42,18 +42,18 @@ private IQueryList Process(IDataContext context, IQueryList queries, List Run(IQueryList queries, IDataContext context) + private List Run(IQueryList queryList, IDataContext context) { var output = new List(); foreach (var engine in queryEngines) { - foreach (var query in queries.Queries) - { - var results = query.Run(engine, context); - if (results != null) - output.AddRange(results); - } + var queries = queryList.Queries.Where(x => engine.CanExecute(x)); + + var results = engine.Execute(queries, 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 8ec3afe..c38cecd 100644 --- a/tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs +++ b/tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs @@ -18,6 +18,8 @@ internal class QueryExecutorTests public void Setup() { _queryEngine = new Mock(); + _queryEngine.Setup(x => x.CanExecute(It.IsAny())).Returns(true); + _queryExecutor = new QueryExecutor(new[] { _queryEngine.Object }); } @@ -26,7 +28,7 @@ public void TestQueryExecutorToReturnWhenNoQueries() { _queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList()); - _queryEngine.Verify(x => x.Execute(It.IsAny(), It.IsAny()), Times.Never()); + _queryEngine.Verify(x => x.Execute(It.IsAny>(), It.IsAny()), Times.Never()); } [Test] @@ -34,7 +36,7 @@ public void TestQueryExecutorToCallEngineWhenQueriesExistForExecution() { _queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList(new[] { new CustomerQuery() }) { }); - _queryEngine.Verify(x => x.Execute(It.IsAny(), It.IsAny()), Times.Once()); + _queryEngine.Verify(x => x.Execute(It.IsAny>(), It.IsAny()), Times.Once()); } [Test] // TODO - All sequence assertions @@ -45,7 +47,7 @@ public void TestQueryExecutorToExecuteConfiguredQueriesInCorrectOrder() _queryExecutor.Execute(new DataContext(new EntityContext()), querList); - _queryEngine.Verify(x => x.Execute(It.IsAny(), It.IsAny()), Times.Once()); + _queryEngine.Verify(x => x.Execute(It.IsAny>(), It.IsAny()), Times.Once()); } } } \ No newline at end of file