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