Beispiel #1
0
        /// <summary>
        /// Read returned results.
        /// </summary>
        public MochaReader <object> ExecuteReader()
        {
            CheckConnection();
            var reader = new MochaReader <object>();

            if (!RETURN.IsReturnableCmd())
            {
                return(reader);
            }
            bool
                fromkw,
                orderby = false,
                groupby = false;

            string lastcommand;
            var    tags = Mhql_AT.GetATS(Command, out lastcommand);

            if (lastcommand.StartsWith("USE", StringComparison.OrdinalIgnoreCase))
            {
                if (tags.Length > 1)
                {
                    throw new MochaException("Multi tags is cannot used with USE keyword!");
                }

                string tag =
                    tags.Length == 0 ?
                    string.Empty :
                    tags.GetFirst();

                if (tag.Equals("@STACKS", StringComparison.OrdinalIgnoreCase))
                {
                    throw new MochaException("@STACKS is cannot target if used with USE keyword!");
                }

                var use = USE.GetUSE(out lastcommand);
                fromkw = use.IndexOf("FROM", StringComparison.OrdinalIgnoreCase) != -1;
                var table =
                    string.IsNullOrEmpty(tag) || tag.Equals("@TABLES", StringComparison.OrdinalIgnoreCase) ?
                    USE.GetTable(use, fromkw) :
                    tag.Equals("@SECTORS", StringComparison.OrdinalIgnoreCase) ?
                    USE.GetSector(use, fromkw) :
                    throw new MochaException("@ mark is cannot processed!");
                do
                {
                    //Orderby.
                    if (ORDERBY.IsORDERBY(lastcommand))
                    {
                        orderby = true;
                        if (groupby)
                        {
                            throw new MochaException("GROUPBY keyword must be specified before ORDERBY!");
                        }
                        ORDERBY.OrderBy(ORDERBY.GetORDERBY(lastcommand, out lastcommand), ref table);
                    }
                    //Groupby.
                    else if (GROUPBY.IsGROUPBY(lastcommand))
                    {
                        groupby = true;
                        GROUPBY.GroupBy(GROUPBY.GetGROUPBY(lastcommand, out lastcommand), ref table);
                    }
                    //Must.
                    else if (MUST.IsMUST(lastcommand))
                    {
                        if (orderby)
                        {
                            throw new MochaException("MUST keyword must be specified before ORDERBY!");
                        }
                        else if (groupby)
                        {
                            throw new MochaException("MUST keyword must be specified before GROUPBY!");
                        }

                        MUST.MustTable(MUST.GetMUST(lastcommand, out lastcommand), ref table, fromkw);
                    }
                    //Return.
                    else if (lastcommand.Equals("RETURN", StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                    else
                    {
                        throw new MochaException($"'{lastcommand}' command is cannot processed!");
                    }
                } while(true);

                reader.array = new MochaArray <object>(table);
            }
            else if (lastcommand.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
            {
                var select = SELECT.GetSELECT(out lastcommand);
                fromkw = select.IndexOf("FROM", StringComparison.OrdinalIgnoreCase) != -1;

                if (fromkw)
                {
                    throw new MochaException("FROM keyword is cannot use with SELECT keyword!");
                }

                List <object> collection = new List <object>();
                if (tags.Length == 0)
                {
                    collection.AddRange(SELECT.GetTables(select));
                }
                else
                {
                    bool
                        tables  = false,
                        sectors = false,
                        stacks  = false;
                    for (int index = 0; index < tags.Length; index++)
                    {
                        if (tags.ElementAt(index).Equals("@TABLES", StringComparison.OrdinalIgnoreCase))
                        {
                            if (tables)
                            {
                                throw new MochaException("@TABLES cannot be targeted more than once!");
                            }

                            tables = true;
                            collection.AddRange(SELECT.GetTables(select));
                        }
                        else if (tags.ElementAt(index).Equals("@SECTORS", StringComparison.OrdinalIgnoreCase))
                        {
                            if (sectors)
                            {
                                throw new MochaException("@SECTORS cannot be targeted more than once!");
                            }

                            sectors = true;
                            collection.AddRange(SELECT.GetSectors(select));
                        }
                        else if (tags.ElementAt(index).Equals("@STACKS", StringComparison.OrdinalIgnoreCase))
                        {
                            if (stacks)
                            {
                                throw new MochaException("@STACKS cannot be targeted more than once!");
                            }

                            stacks = true;
                            collection.AddRange(SELECT.GetStacks(select));
                        }
                        else
                        {
                            throw new MochaException(tags.ElementAt(index));
                        }
                    }
                }

                do
                {
                    //Orderby.
                    if (ORDERBY.IsORDERBY(lastcommand))
                    {
                        throw new MochaException("ORDERBY keyword is canot used with SELECT keyword!");
                    }
                    //Groupby.
                    else if (GROUPBY.IsGROUPBY(lastcommand))
                    {
                        throw new MochaException("GROUPBY keyword is canot used with SELECT keyword!");
                    }
                    //Must.
                    else if (MUST.IsMUST(lastcommand))
                    {
                        throw new MochaException("MUST keyword is canot used with SELECT keyword!");
                    }
                    //Return.
                    else if (lastcommand.Equals("RETURN", StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                    else
                    {
                        throw new MochaException($"'{lastcommand}' command is cannot processed!");
                    }
                } while(true);

                reader.array = new MochaArray <object>(collection);
            }
            else
            {
                throw new MochaException("MHQL is cannot processed!");
            }

            return(reader);
        }
Beispiel #2
0
        /// <summary>
        /// Show results.
        /// </summary>
        /// <param name="results">Result(s).</param>
        public void ShowResults(MochaReader <object> results)
        {
            if (control != null)
            {
                control.Dispose();
            }

            if (results.Count == 0)
            {
                return;
            }

            if (results.Count == 1)
            {
                results.Read();
                if (results.Value.GetType() == typeof(MochaTableResult))
                {
                    var table = (MochaTableResult)results.Value;
                    ShowTable(table);
                    return;
                }
                results.GoBack();
            }

            var explorerTree = new dbtree();

            explorerTree.ForeColor = Color.White;
            explorerTree.BackColor = BackColor;

            TreeNode
                columnsNode,
                cacheNode,
                columnNode;

            while (results.Read())
            {
                if (results.Value.GetType() == typeof(MochaTable))
                {
                    var table = results.Value as MochaTable;
                    columnsNode                    = new TreeNode();
                    columnsNode.Text               = "Columns";
                    columnsNode.Tag                = "Columns";
                    columnsNode.ImageIndex         = 0;
                    columnsNode.SelectedImageIndex = columnsNode.ImageIndex;

                    cacheNode                    = new TreeNode();
                    cacheNode.Text               = table.Name;
                    cacheNode.Tag                = "Table";
                    cacheNode.ImageIndex         = 2;
                    cacheNode.SelectedImageIndex = cacheNode.ImageIndex;
                    cacheNode.Nodes.Add(columnsNode);

                    for (int columnIndex = 0; columnIndex < table.Columns.Count; columnIndex++)
                    {
                        columnNode                    = new TreeNode();
                        columnNode.Text               = table.Columns[columnIndex].Name;
                        columnNode.ImageIndex         = 4;
                        columnNode.SelectedImageIndex = columnNode.ImageIndex;
                        columnNode.Tag                = "Column";
                        columnsNode.Nodes.Add(columnNode);
                    }

                    explorerTree.Nodes.Add(cacheNode);
                }
            }

            control = explorerTree;
            Controls.Add(explorerTree);
        }