public static MDataRowCollection FindAll(MDataTable table, object whereObj)
        {
            if (table != null && table.Rows.Count > 0)
            {
                MDataRowCollection findRows = new MDataRowCollection();
                string where = Convert.ToString(whereObj).Trim();
                if (where == "" || where == "1=1")
                {
                    findRows.AddRange(table.Rows);
                    return(findRows);
                }
                string whereStr = SqlFormat.GetIFieldSql(whereObj);
                string orderby;
                SplitWhereOrderby(ref whereStr, out orderby);

                List <TFilter>   group2  = null;
                List <TFilter>   filters = GetTFilter(whereStr, table.Columns, out group2);
                IList <MDataRow> rows    = table.Rows;
                if (filters.Count > 0)
                {
                    rows = table.Rows.FindAll(delegate(MDataRow row)
                    {
                        return(CompareMore(row, filters) && (group2.Count == 0 || CompareMore(row, group2)));
                    }
                                              );
                }
                findRows.AddRange(rows);                              //添加找到的行。
                filters = null;
                if (!string.IsNullOrEmpty(orderby) && rows.Count > 1) //进行数组排序
                {
                    findRows.Sort(orderby);
                    //MDataRowCollection sortRows = new MDataRowCollection();
                    //sortRows.AddRange(rows);
                    //sortRows.Sort(orderby);
                    //return sortRows;
                }
                return(findRows);
            }
            return(null);
        }
        public static MDataRow FindRow(MDataTable table, object whereObj)
        {
            if (table.Rows.Count > 0)
            {
                if (Convert.ToString(whereObj).Trim() == "")
                {
                    return(table.Rows[0]);
                }
                string whereStr = SqlFormat.GetIFieldSql(whereObj);
                string orderby;
                SplitWhereOrderby(ref whereStr, out orderby);
                MDataRowCollection sortRows = null;
                if (!string.IsNullOrEmpty(orderby) && table.Rows.Count > 1)//进行数组排序
                {
                    sortRows = new MDataRowCollection();
                    sortRows.AddRange(table.Rows);
                    sortRows.Sort(orderby);
                }
                List <TFilter> group2  = null;
                List <TFilter> filters = GetTFilter(whereStr, table.Columns, out group2);

                if (filters.Count > 0)
                {
                    if (sortRows == null)
                    {
                        sortRows = table.Rows;
                    }
                    for (int i = 0; i < sortRows.Count; i++)
                    {
                        if (CompareMore(sortRows[i], filters) && (group2.Count == 0 || CompareMore(sortRows[i], group2)))
                        {
                            return(sortRows[i]);
                        }
                    }
                }
            }
            return(null);
        }
        /// <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);
        }
Exemple #4
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());
        }