Example #1
0
        /// <summary>
        /// Test the value list for True or False.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="datatype"></param>
        /// <returns></returns>
        private bool TestValueList(object o, ColumnType datatype)
        {
            if (_type == ExpressionType.ValueList)
            {
                if (datatype != _columnType)
                {
                    o = Column.ConvertToObject(o, _columnType);
                }

                return(_list.ContainsKey(o));
            }
            else if (_type == ExpressionType.Query)
            {
                // todo: convert to valuelist before if everything is resolvable
                Result     r    = sSelect.GetResult(0, null);
                Record     n    = r.Root;
                ColumnType type = r.Type[0];

                if (datatype != type)
                {
                    o = Column.ConvertToObject(o, type);
                }

                while (n != null)
                {
                    object o2 = n.Data[0];

                    if (o2 != null && o2.Equals(o))
                    {
                        return(true);
                    }

                    n = n.Next;
                }

                return(false);
            }

            throw Trace.Error(Trace.WRONG_DATA_TYPE);
        }
Example #2
0
        // [email protected] end changes from 1.50
        // [email protected] begin changes from 1.50
        public Result GetResult(int start, int cnt, Channel cChannel)
        {
            int maxrows = start + cnt;          //<-new, cut definitly

            // [email protected] begin changes from 1.50
            Resolve();
            CheckResolved();

            if (sUnion != null && sUnion.iResultLen != iResultLen)
            {
                throw Trace.Error(Trace.COLUMN_COUNT_DOES_NOT_MATCH);
            }

            int    len        = eColumn.Length;
            Result r          = new Result(len);
            bool   aggregated = false;
            bool   grouped    = false;

            for (int i = 0; i < len; i++)
            {
                Expression e = eColumn[i];

                r.Type[i] = e.ColumnType;

                if (e.IsAggregate)
                {
                    aggregated = true;
                }
            }

            object[] agg = null;

            if (aggregated)
            {
                agg = new object[len];
            }

            if (iGroupLen > 0)
            {                // has been set in Parser
                grouped = true;
            }

            bool simple_maxrows = false;

            if (maxrows != 0 && grouped == false && sUnion == null && iOrderLen == 0)
            {
                simple_maxrows = true;
            }

            int count  = 0;
            int filter = tFilter.Length;

            bool[] first = new bool[filter];
            int    level = 0;

            while (level >= 0)
            {
                bool found = false;

                if (filter > 0)
                {
                    TableFilter t = tFilter[level];

                    if (!first[level])
                    {
                        found        = t.FindFirst();
                        first[level] = found;
                    }
                    else
                    {
                        found        = t.Next();
                        first[level] = found;
                    }
                }

                if (!found)
                {
                    level--;

                    if (!OnlyVars)
                    {
                        continue;
                    }
                }

                if (level < filter - 1)
                {
                    level++;

                    continue;
                }

                if (eCondition == null || eCondition.Test())
                {
                    object[] row = new object[len];

                    for (int i = 0; i < len; i++)
                    {
                        row[i] = eColumn[i].GetValue();

                        if (cChannel != null && eColumn[i].IsVarAssign)
                        {
                            cChannel.SetDeclareValue(eColumn[i].Arg.ColumnName, row[i]);
                        }
                    }

                    count++;

                    if (aggregated && !grouped)
                    {
                        UpdateAggregateRow(agg, row, len);
                    }
                    else
                    {
                        r.Add(row);

                        if (simple_maxrows && count >= maxrows)
                        {
                            break;
                        }
                    }
                }
            }

            if (aggregated && !grouped)
            {
                AddAggregateRow(r, agg, len, count);
            }
            else if (grouped)
            {
                int[] order = new int[iGroupLen];
                int[] way   = new int[iGroupLen];

                for (int i = iResultLen, j = 0; j < iGroupLen; i++, j++)
                {
                    order[j] = i;
                    way[j]   = 1;
                }

                r = SortResult(r, order, way);

                Record n = r.Root;
                Result x = new Result(len);

                for (int i = 0; i < len; i++)
                {
                    x.Type[i] = r.Type[i];
                }

                do
                {
                    object[] row = new object[len];

                    count = 0;

                    bool newgroup = false;

                    while (n != null && newgroup == false)
                    {
                        count++;

                        for (int i = 0; i < iGroupLen; i++)
                        {
                            if (n.Next == null)
                            {
                                newgroup = true;
                            }
                            else if (Column.Compare(n.Data[i], n.Next.Data[i], r.Type[i]) != 0)
                            {
                                // can't use .Equals because 'null' is also one group
                                newgroup = true;
                            }
                        }

                        UpdateAggregateRow(row, n.Data, len);

                        n = n.Next;
                    }

                    AddAggregateRow(x, row, len, count);
                } while (n != null);

                r = x;
            }

            if (iOrderLen != 0)
            {
                int[] order = new int[iOrderLen];
                int[] way   = new int[iOrderLen];

                for (int i = iResultLen, j = 0; j < iOrderLen; i++, j++)
                {
                    order[j] = i;
                    way[j]   = eColumn[i].IsDescending ? -1 : 1;
                }

                r = SortResult(r, order, way);
            }

            // the result maybe is bigger (due to group and order by)
            // but don't tell this anybody else
            r.SetColumnCount(iResultLen);

            if (bDistinct)
            {
                r = RemoveDuplicates(r);
            }

            for (int i = 0; i < iResultLen; i++)
            {
                Expression e = eColumn[i];

                r.Label[i] = e.Alias;
                r.Table[i] = e.TableName;
                r.Name[i]  = e.ColumnName;
            }

            if (sUnion != null)
            {
                Result x = sUnion.GetResult(0, cChannel);

                if (UnionType == SelectType.Union)
                {
                    r.Append(x);

                    r = RemoveDuplicates(r);
                }
                else if (UnionType == SelectType.UnionAll)
                {
                    r.Append(x);
                }
                else if (UnionType == SelectType.Intersect)
                {
                    r = RemoveDuplicates(r);
                    x = RemoveDuplicates(x);
                    r = RemoveDifferent(r, x);
                }
                else if (UnionType == SelectType.Except)
                {
                    r = RemoveDuplicates(r);
                    x = RemoveDuplicates(x);
                    r = RemoveSecond(r, x);
                }
            }

            if (maxrows > 0 && !simple_maxrows)
            {
                TrimResult(r, maxrows);
            }

            // [email protected] begin changes from 1.50
            if (start > 0)
            {                   //then cut the first 'start' elements
                TrimResultFront(r, start);
            }
            // [email protected] end changes from 1.50

            return(r);
        }