Exemplo n.º 1
0
            void InitFields()
            {
                orderbuf = new List<byte>(DSpace_KeyLength);
                defkey = DSpace_ProcessID;
                TableName = DSpace_ExecArgs[0];
                DfsOutputName = DSpace_ExecArgs[1];
                string QlArgsSelectWhat = DSpace_ExecArgs[2];
                SelectWhat = QlArgsUnescape(QlArgsSelectWhat);
                TopCount = long.Parse(DSpace_ExecArgs[3]);
                string QlArgsOps = DSpace_ExecArgs[4]; // Still encoded with QlArgsEscape.
                Ops = QlArgsUnescape(QlArgsOps);
                if ("*" == Ops)
                {
                    aOps = new string[] { };
                }
                else
                {
                    aOps = Ops.Split('\0');
                }
                string RowInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[5]);
                string DisplayInfo = DSpace_ExecArgs[6];
                InitColTypeInfos(RowInfo, DisplayInfo);
                string OutputRowInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[7]);
                InitOutputColTypeInfos(OutputRowInfo);
                sOptions = (DSpace_ExecArgs.Length > 8) ? DSpace_ExecArgs[8] : "-";

                WhatFunctions = -1 != sOptions.IndexOf("SFUNC");
                GroupBy = -1 != sOptions.IndexOf("GBY");

                if ("*" != SelectWhat && "^" != SelectWhat)
                {
                    awhat = SelectWhat.Split('\0');
                    Whats = new List<int>(awhat.Length);
                    for (int iww = 0; iww < awhat.Length; iww++)
                    {
                        int WColIndex = IndexOfCol(awhat[iww]); // -1 for function (scalar or aggregate).
                        Whats.Add(WColIndex);
                    }
                }

                {
                    int iop = 0;
                    while (iop < aOps.Length)
                    {
                        string op;
                        op = (iop < aOps.Length) ? aOps[iop++] : "";
                        if (0 == string.Compare(op, "WHERE", true))
                        {
                            if (DeleteFromFilter)
                            {
                                throw new Exception("WHERE specified multiple times");
                            }
                            //DSpace_Log("DEBUG:  WHERE");
                            if (null != filter)
                            {
                                throw new Exception("WHERE specified multiple times");
                            }
                            SelectWhereClause swc = new SelectWhereClause(cols, aOps, iop);
                            filter = swc;
                            swc.Parse();
                            iop = swc.CurrentPosition;
                        }
                        else if (0 == string.Compare(op, "SET", true))
                        {
                            if (null != Updates)
                            {
                                if (DeleteFromFilter)
                                {
                                    throw new Exception("Cannot SET in DELETE");
                                }
                                throw new Exception("SET specified multiple times");
                            }
                            Updates = new List<UpdateField>();
                            StringArrayPartReader upr = new StringArrayPartReader(aOps, iop);
                            for (op = upr.NextPart(); ; op = upr.NextPart())
                            {
                                int FieldIndex = IndexOfCol_ensure(op);
                                if ("=" != upr.NextPart())
                                {
                                    throw new Exception("Expected = after SET field");
                                }
                                if (0 == upr.PeekPart().Length)
                                {
                                    throw new Exception("Expected vaule after SET <field> =");
                                }
                                // Types.ReadNextBasicExpression, etc
                                ByteSlice bsval = _ReadNextLiteral(upr, cols[FieldIndex]);
                                UpdateField uf;
                                uf.FieldIndex = FieldIndex;
                                uf.NewValue = bsval;
                                Updates.Add(uf);
                                if ("," != upr.PeekPart())
                                {
                                    break;
                                }
                                upr.NextPart(); // Eat the ",".
                            }
                            if (0 == Updates.Count)
                            {
                                throw new Exception("SET update expression expected");
                            }
                            iop = upr.CurrentPosition;

                        }
                        else if (0 == string.Compare(op, "D.WHERE", true))
                        {
                            if (null != Updates)
                            {
                                throw new Exception("Cannot SET in DELETE");
                            }
                            DeleteFromFilter = true;
                            if (null != filter)
                            {
                                throw new Exception("WHERE specified multiple times");
                            }
                            SelectWhereClause swc = new SelectWhereClause(cols, aOps, iop);
                            filter = swc;
                            swc.Parse();
                            iop = swc.CurrentPosition;

                        }
                        else if (0 == string.Compare(op, "ORDER", true))
                        {
                            //DSpace_Log("DEBUG:  ORDER");
                            op = (iop < aOps.Length) ? aOps[iop++] : "";
                            if (0 == string.Compare(op, "BY", true))
                            {
                                List<GetKeyPart> xorder = new List<GetKeyPart>();
                                for (; ; )
                                {
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if (0 == op.Length || ";" == op)
                                    {
                                        throw new Exception("Expected fields for ORDER BY");
                                    }
                                    int ColIndex = IndexOfCol_ensure(op);
                                    //if (-1 != ColIndex)
                                    {
                                        xorder.Add(
                                            delegate(ByteSlice row, List<byte> AppendKeyPart)
                                            {
                                                GetKeyPartForColumn(row, ColIndex, AppendKeyPart);
                                            });
                                    }
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if ("," != op)
                                    {
                                        if (0 == op.Length || ";" == op)
                                        {
                                            break;
                                        }
                                        //throw new Exception("Unexpected after ORDER BY ...: " + op);
                                        iop--;
                                        break;
                                    }
                                }
                                if (0 == xorder.Count)
                                {
                                    throw new Exception("Must be at least one column to ORDER BY");
                                }
                                if (GroupBy)
                                {
                                    // GROUP BY happens at earlier phase than ORDER BY,
                                    // so if GROUP BY, ignore ORDER BY and use GROUP BY.
                                    // KeyOn will be set to the GroupBy stuff.
                                    throw new Exception("Unexpected use of ORDER BY with GROUP BY");
                                }
                                else
                                {
                                    KeyOn = xorder;
                                }
                            }
                            else
                            {
                                throw new Exception("Expected BY after ORDER, not " + op);
                            }

                        }
                        else if (0 == string.Compare(op, "GROUP", true))
                        {
                            //DSpace_Log("DEBUG:  GROUP");
                            op = (iop < aOps.Length) ? aOps[iop++] : "";
                            if (0 == string.Compare(op, "BY", true))
                            {
                                if (!GroupBy)
                                {
                                    throw new Exception("Unexpected use of GROUP BY");
                                }
                                List<GetKeyPart> xGROUP = new List<GetKeyPart>();
                                for (; ; )
                                {
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if (0 == op.Length || ";" == op)
                                    {
                                        throw new Exception("Expected fields for GROUP BY");
                                    }
                                    int ColIndex = IndexOfCol(op);
                                    if (-1 != ColIndex)
                                    {
                                        xGROUP.Add(
                                            delegate(ByteSlice row, List<byte> AppendKeyPart)
                                            {
                                                GetKeyPartForColumn(row, ColIndex, AppendKeyPart);
                                            });
                                    }
                                    else
                                    {
                                        string sgroup;
                                        {
                                            StringBuilder sbgroup = new StringBuilder();
                                            sbgroup.Append(op);
                                            int nparens = 0;
                                            for (; ; )
                                            {
                                                op = (iop < aOps.Length) ? aOps[iop++] : "";
                                                if (0 == op.Length)
                                                {
                                                    if (0 != nparens)
                                                    {
                                                        throw new Exception("Expected ) in GROUP BY");
                                                    }
                                                    break;
                                                }
                                                sbgroup.Append(' ');
                                                sbgroup.Append(op);
                                                if ("(" == op)
                                                {
                                                    nparens++;
                                                }
                                                else if (")" == op)
                                                {
                                                    if (nparens > 0)
                                                    {
                                                        nparens--;
                                                    }
                                                    if (0 == nparens)
                                                    {
                                                        break;
                                                    }
                                                }
                                            }
                                            sgroup = sbgroup.ToString();
                                        }

                                        if (null == ftools)
                                        {
                                            ftools = new DbFunctionTools();
                                        }
                                        SelectClause selgroup = new SelectClause(ftools, cols);
                                        List<ByteSlice> lrows = new List<ByteSlice>(1);
                                        int rnbytes = -1;

                                        xGROUP.Add(
                                            delegate(ByteSlice row, List<byte> AppendKeyPart)
                                            {
                                                lrows.Clear();
                                                lrows.Add(row);
                                                List<DbValue> lr = selgroup.ProcessSelectPart(sgroup, lrows);
                                                sgroup = null;
                                                if (lr.Count == 1)
                                                {
                                                    DbType rtype;
                                                    ByteSlice r = lr[0].Eval(out rtype);
                                                    if (-1 == rnbytes)
                                                    {
                                                        rnbytes = r.Length;
                                                    }
                                                    else
                                                    {
                                                        if (rnbytes != r.Length)
                                                        {
                                                            throw new Exception("GROUP BY evaluation not returning consistent byte count");
                                                        }
                                                    }
                                                    if (rtype.ID == DbTypeID.CHARS)
                                                    {
                                                        CellStringToCaseInsensitiveAppend(r, AppendKeyPart);
                                                    }
                                                    else
                                                    {
                                                        r.AppendTo(AppendKeyPart);
                                                    }
                                                }
                                                else
                                                {
                                                    throw new Exception("Invalid number of results for GROUP BY evaluation");
                                                }
                                            });

                                    }
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if ("," != op)
                                    {
                                        if (0 == op.Length || ";" == op)
                                        {
                                            break;
                                        }
                                        //throw new Exception("Unexpected after GROUP BY ...: " + op);
                                        iop--;
                                        break;
                                    }
                                }
                                if (0 == xGROUP.Count)
                                {
                                    throw new Exception("Must be at least one column to GROUP BY");
                                }
                                KeyOn = xGROUP;
                            }
                            else
                            {
                                throw new Exception("Expected BY after GROUP, not " + op);
                            }

                        }
                        else
                        {
                            throw new Exception("Unexpected operation " + op);

                        }

                    }

                }

                ValidateSettings();

            }
Exemplo n.º 2
0
            void InitFields()
            {
                orderbuf      = new List <byte>(DSpace_KeyLength);
                defkey        = DSpace_ProcessID;
                TableName     = DSpace_ExecArgs[0];
                DfsOutputName = DSpace_ExecArgs[1];
                string QlArgsSelectWhat = DSpace_ExecArgs[2];

                SelectWhat = QlArgsUnescape(QlArgsSelectWhat);
                TopCount   = long.Parse(DSpace_ExecArgs[3]);
                string QlArgsOps = DSpace_ExecArgs[4]; // Still encoded with QlArgsEscape.

                Ops = QlArgsUnescape(QlArgsOps);
                if ("*" == Ops)
                {
                    aOps = new string[] { };
                }
                else
                {
                    aOps = Ops.Split('\0');
                }
                string RowInfo     = Qa.QlArgsUnescape(DSpace_ExecArgs[5]);
                string DisplayInfo = DSpace_ExecArgs[6];

                InitColTypeInfos(RowInfo, DisplayInfo);
                string OutputRowInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[7]);

                InitOutputColTypeInfos(OutputRowInfo);
                sOptions = (DSpace_ExecArgs.Length > 8) ? DSpace_ExecArgs[8] : "-";

                WhatFunctions = -1 != sOptions.IndexOf("SFUNC");
                GroupBy       = -1 != sOptions.IndexOf("GBY");

                if ("*" != SelectWhat && "^" != SelectWhat)
                {
                    awhat = SelectWhat.Split('\0');
                    Whats = new List <int>(awhat.Length);
                    for (int iww = 0; iww < awhat.Length; iww++)
                    {
                        int WColIndex = IndexOfCol(awhat[iww]); // -1 for function (scalar or aggregate).
                        Whats.Add(WColIndex);
                    }
                }

                {
                    int iop = 0;
                    while (iop < aOps.Length)
                    {
                        string op;
                        op = (iop < aOps.Length) ? aOps[iop++] : "";
                        if (0 == string.Compare(op, "WHERE", true))
                        {
                            if (DeleteFromFilter)
                            {
                                throw new Exception("WHERE specified multiple times");
                            }
                            //DSpace_Log("DEBUG:  WHERE");
                            if (null != filter)
                            {
                                throw new Exception("WHERE specified multiple times");
                            }
                            SelectWhereClause swc = new SelectWhereClause(cols, aOps, iop);
                            filter = swc;
                            swc.Parse();
                            iop = swc.CurrentPosition;
                        }
                        else if (0 == string.Compare(op, "SET", true))
                        {
                            if (null != Updates)
                            {
                                if (DeleteFromFilter)
                                {
                                    throw new Exception("Cannot SET in DELETE");
                                }
                                throw new Exception("SET specified multiple times");
                            }
                            Updates = new List <UpdateField>();
                            StringArrayPartReader upr = new StringArrayPartReader(aOps, iop);
                            for (op = upr.NextPart(); ; op = upr.NextPart())
                            {
                                int FieldIndex = IndexOfCol_ensure(op);
                                if ("=" != upr.NextPart())
                                {
                                    throw new Exception("Expected = after SET field");
                                }
                                if (0 == upr.PeekPart().Length)
                                {
                                    throw new Exception("Expected vaule after SET <field> =");
                                }
                                // Types.ReadNextBasicExpression, etc
                                ByteSlice   bsval = _ReadNextLiteral(upr, cols[FieldIndex]);
                                UpdateField uf;
                                uf.FieldIndex = FieldIndex;
                                uf.NewValue   = bsval;
                                Updates.Add(uf);
                                if ("," != upr.PeekPart())
                                {
                                    break;
                                }
                                upr.NextPart(); // Eat the ",".
                            }
                            if (0 == Updates.Count)
                            {
                                throw new Exception("SET update expression expected");
                            }
                            iop = upr.CurrentPosition;
                        }
                        else if (0 == string.Compare(op, "D.WHERE", true))
                        {
                            if (null != Updates)
                            {
                                throw new Exception("Cannot SET in DELETE");
                            }
                            DeleteFromFilter = true;
                            if (null != filter)
                            {
                                throw new Exception("WHERE specified multiple times");
                            }
                            SelectWhereClause swc = new SelectWhereClause(cols, aOps, iop);
                            filter = swc;
                            swc.Parse();
                            iop = swc.CurrentPosition;
                        }
                        else if (0 == string.Compare(op, "ORDER", true))
                        {
                            //DSpace_Log("DEBUG:  ORDER");
                            op = (iop < aOps.Length) ? aOps[iop++] : "";
                            if (0 == string.Compare(op, "BY", true))
                            {
                                List <GetKeyPart> xorder = new List <GetKeyPart>();
                                for (; ;)
                                {
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if (0 == op.Length || ";" == op)
                                    {
                                        throw new Exception("Expected fields for ORDER BY");
                                    }
                                    int ColIndex = IndexOfCol_ensure(op);
                                    //if (-1 != ColIndex)
                                    {
                                        xorder.Add(
                                            delegate(ByteSlice row, List <byte> AppendKeyPart)
                                        {
                                            GetKeyPartForColumn(row, ColIndex, AppendKeyPart);
                                        });
                                    }
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if ("," != op)
                                    {
                                        if (0 == op.Length || ";" == op)
                                        {
                                            break;
                                        }
                                        //throw new Exception("Unexpected after ORDER BY ...: " + op);
                                        iop--;
                                        break;
                                    }
                                }
                                if (0 == xorder.Count)
                                {
                                    throw new Exception("Must be at least one column to ORDER BY");
                                }
                                if (GroupBy)
                                {
                                    // GROUP BY happens at earlier phase than ORDER BY,
                                    // so if GROUP BY, ignore ORDER BY and use GROUP BY.
                                    // KeyOn will be set to the GroupBy stuff.
                                    throw new Exception("Unexpected use of ORDER BY with GROUP BY");
                                }
                                else
                                {
                                    KeyOn = xorder;
                                }
                            }
                            else
                            {
                                throw new Exception("Expected BY after ORDER, not " + op);
                            }
                        }
                        else if (0 == string.Compare(op, "GROUP", true))
                        {
                            //DSpace_Log("DEBUG:  GROUP");
                            op = (iop < aOps.Length) ? aOps[iop++] : "";
                            if (0 == string.Compare(op, "BY", true))
                            {
                                if (!GroupBy)
                                {
                                    throw new Exception("Unexpected use of GROUP BY");
                                }
                                List <GetKeyPart> xGROUP = new List <GetKeyPart>();
                                for (; ;)
                                {
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if (0 == op.Length || ";" == op)
                                    {
                                        throw new Exception("Expected fields for GROUP BY");
                                    }
                                    int ColIndex = IndexOfCol(op);
                                    if (-1 != ColIndex)
                                    {
                                        xGROUP.Add(
                                            delegate(ByteSlice row, List <byte> AppendKeyPart)
                                        {
                                            GetKeyPartForColumn(row, ColIndex, AppendKeyPart);
                                        });
                                    }
                                    else
                                    {
                                        string sgroup;
                                        {
                                            StringBuilder sbgroup = new StringBuilder();
                                            sbgroup.Append(op);
                                            int nparens = 0;
                                            for (; ;)
                                            {
                                                op = (iop < aOps.Length) ? aOps[iop++] : "";
                                                if (0 == op.Length)
                                                {
                                                    if (0 != nparens)
                                                    {
                                                        throw new Exception("Expected ) in GROUP BY");
                                                    }
                                                    break;
                                                }
                                                sbgroup.Append(' ');
                                                sbgroup.Append(op);
                                                if ("(" == op)
                                                {
                                                    nparens++;
                                                }
                                                else if (")" == op)
                                                {
                                                    if (nparens > 0)
                                                    {
                                                        nparens--;
                                                    }
                                                    if (0 == nparens)
                                                    {
                                                        break;
                                                    }
                                                }
                                            }
                                            sgroup = sbgroup.ToString();
                                        }

                                        if (null == ftools)
                                        {
                                            ftools = new DbFunctionTools();
                                        }
                                        SelectClause     selgroup = new SelectClause(ftools, cols);
                                        List <ByteSlice> lrows    = new List <ByteSlice>(1);
                                        int rnbytes = -1;

                                        xGROUP.Add(
                                            delegate(ByteSlice row, List <byte> AppendKeyPart)
                                        {
                                            lrows.Clear();
                                            lrows.Add(row);
                                            List <DbValue> lr = selgroup.ProcessSelectPart(sgroup, lrows);
                                            sgroup            = null;
                                            if (lr.Count == 1)
                                            {
                                                DbType rtype;
                                                ByteSlice r = lr[0].Eval(out rtype);
                                                if (-1 == rnbytes)
                                                {
                                                    rnbytes = r.Length;
                                                }
                                                else
                                                {
                                                    if (rnbytes != r.Length)
                                                    {
                                                        throw new Exception("GROUP BY evaluation not returning consistent byte count");
                                                    }
                                                }
                                                if (rtype.ID == DbTypeID.CHARS)
                                                {
                                                    CellStringToCaseInsensitiveAppend(r, AppendKeyPart);
                                                }
                                                else
                                                {
                                                    r.AppendTo(AppendKeyPart);
                                                }
                                            }
                                            else
                                            {
                                                throw new Exception("Invalid number of results for GROUP BY evaluation");
                                            }
                                        });
                                    }
                                    op = (iop < aOps.Length) ? aOps[iop++] : "";
                                    if ("," != op)
                                    {
                                        if (0 == op.Length || ";" == op)
                                        {
                                            break;
                                        }
                                        //throw new Exception("Unexpected after GROUP BY ...: " + op);
                                        iop--;
                                        break;
                                    }
                                }
                                if (0 == xGROUP.Count)
                                {
                                    throw new Exception("Must be at least one column to GROUP BY");
                                }
                                KeyOn = xGROUP;
                            }
                            else
                            {
                                throw new Exception("Expected BY after GROUP, not " + op);
                            }
                        }
                        else
                        {
                            throw new Exception("Unexpected operation " + op);
                        }
                    }
                }

                ValidateSettings();
            }