Beispiel #1
0
        /// <summary>
        /// 多个条件
        /// </summary>
        private static List <TFilter> GetTFilter(object whereObj, MDataColumn mdc, out List <TFilter> group2)
        {
            group2 = new List <TFilter>();
            List <TFilter> tFilterList = new List <TFilter>();
            string         whereStr    = SqlFormat.GetIFieldSql(whereObj);

            whereStr = SqlCreate.FormatWhere(whereStr, mdc, DataBaseType.None, null);
            string lowerWhere = whereStr.ToLower();
            string andSign    = " and ";
            string orSign     = " or ";
            int    andIndex   = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign);
            int    orIndex    = IndexOf(lowerWhere, orSign, 0);

            TFilter filter = null;

            if (andIndex == -1 && orIndex == -1)//仅有一个条件
            {
                filter = GetSingleTFilter(whereStr, mdc);
                if (filter != null)
                {
                    tFilterList.Add(filter);
                }
            }
            else if (orIndex == -1) // 只有and条件
            {
                int andStartIndex = 0;
                while (andIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andStartIndex > 0)
                        {
                            filter._Ao = Ao.And;
                        }
                        tFilterList.Add(filter);
                    }
                    andStartIndex = andIndex + andSign.Length;
                    andIndex      = IndexOf(lowerWhere, andSign, andStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.And;
                    tFilterList.Add(filter);
                }
            }
            else if (andIndex == -1) //只有or 条件
            {
                int orStartIndex = 0;
                while (orIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc);
                    if (filter != null)
                    {
                        if (orStartIndex > 0)
                        {
                            filter._Ao = Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    orStartIndex = orIndex + orSign.Length;
                    orIndex      = IndexOf(lowerWhere, orSign, orStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.Or;
                    tFilterList.Add(filter);
                }
            }
            else //有and 又有 or
            {
                bool isAnd      = andIndex < orIndex;
                bool lastAnd    = isAnd;
                int  andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理

                int  andOrStartIndex = 0;
                bool needGroup2      = isAnd;//如果是and开头,则分成两组
                while (andOrIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andOrStartIndex > 0)
                        {
                            filter._Ao = lastAnd ? Ao.And : Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length);
                    if (isAnd)
                    {
                        andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1);
                    }
                    else
                    {
                        orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1);
                    }
                    lastAnd = isAnd;
                    if (andIndex == -1)
                    {
                        isAnd      = false;
                        andOrIndex = orIndex;
                    }
                    else if (orIndex == -1)
                    {
                        isAnd      = true;
                        andOrIndex = andIndex;
                    }
                    else
                    {
                        isAnd      = andIndex < orIndex;
                        andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理
                    }
                }
                filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = lastAnd ? Ao.And : Ao.Or;
                    tFilterList.Add(filter);
                }
                if (tFilterList.Count > 2 && needGroup2)
                {
                    int okflag = -1;
                    for (int i = 0; i < tFilterList.Count; i++)
                    {
                        if (okflag == -1 && tFilterList[i]._Ao == Ao.Or)
                        {
                            i--;//返回上一个索引1,2,3
                            okflag = i;
                        }
                        if (okflag != -1)
                        {
                            group2.Add(tFilterList[i]);
                        }
                    }
                    tFilterList.RemoveRange(okflag, tFilterList.Count - okflag);
                }
            }
            // string firstFilter=whereStr.su


            return(tFilterList);
        }
Beispiel #2
0
        /// <summary>
        /// 多个条件
        /// </summary>
        private static TFilter[] GetTFilter(object whereObj, MDataColumn mdc)
        {
            List <TFilter> tFilterList = new List <TFilter>();
            string         whereStr    = SqlFormat.GetIFieldSql(whereObj);

            whereStr = SqlCreate.FormatWhere(whereStr, mdc, DalType.None, null);
            string lowerWhere = whereStr.ToLower();
            string andSign    = " and ";
            string orSign     = " or ";
            int    andIndex   = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign);
            int    orIndex    = IndexOf(lowerWhere, orSign, 0);

            TFilter filter = null;

            if (andIndex == -1 && orIndex == -1)//仅有一个条件
            {
                filter = GetSingleTFilter(whereStr, mdc);
                if (filter != null)
                {
                    tFilterList.Add(filter);
                }
            }
            else if (orIndex == -1) // 只有and条件
            {
                int andStartIndex = 0;
                while (andIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andStartIndex > 0)
                        {
                            filter._Ao = Ao.And;
                        }
                        tFilterList.Add(filter);
                    }
                    andStartIndex = andIndex + andSign.Length;
                    andIndex      = IndexOf(lowerWhere, andSign, andStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.And;
                    tFilterList.Add(filter);
                }
            }
            else if (andIndex == -1) //只有or 条件
            {
                int orStartIndex = 0;
                while (orIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc);
                    if (filter != null)
                    {
                        if (orStartIndex > 0)
                        {
                            filter._Ao = Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    orStartIndex = orIndex + orSign.Length;
                    orIndex      = IndexOf(lowerWhere, orSign, orStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.Or;
                    tFilterList.Add(filter);
                }
            }
            else //有and 又有 or
            {
                bool isAnd      = andIndex < orIndex;
                bool lastAnd    = isAnd;
                int  andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理

                int andOrStartIndex = 0;

                while (andOrIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andOrStartIndex > 0)
                        {
                            filter._Ao = lastAnd ? Ao.And : Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length);
                    if (isAnd)
                    {
                        andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1);
                    }
                    else
                    {
                        orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1);
                    }
                    lastAnd = isAnd;
                    if (andIndex == -1)
                    {
                        isAnd      = false;
                        andOrIndex = orIndex;
                    }
                    else if (orIndex == -1)
                    {
                        isAnd      = true;
                        andOrIndex = andIndex;
                    }
                    else
                    {
                        isAnd      = andIndex < orIndex;
                        andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理
                    }
                }
                filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = lastAnd ? Ao.And : Ao.Or;
                    tFilterList.Add(filter);
                }
            }
            // string firstFilter=whereStr.su


            return(tFilterList.ToArray());
        }