object QueryChildrenFieldPre_Extend(NoSQL.Condition item, string name)
 {
     if (item.Children.Count < 2)
     {
         return(null);
     }
     return(item.Children.Find(p => string.Equals(p.Name, name, System.StringComparison.OrdinalIgnoreCase))?.Children[0].Value);
 }
 /// <summary>
 /// 查询规则(NoSQL)子集自动处理值。
 /// </summary>
 /// <param name="item">规则</param>
 /// <param name="writer">输出对象</param>
 protected virtual void QueryChildrenAutoValue(NoSQL.Condition item, System.IO.TextWriter writer)
 {
     if (item.Children.Count > 0)
     {
         //throw new System.NotSupportedException("暂不支持 NoSQL.Condition 多级字段:"+item.Name);
         QueryChildren(item.Children, writer);
     }
     else
     {
         writer.Write(AddCommandParameter(item.Value));
     }
 }
 /// <summary>
 /// 查询规则(NoSQL)。
 /// </summary>
 /// <param name="condition">规则</param>
 /// <param name="filter">过滤器</param>
 /// <returns></returns>
 public virtual ISelectCommandBuilder Query(NoSQL.Condition condition, CommandQueryFilterDelegate filter = null)
 {
     if (condition == null || condition.Type != NoSQL.ConditionTypes.Root || condition.Children.Count == 0)
     {
         return(this);
     }
     if (filter != null && !filter(this, condition))
     {
         return(this);
     }
     _whereExpression?.Query(condition);
     return(this);
 }
        /// <summary>
        /// 查询规则(NoSQL)。
        /// </summary>
        /// <param name="condition">规则</param>
        /// <returns></returns>
        public virtual IWhereExpression Query(NoSQL.Condition condition)
        {
            if (condition == null || condition.Type != NoSQL.ConditionTypes.Root || condition.Children.Count == 0)
            {
                return(this);
            }

            if (condition.Children.Count == 0)
            {
                return(this);
            }
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            using (System.IO.StringWriter writer = new System.IO.StringWriter(builder)) {
                QueryChildren(condition.Children, writer);
                writer.Flush();
            }
            if (builder.Length > 0)
            {
                string commandText = builder.ToString();
lb_Next:
                bool b = false;
                if (!string.IsNullOrEmpty(commandText))
                {
                    if (commandText.StartsWith(" and (", System.StringComparison.OrdinalIgnoreCase))
                    {
                        b           = true;
                        commandText = commandText.Substring(6, commandText.Length - 8);
                    }
                    if (commandText.StartsWith(" or (", System.StringComparison.OrdinalIgnoreCase))
                    {
                        b           = true;
                        commandText = commandText.Substring(5, commandText.Length - 7);
                    }
                    if (b)
                    {
                        goto lb_Next;
                    }
                }
                if (!string.IsNullOrEmpty(commandText))
                {
                    Where(WhereOperators.And, " ( " + commandText + " ) ");
                }
            }
            return(this);
        }
 /// <summary>
 /// 查询规则(NoSQL)子集数组。
 /// </summary>
 /// <param name="item">规则</param>
 /// <param name="format">格式串</param>
 /// <param name="writer">输出对象</param>
 protected virtual void QueryChildrenArray(NoSQL.Condition item, string format, System.IO.TextWriter writer)
 {
     System.Text.StringBuilder builder = new System.Text.StringBuilder();
     foreach (object value in (System.Collections.IEnumerable)item.Value)
     {
         if (value == null || (value is string && (string)value == ""))
         {
             continue;
         }
         if (builder.Length > 0)
         {
             builder.Append(',');
         }
         builder.Append(AddCommandParameter(value));
     }
     if (builder.Length == 0)
     {
         return;
     }
     writer.Write(format, builder.ToString());
 }
        /// <summary>
        /// 查询规则(NoSQL)子集逻辑操作。
        /// </summary>
        /// <param name="item">规则</param>
        /// <param name="writer">输出对象</param>
        /// <param name="innerOperation">内联操作符</param>
        protected void QueryChildrenLogical(NoSQL.Condition item, System.IO.TextWriter writer, string innerOperation)
        {
            switch (item.Name)
            {
            case "$eq": {
                writer.Write(_dialect.MatchOperatorGrammar("="));
                QueryChildrenAutoValue(item, writer);
                break;
            }

            case "$gt": {
                writer.Write(_dialect.MatchOperatorGrammar(">"));
                QueryChildrenAutoValue(item, writer);
                break;
            }

            case "$gteq": {
                writer.Write(_dialect.MatchOperatorGrammar(">="));
                QueryChildrenAutoValue(item, writer);
                break;
            }

            case "$lt": {
                writer.Write(_dialect.MatchOperatorGrammar("<"));
                QueryChildrenAutoValue(item, writer);
                break;
            }

            case "$lteq": {
                writer.Write(_dialect.MatchOperatorGrammar("<="));
                QueryChildrenAutoValue(item, writer);
                break;
            }

            case "$noteq": {
                writer.Write(_dialect.MatchOperatorGrammar("!="));
                QueryChildrenAutoValue(item, writer);
                break;
            }

            case "$like":
            case "$start":
            case "$end":
                break;

            //case "$like": {
            //        string value = item.Value as string;
            //        writer.Write(LikeGrammar(item.Parent.Name,false),
            //            AddCommandParameter(LikeValueFilter(value, true, true)));
            //        break;
            //    }
            //case "$start": {
            //        string value = item.Value as string;
            //        writer.Write(LikeGrammar(item.Parent.Name, false),
            //            AddCommandParameter(LikeValueFilter(value, false, true)));
            //        break;
            //    }
            //case "$end": {
            //        string value = item.Value as string;
            //        writer.Write(LikeGrammar(item.Parent.Name, false),
            //            AddCommandParameter(LikeValueFilter(value, true, false)));
            //        break;
            //    }
            case "$null": {
                writer.Write(" is null");
                break;
            }

            case "$in": {
                QueryChildrenArray(item, " in({0})", writer);
                break;
            }

            case "$notin": {
                QueryChildrenArray(item, " not in({0})", writer);
                break;
            }

            case "$and": {
                writer.Write(" and (");
                QueryChildren(item.Children, writer);
                writer.Write(") ");
                break;
            }

            case "$or": {
                if (item.Children.IsArray)
                {
                    writer.Write(" and (");
                    QueryChildren(item.Children, writer, " or ");
                    writer.Write(") ");
                }
                else
                {
                    writer.Write(" or (");
                    QueryChildren(item.Children, writer);
                    writer.Write(") ");
                }
                break;
            }

            case "$not": {
                writer.Write(" not (");
                QueryChildren(item.Children, writer);
                writer.Write(") ");
                break;
            }

            case "$min":
            case "$max":
            case "$sum":
            case "$count": {
                break;
            }

            default:
                throw new System.NotSupportedException("暂不支持NoSQL.Condition.Name=" + item.Name);
            }
        }
        /// <summary>
        /// 查询规则(NoSQL)子集字段预处理。
        /// </summary>
        /// <param name="item">规则</param>
        /// <param name="writer">输出对象</param>
        /// <param name="innerOperation">内联操作符</param>
        /// <param name="firstOperation">第一个操作</param>
        /// <returns>返加是否处理。</returns>
        protected virtual bool QueryChildrenFieldPre(NoSQL.Condition item, System.IO.TextWriter writer, string innerOperation, ref bool firstOperation)
        {
            switch (item.Children[0].Name)
            {
            case "$min":
            case "$max":
            case "$sum":
            case "$count": {
                if (item.Children.Count != 1)
                {
                    return(false);
                }
                writer.Write("{0}({1})", item.Children[0].Name.Substring(1), _dialect.PreName(item.GetNames()));
                QueryChildren(item.Children[0].Children, writer);
                return(true);
            }

            case "$notnull": {
                if (item.Children.Count != 1)
                {
                    return(false);
                }
                firstOperation = false;
                writer.Write(innerOperation);
                writer.Write("not {0} is null", _dialect.PreName(item.GetNames()));
                return(true);
            }

            case "$ref": {
                if (item.Children.Count != 1)
                {
                    return(false);
                }
                firstOperation = false;
                writer.Write(innerOperation);
                writer.Write("{0}={1}", _dialect.PreName(item.GetNames()), _dialect.PreName(item.Children[0].Value as string, "."));
                return(true);
            }

            case "$like": {
                string value = item.Children[0].Value as string;
                if (string.IsNullOrEmpty(value))
                {
                    return(true);
                }
                else
                {
                    firstOperation = false;
                    writer.Write(innerOperation);
                    bool reverse = TypeExtensions.Convert(QueryChildrenFieldPre_Extend(item, "reverse"), false);
                    writer.Write(_dialect.LikeGrammar(_dialect.PreName(item.GetNames()), true, true, reverse),
                                 AddCommandParameter(_dialect.LikeValueFilter(value, true, true, reverse)));
                    return(true);
                }
            }

            case "$start": {
                string value = item.Children[0].Value as string;
                if (string.IsNullOrEmpty(value))
                {
                    return(true);
                }
                else
                {
                    firstOperation = false;
                    writer.Write(innerOperation);
                    bool reverse = TypeExtensions.Convert(QueryChildrenFieldPre_Extend(item, "reverse"), false);
                    writer.Write(_dialect.LikeGrammar(_dialect.PreName(item.GetNames()), false, true, reverse),
                                 AddCommandParameter(_dialect.LikeValueFilter(value, false, true, reverse)));
                    return(true);
                }
            }

            case "$end": {
                string value = item.Children[0].Value as string;
                if (string.IsNullOrEmpty(value))
                {
                    return(true);
                }
                else
                {
                    firstOperation = false;
                    writer.Write(innerOperation);
                    bool reverse = TypeExtensions.Convert(QueryChildrenFieldPre_Extend(item, "reverse"), false);
                    writer.Write(_dialect.LikeGrammar(_dialect.PreName(item.GetNames()), true, false, reverse),
                                 AddCommandParameter(_dialect.LikeValueFilter(value, true, false, reverse)));
                    return(true);
                }
            }

            default:
                return(false);
            }
        }