SqlLexer.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace FastReport.FastQueryBuilder
  5. {
  6. class SqlLexer
  7. {
  8. string sql;
  9. int position;
  10. string qch;
  11. public SqlLexer(string sql, string qch)
  12. {
  13. this.sql = sql;
  14. this.qch = qch;
  15. }
  16. public List<SqlToken> Parse()
  17. {
  18. List<SqlToken> tokens = new List<SqlToken>();
  19. position = 0;
  20. SkipWhiteSpace();
  21. SqlToken token;
  22. while (position < sql.Length)
  23. {
  24. token = IsOperation();
  25. if (token == null)
  26. token = ReadCommand();
  27. if (token == null)
  28. token = ReadName();
  29. if (token == null)
  30. token = GetPunctuation();
  31. if (token == null)
  32. throw new FormatException("Incorect syntax at position " + position.ToString() + ", unexpected character " +sql[position].ToString() + Environment.NewLine + sql);
  33. SkipWhiteSpace();
  34. tokens.Add(token);
  35. }
  36. return tokens;
  37. }
  38. private SqlToken GetPunctuation()
  39. {
  40. if (position < sql.Length && IsPunctuation(sql[position]))
  41. {
  42. position++;
  43. return new SqlToken(SqlTokenType.Punctuation, sql[position - 1].ToString(), position - 1);
  44. }
  45. return null;
  46. }
  47. private SqlToken IsOperation()
  48. {
  49. for (int i = 0; i < operations.Length; i++)
  50. {
  51. if (position + operations[i].Length <= sql.Length)
  52. {
  53. for (int j = 0; j < operations[i].Length; j++)
  54. {
  55. if (Char.ToLower(sql[position + j]) != operations[i][j])
  56. goto nextOperation;
  57. }
  58. if (position + operations[i].Length >= sql.Length ||
  59. Char.IsWhiteSpace(sql[position + operations[i].Length])
  60. || IsPunctuation(sql[position + operations[i].Length])
  61. || (IsPunctuation(sql[position + operations[i].Length - 1]) && !IsPunctuation(sql[position + operations[i].Length])))
  62. {
  63. SqlToken result = new SqlToken(SqlTokenType.Operation, sql.Substring(position, operations[i].Length), position);
  64. position += operations[i].Length;
  65. return result;
  66. }
  67. }
  68. nextOperation:
  69. {
  70. }
  71. }
  72. return null;
  73. }
  74. private string[] operations = new string[] { "||", "*", "/", "+", "-", "<>", ">", "<", ">=", "<=", "==", "=", "!=", "in", "nin", "is", "like", "nlike", "not", "or", "and" };
  75. private string[] keywords = new string[] { "and", "as", "asc", "between", "case", "collate nocase", "desc", "else", "end", "from", "group by", "having", "in", "not in", "is", "limit", "offset", "like", "not between", "not like", "on", "or", "order by", "select", "then", "union", "union all", "using", "when", "where", "with", "join", "full join", "cross join", "inner join", "left join", "right join", "full outer join", "right outer join", "left outer join", };
  76. private bool IsPunctuation(char c)
  77. {
  78. return " ~`!@#$%^&*()-+=[]{};:'\",.<>/?\\|".IndexOf(c) != -1;
  79. }
  80. public SqlToken ReadName()
  81. {
  82. StringBuilder result = new StringBuilder();
  83. int pos = position;
  84. if (position < sql.Length && sql[position] == qch[0])
  85. {
  86. position++;
  87. // read to quote
  88. // TODO not ignore double quote
  89. while (position < sql.Length)
  90. {
  91. if (sql[position] == qch[1])
  92. {
  93. position++;
  94. break;
  95. }
  96. result.Append(sql[position]);
  97. position++;
  98. }
  99. }
  100. else
  101. {
  102. while (position < sql.Length)
  103. {
  104. if (Char.IsWhiteSpace(sql[position]) || IsPunctuation(sql[position]))
  105. {
  106. break;
  107. }
  108. result.Append(sql[position]);
  109. position++;
  110. }
  111. }
  112. if (result.Length > 0)
  113. return new SqlToken(SqlTokenType.Name, result.ToString(), pos);
  114. return null;
  115. }
  116. private SqlToken ReadCommand()
  117. {
  118. for (int i = 0; i < keywords.Length; i++)
  119. {
  120. if (position + keywords[i].Length <= sql.Length)
  121. {
  122. for (int j = 0; j < keywords[i].Length ; j++)
  123. {
  124. if (Char.ToLower(sql[position + j]) != keywords[i][j])
  125. goto nextKeyword;
  126. }
  127. if (position + keywords[i].Length >= sql.Length ||
  128. Char.IsWhiteSpace(sql[position + keywords[i].Length])
  129. || IsPunctuation(sql[position + keywords[i].Length]))
  130. {
  131. SqlToken result = new SqlToken(SqlTokenType.Keyword, keywords[i], position);
  132. position += keywords[i].Length;
  133. return result;
  134. }
  135. }
  136. nextKeyword:
  137. {
  138. }
  139. }
  140. return null;
  141. }
  142. private void SkipWhiteSpace()
  143. {
  144. while (position < sql.Length && char.IsWhiteSpace(sql[position]))
  145. {
  146. position++;
  147. }
  148. }
  149. }
  150. }