You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
7.6 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright � 2004,2010, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Text;
  25. using System.IO;
  26. using System.Collections.Generic;
  27. using System.Diagnostics;
  28. namespace Externals.MySql.Data.MySqlClient
  29. {
  30. internal class MySqlTokenizer
  31. {
  32. private string sql;
  33. private int startIndex;
  34. private int stopIndex;
  35. private bool ansiQuotes;
  36. private bool backslashEscapes;
  37. private bool returnComments;
  38. private bool multiLine;
  39. private bool sqlServerMode;
  40. private bool quoted;
  41. private bool isComment;
  42. private int pos;
  43. public MySqlTokenizer()
  44. {
  45. backslashEscapes = true;
  46. multiLine = true;
  47. pos = 0;
  48. }
  49. public MySqlTokenizer(string input)
  50. : this()
  51. {
  52. sql = input;
  53. }
  54. #region Properties
  55. public string Text
  56. {
  57. get { return sql; }
  58. set { sql = value; pos = 0; }
  59. }
  60. public bool AnsiQuotes
  61. {
  62. get { return ansiQuotes; }
  63. set { ansiQuotes = value; }
  64. }
  65. public bool BackslashEscapes
  66. {
  67. get { return backslashEscapes; }
  68. set { backslashEscapes = value; }
  69. }
  70. public bool MultiLine
  71. {
  72. get { return multiLine; }
  73. set { multiLine = value; }
  74. }
  75. public bool SqlServerMode
  76. {
  77. get { return sqlServerMode; }
  78. set { sqlServerMode = value; }
  79. }
  80. public bool Quoted
  81. {
  82. get { return quoted; }
  83. private set { quoted = value; }
  84. }
  85. public bool IsComment
  86. {
  87. get { return isComment; }
  88. }
  89. public int StartIndex
  90. {
  91. get { return startIndex; }
  92. set { startIndex = value; }
  93. }
  94. public int StopIndex
  95. {
  96. get { return stopIndex; }
  97. set { stopIndex = value; }
  98. }
  99. public int Position
  100. {
  101. get { return pos; }
  102. set { pos = value; }
  103. }
  104. public bool ReturnComments
  105. {
  106. get { return returnComments; }
  107. set { returnComments = value; }
  108. }
  109. #endregion
  110. public List<string> GetAllTokens()
  111. {
  112. List<string> tokens = new List<string>();
  113. string token = NextToken();
  114. while (token != null)
  115. {
  116. tokens.Add(token);
  117. token = NextToken();
  118. }
  119. return tokens;
  120. }
  121. public string NextToken()
  122. {
  123. while (FindToken())
  124. {
  125. string token = sql.Substring(startIndex, stopIndex - startIndex);
  126. return token;
  127. }
  128. return null;
  129. }
  130. public static bool IsParameter(string s)
  131. {
  132. if (String.IsNullOrEmpty(s)) return false;
  133. if (s[0] == '?') return true;
  134. if (s.Length > 1 && s[0] == '@' && s[1] != '@') return true;
  135. return false;
  136. }
  137. public string NextParameter()
  138. {
  139. while (FindToken())
  140. {
  141. if ((stopIndex - startIndex) < 2) continue;
  142. char c1 = sql[startIndex];
  143. char c2 = sql[startIndex + 1];
  144. if (c1 == '?' ||
  145. (c1 == '@' && c2 != '@'))
  146. return sql.Substring(startIndex, stopIndex - startIndex);
  147. }
  148. return null;
  149. }
  150. public bool FindToken()
  151. {
  152. isComment = quoted = false; // reset our flags
  153. startIndex = stopIndex = -1;
  154. while (pos < sql.Length)
  155. {
  156. char c = sql[pos++];
  157. if (Char.IsWhiteSpace(c)) continue;
  158. if (c == '`' || c == '\'' || c == '"' || (c == '[' && SqlServerMode))
  159. ReadQuotedToken(c);
  160. else if (c == '#' || c == '-' || c == '/')
  161. {
  162. if (!ReadComment(c))
  163. ReadSpecialToken();
  164. }
  165. else
  166. ReadUnquotedToken();
  167. if (startIndex != -1) return true;
  168. }
  169. return false;
  170. }
  171. public string ReadParenthesis()
  172. {
  173. StringBuilder sb = new StringBuilder("(");
  174. int start = StartIndex;
  175. string token = NextToken();
  176. while (true)
  177. {
  178. if (token == null)
  179. throw new InvalidOperationException("Unable to parse SQL");
  180. sb.Append(token);
  181. if (token == ")" && !Quoted) break;
  182. token = NextToken();
  183. }
  184. return sb.ToString();
  185. }
  186. private bool ReadComment(char c)
  187. {
  188. // make sure the comment starts correctly
  189. if (c == '/' && (pos >= sql.Length || sql[pos] != '*')) return false;
  190. if (c == '-' && ((pos + 1) >= sql.Length || sql[pos] != '-' || sql[pos + 1] != ' ')) return false;
  191. string endingPattern = "\n";
  192. if (sql[pos] == '*')
  193. endingPattern = "*/";
  194. int startingIndex = pos - 1;
  195. int index = sql.IndexOf(endingPattern, pos);
  196. if (endingPattern == "\n")
  197. index = sql.IndexOf('\n', pos);
  198. if (index == -1)
  199. index = sql.Length - 1;
  200. else
  201. index += endingPattern.Length;
  202. pos = index;
  203. if (ReturnComments)
  204. {
  205. startIndex = startingIndex;
  206. stopIndex = index;
  207. isComment = true;
  208. }
  209. return true;
  210. }
  211. private void CalculatePosition(int start, int stop)
  212. {
  213. startIndex = start;
  214. stopIndex = stop;
  215. if (!MultiLine) return;
  216. }
  217. private void ReadUnquotedToken()
  218. {
  219. startIndex = pos - 1;
  220. if (!IsSpecialCharacter(sql[startIndex]))
  221. {
  222. while (pos < sql.Length)
  223. {
  224. char c = sql[pos];
  225. if (Char.IsWhiteSpace(c)) break;
  226. if (IsSpecialCharacter(c)) break;
  227. pos++;
  228. }
  229. }
  230. Quoted = false;
  231. stopIndex = pos;
  232. }
  233. private void ReadSpecialToken()
  234. {
  235. startIndex = pos - 1;
  236. Debug.Assert(IsSpecialCharacter(sql[startIndex]));
  237. stopIndex = pos;
  238. Quoted = false;
  239. }
  240. /// <summary>
  241. /// Read a single quoted identifier from the stream
  242. /// </summary>
  243. /// <param name="quoteChar"></param>
  244. /// <returns></returns>
  245. private void ReadQuotedToken(char quoteChar)
  246. {
  247. if (quoteChar == '[')
  248. quoteChar = ']';
  249. startIndex = pos - 1;
  250. bool escaped = false;
  251. bool found = false;
  252. while (pos < sql.Length)
  253. {
  254. char c = sql[pos];
  255. if (c == quoteChar && !escaped)
  256. {
  257. found = true;
  258. break;
  259. }
  260. if (escaped)
  261. escaped = false;
  262. else if (c == '\\' && BackslashEscapes)
  263. escaped = true;
  264. pos++;
  265. }
  266. if (found) pos++;
  267. Quoted = found;
  268. stopIndex = pos;
  269. }
  270. private bool IsQuoteChar(char c)
  271. {
  272. return c == '`' || c == '\'' || c == '\"';
  273. }
  274. private bool IsParameterMarker(char c)
  275. {
  276. return c == '@' || c == '?';
  277. }
  278. private bool IsSpecialCharacter(char c)
  279. {
  280. if (Char.IsLetterOrDigit(c) ||
  281. c == '$' || c == '_' || c == '.') return false;
  282. if (IsParameterMarker(c)) return false;
  283. return true;
  284. }
  285. }
  286. }
  287. #endif