Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Mar 20, 2024
1 parent 41a74a4 commit e39c815
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
26 changes: 13 additions & 13 deletions src/Foundatio.Parsers.SqlQueries/Extensions/SqlNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Foundatio.Parsers.SqlQueries.Extensions;

public static class SqlNodeExtensions
{
public static string ToSqlString(this GroupNode node, ISqlQueryVisitorContext context)
public static string ToDynamicLinqString(this GroupNode node, ISqlQueryVisitorContext context)
{
// support overriding the generated query
if (node.TryGetQuery(out string query))
Expand All @@ -36,7 +36,7 @@ public static string ToSqlString(this GroupNode node, ISqlQueryVisitorContext co
builder.Append("(");

if (node.Left != null)
builder.Append(node.Left is GroupNode groupNode ? groupNode.ToSqlString(context) : node.Left.ToSqlString(context));
builder.Append(node.Left is GroupNode groupNode ? groupNode.ToDynamicLinqString(context) : node.Left.ToDynamicLinqString(context));

if (node.Left != null && node.Right != null)
{
Expand All @@ -47,7 +47,7 @@ public static string ToSqlString(this GroupNode node, ISqlQueryVisitorContext co
}

if (node.Right != null)
builder.Append(node.Right is GroupNode groupNode ? groupNode.ToSqlString(context) : node.Right.ToSqlString(context));
builder.Append(node.Right is GroupNode groupNode ? groupNode.ToDynamicLinqString(context) : node.Right.ToDynamicLinqString(context));

if (node.HasParens)
builder.Append(")");
Expand All @@ -61,7 +61,7 @@ public static string ToSqlString(this GroupNode node, ISqlQueryVisitorContext co
return builder.ToString();
}

public static string ToSqlString(this ExistsNode node, ISqlQueryVisitorContext context)
public static string ToDynamicLinqString(this ExistsNode node, ISqlQueryVisitorContext context)
{
if (String.IsNullOrEmpty(node.Field))
context.AddValidationError("Field is required for exists node queries.");
Expand All @@ -81,7 +81,7 @@ public static string ToSqlString(this ExistsNode node, ISqlQueryVisitorContext c
return builder.ToString();
}

public static string ToSqlString(this MissingNode node, ISqlQueryVisitorContext context)
public static string ToDynamicLinqString(this MissingNode node, ISqlQueryVisitorContext context)
{
if (String.IsNullOrEmpty(node.Field))
context.AddValidationError("Field is required for missing node queries.");
Expand Down Expand Up @@ -113,7 +113,7 @@ public static EntityFieldInfo GetFieldInfo(List<EntityFieldInfo> fields, string
new EntityFieldInfo { Field = field };
}

public static string ToSqlString(this TermNode node, ISqlQueryVisitorContext context)
public static string ToDynamicLinqString(this TermNode node, ISqlQueryVisitorContext context)
{
if (!String.IsNullOrEmpty(node.Prefix))
context.AddValidationError("Prefix is not supported for term range queries.");
Expand Down Expand Up @@ -212,7 +212,7 @@ private static void AppendField(StringBuilder builder, EntityFieldInfo field, st
builder.Append("\"" + term + "\"");
}

public static string ToSqlString(this TermRangeNode node, ISqlQueryVisitorContext context)
public static string ToDynamicLinqString(this TermRangeNode node, ISqlQueryVisitorContext context)
{
if (String.IsNullOrEmpty(node.Field))
context.AddValidationError("Field is required for term range queries.");
Expand Down Expand Up @@ -260,15 +260,15 @@ public static string ToSqlString(this TermRangeNode node, ISqlQueryVisitorContex
return builder.ToString();
}

public static string ToSqlString(this IQueryNode node, ISqlQueryVisitorContext context)
public static string ToDynamicLinqString(this IQueryNode node, ISqlQueryVisitorContext context)
{
return node switch
{
GroupNode groupNode => groupNode.ToSqlString(context),
ExistsNode existsNode => existsNode.ToSqlString(context),
MissingNode missingNode => missingNode.ToSqlString(context),
TermNode termNode => termNode.ToSqlString(context),
TermRangeNode termRangeNode => termRangeNode.ToSqlString(context),
GroupNode groupNode => groupNode.ToDynamicLinqString(context),
ExistsNode existsNode => existsNode.ToDynamicLinqString(context),
MissingNode missingNode => missingNode.ToDynamicLinqString(context),
TermNode termNode => termNode.ToDynamicLinqString(context),
TermRangeNode termRangeNode => termRangeNode.ToDynamicLinqString(context),
_ => throw new NotSupportedException($"Node type {node.GetType().Name} is not supported.")
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Foundatio.Parsers.SqlQueries/SqlQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<QueryValidationResult> ValidateAsync(string query, SqlQueryVis
return await ValidationVisitor.RunAsync(node, context);
}

public async Task<string> ToSqlAsync(string query, SqlQueryVisitorContext context)
public async Task<string> ToDynamicLinqAsync(string query, SqlQueryVisitorContext context)
{
var node = await ParseAsync(query, context);
var result = await ValidationVisitor.RunAsync(node, context);
Expand Down Expand Up @@ -110,10 +110,10 @@ private void AddEntityFields(List<EntityFieldInfo> fields, IEntityType entityTyp

foreach (var property in entityType.GetProperties())
{
string propertyPath = prefix + property.Name;
if (!Configuration.EntityTypePropertyFilter(property))
continue;

string propertyPath = prefix + property.Name;
fields.Add(new EntityFieldInfo
{
Field = propertyPath,
Expand All @@ -126,19 +126,19 @@ private void AddEntityFields(List<EntityFieldInfo> fields, IEntityType entityTyp

foreach (var nav in entityType.GetNavigations())
{
string propertyPath = prefix + nav.Name;
if (!Configuration.EntityTypeNavigationFilter(nav))
continue;

string propertyPath = prefix + nav.Name;
AddEntityFields(fields, nav.TargetEntityType, entityTypeStack, propertyPath + ".", false, depth + 1);
}

foreach (var skipNav in entityType.GetSkipNavigations())
{
string propertyPath = prefix + skipNav.Name;
if (!Configuration.EntityTypeSkipNavigationFilter(skipNav))
continue;

string propertyPath = prefix + skipNav.Name;
AddEntityFields(fields, skipNav.TargetEntityType, entityTypeStack, propertyPath + ".", true, depth + 1);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Foundatio.Parsers.SqlQueries/Visitors/GenerateSqlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override Task VisitAsync(GroupNode node, IQueryVisitorContext context)
if (context is not ISqlQueryVisitorContext sqlContext)
throw new InvalidOperationException("The context must be an ISqlQueryVisitorContext.");

_builder.Append(node.ToSqlString(sqlContext));
_builder.Append(node.ToDynamicLinqString(sqlContext));

return Task.CompletedTask;
}
Expand All @@ -26,31 +26,31 @@ public override void Visit(TermNode node, IQueryVisitorContext context)
if (context is not ISqlQueryVisitorContext sqlContext)
throw new InvalidOperationException("The context must be an ISqlQueryVisitorContext.");

_builder.Append(node.ToSqlString(sqlContext));
_builder.Append(node.ToDynamicLinqString(sqlContext));
}

public override void Visit(TermRangeNode node, IQueryVisitorContext context)
{
if (context is not ISqlQueryVisitorContext sqlContext)
throw new InvalidOperationException("The context must be an ISqlQueryVisitorContext.");

_builder.Append(node.ToSqlString(sqlContext));
_builder.Append(node.ToDynamicLinqString(sqlContext));
}

public override void Visit(ExistsNode node, IQueryVisitorContext context)
{
if (context is not ISqlQueryVisitorContext sqlContext)
throw new InvalidOperationException("The context must be an ISqlQueryVisitorContext.");

_builder.Append(node.ToSqlString(sqlContext));
_builder.Append(node.ToDynamicLinqString(sqlContext));
}

public override void Visit(MissingNode node, IQueryVisitorContext context)
{
if (context is not ISqlQueryVisitorContext sqlContext)
throw new InvalidOperationException("The context must be an ISqlQueryVisitorContext.");

_builder.Append(node.ToSqlString(sqlContext));
_builder.Append(node.ToDynamicLinqString(sqlContext));
}

public override async Task<string> AcceptAsync(IQueryNode node, IQueryVisitorContext context)
Expand Down
1 change: 0 additions & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project>
<Import Project="..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;</TargetFrameworks>
<IsPackable>False</IsPackable>
<NoWarn>$(NoWarn);CS1591;NU1701</NoWarn>
Expand Down
12 changes: 6 additions & 6 deletions tests/Foundatio.Parsers.SqlQueries.Tests/SqlQueryParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task CanSearchDefaultFields()
string sqlExpected = db.Employees.Where(e => e.FullName.Contains("John") || e.Title.Contains("John")).ToQueryString();
string sqlActual = db.Employees.Where("""FullName.Contains("John") || Title.Contains("John")""").ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
string sql = await parser.ToSqlAsync("John", context);
string sql = await parser.ToDynamicLinqAsync("John", context);
sqlActual = db.Employees.Where(sql).ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
}
Expand All @@ -90,7 +90,7 @@ public async Task CanUseDateFilter()
string sqlExpected = db.Employees.Where(e => e.Created > new DateTime(2024, 1, 1)).ToQueryString();
string sqlActual = db.Employees.Where("""created > DateTime.Parse("2024-01-01")""").ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
string sql = await parser.ToSqlAsync("created:>2024-01-01", context);
string sql = await parser.ToDynamicLinqAsync("created:>2024-01-01", context);
sqlActual = db.Employees.Where(sql).ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
}
Expand All @@ -108,7 +108,7 @@ public async Task CanUseCollectionDefaultFields()
string sqlExpected = db.Employees.Where(e => e.Companies.Any(c => c.Name.Contains("acme"))).ToQueryString();
string sqlActual = db.Employees.Where("""Companies.Any(Name.Contains("acme"))""").ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
string sql = await parser.ToSqlAsync("acme", context);
string sql = await parser.ToDynamicLinqAsync("acme", context);
sqlActual = db.Employees.Where(sql).ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
}
Expand All @@ -127,16 +127,16 @@ public async Task CanGenerateSql()
string sqlExpected = db.Employees.Where(e => e.Companies.Any(c => c.Name == "acme") && e.DataValues.Any(dv => dv.DataDefinitionId == 1 && dv.NumberValue == 30)).ToQueryString();
string sqlActual = db.Employees.Where("""Companies.Any(Name = "acme") AND DataValues.Any(DataDefinitionId = 1 AND NumberValue = 30) """).ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
string sql = await parser.ToSqlAsync("companies.name:acme age:30", context);
string sql = await parser.ToDynamicLinqAsync("companies.name:acme age:30", context);
sqlActual = db.Employees.Where(sql).ToQueryString();
Assert.Equal(sqlExpected, sqlActual);

var q = db.Employees.AsNoTracking();
sql = await parser.ToSqlAsync("companies.name:acme age:30", context);
sql = await parser.ToDynamicLinqAsync("companies.name:acme age:30", context);
sqlActual = q.Where(sql, db.Employees).ToQueryString();
Assert.Equal(sqlExpected, sqlActual);

await Assert.ThrowsAsync<ValidationException>(() => parser.ToSqlAsync("companies.description:acme", context));
await Assert.ThrowsAsync<ValidationException>(() => parser.ToDynamicLinqAsync("companies.description:acme", context));

var employees = await db.Employees.Where(e => e.Title == "software developer" && e.DataValues.Any(dv => dv.DataDefinitionId == 1 && dv.NumberValue == 30))
.ToListAsync();
Expand Down

0 comments on commit e39c815

Please sign in to comment.