Skip to content

Commit

Permalink
Fix tests again
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Oct 21, 2024
1 parent e36255a commit 30b560f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
61 changes: 46 additions & 15 deletions src/Foundatio.Parsers.SqlQueries/Extensions/SqlNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public static string ToDynamicLinqString(this TermNode node, ISqlQueryVisitorCon
context.Tokenizer.Invoke(searchTerm);
}

fieldTerms.ForEach((kvp, info) =>
fieldTerms.ForEach((kvp, x) =>
{
builder.Append(info.IsFirst ? "(" : " OR ");
builder.Append(x.IsFirst ? "(" : " OR ");
var searchTerm = kvp.Value;
var tokens = kvp.Value.Tokens ?? [kvp.Value.Term];
Expand All @@ -153,12 +153,43 @@ public static string ToDynamicLinqString(this TermNode node, ISqlQueryVisitorCon
string collectionField = searchTerm.FieldInfo.Field.Substring(0, dotIndex);
string fieldName = searchTerm.FieldInfo.Field.Substring(dotIndex + 1);
builder.Append(collectionField);
builder.Append(".Any(");
builder.Append(fieldName);
builder.Append(" in (");
builder.Append(String.Join(',', tokens.Select(t => "\"" + t + "\"")));
builder.Append("))");
if (searchTerm.Operator == SqlSearchOperator.Equals)
{
builder.Append(collectionField);
builder.Append(".Any(");
builder.Append(fieldName);
builder.Append(" in (");
builder.Append(String.Join(',', tokens.Select(t => "\"" + t + "\"")));
builder.Append("))");
}
else if (searchTerm.Operator == SqlSearchOperator.Contains)
{
tokens.ForEach((token, i) => {
builder.Append(i.IsFirst ? "(" : " OR ");
builder.Append(collectionField);
builder.Append(".Any(");
builder.Append(fieldName);
builder.Append(".Contains(\"");
builder.Append(token);
builder.Append("\"))");
if (i.IsLast)
builder.Append(")");
});
}
else if (searchTerm.Operator == SqlSearchOperator.StartsWith)
{
tokens.ForEach((token, i) => {
builder.Append(i.IsFirst ? "(" : " OR ");
builder.Append(collectionField);
builder.Append(".Any(");
builder.Append(fieldName);
builder.Append(".StartsWith(\"");
builder.Append(token);
builder.Append("\"))");
if (i.IsLast)
builder.Append(")");
});
}
}
else
{
Expand All @@ -170,25 +201,25 @@ public static string ToDynamicLinqString(this TermNode node, ISqlQueryVisitorCon
}
else if (searchTerm.Operator == SqlSearchOperator.Contains)
{
searchTerm.Tokens.ForEach((token, info) => {
builder.Append(info.IsFirst ? "(" : " OR ");
tokens.ForEach((token, i) => {
builder.Append(i.IsFirst ? "(" : " OR ");
builder.Append(searchTerm.FieldInfo.Field).Append(".Contains(\"").Append(token).Append("\")");
if (info.IsLast)
if (i.IsLast)
builder.Append(")");
});
}
else if (searchTerm.Operator == SqlSearchOperator.StartsWith)
{
searchTerm.Tokens.ForEach((token, info) => {
builder.Append(info.IsFirst ? "(" : " OR ");
tokens.ForEach((token, i) => {
builder.Append(i.IsFirst ? "(" : " OR ");
builder.Append(searchTerm.FieldInfo.Field).Append(".StartsWith(\"").Append(token).Append("\")");
if (info.IsLast)
if (i.IsLast)
builder.Append(")");
});
}
}
if (info.IsLast)
if (x.IsLast)
builder.Append(")");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public async Task CanUseCollectionDefaultFields()

var context = parser.GetContext(db.Employees.EntityType);

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();
string sqlExpected = db.Employees.Where(e => e.Companies.Any(c => c.Name.StartsWith("acme"))).ToQueryString();
string sqlActual = db.Employees.Where("""Companies.Any(Name.StartsWith("acme"))""").ToQueryString();
Assert.Equal(sqlExpected, sqlActual);
string sql = await parser.ToDynamicLinqAsync("acme", context);
sqlActual = db.Employees.Where(sql).ToQueryString();
Expand Down

0 comments on commit 30b560f

Please sign in to comment.