SelectCommand.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace FastReport.FastQueryBuilder
  5. {
  6. class SelectCommand : ParserBase
  7. {
  8. public override bool CanParse(SqlParser parser)
  9. {
  10. return parser.Token.LowerText == "select";
  11. }
  12. public override void Parse(SqlParser parser)
  13. {
  14. SqlToken token = parser.NextToken();
  15. while (token.Type != SqlTokenType.EOF)
  16. {
  17. if (token.Type == SqlTokenType.Keyword)
  18. {
  19. if (token.LowerText == "from")
  20. {
  21. break;
  22. }
  23. else
  24. {
  25. parser.ThrowFormat(token, "from");
  26. }
  27. }
  28. // parse expression
  29. string function = null;
  30. string table = null;
  31. string variable = null;
  32. string alias = null;
  33. if (token.Type != SqlTokenType.Name)
  34. {
  35. parser.ThrowFormat(token, "name");
  36. }
  37. function = token.Text;
  38. token = parser.NextToken();
  39. if (token.Type == SqlTokenType.Punctuation && token.Text == "(")
  40. {
  41. token = parser.NextToken();
  42. if (token.Type != SqlTokenType.Name)
  43. parser.ThrowFormat(token, "name");
  44. table = token.Text;
  45. token = parser.NextToken();
  46. if (token.Type != SqlTokenType.Punctuation || token.Text != ".")
  47. parser.ThrowFormat(token, ".");
  48. token = parser.NextToken();
  49. if (token.Type != SqlTokenType.Name)
  50. parser.ThrowFormat(token, "name");
  51. variable = token.Text;
  52. token = parser.NextToken();
  53. if (token.Type != SqlTokenType.Punctuation || token.Text != ")")
  54. parser.ThrowFormat(token, ")");
  55. }
  56. else if (token.Type == SqlTokenType.Punctuation && token.Text == ".")
  57. {
  58. table = function;
  59. function = null;
  60. token = parser.NextToken();
  61. if (token.Type != SqlTokenType.Name)
  62. parser.ThrowFormat(token, "name");
  63. variable = token.Text;
  64. }
  65. else
  66. {
  67. parser.ThrowFormat(token, ". or (");
  68. }
  69. token = parser.NextToken();
  70. if (token.Type == SqlTokenType.Keyword && token.Text == "as")
  71. {
  72. token = parser.NextToken();
  73. if (token.Type != SqlTokenType.Name)
  74. parser.ThrowFormat(token, "name");
  75. alias = token.Text;
  76. token = parser.NextToken();
  77. }
  78. SqlParser.FieldStruct field = new SqlParser.FieldStruct();
  79. field.Alias = alias;
  80. field.Name = variable;
  81. field.Func = function;
  82. field.Table = table;
  83. parser.Fields.Add(field);
  84. if (token.Type == SqlTokenType.Punctuation && token.Text == ",")
  85. {
  86. token = parser.NextToken();
  87. }
  88. }
  89. if (parser.Token.Type != SqlTokenType.EOF)
  90. {
  91. SelectCommand_From selectCommand_From = new SelectCommand_From();
  92. if (selectCommand_From.CanParse(parser))
  93. {
  94. selectCommand_From.Parse(parser);
  95. }
  96. else
  97. {
  98. parser.ThrowFormat(parser.Token, "from");
  99. }
  100. SelectCommand_GroupBy groupBy = new SelectCommand_GroupBy();
  101. SelectCommand_OrderBy orderBy = new SelectCommand_OrderBy();
  102. SelectCommand_Where where = new SelectCommand_Where();
  103. while (parser.Token.Type != SqlTokenType.EOF)
  104. {
  105. if (groupBy.CanParse(parser))
  106. groupBy.Parse(parser);
  107. else if (orderBy.CanParse(parser))
  108. orderBy.Parse(parser);
  109. else if (where.CanParse(parser))
  110. where.Parse(parser);
  111. else parser.ThrowFormat(token, "group by, order by or where");
  112. }
  113. }
  114. }
  115. }
  116. }