using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using LeanCloud.Storage.Internal.Query; using LeanCloud.Storage.Internal.Object; namespace LeanCloud.Storage { /// /// 查询类 /// /// public class LCQuery where T : LCObject { public string ClassName { get; private set; } LCCompositionalCondition condition; public LCQuery(string className) { ClassName = className; condition = new LCCompositionalCondition(); } /// /// 等于 /// /// /// /// public LCQuery WhereEqualTo(string key, object value) { condition.WhereEqualTo(key, value); return this; } /// /// 不等于 /// /// /// /// public LCQuery WhereNotEqualTo(string key, object value) { condition.WhereNotEqualTo(key, value); return this; } /// /// 包含 /// /// /// /// public LCQuery WhereContainedIn(string key, IEnumerable values) { condition.WhereContainedIn(key, values); return this; } /// /// 包含全部 /// /// /// /// public LCQuery WhereContainsAll(string key, IEnumerable values) { condition.WhereContainsAll(key, values); return this; } /// /// 存在 /// /// /// public LCQuery WhereExists(string key) { condition.WhereExists(key); return this; } /// /// 不存在 /// /// /// public LCQuery WhereDoesNotExist(string key) { condition.WhereDoesNotExist(key); return this; } /// /// 长度等于 /// /// /// /// public LCQuery WhereSizeEqualTo(string key, int size) { condition.WhereSizeEqualTo(key, size); return this; } /// /// 大于 /// /// /// /// public LCQuery WhereGreaterThan(string key, object value) { condition.WhereGreaterThan(key, value); return this; } /// /// 大于等于 /// /// /// /// public LCQuery WhereGreaterThanOrEqualTo(string key, object value) { condition.WhereGreaterThanOrEqualTo(key, value); return this; } /// /// 小于 /// /// /// /// public LCQuery WhereLessThan(string key, object value) { condition.WhereLessThan(key, value); return this; } /// /// 小于等于 /// /// /// /// public LCQuery WhereLessThanOrEqualTo(string key, object value) { condition.WhereLessThanOrEqualTo(key, value); return this; } /// /// 相邻 /// /// /// /// public LCQuery WhereNear(string key, LCGeoPoint point) { condition.WhereNear(key, point); return this; } /// /// 在坐标区域内 /// /// /// /// /// public LCQuery WhereWithinGeoBox(string key, LCGeoPoint southwest, LCGeoPoint northeast) { condition.WhereWithinGeoBox(key, southwest, northeast); return this; } /// /// 相关 /// /// /// /// public LCQuery WhereRelatedTo(LCObject parent, string key) { condition.WhereRelatedTo(parent, key); return this; } /// /// 前缀 /// /// /// /// public LCQuery WhereStartsWith(string key, string prefix) { condition.WhereStartsWith(key, prefix); return this; } /// /// 后缀 /// /// /// /// public LCQuery WhereEndsWith(string key, string suffix) { condition.WhereEndsWith(key, suffix); return this; } /// /// 字符串包含 /// /// /// /// public LCQuery WhereContains(string key, string subString) { condition.WhereContains(key, subString); return this; } /// /// 按 key 升序 /// /// /// public LCQuery OrderBy(string key) { condition.OrderBy(key); return this; } /// /// 按 key 降序 /// /// /// public LCQuery OrderByDescending(string key) { condition.OrderByDescending(key); return this; } /// /// 拉取 key 的完整对象 /// /// /// public LCQuery Include(string key) { condition.Include(key); return this; } /// /// 包含 key /// /// /// public LCQuery Select(string key) { condition.Select(key); return this; } /// /// 跳过 /// /// /// public LCQuery Skip(int value) { condition.Skip = value; return this; } /// /// 限制数量 /// /// /// public LCQuery Limit(int value) { condition.Limit = value; return this; } public async Task Count() { string path = $"classes/{ClassName}"; Dictionary parameters = BuildParams(); parameters["limit"] = 0; parameters["count"] = 1; Dictionary ret = await LeanCloud.HttpClient.Get>(path, queryParams: parameters); return (int)ret["count"]; } public async Task Get(string objectId) { if (string.IsNullOrEmpty(objectId)) { throw new ArgumentNullException(nameof(objectId)); } WhereEqualTo("objectId", objectId); Limit(1); List results = await Find(); if (results != null) { if (results.Count == 0) { return null; } return results[0]; } return null; } public async Task> Find() { string path = $"classes/{ClassName}"; Dictionary parameters = BuildParams(); Dictionary response = await LeanCloud.HttpClient.Get>(path, queryParams: parameters); List results = response["results"] as List; List list = new List(); foreach (object item in results) { LCObjectData objectData = LCObjectData.Decode(item as Dictionary); T obj = LCObject.Create(ClassName) as T; obj.Merge(objectData); list.Add(obj); } return list; } public async Task First() { Limit(1); List results = await Find(); if (results != null && results.Count > 0) { return results[0]; } return null; } public static LCQuery And(IEnumerable> queries) { if (queries == null || queries.Count() < 1) { throw new ArgumentNullException(nameof(queries)); } LCQuery compositionQuery = new LCQuery(null); string className = null; foreach (LCQuery query in queries) { if (className != null && className != query.ClassName) { throw new Exception("All of the queries in an or query must be on the same class."); } className = query.ClassName; compositionQuery.condition.Add(query.condition); } compositionQuery.ClassName = className; return compositionQuery; } public static LCQuery Or(IEnumerable> queries) { if (queries == null || queries.Count() < 1) { throw new ArgumentNullException(nameof(queries)); } LCQuery compositionQuery = new LCQuery(null); compositionQuery.condition = new LCCompositionalCondition(LCCompositionalCondition.Or); string className = null; foreach (LCQuery query in queries) { if (className != null && className != query.ClassName) { throw new Exception("All of the queries in an or query must be on the same class."); } className = query.ClassName; compositionQuery.condition.Add(query.condition); } compositionQuery.ClassName = className; return compositionQuery; } Dictionary BuildParams() { return condition.BuildParams(); } internal string BuildWhere() { return condition.BuildWhere(); } } }