Skip to content

Commit

Permalink
- fix query executor to take collection of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Sep 24, 2024
1 parent 46aec18 commit 497c761
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/Schemio.EF/BaseSQLQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public abstract class BaseSQLQuery<TQueryParameter, TQueryResult>
/// Delegate returns a collection from db.
/// </summary>
/// <returns>Func<DbContext, IEnumerable<IQueryResult>></returns>
public abstract Func<IDbContext, IEnumerable<IQueryResult>> GetQuery();
public abstract IEnumerable<IQueryResult> Run(IDbContext dbContext);
}
}
21 changes: 10 additions & 11 deletions src/Schemio.EF/EFQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ public class EFQueryEngine<T> : IQueryEngine where T : DbContext
public EFQueryEngine(IDbContextFactory<T> _dbContextFactory)
=> this._dbContextFactory = _dbContextFactory;

public IQueryResult[] Execute(IQuery query, IDataContext context)
public bool CanExecute(IQuery query) => query != null && query is ISQLQuery;

public IEnumerable<IQueryResult> Execute(IEnumerable<IQuery> queries, IDataContext context)
{
var output = new List<IQueryResult>();

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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schemio.EF/ISQLQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public interface ISQLQuery
/// Delegate returns a collection from db.
/// </summary>
/// <returns>Func<DbContext, IEnumerable<IQueryResult>></returns>
Func<IDbContext, IEnumerable<IQueryResult>> GetQuery();
IEnumerable<IQueryResult> Run(IDbContext dbContext);
}
}
33 changes: 18 additions & 15 deletions src/Schemio.SQL/SQLEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IQueryResult> Execute(IEnumerable<IQuery> queries, IDataContext context)
{
var output = new List<IQueryResult>();

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();
}
Expand Down
11 changes: 0 additions & 11 deletions src/Schemio/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,5 @@ public virtual void ResolveParameterInChildMode(IDataContext context, IQueryResu
public virtual void ResolveParameterInParentMode(IDataContext context)
{
}

/// <summary>
/// Runs this query with query engine instance.
/// </summary>
/// <param name="engine"></param>
/// <param name="context"></param>
/// <returns></returns>
public IQueryResult[] Run(IQueryEngine engine, IDataContext context)
{
return engine.Execute(this, context);
}
}
}
2 changes: 0 additions & 2 deletions src/Schemio/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public interface IQuery
void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult);

bool IsContextResolved();

IQueryResult[] Run(IQueryEngine engine, IDataContext context);
}

// public interface IQuery
Expand Down
4 changes: 2 additions & 2 deletions src/Schemio/IQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IQueryResult> Execute(IEnumerable<IQuery> queries, IDataContext context);
}
}
14 changes: 7 additions & 7 deletions src/Schemio/Impl/QueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ private IQueryList Process(IDataContext context, IQueryList queries, List<IQuery
return subscriber.ResolvedDependents;
}

private List<IQueryResult> Run(IQueryList queries, IDataContext context)
private List<IQueryResult> Run(IQueryList queryList, IDataContext context)
{
var output = new List<IQueryResult>();

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;
Expand Down
8 changes: 5 additions & 3 deletions tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal class QueryExecutorTests
public void Setup()
{
_queryEngine = new Mock<IQueryEngine>();
_queryEngine.Setup(x => x.CanExecute(It.IsAny<IQuery>())).Returns(true);

_queryExecutor = new QueryExecutor(new[] { _queryEngine.Object });
}

Expand All @@ -26,15 +28,15 @@ public void TestQueryExecutorToReturnWhenNoQueries()
{
_queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList());

_queryEngine.Verify(x => x.Execute(It.IsAny<IQuery>(), It.IsAny<IDataContext>()), Times.Never());
_queryEngine.Verify(x => x.Execute(It.IsAny<IEnumerable<IQuery>>(), It.IsAny<IDataContext>()), Times.Never());
}

[Test]
public void TestQueryExecutorToCallEngineWhenQueriesExistForExecution()
{
_queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList(new[] { new CustomerQuery() }) { });

_queryEngine.Verify(x => x.Execute(It.IsAny<IQuery>(), It.IsAny<IDataContext>()), Times.Once());
_queryEngine.Verify(x => x.Execute(It.IsAny<IEnumerable<IQuery>>(), It.IsAny<IDataContext>()), Times.Once());
}

[Test] // TODO - All sequence assertions
Expand All @@ -45,7 +47,7 @@ public void TestQueryExecutorToExecuteConfiguredQueriesInCorrectOrder()

_queryExecutor.Execute(new DataContext(new EntityContext()), querList);

_queryEngine.Verify(x => x.Execute(It.IsAny<IQuery>(), It.IsAny<IDataContext>()), Times.Once());
_queryEngine.Verify(x => x.Execute(It.IsAny<IEnumerable<IQuery>>(), It.IsAny<IDataContext>()), Times.Once());
}
}
}

0 comments on commit 497c761

Please sign in to comment.