From 06707f75fedcebfb553e57f2a361a2422cd971d2 Mon Sep 17 00:00:00 2001 From: oneRain Date: Mon, 16 Sep 2019 18:25:05 +0800 Subject: [PATCH] =?UTF-8?q?*=20AVIMConversationQuery.cs:=20chore:=20?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=20QueryCondition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * AVQuery.cs: * AVRelation.cs: * AVQueryExtensions.cs: * AVQueryExtensions.cs: * QueryCompositionalCondition.cs: --- RTM/RTM/Public/AVIMConversationQuery.cs | 2 +- .../Query/QueryCompositionalCondition.cs | 81 ++++++++++++- .../Internal/Utilities/AVQueryExtensions.cs | 4 - Storage/Storage/Public/AVQuery.cs | 107 +++--------------- Storage/Storage/Public/AVQueryExtensions.cs | 32 ------ Storage/Storage/Public/AVRelation.cs | 1 - 6 files changed, 99 insertions(+), 128 deletions(-) diff --git a/RTM/RTM/Public/AVIMConversationQuery.cs b/RTM/RTM/Public/AVIMConversationQuery.cs index caf8f8c..287d635 100644 --- a/RTM/RTM/Public/AVIMConversationQuery.cs +++ b/RTM/RTM/Public/AVIMConversationQuery.cs @@ -12,7 +12,7 @@ namespace LeanCloud.Realtime /// /// 对话查询类 /// - public class AVIMConversationQuery : AVQuery + public class AVIMConversationQuery { internal AVIMClient CurrentClient { get; set; } internal AVIMConversationQuery(AVIMClient _currentClient) diff --git a/Storage/Storage/Internal/Query/QueryCompositionalCondition.cs b/Storage/Storage/Internal/Query/QueryCompositionalCondition.cs index 3783d3d..f1d997f 100644 --- a/Storage/Storage/Internal/Query/QueryCompositionalCondition.cs +++ b/Storage/Storage/Internal/Query/QueryCompositionalCondition.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Linq; +using System.Collections.Generic; +using System.Collections.ObjectModel; namespace LeanCloud.Storage.Internal { internal class QueryCompositionalCondition : IQueryCondition { @@ -8,9 +11,18 @@ namespace LeanCloud.Storage.Internal { readonly List conditions; readonly string composition; + internal ReadOnlyCollection orderBy; + internal HashSet includes; + internal HashSet selectedKeys; + internal string redirectClassNameForKey; + internal int skip; + internal int limit; + internal QueryCompositionalCondition(string composition = AND) { conditions = new List(); this.composition = composition; + skip = 0; + limit = 30; } public bool Equals(IQueryCondition other) { @@ -33,6 +45,73 @@ namespace LeanCloud.Storage.Internal { }; } + /// + /// 构建查询字符串 + /// + /// + public IDictionary BuildParameters(string className) { + Dictionary result = new Dictionary(); + if (conditions != null) { + result["where"] = ToJSON(); + } + if (orderBy != null) { + result["order"] = string.Join(",", orderBy.ToArray()); + } + if (includes != null) { + result["include"] = string.Join(",", includes.ToArray()); + } + if (selectedKeys != null) { + result["keys"] = string.Join(",", selectedKeys.ToArray()); + } + if (!string.IsNullOrEmpty(className)) { + result["className"] = className; + } + if (redirectClassNameForKey != null) { + result["redirectClassNameForKey"] = redirectClassNameForKey; + } + result["skip"] = skip; + result["limit"] = limit; + return result; + } + + internal void OrderBy(string key) { + orderBy = new ReadOnlyCollection(new List { key }); + } + + internal void OrderByDescending(string key) { + orderBy = new ReadOnlyCollection(new List { "-" + key }); + } + + internal void Include(string key) { + if (includes == null) { + includes = new HashSet(); + } + try { + includes.Add(key); + } catch (Exception e) { + AVClient.PrintLog(e.Message); + } + } + + internal void Select(string key) { + if (selectedKeys == null) { + selectedKeys = new HashSet(); + } + try { + selectedKeys.Add(key); + } catch (Exception e) { + AVClient.PrintLog(e.Message); + } + } + + internal void Skip(int count) { + skip = count; + } + + internal void Limit(int count) { + limit = count; + } + internal void AddCondition(IQueryCondition condition) { if (condition == null) { return; diff --git a/Storage/Storage/Internal/Utilities/AVQueryExtensions.cs b/Storage/Storage/Internal/Utilities/AVQueryExtensions.cs index 56cdb2c..4e4dd59 100644 --- a/Storage/Storage/Internal/Utilities/AVQueryExtensions.cs +++ b/Storage/Storage/Internal/Utilities/AVQueryExtensions.cs @@ -17,10 +17,6 @@ namespace LeanCloud.Storage.Internal { return query.ClassName; } - public static IDictionary BuildParameters(this AVQuery query) where T: AVObject { - return query.BuildParameters(false); - } - public static object GetConstraint(this AVQuery query, string key) where T : AVObject { return query.GetConstraint(key); } diff --git a/Storage/Storage/Public/AVQuery.cs b/Storage/Storage/Public/AVQuery.cs index e552e24..459db52 100644 --- a/Storage/Storage/Public/AVQuery.cs +++ b/Storage/Storage/Public/AVQuery.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -31,13 +30,6 @@ namespace LeanCloud { internal QueryCompositionalCondition condition; - internal ReadOnlyCollection orderBy; - internal ReadOnlyCollection includes; - internal ReadOnlyCollection selectedKeys; - internal string redirectClassNameForKey; - internal int? skip; - internal int? limit; - internal static AVQueryController QueryController { get { return AVPlugins.Instance.QueryController; @@ -121,12 +113,9 @@ namespace LeanCloud { } public virtual async Task GetAsync(string objectId, CancellationToken cancellationToken) { - AVQuery singleItemQuery = new AVQuery(ClassName) - .WhereEqualTo("objectId", objectId); - singleItemQuery.includes = includes; - singleItemQuery.selectedKeys = selectedKeys; - singleItemQuery.limit = 1; - var result = await singleItemQuery.FindAsync(cancellationToken); + WhereEqualTo("objectId", objectId); + Limit(1); + var result = await FindAsync(cancellationToken); var first = result.FirstOrDefault(); if (first == null) { throw new AVException(AVException.ErrorCode.ObjectNotFound, @@ -179,40 +168,6 @@ namespace LeanCloud { #endregion - /// - /// 构建查询字符串 - /// - /// 是否包含 ClassName - /// - public IDictionary BuildParameters(bool includeClassName = false) { - Dictionary result = new Dictionary(); - if (condition != null) { - result["where"] = condition.ToJSON(); - } - if (orderBy != null) { - result["order"] = string.Join(",", orderBy.ToArray()); - } - if (skip != null) { - result["skip"] = skip.Value; - } - if (limit != null) { - result["limit"] = limit.Value; - } - if (includes != null) { - result["include"] = string.Join(",", includes.ToArray()); - } - if (selectedKeys != null) { - result["keys"] = string.Join(",", selectedKeys.ToArray()); - } - if (includeClassName) { - result["className"] = ClassName; - } - if (redirectClassNameForKey != null) { - result["redirectClassNameForKey"] = redirectClassNameForKey; - } - return result; - } - /// /// Determines whether the specified object is equal to the current object. /// @@ -225,12 +180,7 @@ namespace LeanCloud { var other = obj as AVQuery; return ClassName.Equals(other.ClassName) && - condition.Equals(other.condition) && - orderBy.CollectionsEqual(other.orderBy) && - includes.CollectionsEqual(other.includes) && - selectedKeys.CollectionsEqual(other.selectedKeys) && - Equals(skip, other.skip) && - Equals(limit, other.limit); + condition.Equals(other.condition); } public override int GetHashCode() { @@ -240,59 +190,34 @@ namespace LeanCloud { #region Order By public AVQuery OrderBy(string key) { - orderBy = new ReadOnlyCollection(new List { key }); + condition.OrderBy(key); return this; } public AVQuery OrderByDescending(string key) { - orderBy = new ReadOnlyCollection(new List { "-" + key }); - return this; - } - - public AVQuery ThenBy(string key) { - if (orderBy == null) { - throw new ArgumentException("You must call OrderBy before calling ThenBy"); - } - List newOrderBy = orderBy.ToList(); - newOrderBy.Add(key); - orderBy = new ReadOnlyCollection(newOrderBy); - return this; - } - - public AVQuery ThenByDescending(string key) { - if (orderBy == null) { - throw new ArgumentException("You must call OrderBy before calling ThenBy"); - } - List newOrderBy = orderBy.ToList(); - newOrderBy.Add($"-{key}"); - orderBy = new ReadOnlyCollection(newOrderBy); + condition.OrderByDescending(key); return this; } #endregion public AVQuery Include(string key) { - includes = new ReadOnlyCollection(new List { key }); + condition.Include(key); return this; } public AVQuery Select(string key) { - selectedKeys = new ReadOnlyCollection(new List { key }); + condition.Select(key); return this; } public AVQuery Skip(int count) { - skip = count; + condition.Skip(count); return this; } public AVQuery Limit(int count) { - limit = count; - return this; - } - - internal AVQuery RedirectClassName(string key) { - redirectClassNameForKey = key; + condition.Limit(count); return this; } @@ -320,7 +245,7 @@ namespace LeanCloud { public AVQuery WhereDoesNotMatchQuery(string key, AVQuery query) where TOther : AVObject { - AddCondition(key, "$notInQuery", query.BuildParameters(true)); + AddCondition(key, "$notInQuery", query.BuildParameters(query.ClassName)); return this; } @@ -389,7 +314,7 @@ namespace LeanCloud { public AVQuery WhereMatchesKeyInQuery(string key, string keyInQuery, AVQuery query) where TOther : AVObject { var parameters = new Dictionary { - { "query", query.BuildParameters(true)}, + { "query", query.BuildParameters(query.ClassName)}, { "key", keyInQuery} }; AddCondition(key, "$select", parameters); @@ -399,7 +324,7 @@ namespace LeanCloud { public AVQuery WhereDoesNotMatchesKeyInQuery(string key, string keyInQuery, AVQuery query) where TOther : AVObject { var parameters = new Dictionary { - { "query", query.BuildParameters(true)}, + { "query", query.BuildParameters(query.ClassName)}, { "key", keyInQuery} }; AddCondition(key, "$dontSelect", parameters); @@ -408,7 +333,7 @@ namespace LeanCloud { public AVQuery WhereMatchesQuery(string key, AVQuery query) where TOther : AVObject { - AddCondition(key, "$inQuery", query.BuildParameters(true)); + AddCondition(key, "$inQuery", query.BuildParameters(query.ClassName)); return this; } @@ -453,6 +378,10 @@ namespace LeanCloud { #endregion + internal IDictionary BuildParameters(string className = null) { + return condition.BuildParameters(className); + } + private string RegexQuote(string input) { return "\\Q" + input.Replace("\\E", "\\E\\\\E\\Q") + "\\E"; } diff --git a/Storage/Storage/Public/AVQueryExtensions.cs b/Storage/Storage/Public/AVQueryExtensions.cs index c726b41..a1c90bf 100644 --- a/Storage/Storage/Public/AVQueryExtensions.cs +++ b/Storage/Storage/Public/AVQueryExtensions.cs @@ -703,38 +703,6 @@ namespace LeanCloud return source.OrderByDescending(GetOrderByPath(keySelector)); } - /// - /// Performs a subsequent ordering of a query based upon the key selector provided. - /// - /// The type of AVObject being queried for. - /// The type of key returned by keySelector. - /// The query to order. - /// A function to extract a key from the AVObject. - /// A new AVQuery based on Source whose results will be ordered by - /// the key specified in the keySelector. - public static AVQuery ThenBy( - this AVQuery source, Expression> keySelector) - where TSource : AVObject - { - return source.ThenBy(GetOrderByPath(keySelector)); - } - - /// - /// Performs a subsequent ordering of a query based upon the key selector provided. - /// - /// The type of AVObject being queried for. - /// The type of key returned by keySelector. - /// The query to order. - /// A function to extract a key from the AVObject. - /// A new AVQuery based on Source whose results will be ordered by - /// the key specified in the keySelector. - public static AVQuery ThenByDescending( - this AVQuery source, Expression> keySelector) - where TSource : AVObject - { - return source.ThenByDescending(GetOrderByPath(keySelector)); - } - /// /// Correlates the elements of two queries based on matching keys. /// diff --git a/Storage/Storage/Public/AVRelation.cs b/Storage/Storage/Public/AVRelation.cs index 739a80b..1be6f4b 100644 --- a/Storage/Storage/Public/AVRelation.cs +++ b/Storage/Storage/Public/AVRelation.cs @@ -63,7 +63,6 @@ namespace LeanCloud { } return new AVQuery(parent.ClassName) - .RedirectClassName(key) .WhereRelatedTo(parent, key); }