Skip to content

Commit

Permalink
- Add Visitor to query execution
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Sep 24, 2024
1 parent 941109a commit 46aec18
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 61 deletions.
15 changes: 10 additions & 5 deletions Schemio.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 1 addition & 3 deletions src/Schemio.EF/BaseSQLQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.EntityFrameworkCore;

namespace Schemio.EF
{
public abstract class BaseSQLQuery<TQueryParameter, TQueryResult>
Expand All @@ -12,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<DbContext, IEnumerable<IQueryResult>> GetQuery();
public abstract Func<IDbContext, IEnumerable<IQueryResult>> GetQuery();
}
}
35 changes: 14 additions & 21 deletions src/Schemio.EF/EFQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,25 @@ public class EFQueryEngine<T> : IQueryEngine where T : DbContext
public EFQueryEngine(IDbContextFactory<T> _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<IQueryResult>();

var queries = queryList.Queries.Cast<ISQLQuery>();

if (!queries.Any())
return Array.Empty<IQueryResult>();

var output = new List<IQueryResult>();

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();
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/Schemio.EF/IDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Schemio.EF
{
Expand Down
4 changes: 1 addition & 3 deletions src/Schemio.EF/ISQLQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.EntityFrameworkCore;

namespace Schemio.EF
{
public interface ISQLQuery
Expand All @@ -9,6 +7,6 @@ public interface ISQLQuery
/// Delegate returns a collection from db.
/// </summary>
/// <returns>Func<DbContext, IEnumerable<IQueryResult>></returns>
Func<DbContext, IEnumerable<IQueryResult>> GetQuery();
Func<IDbContext, IEnumerable<IQueryResult>> GetQuery();
}
}
3 changes: 0 additions & 3 deletions src/Schemio.SQL/ISQLQuery.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Data;
using Dapper;

namespace Schemio.SQL
{
public interface ISQLQuery : IQuery
{
CommandDefinition GetCommandDefinition();

IEnumerable<IQueryResult> Run(IDbConnection conn);
}
}
22 changes: 5 additions & 17 deletions src/Schemio.SQL/SQLEngine.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<IQueryResult>();
var queries = list.Queries.Cast<ISQLQuery>();

if (!queries.Any())
if (query == null || query is not ISQLQuery)
return output.ToArray();

var factory = DbProviderFactories.GetFactory(sqlConfiguration.ConnectionSettings.ProviderName)
Expand All @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions src/Schemio/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ 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: 2 additions & 0 deletions src/Schemio/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface IQuery
void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult);

bool IsContextResolved();

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

// public interface IQuery
Expand Down
4 changes: 3 additions & 1 deletion src/Schemio/IQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 6 additions & 2 deletions src/Schemio/Impl/QueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ private List<IQueryResult> 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;
Expand Down
8 changes: 4 additions & 4 deletions tests/Schemio.Tests/DataProvider.Tests/QueryExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ public void TestQueryExecutorToReturnWhenNoQueries()
{
_queryExecutor.Execute(new DataContext(new EntityContext()), new QueryList());

_queryEngine.Verify(x => x.Run(It.IsAny<IQueryList>(), It.IsAny<IDataContext>()), Times.Never());
_queryEngine.Verify(x => x.Execute(It.IsAny<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.Run(It.IsAny<IQueryList>(), It.IsAny<IDataContext>()), Times.Once());
_queryEngine.Verify(x => x.Execute(It.IsAny<IQuery>(), It.IsAny<IDataContext>()), Times.Once());
}

[Test] // TODO -
[Test] // TODO - All sequence assertions
public void TestQueryExecutorToExecuteConfiguredQueriesInCorrectOrder()
{
var querList = new QueryBuilder<Customer>(new CustomerSchema(), new XPathMatcher())
.Build(new DataContext(new CustomerContext()));

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

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

0 comments on commit 46aec18

Please sign in to comment.