/////////////////////// // Overridden functions /////////////////////// // Public public void Parse(HqlTokenProcessor processor) { HqlToken token; HqlToken option; // first thing should be from token = processor.GetToken(); if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.WITH) { return; // throw new Exception("Expected WITH"); } // now pull back things until you get to EOF for (; ;) { processor.MoveNextToken(); token = processor.GetToken(); if (processor.MatchesEndOfProcessing()) { break; } if ( token.WordType == HqlWordType.UNKNOWN || token.WordType == HqlWordType.KEYWORD ) { switch (token.Data.ToUpper()) { case "HEADER": { PrintHeader = true; break; } case "PQ": case "PRESERVE-QUOTES": case "PRESERVE_QUOTES": { // TODO, not implemented PreserveQuotes = true; break; } case "TEMPDIR": { option = processor.GetOptionData(token.Data); if (option.WordType != HqlWordType.TEXT && option.WordType != HqlWordType.LITERAL_STRING) { throw new Exception(String.Format("Expected a valid directory after {0}", token.Data)); } // TODO, save directory break; } case "OD": case "OUT-DELIMITER": case "OUT_DELIMITER": { 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)); } OutDelimiter = HqlTokenProcessor.CleanupDelimiter(option.Data); break; } case "D": case "DELIM": case "DELIMITER": { 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)); } InDelimiter = HqlTokenProcessor.CleanupDelimiter(option.Data); break; } case "PFD": case "PRINT-FINAL-DELIMITER": case "PRINT_FINAL_DELIMITER": { HasFinalDelimiter = true; break; } case "OUTPUT": { option = processor.GetOptionData(token.Data); if (option.WordType != HqlWordType.TEXT && option.WordType != HqlWordType.LITERAL_STRING) { throw new Exception(String.Format("Expected a filename after {0}", token.Data)); } OutputFilename = option.Data; ProcessOutFilename(processor); break; } //case "SKIP": // { // option = GetOptionData(processor, token.Data); // if (option.WordType != HqlWordType.INT || (int)option.Parsed <= 0) // throw new Exception(String.Format("Expected a greater than zero integer after {0}", token.Data)); // SkipRecords = (Int32)(Int64)option.Parsed; // break; // } default: throw new Exception(String.Format("Unknown WITH option of {0}", token.Data.ToString())); } } else { throw new Exception("Unknown type in WITH clause"); } } }
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"); } }