Skip to content

Commit

Permalink
Starting WHERE clause parsing in the Linq overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jul 11, 2023
1 parent 9dbd095 commit afe3807
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/LinqTests/playing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task try_linq()

var targets = await theSession.Query<Target>()
.OrderBy(x => x.Double)
//.Where(x => x.Number == 5)
.Where(x => x.Number == 5)
.Take(5)
.ToListAsync();

Expand Down
11 changes: 10 additions & 1 deletion src/Marten/Linq/New/CollectionUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ public NewStatement BuildStatement(DocumentCollection collection)
Offset = _offset
};

// TODO - Skip, Ordering, Where
// TODO - Where

foreach (var ordering in Ordering)
{
statement.Ordering.Expressions.Add(ordering.BuildExpression(collection));
}

// TODO -- watch for "ANDS"
var parser = new WhereClauseParser(collection, statement);
foreach (var expression in Wheres)
{
parser.Visit(expression);
}

// TODO -- have the document storage wrap or using default where fragments


return statement;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Marten/Linq/New/Fragments/EqualsFragment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Weasel.Postgresql;
using Weasel.Postgresql.SqlGeneration;

namespace Marten.Linq.New.Fragments;

public record EqualsFragment(string Locator, object Value) : ISqlFragment
{
public void Apply(CommandBuilder builder)
{
builder.Append(Locator);
builder.Append(" = ");
builder.AppendParameter(Value);
}

public bool Contains(string sqlText)
{
return false;
}
}
11 changes: 9 additions & 2 deletions src/Marten/Linq/New/NewStatement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using Marten.Linq.SqlGeneration;
using Weasel.Postgresql;
using Weasel.Postgresql.SqlGeneration;
Expand Down Expand Up @@ -46,9 +45,10 @@ bool ISqlFragment.Contains(string sqlText)
// TODO -- revisit this later for multi-tenancy searching
return false;
}

}

public class NewSelectorStatement : NewStatement
public class NewSelectorStatement : NewStatement, IWhereFragmentHolder
{
//public ISqlFragment FromClause { get; set; }

Expand Down Expand Up @@ -85,6 +85,13 @@ protected override void apply(CommandBuilder builder)


public List<ISqlFragment> Wheres { get; } = new();

public void Register(ISqlFragment fragment)
{
Wheres.Add(fragment);
}


public OrderByFragment Ordering { get; } = new();

// TODO -- this would conceivably overwritten depending on usage of
Expand Down
45 changes: 45 additions & 0 deletions src/Marten/Linq/New/WhereClauseParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq.Expressions;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Parsing;
using Weasel.Postgresql.SqlGeneration;

namespace Marten.Linq.New;

public class WhereClauseParser : RelinqExpressionVisitor
{
private readonly IQueryableCollection _collection;
private readonly IWhereFragmentHolder _holder;

public WhereClauseParser(IQueryableCollection collection, IWhereFragmentHolder holder)
{
_collection = collection;
_holder = holder;
}

protected override Expression VisitNew(NewExpression expression)
{
return base.VisitNew(expression);
}

protected override Expression VisitSubQuery(SubQueryExpression expression)
{
return base.VisitSubQuery(expression);
}

protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
{
return base.VisitQuerySourceReference(expression);
}

protected override Expression VisitBinary(BinaryExpression node)
{
return base.VisitBinary(node);
}

protected override Expression VisitUnary(UnaryExpression node)
{
return base.VisitUnary(node);
}


}
2 changes: 1 addition & 1 deletion src/Marten/Linq/Parsing/WhereClauseParser.BinarySide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public BinarySide(Expression memberExpression)
public IField Field { get; set; }
public IComparableFragment Comparable { get; set; }

public Expression MemberExpression { get; }
public Expression MemberExpression { get; }s

public ISqlFragment CompareTo(BinarySide right, string op)
{
Expand Down

0 comments on commit afe3807

Please sign in to comment.