コード例 #1
0
ファイル: TableFilter.cs プロジェクト: maikebing/SharpHSQL
        private bool Test(Expression e)
        {
            if (e == null)
            {
                return(true);
            }

            return(e.Test());
        }
コード例 #2
0
ファイル: Expression.cs プロジェクト: maikebing/SharpHSQL
        /// <summary>
        /// Gets the expression resulting value.
        /// </summary>
        /// <returns></returns>
        public object GetValue()
        {
            switch (_type)
            {
            case ExpressionType.Value:
                return(_data);

            case ExpressionType.DatabaseColumn:
                try
                {
                    return(tFilter.oCurrentData[iColumn]);
                }
                catch
                {
                    throw Trace.Error(Trace.COLUMN_NOT_FOUND, sColumn);
                }

            case ExpressionType.Function:
                return(fFunction.GetValue());

            case ExpressionType.Query:
                return(sSelect.GetValue(_columnType));

            case ExpressionType.Negate:
                return(Column.Negate(eArg.GetValue(_columnType), _columnType));

            case ExpressionType.Count:

                // count(*): sum(1); count(col): sum(col<>null)
                if (eArg.Type == ExpressionType.Asterix || eArg.GetValue() != null)
                {
                    return(1);
                }

                return(0);

            case ExpressionType.Maximum:
            case ExpressionType.Minimum:
            case ExpressionType.Sum:
            case ExpressionType.Average:
                return(eArg.GetValue());

            case ExpressionType.Exists:
                return(Test());

            case ExpressionType.Convert:
                return(eArg.GetValue(_columnType));

            case ExpressionType.CaseWhen:
                if (eArg.Test())
                {
                    return(eArg2.eArg.GetValue());
                }
                else
                {
                    return(eArg2.eArg2.GetValue());
                }

            case ExpressionType.Variable:
                return(eArg.GetValue());
            }

            // todo: simplify this
            object a = null, b = null;

            if (eArg != null)
            {
                a = eArg.GetValue(_columnType);
            }

            if (eArg2 != null)
            {
                b = eArg2.GetValue(_columnType);
            }

            switch (_type)
            {
            case ExpressionType.Add:
                return(Column.Add(a, b, _columnType));

            case ExpressionType.Subtract:
                return(Column.Subtract(a, b, _columnType));

            case ExpressionType.Multiply:
                return(Column.Multiply(a, b, _columnType));

            case ExpressionType.Divide:
                return(Column.Divide(a, b, _columnType));

            case ExpressionType.Concat:
                return(Column.Concat(a, b, _columnType));

            case ExpressionType.IfNull:
                return(a == null ? b : a);

            case ExpressionType.Equal:
                if (eArg.Type == ExpressionType.Variable)
                {
                    Trace.Check(eArg2 != null, Trace.GENERAL_ERROR);

                    return(eArg2.GetValue(eArg._columnType));
                }
                else
                {
                    return(Test());
                }

            default:

                // must be comparisation
                // todo: make sure it is
                return(Test());
            }
        }
コード例 #3
0
ファイル: TableFilter.cs プロジェクト: furesoft/SharpHSQL
        private bool Test(Expression e)
        {
            if (e == null)
            {
                return true;
            }

            return e.Test();
        }
コード例 #4
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);
        }