Esempio n. 1
0
        static public void GetResultOfNestedQueryForInStatement(HqlTokenProcessor processor, ArrayList list)
        {
            try
            {
                HqlStream cs = new HqlStream(processor.GetRemainingSql(true), ")");
                if (cs.CountReturnVisibleFields != 1)
                {
                    throw new Exception("Internal Hql streams must return only one value");
                }

                for (; !cs.EndOfStream;)
                {
                    string s = cs.ReadLine();
                    if (s == null || s.Length == 0)
                    {
                        continue;
                    }

                    HqlToken token = new HqlToken(HqlWordType.UNKNOWN, s);
                    HqlTokenProcessor.Categorize(ref token, false, false, true, false);
                    if (token.WordType == HqlWordType.UNKNOWN)
                    {
                        token = new HqlToken(HqlWordType.LITERAL_STRING, token.Data);
                    }
                    list.Add(token);
                }
                cs.Close();
                processor.SetRemainingSql(cs.RemainingSql);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to execute nested IN statement SQL", ex);
            }
        }
Esempio n. 2
0
        public void Parse(HqlTokenProcessor processor)
        {
            HqlToken   token;
            bool       FileExpected = true;
            HqlKeyword joinType;

            // first thing should be from
            token = processor.GetToken();
            if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.FROM)
            {
                throw new Exception("Expected FROM");
            }

            bool ReadNextToken = true;

            // now pull back things until you get to EOF or WHERE
            for (; ;)
            {
                joinType = HqlKeyword.UNKNOWN;

                if (ReadNextToken)
                {
                    processor.MoveNextToken();
                }
                ReadNextToken = true;

                token = processor.GetToken();
                if (processor.MatchesEndOfProcessing())
                {
                    break;
                }

                if (token.WordType == HqlWordType.KEYWORD &&
                    (
                        token.Keyword == HqlKeyword.WHERE ||
                        token.Keyword == HqlKeyword.GROUPBY ||
                        token.Keyword == HqlKeyword.HAVING ||
                        token.Keyword == HqlKeyword.ORDERBY
                    )
                    )
                {
                    break;
                }

                if (token.WordType == HqlWordType.KEYWORD &&
                    (
                        token.Keyword == HqlKeyword.FULL_OUTER ||
                        token.Keyword == HqlKeyword.LEFT_OUTER ||
                        token.Keyword == HqlKeyword.RIGHT_OUTER ||
                        token.Keyword == HqlKeyword.INNER
                    )
                    )
                {
                    joinType = token.Keyword;

                    processor.MoveNextToken();
                    token = processor.GetToken();
                    if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.JOIN)
                    {
                        throw new Exception("Expected JOIN keyword");
                    }
                    FileExpected = true;

                    processor.MoveNextToken();
                    token = processor.GetToken();
                }

                // else time to process it!
                if (!FileExpected)
                {
                    break;
                }

                FileExpected = false;

                if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.STDIN)
                {
                    AddStdin();
                }
                else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.DUAL)
                {
                    AddDual();
                }
                else if (token.WordType == HqlWordType.TEXT || token.WordType == HqlWordType.LITERAL_STRING)
                {
                    AddFile(token.Data);
                }
                else if (token.WordType == HqlWordType.STREAM)
                {
                    HqlDataSource source = new HqlDataSource(HqlDataSourceType.STREAM, (System.IO.StreamReader)token.Parsed);
                    AddSource(source);
                }
                else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OPENPAREN)
                {
                    // NESTED QUERY!
                    HqlStream cs           = new HqlStream(processor.GetRemainingSql(false), ")");
                    string    remainingSql = cs.RemainingSql;
                    processor.SetRemainingSql(remainingSql);
                    processor.MoveNextToken();
                    token = processor.GetToken();
                    if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.CLOSEDPAREN)
                    {
                        throw new Exception("Nested query SQL did not return correctly");
                    }

                    AddTextReader(cs);
                }
                else
                {
                    throw new Exception("Unknown type of FROM clause");
                }

                // look to process SKIP
                for (;;)
                {
                    bool breakOut = false;

                    if (ReadNextToken)
                    {
                        processor.MoveNextToken();
                        token = processor.GetToken();
                    }

                    if (_sources != null && _sources.Length > 0 && (token.WordType == HqlWordType.UNKNOWN || token.WordType == HqlWordType.KEYWORD))
                    {
                        HqlToken option;
                        switch (token.Data.ToUpper())
                        {
                        case "SKIP":
                        {
                            option = processor.GetOptionData(token.Data);
                            if (option.WordType != HqlWordType.INT || option.ParsedAsInt <= 0)
                            {
                                throw new Exception("Skip count must be greater than zero");
                            }
                            CurrentSource.SkipRecords = option.ParsedAsInt;
                            ReadNextToken             = true;
                            break;
                        }

                        case "DELIMITER":
                        case "DELIM":
                        {
                            option = processor.GetOptionData(token.Data);
                            if (option.WordType != HqlWordType.TEXT && option.WordType != HqlWordType.LITERAL_STRING)
                            {
                                throw new Exception(String.Format("Expected a valid delimiter after {0}", token.Data));
                            }
                            CurrentSource.Delimiter = HqlTokenProcessor.CleanupDelimiter(option.Data);
                            ReadNextToken           = true;
                            break;
                        }

                        case "AS":
                        {
                            option = processor.GetOptionData(token.Data);
                            if (option.WordType != HqlWordType.UNKNOWN)
                            {
                                throw new Exception("Expected a table reference name following AS");
                            }
                            CurrentSource.TableReference = option.Data;
                            break;
                        }

                        default:
                            breakOut = true;
                            break;
                        }
                    }
                    else
                    {
                        breakOut = true;
                    }

                    if (breakOut)
                    {
                        ReadNextToken = false;
                        break;
                    }
                }


                if (joinType != HqlKeyword.UNKNOWN)
                {
                    HqlDataSource src = LeftSideOfJoin;
                    src.JoinType = joinType;

                    if (ReadNextToken)
                    {
                        processor.MoveNextToken();
                        token = processor.GetToken();
                    }

                    if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.ON)
                    {
                        throw new Exception("Expected ON following a table");
                    }

                    src.InitJoining();
                    src.OnJoin.Parse(processor, HqlKeyword.ON, HqlKeyword.ON, null);
                    ReadNextToken = false;
                }
            }

            if (FileExpected)
            {
                throw new Exception("Expected additional FROM fields");
            }
        }
Esempio n. 3
0
        private void AddTextReader(HqlStream stream)
        {
            HqlDataSource source = new HqlDataSource(HqlDataSourceType.HQLQUERY, stream);

            AddSource(source);
        }