コード例 #1
0
        private static int CompareRecord(object[] a, object[] b, Result r, int len)
        {
            for (int j = 0; j < len; j++)
            {
                int i = Column.Compare(a[j], b[j], r.Type[j]);

                if (i != 0)
                {
                    return(i);
                }
            }

            return(0);
        }
コード例 #2
0
        private static int CompareRecord(object[] a, object[] b, Result r, int[] order, int[] way)
        {
            int i = Column.Compare(a[order[0]], b[order[0]], r.Type[order[0]]);

            if (i == 0)
            {
                for (int j = 1; j < order.Length; j++)
                {
                    i = Column.Compare(a[order[j]], b[order[j]],
                                       r.Type[order[j]]);

                    if (i != 0)
                    {
                        return(i * way[j]);
                    }
                }
            }

            return(i * way[0]);
        }
コード例 #3
0
        private int CompareRow(object[] a, object[] b)
        {
            int i = Column.Compare(a[_column_0], b[_column_0], _type_0);

            if (i != 0)
            {
                return(i);
            }

            for (int j = 1; j < _fields; j++)
            {
                i = Column.Compare(a[_column[j]], b[_column[j]], _type[j]);

                if (i != 0)
                {
                    return(i);
                }
            }

            return(0);
        }
コード例 #4
0
 private int CompareValue(object a, object b)
 {
     return(Column.Compare(a, b, _type_0));
 }
コード例 #5
0
ファイル: Expression.cs プロジェクト: maikebing/SharpHSQL
        /// <summary>
        /// Test condition for True or False value.
        /// </summary>
        /// <returns></returns>
        public bool Test()
        {
            switch (_type)
            {
            case ExpressionType.True:
                return(true);

            case ExpressionType.Not:
                Trace.Assert(eArg2 == null, "Expression.test");

                return(!eArg.Test());

            case ExpressionType.And:
                return(eArg.Test() && eArg2.Test());

            case ExpressionType.Or:
                return(eArg.Test() || eArg2.Test());

            case ExpressionType.Like:

                // todo: now for all tests a new 'like' object required!
                string     s    = (string)eArg2.GetValue(ColumnType.VarChar);
                ColumnType type = eArg._columnType;
                Like       l    = new Like(s, cLikeEscape, type == ColumnType.VarCharIgnoreCase);
                string     c    = (string)eArg.GetValue(ColumnType.VarChar);

                return(l.Compare(c));

            case ExpressionType.In:
                return(eArg2.TestValueList(eArg.GetValue(), eArg._columnType));

            case ExpressionType.Exists:
                Result r = eArg.sSelect.GetResult(1, null);                            // 1 is already enough

                return(r.Root != null);
            }

            Trace.Check(eArg != null, Trace.GENERAL_ERROR);

            object     o     = eArg.GetValue();
            ColumnType dtype = eArg._columnType;

            Trace.Check(eArg2 != null, Trace.GENERAL_ERROR);

            object o2     = eArg2.GetValue(dtype);
            int    result = Column.Compare(o, o2, dtype);

            switch (_type)
            {
            case ExpressionType.Equal:
                return(result == 0);

            case ExpressionType.Bigger:
                return(result > 0);

            case ExpressionType.BiggerEqual:
                return(result >= 0);

            case ExpressionType.SmallerEqual:
                return(result <= 0);

            case ExpressionType.Smaller:
                return(result < 0);

            case ExpressionType.NotEqual:
                return(result != 0);
            }

            Trace.Assert(false, "Expression.test2");

            return(false);
        }
コード例 #6
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);
        }