PageRenderTime 952ms CodeModel.GetById 40ms RepoModel.GetById 5ms app.codeStats 0ms

/SmarterSql/SmarterSql/Parsing/Keywords/KeywordCTE.cs

#
C# | 100 lines | 79 code | 10 blank | 11 comment | 35 complexity | 4296f6d44903c1622d40fa5c3b659eef MD5 | raw file
  1. // ---------------------------------
  2. // SmarterSql (c) Johan Sassner 2008
  3. // ---------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using Sassner.SmarterSql.ParsingUtils;
  7. using Sassner.SmarterSql.Tree;
  8. using Sassner.SmarterSql.Utils;
  9. namespace Sassner.SmarterSql.Parsing.Keywords {
  10. public class KeywordCTE {
  11. #region Member variables
  12. private const string ClassName = "KeywordCTE";
  13. #endregion
  14. #region Parse CTE's
  15. /// <summary>
  16. /// Parse a CTE for its content
  17. /// </summary>
  18. /// <param name="parser"></param>
  19. /// <param name="lstTokens"></param>
  20. /// <param name="i"></param>
  21. public static void ParseCTE(Parser parser, List<TokenInfo> lstTokens, ref int i) {
  22. try {
  23. TokenInfo token = InStatement.GetNextNonCommentToken(lstTokens, ref i);
  24. if (null != token) {
  25. i++;
  26. token = InStatement.GetNextNonCommentToken(lstTokens, ref i);
  27. if (null != token && token.Type == TokenType.Identifier) {
  28. string Table = token.Token.UnqoutedImage;
  29. token.TokenContextType = TokenContextType.CTEName;
  30. i++;
  31. token = InStatement.GetNextNonCommentToken(lstTokens, ref i);
  32. List<string> lstColumNames = new List<string>();
  33. // If parenthesis, store named columns
  34. if (null != token && token.Kind == TokenKind.LeftParenthesis) {
  35. int matchingBracesIndex = token.MatchingParenToken;
  36. if (-1 != matchingBracesIndex && i < matchingBracesIndex) {
  37. i++;
  38. while (i < matchingBracesIndex) {
  39. token = InStatement.GetNextNonCommentToken(lstTokens, ref i);
  40. if (null != token) {
  41. if (token.Type == TokenType.Identifier) {
  42. lstColumNames.Add(token.Token.UnqoutedImage);
  43. token.TokenContextType = TokenContextType.NewColumnAlias;
  44. }
  45. } else {
  46. return;
  47. }
  48. i++;
  49. }
  50. i++;
  51. } else {
  52. return;
  53. }
  54. }
  55. // Find the SELECT span and store it for later parsing
  56. token = InStatement.GetNextNonCommentToken(lstTokens, ref i);
  57. if (null != token && token.Kind == TokenKind.KeywordAs) {
  58. i++;
  59. token = InStatement.GetNextNonCommentToken(lstTokens, ref i);
  60. if (null != token && token.Kind == TokenKind.LeftParenthesis) {
  61. int matchingBracesIndex = token.MatchingParenToken;
  62. if (-1 != matchingBracesIndex && i < matchingBracesIndex) {
  63. int startIndex = InStatement.GetNextNonCommentToken(lstTokens, i + 1, i + 1);
  64. int endIndex = matchingBracesIndex - 1;
  65. token = InStatement.GetNextNonCommentToken(lstTokens, startIndex);
  66. StatementSpans ss;
  67. Common.GetStatementSpan(token.Span, parser.SegmentUtils.StartTokenIndexes, token.ParenLevel, out ss);
  68. int selectStartIndex;
  69. int selectEndIndex;
  70. if (null != ss) {
  71. selectStartIndex = (-1 == ss.FromIndex ? ss.StartIndex : InStatement.GetNextNonCommentToken(lstTokens, ss.FromIndex + 1, ss.FromIndex + 1));
  72. selectEndIndex = ss.EndIndex;
  73. } else {
  74. selectStartIndex = startIndex;
  75. selectEndIndex = endIndex;
  76. }
  77. ScannedTable newScannedTable = new ScannedTable("", "", "", Table, Table, token.Span, token.ParenLevel, startIndex, endIndex, selectStartIndex, selectEndIndex, Common.enSqlTypes.CTE, lstColumNames);
  78. parser.ScannedTables.Add(newScannedTable);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. } catch (Exception e) {
  85. Common.LogEntry(ClassName, "ParseCTE", e, Common.enErrorLvl.Error);
  86. }
  87. }
  88. #endregion
  89. }
  90. }