SqlParser.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 { get
  36. {
  37. return fields;
  38. }
  39. }
  40. public List<LinkStruct> Links
  41. {
  42. get
  43. {
  44. return links;
  45. }
  46. }
  47. public List<FieldStruct> Groups
  48. {
  49. get
  50. {
  51. return groups;
  52. }
  53. }
  54. public List<FieldStruct> Orders
  55. {
  56. get
  57. {
  58. return orders;
  59. }
  60. }
  61. public List<TableStruct> Tables { get { return tables; } }
  62. public List<FieldStruct> Where { get { return where; } }
  63. public SqlToken NextToken()
  64. {
  65. tokenPosition++;
  66. return Token;
  67. }
  68. public int Position
  69. {
  70. get { return tokenPosition; }
  71. set { tokenPosition = value; }
  72. }
  73. public Query Parse()
  74. {
  75. fields = new List<FieldStruct>();
  76. tables = new List<TableStruct>();
  77. links = new List<LinkStruct>();
  78. groups = new List<FieldStruct>();
  79. orders = new List<FieldStruct>();
  80. where = new List<FieldStruct>();
  81. SqlLexer lexer = new SqlLexer(sql, qch);
  82. tokens = lexer.Parse();
  83. tokenPosition = 0;
  84. SelectCommand selectParser = new SelectCommand();
  85. if (selectParser.CanParse(this))
  86. selectParser.Parse(this);
  87. else ThrowFormat(Token, "select");
  88. return null;
  89. }
  90. public void ThrowFormat(SqlToken token, string expectToken)
  91. {
  92. throw new FormatException("Incorect syntax at position " + token.Position.ToString() + ", unexpected token " + token .Text+ ", " + "expecting " + expectToken + "." + Environment.NewLine + sql);
  93. }
  94. public class FieldStruct
  95. {
  96. private string name = string.Empty;
  97. private string alias = string.Empty;
  98. private string func = string.Empty;
  99. private string table = string.Empty;
  100. private string filter = string.Empty;
  101. private SortTypes sortType;
  102. public string Name { get { return name; } set { if (value == null) value = string.Empty; name = value; } }
  103. public string Alias { get { return alias; } set { if (value == null) value = string.Empty; alias = value; } }
  104. public string Func { get { return func; } set { if (value == null) value = string.Empty; func = value; } }
  105. public string Table { get { return table; } set { if (value == null) value = string.Empty; table = value; } }
  106. public SortTypes SortType { get { return sortType; } set { sortType = value; } }
  107. public string Filter { get { return filter; } set { filter = value; } }
  108. public override string ToString()
  109. {
  110. return Table + "." + Name;
  111. }
  112. }
  113. public class TableStruct
  114. {
  115. private string name = string.Empty;
  116. private string alias = string.Empty;
  117. public string Name { get { return name; } set { if (value == null) value = string.Empty; name = value; } }
  118. public string Alias { get { return alias; } set { if (value == null) value = string.Empty; alias = value; } }
  119. }
  120. public class LinkStruct
  121. {
  122. private QueryEnums.WhereTypes whereType;
  123. private QueryEnums.JoinTypes joinType;
  124. private FieldStruct one;
  125. private FieldStruct second;
  126. private string table = string.Empty;
  127. public QueryEnums.WhereTypes WhereType { get { return whereType; } set { whereType = value; } }
  128. public QueryEnums.JoinTypes JoinType { get { return joinType; } set { joinType = value; } }
  129. public FieldStruct One { get { return one; } set { one = value; } }
  130. public FieldStruct Two { get { return second; } set { second = value; } }
  131. public string Table { get { return table; } set { if (value == null) value = string.Empty; table = value; } }
  132. }
  133. }
  134. }