1 {*********************************************************}
3 { Zeos Database Objects }
4 { String tokenizing classes for MySQL }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
59 Classes, {$IFDEF MSEgui}mclasses,{$ENDIF} SysUtils,
60 ZSysUtils, ZTokenizer, ZGenericSqlToken, ZCompatibility;
64 {** Implements a MySQL-specific number state object. }
65 TZMySQLNumberState = class (TZNumberState)
67 function NextToken(Stream: TStream; FirstChar: Char;
68 Tokenizer: TZTokenizer): TZToken; override;
71 {** Implements a MySQL-specific quote string state object. }
72 TZMySQLQuoteState = class (TZQuoteState)
74 function NextToken(Stream: TStream; FirstChar: Char;
75 Tokenizer: TZTokenizer): TZToken; override;
77 function EncodeString(const Value: string; QuoteChar: Char): string; override;
78 function DecodeString(const Value: string; QuoteChar: Char): string; override;
82 This state will either delegate to a comment-handling
83 state, or return a token with just a slash in it.
85 TZMySQLCommentState = class (TZCppCommentState)
87 function NextToken(Stream: TStream; FirstChar: Char;
88 Tokenizer: TZTokenizer): TZToken; override;
91 {** Implements a symbol state object. }
92 TZMySQLSymbolState = class (TZSymbolState)
97 {** Implements a word state object. }
98 TZMySQLWordState = class (TZGenericSQLWordState)
103 {** Implements a default tokenizer object. }
104 TZMySQLTokenizer = class (TZTokenizer)
113 { TZMySQLNumberState }
116 Return a number token from a reader.
117 @return a number token from a reader
119 function TZMySQLNumberState.NextToken(Stream: TStream; FirstChar: Char;
120 Tokenizer: TZTokenizer): TZToken;
126 function ReadHexDigits: string;
130 while Stream.Read(LastChar, SizeOf(Char)) > 0 do
132 if CharInSet(LastChar, ['0'..'9','a'..'f','A'..'F']) then
134 Result := Result + LastChar;
135 HexDecimal := HexDecimal or CharInSet(LastChar, ['a'..'f','A'..'F']);
140 Stream.Seek(-SizeOf(Char), soFromCurrent);
146 function ReadDecDigits: string;
150 while Stream.Read(LastChar, SizeOf(Char)) > 0 do
152 if CharInSet(LastChar, ['0'..'9']) then
154 Result := Result + LastChar;
159 Stream.Seek(-SizeOf(Char), soFromCurrent);
167 FloatPoint := FirstChar = '.';
168 Result.Value := FirstChar;
169 Result.TokenType := ttUnknown;
172 { Reads the first part of the number before decimal point }
173 if not FloatPoint then
175 Result.Value := Result.Value + ReadDecDigits;
176 FloatPoint := (LastChar = '.') and not HexDecimal;
179 Stream.Read(LastChar, SizeOf(Char));
180 Result.Value := Result.Value + LastChar;
184 { Reads the second part of the number after decimal point }
186 Result.Value := Result.Value + ReadDecDigits;
188 { Reads a power part of the number }
189 if not HexDecimal and CharInSet(LastChar, ['e','E']) then
191 Stream.Read(LastChar, SizeOf(Char));
192 Result.Value := Result.Value + LastChar;
195 Stream.Read(LastChar, SizeOf(Char));
196 if CharInSet(LastChar, ['0'..'9','-','+']) then
197 Result.Value := Result.Value + LastChar + ReadDecDigits
200 Result.Value := Copy(Result.Value, 1, Length(Result.Value) - 1);
201 Stream.Seek(-2*SizeOf(Char), soFromCurrent);
205 { Reads the nexdecimal number }
206 if (Result.Value = '0') and CharInSet(LastChar, ['x','X']) then
208 Stream.Read(LastChar, SizeOf(Char));
209 Result.Value := Result.Value + LastChar + ReadHexDigits;
213 { Prepare the result }
214 if Result.Value = '.' then
216 if Tokenizer.SymbolState <> nil then
217 Result := Tokenizer.SymbolState.NextToken(Stream, FirstChar, Tokenizer);
222 Result.TokenType := ttHexDecimal
223 else if FloatPoint then
224 Result.TokenType := ttFloat
225 else Result.TokenType := ttInteger;
229 { TZMySQLQuoteState }
232 Return a quoted string token from a reader. This method
233 will collect characters until it sees a match to the
234 character that the tokenizer used to switch to this state.
236 @return a quoted string token from a reader
238 function TZMySQLQuoteState.NextToken(Stream: TStream; FirstChar: Char;
239 Tokenizer: TZTokenizer): TZToken;
240 const BackSlash = Char('\');
245 //QuoteCount: Integer;
247 Result.Value := FirstChar;
249 If FirstChar = '`' then
250 Result.TokenType := ttQuotedIdentifier
252 Result.TokenType := ttQuoted;
254 //QuoteChar := FirstChar;
258 while Stream.Read(ReadChar, SizeOf(Char)) > 0 do
260 //if ReadChar = QuoteChar then Inc(QuoteCount);
261 if (LastChar = FirstChar) and (ReadChar <> FirstChar) then
263 //if QuoteCount mod 2 = 0 then // only valid for Pascal AnsiQuoted/QuotedStr
265 Stream.Seek(-SizeOf(Char), soFromCurrent);
269 Result.Value := Result.Value + ReadChar;
270 if LastChar = BackSlash then
272 // if Readchar = FirstChar then Inc(QuoteCount); //Escaped single Quote (A QuoteChar instead of FirstChar would be better..)
275 else if (LastChar = FirstChar) and (ReadChar = FirstChar) then
277 else LastChar := ReadChar;
282 Encodes a string value.
283 @param Value a string value to be encoded.
284 @param QuoteChar a string quote character.
285 @returns an encoded string.
287 function TZMySQLQuoteState.EncodeString(const Value: string; QuoteChar: Char): string;
289 if CharInSet(QuoteChar, [#39, '"', '`']) then
290 Result := QuoteChar + EncodeCString(Value) + QuoteChar
291 else Result := Value;
295 Decodes a string value.
296 @param Value a string value to be decoded.
297 @param QuoteChar a string quote character.
298 @returns an decoded string.
300 function TZMySQLQuoteState.DecodeString(const Value: string; QuoteChar: Char): string;
304 Len := Length(Value);
305 if (Len >= 2) and CharInSet(QuoteChar, [#39, '"', '`'])
306 and (Value[1] = QuoteChar) and (Value[Len] = QuoteChar) then
309 Result := DecodeCString(Copy(Value, 2, Len - 2))
312 else Result := Value;
315 { TZMySQLCommentState }
318 Gets a MySQL specific comments like # or /* */.
319 @return either just a slash token, or the results of
320 delegating to a comment-handling state
322 function TZMySQLCommentState.NextToken(Stream: TStream; FirstChar: Char;
323 Tokenizer: TZTokenizer): TZToken;
326 ReadNum, ReadNum2: Integer;
328 Result.TokenType := ttUnknown;
329 Result.Value := FirstChar;
331 if FirstChar = '-' then
333 ReadNum := Stream.Read(ReadChar, SizeOf(Char));
334 if (ReadNum > 0) and (ReadChar = '-') then
336 Result.TokenType := ttComment;
337 Result.Value := '--' + GetSingleLineComment(Stream);
342 Stream.Seek(-SizeOf(Char), soFromCurrent);
345 else if FirstChar = '#' then
347 Result.TokenType := ttComment;
348 Result.Value := '#' + GetSingleLineComment(Stream);
350 else if FirstChar = '/' then
352 ReadNum := Stream.Read(ReadChar, SizeOf(Char));
353 if (ReadNum > 0) and (ReadChar = '*') then
355 ReadNum2 := Stream.Read(ReadChar, SizeOf(Char));
356 // Don't treat '/*!' comments as normal comments!!
357 if (ReadNum2 > 0) and (ReadChar <> '!') then
359 Result.TokenType := ttComment;
360 Result.Value := '/*'+ReadChar + GetMultiLineComment(Stream);
365 Result.TokenType := ttSymbol;
366 Result.Value := '/*!' + GetMultiLineComment(Stream);
372 Stream.Seek(-SizeOf(Char), soFromCurrent);
376 if (Result.TokenType = ttUnknown) and (Tokenizer.SymbolState <> nil) then
377 Result := Tokenizer.SymbolState.NextToken(Stream, FirstChar, Tokenizer);
380 { TZMySQLSymbolState }
383 Creates this MySQL-specific symbol state object.
385 constructor TZMySQLSymbolState.Create;
393 {BEGIN PATCH: added by fduenas}
395 {END PATCH: added by fduenas}
401 Constructs this MySQL-specific word state object.
403 constructor TZMySQLWordState.Create;
405 SetWordChars(#0, #191, False);
406 SetWordChars(#192, high(char), True);
407 SetWordChars('a', 'z', True);
408 SetWordChars('A', 'Z', True);
409 SetWordChars('0', '9', True);
410 SetWordChars('$', '$', True);
411 SetWordChars('_', '_', True);
417 Constructs a tokenizer with a default state table (as
418 described in the class comment).
420 constructor TZMySQLTokenizer.Create;
422 WhitespaceState := TZWhitespaceState.Create;
424 EscapeState := TZEscapeState.Create;
425 SymbolState := TZMySQLSymbolState.Create;
426 NumberState := TZMySQLNumberState.Create;
427 QuoteState := TZMySQLQuoteState.Create;
428 WordState := TZMySQLWordState.Create;
429 CommentState := TZMySQLCommentState.Create;
431 SetCharacterState(#0, #32, WhitespaceState);
432 SetCharacterState(#33, #191, SymbolState);
433 SetCharacterState(#192, High(Char), WordState);
435 SetCharacterState('a', 'z', WordState);
436 SetCharacterState('A', 'Z', WordState);
437 SetCharacterState('_', '_', WordState);
438 SetCharacterState('$', '$', WordState);
440 SetCharacterState('0', '9', NumberState);
441 SetCharacterState('.', '.', NumberState);
443 SetCharacterState('"', '"', QuoteState);
444 SetCharacterState(#39, #39, QuoteState);
445 SetCharacterState('`', '`', QuoteState);
447 SetCharacterState('/', '/', CommentState);
448 SetCharacterState('#', '#', CommentState);
449 SetCharacterState('-', '-', CommentState);