コード例 #1
0
ファイル: CollectionQuery.cs プロジェクト: yinjuan1123/chord
        public static FilterDefinition <MessageItem> BuildMongoQuery(string text,
                                                                     string field,
                                                                     string default_type = "(")
        {
            CollectionQuery query = new CollectionQuery(text, default_type);

            return(query.BuildMongoQuery(field));
        }
コード例 #2
0
        // 返回空表示任意匹配
        // parameters:
        //      userCondiction  消息创建者的用户ID的列表,逗号分隔。如果为空,表示不在意消息创建者
        FilterDefinition <MessageItem> BuildQuery(GroupQuery group_query,
                                                  string userCondition,
                                                  string timeRange,
                                                  string idCondition,
                                                  string subjectCondition)
        {
            string strStart = "";
            string strEnd   = "";

            StringUtil.ParseTwoPart(timeRange, "~", out strStart, out strEnd);
            DateTime startTime;
            DateTime endTime;

            try
            {
                startTime = string.IsNullOrEmpty(strStart) ? new DateTime(0) : DateTime.Parse(strStart);
                endTime   = string.IsNullOrEmpty(strEnd) ? new DateTime(0) : DateTime.Parse(strEnd);
            }
            catch (Exception)
            {
                throw new ArgumentException("时间范围字符串 '" + timeRange + "' 不合法", "timeRange");
            }

            FilterDefinition <MessageItem> user_filter = null;

            if (string.IsNullOrEmpty(userCondition) == false)
            {
                string[] list = userCondition.Split(new char[] { ',' });
                List <FilterDefinition <MessageItem> > items = new List <FilterDefinition <MessageItem> >();
                foreach (string user in list)
                {
                    string name = user;
                    if (name.StartsWith("ui:"))
                    {
                        name = name.Substring(3);
                    }
                    FilterDefinition <MessageItem> item = Builders <MessageItem> .Filter.Eq("creator", name);

                    items.Add(item);
                }
                if (items.Count == 1)
                {
                    user_filter = items[0];
                }
                else
                {
                    user_filter = Builders <MessageItem> .Filter.Or(items);
                }
            }

            FilterDefinition <MessageItem> time_filter = null;

            if (startTime == new DateTime(0) && endTime == new DateTime(0))
            {
                time_filter = null;  // Builders<MessageItem>.Filter.Gte("publishTime", startTime);
            }
            else if (startTime == new DateTime(0))
            {
                time_filter = Builders <MessageItem> .Filter.Lt("publishTime", endTime);
            }
            else if (endTime == new DateTime(0))
            {
                time_filter = Builders <MessageItem> .Filter.Gte("publishTime", startTime);
            }
            else
            {
                time_filter = Builders <MessageItem> .Filter.And(
                    Builders <MessageItem> .Filter.Gte("publishTime", startTime),
                    Builders <MessageItem> .Filter.Lt("publishTime", endTime));
            }

            FilterDefinition <MessageItem> expire_filter = Builders <MessageItem> .Filter.Or(
                Builders <MessageItem> .Filter.Eq("expireTime", new DateTime(0)),
                Builders <MessageItem> .Filter.Gt("expireTime", DateTime.Now));

            // 构造一个 AND 运算的检索式
            FilterDefinition <MessageItem> group_filter = group_query.BuildMongoQuery();

            // id 列表
            FilterDefinition <MessageItem> id_filter = null;

            if (string.IsNullOrEmpty(idCondition) == false)
            {
                string[] list = idCondition.Split(new char[] { ',' });
                List <FilterDefinition <MessageItem> > items = new List <FilterDefinition <MessageItem> >();
                foreach (string id in list)
                {
                    FilterDefinition <MessageItem> item = Builders <MessageItem> .Filter.Eq("id", id);

                    items.Add(item);
                }
                if (items.Count == 1)
                {
                    id_filter = items[0];
                }
                else
                {
                    id_filter = Builders <MessageItem> .Filter.Or(items);
                }
            }

            {
                List <FilterDefinition <MessageItem> > items = new List <FilterDefinition <MessageItem> >();
                if (time_filter != null)
                {
                    items.Add(time_filter);
                }
                if (expire_filter != null)
                {
                    items.Add(expire_filter);
                }
                if (group_filter != null)
                {
                    items.Add(group_filter);
                }
                if (user_filter != null)
                {
                    items.Add(user_filter);
                }
                if (id_filter != null)
                {
                    items.Add(id_filter);
                }
                if (string.IsNullOrEmpty(subjectCondition) == false)
                {
                    items.Add(CollectionQuery.BuildMongoQuery(subjectCondition, "subjects"));
                }

                return(Builders <MessageItem> .Filter.And(items));
            }

#if NO
            if (time_filter == null)
            {
                return(Builders <MessageItem> .Filter.And(expire_filter,
                                                          group_filter));
            }

            return(time_filter = Builders <MessageItem> .Filter.And(time_filter,
                                                                    expire_filter,
                                                                    group_filter));
#endif
        }