-
-
Notifications
You must be signed in to change notification settings - Fork 551
Address Dictionary query generation #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
034b8d3
add wherefragment overload that allows for per-fragment templateing c…
a6ace9f
add failing tests
4df819c
a couple tests passing, but I am sure that we're not creating the que…
00648a8
add tests that hit database and add ability to set parameter type
e76bd40
make the dictionary stuff generic, so that dictionaries can be any ty…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,53 @@ | ||
| using Marten.Services; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace Marten.Testing.Linq | ||
| { | ||
| public class dictionary_is_translated : DocumentSessionFixture<NulloIdentityMap> | ||
| { | ||
| public dictionary_is_translated() | ||
| { | ||
| theStore.BulkInsert(Target.GenerateRandomData(100).ToArray()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void dictionary_containskey_is_translated_to_json_map() | ||
| { | ||
| var query = theSession.Query<Target>().Where(t => t.StringDict.ContainsKey("foo")); | ||
| var command = query.ToCommand(Marten.Linq.FetchType.FetchMany); | ||
| var dictParam = command.Parameters[0]; | ||
| (dictParam.DbType == System.Data.DbType.String).ShouldBeTrue(); | ||
| (dictParam.Value.ToString() == "foo").ShouldBeTrue(); | ||
| } | ||
|
|
||
| // using key0 and value0 for these because the last node, which is deep, should have at least a single dict node | ||
|
|
||
| [Fact] | ||
| public void dict_can_query_using_containskey() | ||
| { | ||
| var results = theSession.Query<Target>().Where(x => x.StringDict.ContainsKey("key0")).ToList(); | ||
| results.All(r => r.StringDict.ContainsKey("key0")).ShouldBeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void dict_can_query_using_containsKVP() | ||
| { | ||
| var kvp = new KeyValuePair<string, string>("key0", "value0"); | ||
| var results = theSession.Query<Target>().Where(x => x.StringDict.Contains(kvp)).ToList(); | ||
| results.All(r => r.StringDict.Contains(kvp)).ShouldBeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void icollection_keyvaluepair_contains_is_translated_to_json_map() | ||
| { | ||
| var query = theSession.Query<Target>().Where(t => t.StringDict.Contains(new KeyValuePair<string, string>("foo", "bar"))); | ||
| var command = query.ToCommand(Marten.Linq.FetchType.FetchMany); | ||
| var dictParam = command.Parameters[0]; | ||
| (dictParam.NpgsqlDbType == NpgsqlTypes.NpgsqlDbType.Jsonb).ShouldBeTrue(); | ||
| (dictParam.Value.ToString() == "{\"foo\":\"bar\"}").ShouldBeTrue(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| using System; | ||
| using System.Linq.Expressions; | ||
| using Marten.Schema; | ||
| using System.Reflection; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Marten.Linq.Parsing | ||
| { | ||
| public class DictionaryExpressions : IMethodCallParser | ||
| { | ||
| static bool IsCollectionContainsWithStringKey(MethodInfo m) => | ||
| m.Name == "Contains" | ||
| && m.DeclaringType.IsConstructedGenericType | ||
| && m.DeclaringType.GetGenericTypeDefinition() == typeof(ICollection<>) | ||
| && m.DeclaringType.GenericTypeArguments[0].IsConstructedGenericType | ||
| && m.DeclaringType.GenericTypeArguments[0].GetGenericTypeDefinition() == typeof(KeyValuePair<,>) | ||
| && m.DeclaringType.GenericTypeArguments[0].GenericTypeArguments[0] == typeof(string); | ||
|
|
||
| static bool IsDictionaryContainsKey(MethodInfo m) => | ||
| m.Name == "ContainsKey" | ||
| && m.DeclaringType.IsConstructedGenericType | ||
| && m.DeclaringType.GetGenericTypeDefinition() == typeof(IDictionary<,>) | ||
| && m.DeclaringType.GenericTypeArguments[0] == typeof(string); | ||
|
|
||
| public bool Matches(MethodCallExpression expression) | ||
| { | ||
| return IsCollectionContainsWithStringKey(expression.Method) | ||
| || IsDictionaryContainsKey(expression.Method); | ||
| } | ||
|
|
||
| public IWhereFragment Parse(IQueryableDocument mapping, ISerializer serializer, MethodCallExpression expression) | ||
| { | ||
| var finder = new FindMembers(); | ||
| finder.Visit(expression); | ||
| var members = finder.Members; | ||
| var fieldlocator = mapping.FieldFor(members).SqlLocator; | ||
|
|
||
| if (IsCollectionContainsWithStringKey(expression.Method)) | ||
| { | ||
| return QueryFromICollectionContains(expression, fieldlocator, serializer); | ||
| } | ||
| else if (IsDictionaryContainsKey(expression.Method)) | ||
| { | ||
| return QueryFromDictionaryContainsKey(expression, fieldlocator); | ||
| } | ||
| else throw new NotImplementedException("Could not understand the format of the dictionary access"); | ||
| } | ||
|
|
||
| static IWhereFragment QueryFromDictionaryContainsKey(MethodCallExpression expression, string fieldLocator) | ||
| { | ||
| var key = (string)expression.Arguments[0].Value(); | ||
| // have to use different token here because we actually want the `?` character as the operator! | ||
| return new CustomizableWhereFragment($"{fieldLocator} ? @1", "@1", Tuple.Create<object, NpgsqlTypes.NpgsqlDbType?>(key, NpgsqlTypes.NpgsqlDbType.Text)); | ||
| } | ||
|
|
||
| static IWhereFragment QueryFromICollectionContains(MethodCallExpression expression, string fieldPath, ISerializer serializer) | ||
| { | ||
| var constant = expression.Arguments[0] as ConstantExpression; | ||
| var kvp = constant.Value; // is kvp<string, unknown> | ||
| var kvpType = kvp.GetType(); | ||
| var key = kvpType.GetProperty("Key").GetValue(kvp); | ||
| var value = kvpType.GetProperty("Value").GetValue(kvp); | ||
| var dictType = typeof(Dictionary<,>).MakeGenericType(kvpType.GenericTypeArguments[0], kvpType.GenericTypeArguments[1]); | ||
| var dict = dictType.GetConstructors()[0].Invoke(null); | ||
| dictType.GetMethod("Add").Invoke(dict, new[] { key, value }); | ||
| var json = serializer.ToJson(dict); | ||
| return new CustomizableWhereFragment($"{fieldPath} @> ?", "?", Tuple.Create<object, NpgsqlTypes.NpgsqlDbType?>(json, NpgsqlTypes.NpgsqlDbType.Jsonb)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed this to make the tests green up on my machine