Exemplo n.º 1
0
        /**
         * Method declaration
         *
         *
         * @param e
         *
         * @return
         *
         * @throws Exception
         */
        private bool test(Expression e)
        {
            if (e == null)
            {
                return(true);
            }

            return(e.test());
        }
Exemplo n.º 2
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        public object getValue()
        {
            switch (iType)
            {
            case VALUE:
                return(oData);

            case COLUMN:
                try
                {
                    return(tFilter.oCurrentData[iColumn]);
                }
                catch (Exception e)
                {
                    throw Trace.error(Trace.COLUMN_NOT_FOUND, sColumn);
                }

            /*				case FUNCTION:
             *                                      return fFunction.getValue();
             */
            case QUERY:
                return(sSelect.getValue(iDataType));

            case NEGATE:
                return(Column.negate(eArg.getValue(iDataType), iDataType));

            case COUNT:

                // count(*): sum(1); count(col): sum(col<>null)
                if (eArg.iType == ASTERIX || eArg.getValue() != null)
                {
                    return(1);
                }

                return(0);

            case MAX:

            case MIN:

            case SUM:

            case AVG:
                return(eArg.getValue());

            case EXISTS:
                return(test());

            case CONVERT:
                return(eArg.getValue(iDataType));

            case CASEWHEN:
                if (eArg.test())
                {
                    return(eArg2.eArg.getValue());
                }
                else
                {
                    return(eArg2.eArg2.getValue());
                }
            }

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

            if (eArg != null)
            {
                a = eArg.getValue(iDataType);
            }

            if (eArg2 != null)
            {
                b = eArg2.getValue(iDataType);
            }

            switch (iType)
            {
            case ADD:
                return(Column.add(a, b, iDataType));

            case SUBTRACT:
                return(Column.subtract(a, b, iDataType));

            case MULTIPLY:
                return(Column.multiply(a, b, iDataType));

            case DIVIDE:
                return(Column.divide(a, b, iDataType));

            case CONCAT:
                return(Column.concat(a, b));

            case IFNULL:
                return(a == null ? b : a);

            default:

                // must be comparisation
                // todo: make sure it is
                return(test());
            }
        }
Exemplo n.º 3
0
        /**
         * Method declaration
         *
         *
         * @param e
         *
         * @return
         *
         * @throws Exception
         */
        private bool test(Expression e)
        {
            if (e == null)
            {
                return true;
            }

            return e.test();
        }
Exemplo n.º 4
0
        // [email protected] end changes from 1.50
        // [email protected] begin changes from 1.50
        public Result getResult(int start, int cnt)
        {
            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.iType[i] = e.getDataType();

                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;
            }
            else
            {
                simple_maxrows = false;
            }

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

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

            while (level >= 0)
            {
                TableFilter t = tFilter[level];
                bool        found;

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

                if (!found)
                {
                    level--;

                    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();
                    }

                    count++;

                    if (aggregated)
                    {
                        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.rRoot;
                Result x = new Result(len);

                for (int i = 0; i < len; i++)
                {
                    x.iType[i] = r.iType[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.iType[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 is maybe 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.sLabel[i] = e.getAlias();
                r.sTable[i] = e.getTableName();
                r.sName[i]  = e.getColumnName();
            }

            if (sUnion != null)
            {
                Result x = sUnion.getResult(0);

                if (iUnionType == UNION)
                {
                    r.append(x);

                    r = removeDuplicates(r);
                }
                else if (iUnionType == UNIONALL)
                {
                    r.append(x);
                }
                else if (iUnionType == INTERSECT)
                {
                    r = removeDuplicates(r);
                    x = removeDuplicates(x);
                    r = removeDifferent(r, x);
                }
                else if (iUnionType == 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);
        }