SqlParser.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace FastReport.FastQueryBuilder
  5. {
  6. class SqlParser
  7. {
  8. public string qch;
  9. private string sql;
  10. private List<FieldStruct> fields;
  11. private List<TableStruct> tables;
  12. private List<LinkStruct> links;
  13. private List<FieldStruct> groups;
  14. private List<FieldStruct> orders;
  15. private List<FieldStruct> where;
  16. int tokenPosition = 0;
  17. public SqlParser(string sql)
  18. {
  19. this.sql = sql;
  20. }
  21. public string SQL
  22. {
  23. get { return sql; }
  24. }
  25. private List<SqlToken> tokens;
  26. public SqlToken Token
  27. {
  28. get
  29. {
  30. if (tokenPosition < tokens.Count)
  31. return tokens[tokenPosition];
  32. return new SqlToken(SqlTokenType.EOF, "", sql.Length);
  33. }
  34. }
  35. public List<FieldStruct> Fields
  36. {
  37. get
  38. {
  39. return fields;
  40. }
  41. }
  42. public List<LinkStruct> Links
  43. {
  44. get
  45. {
  46. return links;
  47. }
  48. }
  49. public List<FieldStruct> Groups
  50. {
  51. get
  52. {
  53. return groups;
  54. }
  55. }
  56. public List<FieldStruct> Orders
  57. {
  58. get
  59. {
  60. return orders;
  61. }
  62. }
  63. public List<TableStruct> Tables { get { return tables; } }
  64. public List<FieldStruct> Where { get { return where; } }
  65. public SqlToken NextToken()
  66. {
  67. tokenPosition++;
  68. return Token;
  69. }
  70. public int Position
  71. {
  72. get { return tokenPosition; }
  73. set { tokenPosition = value; }
  74. }
  75. public Query Parse()
  76. {
  77. fields = new List<FieldStruct>();
  78. tables = new List<TableStruct>();
  79. links = new List<LinkStruct>();
  80. groups = new List<FieldStruct>();
  81. orders = new List<FieldStruct>();
  82. where = new List<FieldStruct>();
  83. SqlLexer lexer = new SqlLexer(sql, qch);
  84. tokens = lexer.Parse();
  85. tokenPosition = 0;
  86. SelectCommand selectParser = new SelectCommand();
  87. if (selectParser.CanParse(this))
  88. selectParser.Parse(this);
  89. else ThrowFormat(Token, "select");
  90. return null;
  91. }
  92. public void ThrowFormat(SqlToken token, string expectToken)
  93. {
  94. throw new FormatException("Incorect syntax at position " + token.Position.ToString() + ", unexpected token " + token.Text + ", " + "expecting " + expectToken + "." + Environment.NewLine + sql);
  95. }
  96. public class FieldStruct
  97. {
  98. private string name = string.Empty;
  99. private string alias = string.Empty;
  100. private string func = string.Empty;
  101. private string table = string.Empty;
  102. private string filter = string.Empty;
  103. private SortTypes sortType;
  104. public string Name { get { return name; } set { if (value == null) value = string.Empty; name = value; } }
  105. public string Alias { get { return alias; } set { if (value == null) value = string.Empty; alias = value; } }
  106. public string Func { get { return func; } set { if (value == null) value = string.Empty; func = value; } }
  107. public string Table { get { return table; } set { if (value == null) value = string.Empty; table = value; } }
  108. public SortTypes SortType { get { return sortType; } set { sortType = value; } }
  109. public string Filter { get { return filter; } set { filter = value; } }
  110. public override string ToString()
  111. {
  112. return Table + "." + Name;
  113. }
  114. }
  115. public class TableStruct
  116. {
  117. private string name = string.Empty;
  118. private string alias = string.Empty;
  119. public string Name { get { return name; } set { if (value == null) value = string.Empty; name = value; } }
  120. public string Alias { get { return alias; } set { if (value == null) value = string.Empty; alias = value; } }
  121. }
  122. public class LinkStruct
  123. {
  124. private QueryEnums.WhereTypes whereType;
  125. private QueryEnums.JoinTypes joinType;
  126. private FieldStruct one;
  127. private FieldStruct second;
  128. private string table = string.Empty;
  129. public QueryEnums.WhereTypes WhereType { get { return whereType; } set { whereType = value; } }
  130. public QueryEnums.JoinTypes JoinType { get { return joinType; } set { joinType = value; } }
  131. public FieldStruct One { get { return one; } set { one = value; } }
  132. public FieldStruct Two { get { return second; } set { second = value; } }
  133. public string Table { get { return table; } set { if (value == null) value = string.Empty; table = value; } }
  134. }
  135. }
  136. }