2020-02-19 18:50:51 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2020-02-25 20:39:49 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2020-02-19 18:50:51 +08:00
|
|
|
|
using LeanCloud.Storage.Internal.Codec;
|
|
|
|
|
|
|
|
|
|
namespace LeanCloud.Storage.Internal.Query {
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public class LCCompositionalCondition : ILCQueryCondition {
|
|
|
|
|
public const string And = "$and";
|
|
|
|
|
public const string Or = "$or";
|
2020-02-19 18:50:51 +08:00
|
|
|
|
|
|
|
|
|
readonly string composition;
|
|
|
|
|
|
|
|
|
|
List<ILCQueryCondition> conditionList;
|
|
|
|
|
|
|
|
|
|
List<string> orderByList;
|
|
|
|
|
HashSet<string> includes;
|
|
|
|
|
HashSet<string> selectedKeys;
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public int Skip {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public int Limit {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public LCCompositionalCondition(string composition = And) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
this.composition = composition;
|
|
|
|
|
Skip = 0;
|
|
|
|
|
Limit = 30;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询条件
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereEqualTo(string key, object value) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
Add(new LCEqualCondition(key, value));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereNotEqualTo(string key, object value) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$ne", value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereContainedIn(string key, IEnumerable values) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$in", values);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereNotContainedIn(string key, IEnumerable values) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "nin", values);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereContainsAll(string key, IEnumerable values) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$all", values);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereExists(string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$exists", true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereDoesNotExist(string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$exists", false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereSizeEqualTo(string key, int size) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$size", size);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereGreaterThan(string key, object value) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$gt", value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereGreaterThanOrEqualTo(string key, object value) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$gte", value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereLessThan(string key, object value) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
AddOperation(key, "$lt", value);
|
2020-02-19 18:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereLessThanOrEqualTo(string key, object value) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$lte", value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereNear(string key, LCGeoPoint point) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$nearSphere", point);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereWithinGeoBox(string key, LCGeoPoint southwest, LCGeoPoint northeast) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
Dictionary<string, object> value = new Dictionary<string, object> {
|
|
|
|
|
{ "$box", new List<object> { southwest, northeast } }
|
|
|
|
|
};
|
|
|
|
|
AddOperation(key, "$within", value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereRelatedTo(LCObject parent, string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
Add(new LCRelatedCondition(parent, key));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereStartsWith(string key, string prefix) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$regex", $"^{prefix}.*");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereEndsWith(string key, string suffix) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$regex", $".*{suffix}$");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void WhereContains(string key, string subString) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
AddOperation(key, "$regex", $".*{subString}.*");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddOperation(string key, string op, object value) {
|
|
|
|
|
LCOperationCondition cond = new LCOperationCondition(key, op, value);
|
|
|
|
|
Add(cond);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void Add(ILCQueryCondition cond) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
if (cond == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (conditionList == null) {
|
|
|
|
|
conditionList = new List<ILCQueryCondition>();
|
|
|
|
|
}
|
|
|
|
|
conditionList.RemoveAll(item => item.Equals(cond));
|
|
|
|
|
conditionList.Add(cond);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 筛选条件
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void OrderBy(string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
if (orderByList == null) {
|
|
|
|
|
orderByList = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
orderByList.Add(key);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void OrderByDescending(string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
OrderBy($"-{key}");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void Include(string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
if (includes == null) {
|
|
|
|
|
includes = new HashSet<string>();
|
|
|
|
|
}
|
|
|
|
|
includes.Add(key);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public void Select(string key) {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
if (selectedKeys == null) {
|
|
|
|
|
selectedKeys = new HashSet<string>();
|
|
|
|
|
}
|
|
|
|
|
selectedKeys.Add(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(ILCQueryCondition other) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, object> Encode() {
|
|
|
|
|
if (conditionList == null || conditionList.Count == 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (conditionList.Count == 1) {
|
|
|
|
|
ILCQueryCondition cond = conditionList[0];
|
|
|
|
|
return cond.Encode();
|
|
|
|
|
}
|
|
|
|
|
return new Dictionary<string, object> {
|
|
|
|
|
{ composition, LCEncoder.Encode(conditionList) }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public Dictionary<string, object> BuildParams() {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
Dictionary<string, object> dict = new Dictionary<string, object> {
|
|
|
|
|
{ "skip", Skip },
|
|
|
|
|
{ "limit", Limit }
|
|
|
|
|
};
|
|
|
|
|
if (conditionList != null && conditionList.Count > 0) {
|
2020-02-26 11:16:21 +08:00
|
|
|
|
dict["where"] = JsonConvert.SerializeObject(Encode());
|
2020-02-19 18:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
if (orderByList != null && orderByList.Count > 0) {
|
|
|
|
|
dict["order"] = string.Join(",", orderByList);
|
|
|
|
|
}
|
|
|
|
|
if (includes != null && includes.Count > 0) {
|
|
|
|
|
dict["include"] = string.Join(",", includes);
|
|
|
|
|
}
|
|
|
|
|
if (selectedKeys != null && selectedKeys.Count > 0) {
|
|
|
|
|
dict["keys"] = string.Join(",", selectedKeys);
|
|
|
|
|
}
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 11:41:38 +08:00
|
|
|
|
public string BuildWhere() {
|
2020-02-19 18:50:51 +08:00
|
|
|
|
if (conditionList == null || conditionList.Count == 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-25 20:39:49 +08:00
|
|
|
|
return JsonConvert.SerializeObject(Encode());
|
2020-02-19 18:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|