Skip to content

Commit

Permalink
- Fix ResolveQueryParameter()
Browse files Browse the repository at this point in the history
- Fix bug on parent/child query building.
  • Loading branch information
NinjaRocks committed Nov 6, 2024
1 parent 5842e63 commit 8f06f4e
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 69 deletions.
25 changes: 3 additions & 22 deletions src/Schemio/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio
/// </summary>
/// <typeparam name="TQueryParameter"></typeparam>
/// <typeparam name="TQueryResult"></typeparam>
public abstract class BaseQuery<TQueryParameter, TQueryResult> : IQuery, IChildQuery, IRootQuery
public abstract class BaseQuery<TQueryParameter, TQueryResult> : IQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
Expand Down Expand Up @@ -36,27 +36,8 @@ public Type ResultType
/// <summary>
/// Implement to resolve query parameter.
/// </summary>
/// <param name="context">root context.</param>
/// <param name="context">data context passed to the data provider.</param>
/// <param name="parentQueryResult">query result from parent query (when configured as nested query). Can be null.</param>
protected abstract void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult);

/// <summary>
/// Implement to resolve query parameter for nested queries
/// </summary>
/// <param name="context">root context</param>
/// <param name="parentQueryResult">query result from parent query.</param>
public void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
ResolveQueryParameter(context, parentQueryResult);
}

/// <summary>
/// Implement to resolve query parameter for first level queries.
/// </summary>
/// <param name="context">root context</param>
public void ResolveRootQueryParameter(IDataContext context)
{
ResolveQueryParameter(context, null);
}
public abstract void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult);
}
}
15 changes: 0 additions & 15 deletions src/Schemio/IChildQuery.cs

This file was deleted.

2 changes: 2 additions & 0 deletions src/Schemio/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface IQuery
Type ResultType { get; }

bool IsContextResolved();

void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult = null);
}
}
10 changes: 0 additions & 10 deletions src/Schemio/IRootQuery.cs

This file was deleted.

16 changes: 16 additions & 0 deletions src/Schemio/Impl/DataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ internal TEntity GetData(IDataContext context)
// Build queries for the data source based on the included xPaths
var watch = System.Diagnostics.Stopwatch.StartNew();
var queries = queryBuilder.Build(context);

foreach (var item in queries.Queries)
{
Console.WriteLine("L1 Query To Execute: " + item.GetType().Name);

foreach (var item2 in item.Children)
{
Console.WriteLine("L2 Query To Execute: " + item2.GetType().Name);

foreach (var item3 in item2.Children)
{
Console.WriteLine("L3 Query To Execute: " + item3.GetType().Name);
}
}
}

watch.Stop();
logger?.LogInformation("Query builder executed in " + watch.ElapsedMilliseconds + " ms");

Expand Down
8 changes: 4 additions & 4 deletions src/Schemio/Impl/EventSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void OnEventHandler(IDataContext context, ExecutorResultArgs args)
if (unresolved.ParentQueryResultType != result.GetType())
continue;

foreach (var query in unresolved.Queries.Cast<IChildQuery>())
query.ResolveChildQueryParameter(context, queryResult);
foreach (var query in unresolved.Queries)
query.ResolveQueryParameter(context, queryResult);
}
}

Expand All @@ -48,8 +48,8 @@ public void OnEventHandler(IDataContext context, ExecutorResultArgs args)
!unresolved.ParentQueryResultType.IsAssignableFrom(result.GetType()))
continue;

foreach (var query in unresolved.Queries.Cast<IChildQuery>())
query.ResolveChildQueryParameter(context, queryResult);
foreach (var query in unresolved.Queries)
query.ResolveQueryParameter(context, queryResult);
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/Schemio/Impl/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public QueryBuilder(IEntitySchema<T> entitySchema, ISchemaPathMatcher schemaPath
/// <returns></returns>
public IQueryList Build(IDataContext context)
{
var queries = GetMappedQueries(entitySchema.Mappings.ToList(), context);
var mappings = entitySchema.Mappings.ToList();

foreach (var query in queries.Queries.Cast<IRootQuery>())
query.ResolveRootQueryParameter(context);
var queries = GetMappedQueries(mappings, context);

foreach (var query in queries.Queries)
query.ResolveQueryParameter(context);

return new QueryList(queries.Queries);
}
Expand All @@ -32,16 +34,17 @@ private QueryList GetMappedQueries(IReadOnlyCollection<Mapping<T, IQueryResult>>

for (var index = 1; index <= queryDependencyDepth; index++)
{
var maps = mappings.Where(x => x.Order == index);
var maps = mappings.Where(x => x.Order == index).ToList();

foreach (var map in maps)
{
var dependentQueries =
mappings.Where(x => x.Order == index + 1 && x.DependentOn != null && x.DependentOn.GetType() == map.Query.GetType()).ToList();

map.Query.Children ??= new List<IQuery>();
map.Query.Children = new List<IQuery>();

map.Query.Children.AddRange(FilterByPaths(context, dependentQueries));
var filtered = FilterByPaths(context, dependentQueries);
map.Query.Children.AddRange(filtered);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.EntityFramework.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerCommunicationQuery : BaseSQLQuery<CustomerParameter, CommunicationResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to customer query.
var customer = (CustomerResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.EntityFramework.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerOrderItemsQuery : BaseSQLQuery<OrderItemParameter, OrderItemResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to order query.
var ordersResult = (CustomerOrderResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.EntityFramework.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerOrdersQuery : BaseSQLQuery<CustomerParameter, CustomerOrderResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to customer query.
var customer = (CustomerResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.EntityFramework.Tests.EntitySetup.EntitySchemas.Queries
{
public class CustomerQuery : BaseSQLQuery<CustomerParameter, CustomerResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerCommunicationQuery : BaseSQLQuery<CustomerParameter, CommunicationResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to customer query.
var customer = (CustomerResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerOrderItemsQuery : BaseSQLQuery<OrderItemParameter, OrderItemResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child query to order query taking OrderResult to resolve query parameter.
var ordersResult = (OrderResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerOrdersQuery : BaseSQLQuery<CustomerParameter, OrderResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to customer query.
var customer = (CustomerResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
public class CustomerQuery : BaseSQLQuery<CustomerParameter, CustomerResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Schemio.Tests.EntitySetup.Queries
{
internal class CustomerCommunicationQuery : BaseQuery<CustomerParameter, CommunicationResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to customer query.
var customer = (CustomerResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Schemio.Tests.EntitySetup.Queries
{
internal class CustomerOrderItemsQuery : BaseQuery<OrderItemParameter, CollectionResult<OrderItemValue>>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Execute as child to order query.
var ordersResult = (OrderCollectionResult)parentQueryResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Schemio.Tests.EntitySetup.Queries
{
internal class CustomerOrdersQuery : BaseQuery<CustomerParameter, CollectionResult<OrderValue>>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Does not execute as child to any query.
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Schemio.Tests/EntitySetup/Queries/CustomerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Schemio.Tests.EntitySetup.Queries
{
public class CustomerQuery : BaseQuery<CustomerParameter, CustomerResult>
{
protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
public override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
Expand Down

0 comments on commit 8f06f4e

Please sign in to comment.