-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved LINQ support for Dictionaries. Closes GH-2651
- Loading branch information
1 parent
5aae5b6
commit 2150cbf
Showing
37 changed files
with
921 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Marten/Linq/Members/Dictionaries/DictionaryContainsKeyFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration; | ||
using Marten.Exceptions; | ||
using Marten.Internal.CompiledQueries; | ||
using Marten.Linq.Parsing; | ||
using Weasel.Postgresql; | ||
using Weasel.Postgresql.SqlGeneration; | ||
|
||
namespace Marten.Linq.Members.Dictionaries; | ||
|
||
internal class DictionaryContainsKeyFilter: ISqlFragment, ICompiledQueryAwareFilter | ||
{ | ||
private readonly object _value; | ||
private readonly string _keyText; | ||
private readonly IDictionaryMember _member; | ||
|
||
public DictionaryContainsKeyFilter(IDictionaryMember member, ISerializer serializer, ConstantExpression constant) | ||
{ | ||
_value = constant.Value; | ||
_keyText = serializer.ToCleanJson(_value); | ||
|
||
_member = member; | ||
} | ||
|
||
public DictionaryContainsKeyFilter(IDictionaryMember member, ISerializer serializer, object value) | ||
{ | ||
_value = value; | ||
_keyText = serializer.ToCleanJson(_value); | ||
|
||
_member = member; | ||
} | ||
|
||
public void Apply(CommandBuilder builder) | ||
{ | ||
builder.Append("d.data #> '{"); | ||
foreach (var segment in _member.JsonPathSegments()) | ||
{ | ||
builder.Append(segment); | ||
builder.Append(", "); | ||
} | ||
|
||
builder.Append(_keyText); | ||
builder.Append("}' is not null"); | ||
} | ||
|
||
public bool Contains(string sqlText) | ||
{ | ||
return false; | ||
} | ||
|
||
public bool TryMatchValue(object value, MemberInfo member) | ||
{ | ||
throw new BadLinqExpressionException("Marten does not (yet) support Dictionary.ContainsKey() in compiled queries"); | ||
} | ||
|
||
public void GenerateCode(GeneratedMethod method, int parameterIndex) | ||
{ | ||
throw new BadLinqExpressionException("Marten does not (yet) support Dictionary.ContainsKey() in compiled queries"); | ||
} | ||
|
||
public string ParameterName { get; } = "NONE"; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Marten/Linq/Members/Dictionaries/DictionaryCountMember.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Linq.Expressions; | ||
using Weasel.Postgresql.SqlGeneration; | ||
|
||
namespace Marten.Linq.Members.Dictionaries; | ||
|
||
internal class DictionaryCountMember: QueryableMember, IComparableMember | ||
{ | ||
public DictionaryCountMember(IDictionaryMember parent): base(parent, "Count", typeof(int)) | ||
{ | ||
RawLocator = TypedLocator = $"jsonb_array_length(jsonb_path_query_array({parent.TypedLocator}, '$.keyvalue()'))"; | ||
Parent = parent; | ||
} | ||
|
||
public ICollectionMember Parent { get; } | ||
|
||
public override ISqlFragment CreateComparison(string op, ConstantExpression constant) | ||
{ | ||
var def = new CommandParameter(constant); | ||
return new ComparisonFilter(this, def, op); | ||
} | ||
} |
Oops, something went wrong.